Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Python Lecture 5

Uploaded by

jibransarwari2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python Lecture 5

Uploaded by

jibransarwari2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MODERN PROGRAMMING LANGUAGE

(Python)
Control Statements
Learning Outcome

• You should learn Comparison operators


• Should Know about If statement in python
• Should be able to use elif statements in python
• How to use For loops in python
• How to use While loops in python
• Some General programming practice
Comparison operator

•Python Arithmetic Operator


•Python Relational Operator
•Python Assignment Operator
•Python Logical Operator
•Python Membership Operator
•Python Identity Operator
•Python Bitwise Operator
• EX: X >3 and X<5
• X== 6 or Y>4
Conditional statements
• There are basic three types of conditional statements:

• if statements
• elif statements
• else statement
Indentation

• For the control flow in python we need to use indentation and colon
• Indentation makes the python to be different than other languages
• It is used to create blocks of code instead of bracket ( { } )
• Example:
elif, else
Practice
• Using if elif statements, print the number for the corresponding grade.

• A=4, B=3, C=2, D=1, F=0

• Get the input from user, using:


• Grade=int(input(“Enter the grade”) ) == 99 A+
• if grad => 91 and grad <=100:
• Print (“A + ”)
if grad == 4:
print (“A”)
For loops
• Most of Objects in Python are iterable
• We can iterate through the different parts or elements of a list, tuple, dictionary
and string
• Example:
Customized for loop
• To print only some portions of the elements of list we can define as the
following:
• consider the following list example:

• To print from 2 to 5 :
• Start list[1:2]
• End
• Step
More examples
• Function range (num) can provide any number from 0 up to the range number-1.
• The range(starting, ending, step)
• for (int I =0; I < 5 ; I ++)
• Here we get number from zero up to 4

• To combine for with if :


Practice
• Question 1. Create a list of numbers
• sum
• Range(10)
• sum/Len (list)
• Sum all the numbers
• Find the average and print the output
Iterate through String
• We can iterate through different letters or characters of a string :
Having nested for
Iterate through dictionary
• There are three function for the dictionary:
• Dic.values()
• Dic.keys()
• Dic.items()
• Example:
Break , Continue, Pass

• Break:
• Breaks out the current loop and goes to the rest of the code
• Continue:
• Skips the rest of the current iteration and goes to the next one
• Pass:
• Does nothing, lets the interpreter to continue with code
Thank You!

You might also like