Meeting3-Block 1-Part 2-Introduction To Python
Meeting3-Block 1-Part 2-Introduction To Python
Information Technology 2
Meeting #3
Block 1 (Part 2- Intro)
Introduction to Python
• Interactive Mode
• gives you immediate feedback
• Not designed to create programs to be saved and run later
• Script Mode
• Write, edit, save, and run (later)
• Save your file using the “.py” extension
The message can be a string, or any other object, the object will be
converted into a string before written to the screen.
6
Your First Python Program
• Python is "case-sensitive":
• print("hello") #correct
• print('hello') #correct
• Print("hello") #error
• PRINT("hello") #error
8
String Literals
Example 1: Get the character at position 1 (remember that the first character
has the position 0):
a = "Hello, World!"
e
print(a[1])
9
Program Documentation
• Example1:
>>> x, y = 2, 3
>>> x
2
>>> y
3
• Example2:
>>> x = 5; y = 4; L = [0,1,2]
>>> x = '100'
>>> y = '-90'
>>> print (x + y)
Since they are strings, x and
100-90 y will be concatenated
• Casting to floats:
x=float(input(“enter the value”)) #5.0
y=float(input(“enter the value”)) #10.0
x+y = 15.0
• Casting to strings:
x = str("s1") # x will be 's1'
y = str(2) # y will be '2'
z = str(3.0) # z will be '3.0'
Can be used
also for string
concatenation:
y="hello"
y=y+“ world!"
Monday, Novemb 21
er 21, 2022
If statement
●
The general form of an if statement is:
If condition:
block
● Example
if grade >=50:
Needs print “pass”
indentation
• The condition is a Boolean expression
• The block is a series of statements
• If the condition evaluates to true the block is executed
Monday, Novemb 22
er 21, 2022
Indentation-No Braces
Example:
a = 33
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
Output:
a and b are equal
25
The else Statement
The else keyword catches anything which isn't caught by
the preceding conditions.
Example:
a = 200
b = 33
if b > a:
print("b is greater than a")
elif a == b:
print("a and b are equal")
else:
print("a is greater than b")
Output:
a is greater than b
Output:
Enter the temperature: 50
Enter the unit: C/F: C
50.0 Celsius is 122.0 Fahrenheit
>>>
Monday, November 21, 31
2022
For Loop
A for loop is used for iterating over a sequence (that is either a list, a
tuple, a dictionary, a set, or a string).
With the for loop we can execute a set of statements, once for each
item in a list, tuple, set etc.
The output:
No
indentation
here; so it is
outside the
loop
33
Monday, November 21,
2022
For Loop
--output--
A
B
C
D
C
D
C
D
C
D
C
D
E
Monday, November 21, 34
2022
The range function
• To loop through a set of code a specified number of times, we can
use the range() function.
• The range() function returns a sequence of numbers, starting from 0
by default, and increments by 1 (by default), and ends at a specified
number.
• The value we put in the range function determines how many times
we will loop.
• The range function produces a list of numbers from zero (by
default, unless other is specified) to the value minus one.
• For instance, range(5) produces five values:
0, 1, 2, 3, and 4.
Monday, November 21, 35
2022
The range function
--output--
0
1
2
• Prints the numbers from 0 to 99. 3
.
.
.
.
.
.
.
.
99
Monday, November 21, 36
2022
The range function
Example
• Since the loop variable i, gets increased by 1 each time
through the loop, it can be used to keep track of where we
are in the looping process. Consider the example below:
• Another thing we can do is to get the list of values to go up by more than one at a time
To do this, we can specify an optional step as the third argument.
range(1,10,2) steps through the list by twos, producing 1, 3, 5, 7, 9.
Output
----output----
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625
6 36 216 1296
>>>
Monday, November 21, 40
2022
The range function
Here is a program that counts down from 5 and then prints a message:
Output:
Note:
• Python’s print() function comes with a parameter called ‘end’.
• By default, the value of this parameter is ‘\n’ (the new line character).
• You can end a print statement with any character or string using this parameter.
while condition:
action
As long as the condition is true, the while statement will execute the action.
Example:
x = 1
while x < 4: # as long as x < 4...
print x**2 # print the square of x
x = x+1 # increment x by +1
--output--
1 # only the squares of 1, 2, and 3 are printed, because
4 # once x = 4, the condition is false
9
Monday, November 21, 43
2022
The while loop
The following while and for loops are equivalent
Example:
x = 1
while x == 1:
print('Hello world‘)
# so-called Infinite loop! Python will keep printing
# “Hello world” because x does not change
x = 1
---output---
while x < 3 :
1
print (x)
2
x = x + 1 hello
else:
print ('hello')
6
Functions & Returns
• Other function are not built-in and require you to import the
math library.