1.VarStrNums
1.VarStrNums
Hello world
go
Go
GO
• Lower case as well
1 last_name = "Lamp"
2 print(last_name.lower())
lamp
Derived from notebook prepared by John Lui for CSCI2040 8
Combine strings - Concatenation
1 print ("Our king's first name is =", first_name)
2 print ("Our king's last name is =", last_name)
3 full_name = first_name + last_name
4 print ("Our king's name is = ", full_name)
3.0
1.0
10.0
2.0
16.0
14
20 Derived from notebook prepared by John Lui for CSCI2040 13
Numbers and numerics
• Floating points
print (0.1+0.1)
print (0.1+0.2)
0.2
0.30000000000000004
• There are more differentiation between 3.X and 2.X for Python
8
8.0
AaaBbbbbbCCC
5 > 3 returns True. If both operands are numbers, they are first
> Greater Than converted to a common type. Otherwise, it always returns
False.
Less Than or
<= x = 3; y = 6; x <= y returns True.
Equal To
Greater Than
>= x = 4; y = 3; x >= 3 returns True.
or Equal To
x = 2; y = 2; x == y returns True. x = 'str'; y = 'stR'; x == y
== Equal To
returns False. x = 'str'; y = 'str'; x == y returns True.
!= Not Equal To x = 2; y = 3; x != y returns True.
not Boolean NOT x = True; not y returns False.
x = False; y = True; x and y returns False since x is False. In this
case, Python will not evaluate y since it knows that the value
and Boolean AND
of the expression will has to be false (since x is False). This is
called short-circuit evaluation.
x = True; y = False; x or y returns True. Short-circuit evaluation
or Boolean OR
applies here as well.
Introduction to Programming in Python 18
If statement
• Selection statement for 1 or more choices
if choice == 1:
print ('You choose Cola')
out = 'coke'
Only one outcome
elif choice == 2: is selected
print ('You choose Lemon tea')
out = 'Lemon Tea'
elif choice == 3:
print ('You choose Orange juice')
out = 'Orange Juice'
else:
print ('Invalid choice!')
print ('choose again')
Introduction to Programming in Python 19
If statement
• Selection statement for 1 or more choices
if choice == 1:
print ('You choose Cola') Note the : after each
out = 'coke'
elif choice == 2: Condition
print ('You choose Lemon tea')
out = 'Lemon Tea'
elif choice == 3: Also statements
indented are executed
print ('You choose Orange juice')
out = 'Orange Juice'
else:
print ('Invalid choice!')
for each choice
print ('choose again')
• Or
if score >= 50:
print ('Passed!')
else:
print ('Failed!')
print ('Prepare for next test.')
Introduction to Programming in Python 21
Control Flow
a, b = 0, 1
Output
while b < 10: 1
print (b) 1
2
a, b = b, a+b 3
print ('END') 5
8
END
for x in range(0, 3) :
print (x) Output
0
1
2
42
Conditional Expressions
• In Python, there is a conditional expression construct similar to the
ternary operator in C.
• Syntax: var = true_value if condition else false_value
if condition:
which is equivalent to: var = true_value
else:
var = false_value
• Example:
a, b = 10, 20
if a < b:
a, b = 10, 20 min = a
min = a if a < b else b else :
print(min) # output 10 min = b
print(min) # output 10 43
Output Formatting using a string modulo
• Much like a printf()-style format as in C language, old formatting style
from Python 2
• Examples:
print('%s %s' % ('one','two'))
one two
b = 2.345
print('%02d %.2f' % (1,b))
01 2.35
44
Output Formatting using the format method
• Examples:
print('{} {}'.format('one','two'))
one two
b = 2
print('{} {}'.format(1,b))
1 2
45
Output Formatting using the format method
• With new style formatting, it is possible (and in Python 2.6 even
mandatory) to give placeholders an explicit positional index.
• This allows for rearranging the order of display without changing the
arguments.
This operation is not available
print('{1} {0}'.format('one','two'))
with old-style formatting.
two one
b = 2.345
print('{0} {1:5.2f}'.format(1,b))
1 2.35
46
Output Formatting using the format method
• Indeed, the placeholders can be identified by named indexes {price},
numbered indexes {0}, or empty braces {}.
txt = "For only {price:.2f} dollars!"
print(txt.format(price = 32.1))
For only 32.10 dollars!