L04 - Main Function, Arguments and Expressions
L04 - Main Function, Arguments and Expressions
Semester 1, 2021
Lecture 4,
Main Function, Arguments and Expressions
COMMONWEALTH OF AUSTRALIA
WARNING
2
Lecture 4: Main Function, Arguments
and Expressions
Python’s does not have a main function. Instead, the code always
begins from the beginning of the .py file.
[1]Technically, there can be anything that executes before this. The programmer is not
If you don’t want to use input, such as if you want to run your program
like this:
7
The command-linearguments
8
Program arguments
Now that we know the starting point, we can obtain the values we
provide at command line
1 import sys
print( sys. argv [0]) # print program name
9is / 36
2
INFO1110 2020 S2 Dr. John Stavrakak
1 import sys
2
3 x = float( sys. argv [1])
4 y = float( sys. argv [2])
5
6 print(" The sum of " + str( x) + " and " + str( y) + " is " + str( x+ y));
10
float()
float() is a way of converting a String into a float.
now the String “12.3” has been parsed — that is, read and understood — as a
float value, and then stored in x.
(There is also int() : it parses a string like “123” as the integer number
123.)
11
Expression
13
Assignment
1 x = 4
2 y = x
14
We construct expressions using operators
15
Operator terminology
An operator can be
unary operating on one operand;
binary operating on two operands
ternary operating on three operands
16
Assignment operator =
There are several operators in Python to make your life easier. The most
common you’ll probably use is the assignment operator:
=
We’ve seen this before: use = to assign the value on the left to take the
value on the right:
lvalue ← rvalue
lvalue = rvalue
17
Operators +, - , *, /
The next set of operators are very straightforward: they are the standard
operators of mathematics:
+ − × ÷ (mathematical)
+ - * / (code)
For example:
1 x = 5
2 y = 3
3 z = x + y # z gets the value of ( x+ y), which is 8
4 x = -x # now x is -5
5 z = z + x # now z is 3
6 y = y* 2 # now y is 6
18
Warnings about operators
Integer division
1 x=3/2
2
print( x)
3
4 y=1/0
5 print( y)
1 x =6
2 y = ++ x
3 z =( x + --y)
4 n =0
5 n += 1
6 print("x = " + str( x))
7 print("y = " + st r( y))
8 print("z = " + str( z))
9 print("n = " + str( n))
10 x =5
11 r = 1.2
12 s =x * r
13 print("x = " + str( x))
14 print("r = " + str( r))
15 print("s = " + str( s))
16 s = s - 1.1
17 print("s = " + str( s))
20
Simple Calculations
21
+ and Strings
Above, in the lines
10 x =5
11 r = 1.2
12 s =x * r
and
14 print(" r =" +str( r)) 22is / 36
INFO1110 2020 S2 Dr. John Stavrakak
there is a “+” sign in arguments of the print method call. It’s there to
concatenate two Strings.
"x = " is a String, and print can only print String objects, so it converts the
whole expression to a single String.
+ and Strings(cont.)
What do you think this prints out:
???
In general you can use the “+” to concatenate any two Strings, e.g.,
like this:
1 msg = " H e l l o , " + s y s . a r g v [ 1 ] + " , how a r e you ? "
23
Operators +=, -= , *= and / =
These operators are a very nice shorthand. They all operate in the same
way, by modifying the operand on the left, using the operand on the
right.
shorthand equivalent to
x += n x=x+n
x -= n x=x–n
x *= k x = x*k
x /= k x = x/k
In general,
x □= y
is equivalent to
x = x □ y,
25
Comparing int and boolean
1 x = 4
2 y = 4
3 z = 2
4 xySame = ( x == y )
5 xzSame = ( x == z )
6 p r i n t ( " xy Same = " + s t r ( xy S a m e ) )
7 p r i n t ( " xzSame = " + s t r ( x z S a m e ) )
8 p r i n t ( " ( xy Same == xz S a me ) = " + s t r ( xySame == xzSame))
26
Comparing Strings: msg1 = = msg2 ?
Yes. The = = method will visit the area of memory of the two strings
and compare the content.
String msg1
char 0 char 1 char 2 char 3 char 4
0x00FA1100
H E L L O
String msg2
char 0 char 1 char 2 char 3 char 4
0x00100FF
H E L L O
not!
There are two ways to negate. There is the keyword not and the
negation operator: “!” — it’s the exclamation mark. In code you’ll see
this quite often, for example in expressions like
1 x = 1
2 y = 2
3 same = x != y
4 print( same)
is equivalent to
( x != y )
but
( n o t x ) == y i s d i f f e r e n t
30
And and Or
[2]It is not obvious, but other languages will use other symbols
Operator Precedence
What is the result of:
1 x =1 +5 * 3 +2
2 y =8 / 4 - 1 / 1 - 1
1 x = (1 + 5) * (3 + 2)
2 y = 8 / (4 - (1 / 1) - 1)
Assignment
33
Assignment is not equality
Saying x = 3 in mathematics means “x is a variable whose value is
currently 3”.
In that sense the equivalent statement is 3 = x, but writing that in a
Python program doesn’t make sense: you would be attempting to
change the value of 3!
If you want to test whether x has the value 3, you would evaluate the
following expression:
1 ( x = = 3)
which has the Boolean value True if x really does equal 3 and the value
False otherwise.
34
Comparison —warning!
Comparing floating point numbers for equality is a BAD IDEA .
1 f1 = 0.00015 + 0.00015
2 f2 = 0.0002 + 0.0001
3 matches = ( ( f1 == f2 ) )
4 print(" f1 = " + str( f1 ) )
5 print(" f2 = " + str( f2 ) )
6 print(" matches = " + str( matches))
prints out
f1 = 0.0003
f2 = 0.00030000000000000003
matches = False
36