Lab 5 - Python Language
Lab 5 - Python Language
Content
Introduction
IDLE
Data types
Mathematical operators
Input /Output
Strings
Range() function
Control structure
Iteration
For loop and while loop
Introduction to Python
You can download the latest version by visiting the Python home page,
at http://www.python.org.
-20%3
Operator Precedence
Print() function
A program consists of one or more statements. A statement
is an instruction that the interpreter executes.
a=input(‘Enter a number:’)
print(a)
a=int(input(‘Enter a number:’))
print(a)
a=float(input(‘Enter a number:’))
print(a)
Built-in Functions
Note: Open a new file in file menu and save the file
with the extension of .py
Built-in Functions (Cont)
Strings
S=‘ABCDEFGHIJKL’
1. S[2:6]
2. S[:8]
3. S[3:]
4. S[2:9:2]
5. S[-3:]
6. S[:]
7. S[::-1]
8. len(S)
The range function
The range function (Cont)
Execute the following codes:
for i in range(10):
print(i)
for i in range(10):
print(i, end=‘ ’)
for i in range(3,9):
print(i)
Conditional Statement
Comparison Operators
For loops
Probably the most powerful thing about computers
is that they can repeat things over and over very
quickly.
Iteration and Loops
Use this syntax when you have only one statement to repeat
Use indentation to format two or more statements below the loop header
for count in range(3):
print('Hello!')
Count n print('goodbye')
times
Hello!
goodbye
Hello!
Statements goodbye
Hello!
goodbye
Using the Loop Variable
The loop variable picks up the next value in a sequence on each pass
through the loop
Compute and print the sum of the numbers between 1 and 5, inclusive
total = 0
for n in range(1, 6):
total = total + n
print(total)
Output
Iteration (Cont)
Output
Iteration (Cont)
While Loop