02-Python2
02-Python2
if condition:
# Statements to execute if condition is true
i = 10
if (i > 15):
print("10 is less than 15")
print("I am Not in if")
If…else statement
if (condition):
# Executes this block if condition is true
else:
# Executes this block if condition is false
i = 20
if (i < 15):
print("i is smaller than 15")
print("in if Block")
else:
print("i is greater than 15")
print("in else Block")
print("not in if and not in else Block")
If… elif… else
Statement
if (condition):
statement
elif (condition):
... statement
else:
statement
i = 20
if (i == 10):
print("i is 10")
elif (i == 15):
print("i is 15")
elif (i == 20):
print("i is 20")
else:
print("i is not present")
Nested If Statement
if (condition1):
# Executes when condition1 is true
if (condition2):
# Executes when condition2 is true
# if Block is end here
# if Block is end here
i = 10
if (i == 10):
if (i < 15):
print("smaller than 15")
if (i < 12):
print("smaller than 12")
else:
print("greater than 15")
Short- hand if & if…else statements
➔ If there is only one statement to execute, the If & If … else statements can
be put on the same line
if condition: Statement
i = 10
if (i > 15): print("10 is less than 15")
i = 10
print(True)if (i < 15) else print(False)
Loop Control
Statements
○ for loop statements
○ while loop statements
○ The range() function
○ Loops with break statement
○ Loops with continue statement
○ Loops with else statement
○ Loops with pass statement
for Loop Statements
➔ is used for sequential traversals, i.e. iterate over the items of squensence
like list, string, tuple, etc.
➔ In Python, for loops only implements the collection-based iteration.
while expression:
statement(s)
count = 0
while (count < 10):
count = count + 1
print(count)
The range() function
➔ is used to specific number of times whereby a set of code in the
for loop is executed.
➔ returns a sequence of numbers, starting from 0 by default, and
increments by 1 (by default), and ends at a specified number.
range(start_number, last_number,
increment_value)
i = 0
while i < 7: for x in range(7):
i += 1 if (x == 4):
if i == 4: continue
continue print(x)
print(i)
➔ Definition syntax:
Function
Ex:
# A function to check
# whether n is even or
odd
def CheckEvenOdd(n):
if (n % 2 == 0):
print("even")
else:
print("odd")
➔ Opening file
➔ Reading file
➔ Writing to file
➔ Appending file
➔ With statement
Opening file
➔ Using the function open(): File_object=open(filename,
■ Filename: the name of file mode)
■ mode represents the purpose of the opening file with one of the
following values: Ex:
# a file named "sample.txt",
● r: open an existing file for a read operation. will be opened with the
● w: open an existing file for a write operation. reading mode.
● a: open an existing file for append operation. file = open('sample.txt',
● r+: to read and write data into the file. 'r')
The previous data in the file will be overridden. # This will print every line
● w+: to write and read data. It will override existing data. one by one in the file
● a+: to append and read data from the file. It won’t override existing for each in file:
data. print (each)
Reading file
➔ Using the function read(): File_object.read(siz
e)
■ size <=0: returning a string that contains all characters in the file
# read() mode
file = open("sample.txt",
"r")
print (file.read())
■ size>0: return a string that contains a certain number of characters
size
# read() mode character wise
file = open("sample.txt",
"r")
print (file.read(3))
Closing File
➔ Using close() function to close the file and to free the memory space
acquired by that file
➔ used at the time when the file is no longer needed or if it is to be
opened in a different file mode.
File_object.close()
Writing to file
➔ Using the function write()to insert a string in a single line in the text file and the
function writelines()to insert multiple strings in the text file at a once time.
Note: the file is opened in write mode
File_object.write/
writelines(text)