[go: up one dir, main page]

0% found this document useful (0 votes)
4 views22 pages

Unit-I

The document provides an overview of object-oriented programming in Python, covering fundamental concepts such as Python syntax, control statements, loops, and functions. It explains various control structures like if-else statements, nested ifs, and looping mechanisms including for and while loops. Additionally, it discusses Python-specific features like lambda functions, variable scope, and the use of global variables.

Uploaded by

muditdeodhar007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views22 pages

Unit-I

The document provides an overview of object-oriented programming in Python, covering fundamental concepts such as Python syntax, control statements, loops, and functions. It explains various control structures like if-else statements, nested ifs, and looping mechanisms including for and while loops. Additionally, it discusses Python-specific features like lambda functions, variable scope, and the use of global variables.

Uploaded by

muditdeodhar007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Object-oriented Programming

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")

# Nested - if statement Will only be executed if statement above it is true

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

statement_when_True if condition else statement_when_False


i = 10
if i < 15: print("i is less than 15") i = 10

print(True) if i < 15 else print(False)


Looping Statement- While Loop
• Until a specified criterion is
true, a block of statements
will be continuously executed
in a Python while loop. And
the line in the program that
follows the loop is run when
the condition changes to
false.

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

• With the else statement we can run a block of code


once when the condition no longer is true:

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.

fruits = ["apple", "banana", "cherry"] for x in "banana":


for x in fruits: print(x)
print(x)

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

# No error will be raised


fun()
Python Functions
• A function is a block of code which only runs when it is called.
• You can pass data, known as parameters, into a function.
• A function can return data as a result.
def my_function(fname):
print(fname + " Refsnes")
def my_function():
print("Hello from a function")
my_function("Emil")
my_function("Tobias")
my_function()
my_function("Linus")

def my_function(fname, lname):


print(fname + " " + lname) def my_function(fname, lname):
print(fname + " " + lname)
my_function("Emil", "Refsnes")
my_function("Emil")
Arbitrary Arguments, *args
• If you do not know how many arguments that will be passed into your function, add a *
before the parameter name in the function definition.
• This way the function will receive a tuple of arguments, and can access the items
accordingly:
def my_function(*kids):
print("The youngest child is " + kids[2])

my_function("Emil", "Tobias", "Linus")

Passing a List as an Argument def my_function(food):


for x in food:
You can send any data types of argument to a
print(x)
function (string, number, list, dictionary etc.), and it
will be treated as the same data type inside the
fruits = ["apple", "banana", "cherry"]
function.
my_function(fruits)
Python Lambda
• A lambda function is a small anonymous function.
• A lambda function can take any number of arguments, but can only
have one expression.
lambda arguments : expression

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

Global Scope def myfunc():


print(x)
• A variable created in the main body of the Python code is
a global variable and belongs to the global scope. myfunc()

• Global variables are available from within any scope, print(x)


global and local.

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

• The global keyword makes the variable global. myfunc()

print(x)

To change the value of a global variable inside a x = 300


function, refer to the variable by using
the global keyword: def myfunc():
global x
x = 200

myfunc()

print(x)

You might also like