Python basics Notes as per aktu syllabus unit-1

mkr280496 5 views 23 slides Sep 16, 2025
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

This is the notes for the python basics.


Slide Content

CONDITIONALSTATEMENTS
Decisionmakingisanticipationofconditionsoccurringwhileexecution
oftheprogramandspecifying actionstakenaccordingtotheconditions.
Followingisthegeneralformofatypicaldecisionmakingstructurefound
in mostoftheprogramminglanguages−
Pythonprogramminglanguage
assumes
zeroand
any non-
non-nullvaluesas
TRUE,
and
ifitiseitherzeroornull,then
itisassumedasFALSEvalue.

Pythonprogramminglanguageprovidesfollowing
typesofdecisionmakingstatements:
•Ifstatements:

Anifstatementconsistsofabooleanexpression
followedbyoneormorestatements.

If-elsestatements:

Anifstatementcanbefollowedbyanoptional else
statement,whichexecuteswhenthebooleanexpression
isFALSE.•
Nestedifelse statements:

Youcanuseone iforelseifstatementinside
anotheriforelseifstatement(s).
CONDITIONALSTATEMENTS

CONDITIONALSTATEMENTS
Ifstatements:
Syntax:
if(expression):
statement(s)
Ex:
val=5
if(val==5):
print("Iamfive")
OUTPUT:

CONDITIONALSTATEMENTS
Ifstatements:
EX:ProgramtocheckthegivennumberisNegative
num=float(input("Enterthenumber::"))
ifnum<0:
print("Thenumber{0} is Negative:".format(num))
OUTPUT:

CONDITIONALSTATEMENTS
If-elsestatements:
Syntax:
if(expression):
statement(s)
else:
statement(s)
Ex:
val=6
if(val==5):
print("Iamfive")
else:
print(“Iamnotfive”)
OUTPUT:

CONDITIONALSTATEMENTS
If-elsestatements:
EX: ProgramtocheckthegivennumberisPositiveorNegative
num=float(input("Enterthenumber::"))
ifnum<0:
print("Thenumber{}isNegative:".format(num))
else:
print("Thenumber{}isPositive:".format(num))
OUTPUT:

If-elif-elsestatements:
Syntax:
CONDITIONALSTATEMENTS
if (expression):
statement(s)
elif(exp):
statement(s)
else:
statement(s)
Ex:
val=10
ifval>10:
print("Value is greaterthan10")
elif(val<10):
print("value is less than 10")
else:
print("Valueis equalto 10")
OUTPUT:

CONDITIONALSTATEMENTS
If-elif-elsestatements:
EX:ProgramtocheckthegivenYearis LeaporNot
year=int(input("Entertheyear::"))
ifyear%400==0andyear%100==0:
print("Theyear{0} isLeapyear.".format(year))
elifyear%4==0andyear%100!=0:
print("Theyear{0}isLeapyear.".format(year))
else:
print("Theyear{0}is NOTLeapyear.".format(year))
OUTPUT:

NestedIf-elsestatements:
Syntax:
ifexpression1:
statement(s)
CONDITIONALSTATEMENTS
if expression2:
statement(s)
elifexpression3:
statement(s)
else:
statement(s)
else:
statement(s)
var=100
ifvar<200:
print(“Valueislessthan200”)
ifvar==150:
print(“Whichis150”)
elifvar==100:
print(“Whichis100”)
else:
print(“Thisisinnerelse”)
else:
print(“Thisisoutelsepart”)

CONDITIONALSTATEMENTS
NestedIf-elsestatements:
EX: Programtocheck thelargestnumberofgiventhreenumbers
num1=float(input("Enter the First number:: "))
num2=float(input("Enter the Second number:: "))
num3=float(input("EntertheThird number::"))
ifnum1>num2:
if(num1>num3):
print("Thenumber{} isthelargestnumber.".format(num1))
else:
print("The number {} is the largest number.".format(num3))
elif(num2>num3):
print("Thenumber{} isthelargestnumber.".format(num2))
else:
print("Thenumber{} isthelargestnumber.".format(num3))

Example:
1.Write a program to find small number of two numbers (if)
2.Write a Program to input age and check whether the person is eligible to vote or not.
3.Write a program to find biggest number among three numbers.

Python For Loops
PythonForLoops
•Aforloopisusedforiteratingoverasequence
(thatiseitheralist,atuple,adictionary,aset,ora
string).
•Thisislessliketheforkeywordinother
programminglanguages,andworksmorelikean
iteratormethodasfoundinotherobject-orientated
programminglanguages.
•Withtheforloopwecanexecuteasetof
statements,onceforeachiteminalist,tuple,set
etc.

Example
Print each fruit in a fruit list:
fruits = ["apple", "kiwi", "cherry"]
for x in fruits:
print(x)

Python While Loops

Looping Through a String
Even strings are iterable objects, they contain a sequence of
characters:
Example
Loop through the letters in the word "banana":for x in "banana":
print(x)

The pass Statement
•ifstatements cannot be empty, but if you for some reason have
an ifstatement with no content, put in the passstatement to
avoid getting an error.
•Example
a = 33
b = 200
if b > a:
pass

The break Statement
•With the break statement we can stop the loop even if the
while condition is true:
•Example
Exit the loop when iis 3:i= 1
while i< 6:
print(i)
if i== 3:
break
i+= 1

The break Statement
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
if x == "banana":
break

The continue Statement
•With the continue statement we can stop the current iteration,
and continue with the next:
•Example
Continue to the next iteration if iis 3:i= 0
while i< 6:
i+= 1
if i== 3:
continue
print(i)

fruits = ["apple", "banana", "cherry"]
for x in fruits:
if x == "banana":
continue
print(x)
The continue Statement
The continue Statement

The range() Function
Example
Using the range() function:
for x in range(6):
print(x)

for x in range(2, 6):
print(x)

Nested Loops
•A nested loop is a loop inside a loop.
•The "inner loop" will be executed one time for each iteration
of the "outer loop":
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]
for x in adj:
for y in fruits:
print(x, y)