Professional Documents
Culture Documents
PYTHON
PROGRAMMING
A Beginners Guide to the Python Programming Language
TABLE OF
CONTENTS
Introduction
Math
Variables
While Loops
For Loops
If Statements
If Else Statements
If Elif Statements
Functions
Function Parameters
Global and Local Variables
INTRODUCTION
MATH
Eg:
Eg:
Note:
The first statement squares the integer 4 while the second
statement cubes the integer. However; it is important to note that
the following statement is equivalent to writing 4*4 or 4*4*4 as the
number after the * symbol in the expression give the power of the
number before it.
VARIABLES
Eg:
Output:
Eg:
You can clearly notice the difference in the length of the code
between C and Python. This code was just for printing a value in a
variable and still it had a prominent difference; now imagine the
difference for a code that involves more complex functions and
operations.
Eg:
Output:
Eg:
The text comes inside the double quotes and the variable name
after the end of the quotation marks. A % sign must precede the
name of the variable and a formatter (%d in this case) must be
written with the text inside the print command. We will learn
more about variables, formatters and their use as we move to the
next chapter in this intro to Python Programming.
10
WHILE LOOPS
11
There are quite a few interesting points to note about the while
loop. First and foremost, a while loop provides a condition and as
long as that condition holds true, performs a specified operation.
The while loop is essentially used to optimize a given code and to
ensure the perfect use of flow control statements in it, therefore it
has a few similarities to both, the IF statement and the FOR
statement.
Eg:
12
13
FOR LOOPS
Both the while loop and the for-loop are used to execute a given
operation for a specified number of time. In most of the cases,
while loop and for loop are interchangeable and the choice
depends upon how comfortable a user is with each one of them.
As we mentioned in the chapter on while loop, for a beginner in
python, it is better to prefer for loop over while, however; learning
both the flow control statements and getting a good grip of them is
extremely essential to get better at any programming language,
including Python.
14
15
Eg 2:
16
IF STATEMENTS
If you will ask a programmer about one flow control statement that
he uses the most and is the most comfortable with, more often
than not the answer would be If Statement. The if statement
finds great application in codes ranging from printing a simple
series to big applications used in our day to day life. The if
statement is also used alongside other flow control statements to
ensure that the operation inside them are carried precisely the way
they are intended to be.
17
Eg:
18
Note:
1. As described in the chapter, you cannot use the = sign to compare the
values of two variables. The comparison operator in computer programming
is denoted by the == sign while the = is used for assignment operator.
2. Unlike in the case of languages such as C, C++ or Java, Pythons internal
processing of code is so strong that you do not have to write multiple
conditions with a AND or OR separator. You can simply write all the
conditions in the same line and Python will process the answer for you.
Eg:
19
IF ELSE STATEMENTS
20
Eg:
Output:
As you can notice, the condition in the if statement does not hold
true and therefore the control jumps to the else statement and the
print statement inside it is displayed on the console. Before we
knew about the else statement, we were using another if
statement to execute the same program, with the else statement,
the program becomes a lot more organized and faster.
21
Note:
1. There are a few things that you must keep in mind while using the if else
statement in your Python code. To start with, one thing that we have
regularly cautioned the beginners in Python about; the proper use of colon
(:) in your code and especially after the statements that lead to a flow
control loop.
2. The other important thing that many programmers miss, is the legitimate
use of the else statement. An else statement is only valid for the condition
that is directly precedes it and therefore any statement before that holds no
control over the functioning of the operation inside the else statement. To
explain it in a better way, let us have a look at this example.
Eg:
Output:
22
As you can notice, the else statement is executed for the condition
in the 2nd if statement and not for the 1st if statement and therefore
you will see the following output in the console. This highlights
another point that the condition in the if statement and the
operation to be performed by the if block and the else block must
be accurate and logical in sense to deliver the desired results on the
console.
23
IF ELIF STATEMENTS
Following on the if, else and the if else statement, in this chapter
we are going to talk about the ifelifelse statements in Python.
The inclusion of the elif condition might be a little new for all the
beginners in Python as they might feel that programming
languages such as C, C++ do not really require or have the elif
statement, but actually elif is just a shorter way of writing elseif.
Having already discussed about the other flow control statements,
in this chapter, we will pay more attention towards the elif part
and the whole combination of flow control statements.
Even though both elif and the else statements are optional in a
Python code, a big difference is the number of elif or the else
statements that allowed for a specified block of code. While we can
24
Eg:
25
Output:
As you can see in the above example, only the part of the code that
satisfies a condition runs while all other conditions after the
successful part of the code are neglected. Once a condition holds
true, the control breaks out of the block and all the conditions after
that elif part hold no significance in the final output on the
console.
FUNCTIONS
Eg:
28
Points to Ponder:
1. There are a few important points to note about using function in Python.
First of all, declaring a Python function is a little different from how
functions are declared in other programming languages. To declare a
function, we must use the keyword def before the name of the function in
the declaration part to tell Python that you intend to start a function
declaration.
2. Since we have mentioned this numerous times in the previous chapters, it
goes without saying that using the colon (:) symbol is necessary and critical
to the correct execution of the Python code.
3. Once you have declared the function and its operation, the next thing is
making the function call. A function call is typically giving a green signal to
the Python that now is the time to execute the operation inside the
function.
4. To make a function call, we just have to type the name of the function
followed by the open and closed parenthesis. We can even call a function
inside any other function like it is done in the example above or we can call
it anyplace else on the same program.
29
FUNCTION PARAMETERS
30
Note:
1. Even though there is no restriction on the number of arguments passed to a
Python function, it is important to keep in mind that Python can process
only as many number of parameters as the arguments passed to it. In other
words, if you pass three arguments and then initialize values for more than
these three arguments in the function, then Python will not run and show
an error.
2. The values to be given to a parameter that is passed to a function muse be
carefully assigned in case of functions with multiple parameters. Any
discrepancy in this part will not be detected and the Python program will
run but show an output on the console that is not desired.
Eg:
31
Eg:
Above, you will note that the arguments passed to the function by
the name website, are then set as default by saving them to a
variable. This new variable can then later be used anytime to assign
a new value of edit the current value in the function parameter. It
should be noted that even though this saves a lot of time and
effort, such an operation is not feasible in case your program has
numerous function calls and definitions as setting defaults for each
one of them will affect the performance and speed of the program.
32
33
Eg:
34
Eg:
35
Note:
1. First and the most important point about using global variables is that their
values cannot be altered. Since global variable are available to the entire
length of the program, their value remains the same as the one initialized at
the beginning. Therefore, an operation such as
x +=1 is not valid if x is a global variable. You can however do this using a
global keyword, the same will be discussed later in the blog post.
2. However; you can use the global variable in any calculation and assign the
value of the calculation to a new variable. This statement can be verified in
the example where global variable x is used to perform an addition
calculation and then the value is assigned to the local variable y.
Eg:
36