Unit-I
Unit-I
in Python
Dr. Taranpreet Singh Ruprah
Assistant Professor
CSE Department
8600465667- taranpreet.singh@ritindia.edu
Unit-I Content
• Introduction to Python fundamentals:
• Python introduction, Python syntax, Python comments, Python
variables, Python
• data types, Python numbers, Python casting, Python strings, Python
Booleans,
• Python operators, Loops and Conditional Statement If-else, while, for,
lambda, arrays, Python Iterators, Python scope
Control Statements
• Python If else
• Nested if statement
• Python if-elif-else Ladder
• Python If Else on One Line
• Ternary Condition in Python
• Match Case Statement
• For Loop
• While Loop
• Loop control statements (break, continue, pass)
Python If Statement
• The if statement is the most simple decision-making
statement.
• It is used to decide whether a certain statement or
block of statements will be executed or not.
if condition:
# Statements to execute if
# condition is true
i = 10
if condition:
statement1 if (i > 15):
statement2 print("10 is less than 15")
# Here if the condition is true,
if block will consider only statement1
to be inside its block.
Python If Else Statement
• The if statement alone tells us that if a condition is
true it will execute a block of statements and if the
condition is false it won’t. But if we want to do
something else if the condition is false, we can use
the else statement with the if statement Python to
execute a block of code when the Python if condition
is false.
i = 20
if (condition): if (i < 15):
# Executes this block if print("i is smaller than 15")
# condition is true print("i'm in if Block")
else: else:
# Executes this block if print("i is greater than 15")
# condition is false print("i'm in else Block")
print("i'm not in if and not in else Block")
Nested If Statement
• A nested if is an if
statement that is the
target of another if
statement. Nested if
statements mean an if
statement inside
another 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):
# First if statement
if (i < 15):
print("i is smaller than 15")
if (i < 12):
print("i is smaller than 12 too")
else:
print("i is greater than 15")
Python Elif if (condition):
statement
elif (condition):
• The if statements are statement
executed from the top .
down. .
else:
• As soon as one of the statement
conditions controlling
the if is true, the
statement associated i = 25
with that if is executed, if (i == 10):
and the rest of the print("i is 10")
elif (i == 15):
ladder is bypassed. If print("i is 15")
none of the conditions elif (i == 20):
is true, then the final print("i is 20")
“else” statement will else:
print("i is not present")
be executed.
Ternary Statement | Short Hand If Else Statement
• Whenever there is only a single statement to be executed inside the
if block then shorthand if can be used. The statement can be put on
the same line as the if statement.
if condition: statement
while expression: i = 1
statement(s) while i < 6:
print(i)
i += 1
The break Statement The continue Statement
• With the break statement we can • With the continue statement we
stop the loop even if the while can stop the current iteration, and
condition is true: continue with the next:
i = 1
while i < 6:
i = 0
print(i)
while i < 6:
if i == 3:
i += 1
break
if i == 3:
i += 1
continue
print(i)
for loops cannot be empty, but if you for some reason have a for loop with
no content, put in the pass statement to avoid getting an error.
for x in [0, 1, 2]:
pass
The else Statement
i = 1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
Python For Loops
• A for loop is used for iterating over a sequence (that is either a list, a tuple, a
dictionary, a set, or a string).
• This is less like the for keyword in other programming languages, and works
more like an iterator method as found in other object-orientated
programming languages.
• With the for loop we can execute a set of statements, once for each item in
a list, tuple, set etc.
n=4
for i in range(0, n):
print(i)
The pass Statement
• for loops cannot be empty, but if you for some reason have a for loop
with no content, put in the pass statement to avoid getting an error.
for x in [0, 1, 2]:
pass
s = “RIT”
# Pass statement
# Empty loop for i in s:
for i in s: if i == 'k':
# No error will be raised print('Pass executed')
pass pass
print(i
# Empty function
def fun():
pass
x = lambda a : a + 10 x = lambda a, b : a * b
print(x(5)) print(x(5, 6))
def myfunc(n):
return lambda a : a * n
mydoubler = myfunc(2)
print(mydoubler(11)
Python Scope
• A variable is only available from inside the region it is created. This is
called scope.
Local Scope
def myfunc():
A variable created inside a function belongs to x = 300
print(x)
the local scope of that function, and can only be used
inside that function. myfunc()
def f():
def myfunc(): # local variable
x = 300 s = "I love RIT"
def myinnerfunc(): print("Inside Function:", s)
print(x)
myinnerfunc()
# Driver code
f()
myfunc() print(s)
x = 300
x = 300
If you operate with the same variable name inside and def myfunc():
x = 200
outside of a function, Python will treat them as two print(x)
separate variables, one available in the global scope
myfunc()
(outside the function) and one available in the local
scope (inside the function): print(x)
Global Keyword
def myfunc():
• If you need to create a global variable, but are stuck in global x
the local scope, you can use the global keyword. x = 300
print(x)
myfunc()
print(x)