Python Programming - Introduction All
Python Programming - Introduction All
• Like Perl, Python source code is also available under the GNU
# Integer number
num = 100
print(num)
print("Data Type of variable num is", type(num))
Float
Values with decimal points are the float values, there is no need to specify the data type in
Python. It is automatically inferred based on the value we are assigning to a variable.
# float number
fnum = 34.45
print(fnum)
print("Data Type of variable fnum is", type(fnum))
2. Python Data Type – String
String is a sequence of characters in Python. The data type of String in Python is called
“str”.
Strings in Python are either enclosed with single quotes or double quotes.
# Python program to print strings and type
s = "This is a String"
s2 = 'This is also a String'
tup1 = ();
To write a tuple containing a single value you have to include a comma, even though there
is only one value −
tup1 = (50,);
Accessing Values in Tuples
To access values in tuple, use the square brackets for slicing along with the index or indices to
obtain value available at that index.
flag = True
if flag:
print("Welcome")
print("To")
print("BeginnersBook.com")
Python –if..else statement
if condition:
block_of_code_1
else:
block_of_code_2
block_of_code_1: This would execute if the given condition is true
block_of_code_2: This would execute if the given condition is false
num = 22
if num % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Python –elif statement
The elif statement allows you to check multiple expressions for TRUE and execute a block of code as
soon as one of the conditions evaluates to TRUE.
Similar to the else, the elif statement is optional. However, unlike else, for which there can be at most
one statement, there can be an arbitrary number of elif statements following an if.
syntax
if expression1:
statement(s)
elif expression2:
statement(s)
elif expression3:
statement(s)
else:
statement(s)
num = 1122
if 9 < num < 99:
print("Two digit number")
elif 99 < num < 999:
print("Three digit number")
elif 999 < num < 9999:
print("Four digit number")
else:
print("number is <= 9 or >= 9999")
Python –Loops
A loop statement allows us to execute a statement or group of statements multiple times.
Python –while loop
A while loop statement in Python programming language repeatedly executes a target statement as
long as a given condition is true.
Syntax
The syntax of a while loop in Python programming language is −
while expression:
statement(s)
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any non-zero value. The loop iterates while the condition is true.
When the condition becomes false, program control passes to the line immediately following the loop.
In Python, all the statements indented by the same number of character spaces after a programming
construct are considered to be part of a single block of code. Python uses indentation as its method of
grouping statements.
Python –while loop
count = 0
while (count < 9):
print ('The count is:', count)
count = count + 1
count = 0
while count < 5:
print (count, " is less than 5 ")
count = count + 1
else:
print (count, " is not less than 5 “)
Single Statement Suites
Similar to the if statement syntax, if your while clause consists only of a single statement, it may be placed on
the same line as the while header.
flag = 1
while (flag): print ('Given flag is really true!')
print ("Good bye!")
array = [10,20,30,40,50]
for x in array: # First Example
print ('elements:', x)
For Loop
It has the ability to iterate over the items of any sequence, such as a list or a string.
Syntax
for iterating_var in sequence:
statements(s)
If a sequence contains an expression list, it is evaluated first. Then, the first item in the sequence is assigned to
the iterating variable iterating_var. Next, the statements block is executed. Each item in the list is assigned to
iterating_var, and the statement(s) block is executed until the entire sequence is exhausted.
If the else statement is used with a for loop, the else statement is executed when the loop has exhausted
iterating the list.
for num in range(10,20): #to iterate between 10 to 20
for i in range(2,num): #to iterate on the factors of the number
if num%i == 0: #to determine the first factor
j=num/i #to calculate the second factor
print '%d equals %d * %d' % (num,i,j)
break #to move to the next number, the #first FOR
else: # else part of the loop
print num, 'is a prime number'
break
Python nested loops
Python programming language allows to use one loop inside another loop.
for iterating_var in sequence:
for iterating_var in sequence:
statements(s)
statements(s)
The syntax for a nested while loop statement in Python programming language is as follows −
while expression:
while expression:
statement(s)
statement(s)
Nesting is that we can put any type of loop inside of any other type of loop. For example a for loop can be
inside a while loop or vice versa.
i=2
while(i < 100):
j=2
while(j <= (i/j)):
if not(i%j): break
j=j+1
if (j > i/j) : print i, " is prime"
i=i+1
print "Good bye!"