Professional Documents
Culture Documents
• str(<number>)
• str(73) à ‘73’
Type conversion functions for numbers
• round(<number>) #looks at first digit after decimal point
• round(7.6) à 8
• round(7.49) à 7
• gpa = 3.7
• message = ‘my gpa is ‘+ gpa #error cant convert float to str
• To fix
• message = ‘my gpa is ‘ + str(gpa)
Unicode character set
• ord(‘a’) à 97
• chr(97) à’a’
• chr(1592) à '’ظ
• ord(' )’ظà1592
comments
• # single line comment
• Multi line comment
“”” multi
Line
Comments “””
Or
‘’’ multi
Line
Comments ‘’’
Escape sequence
Escape sequnce meaning
\n New line
\t tab
\\ The \ character
\’ The ‘ character
\” The “ character
help
• help(<functin name>)
• Example help on function int()
• help(‘modules’) – list all modules
• help(help) – how to use help
help
help() – enter interactive help
help> math -- lists all functions in math module
help> math.sqrt – help on sqrt
help> quit – to exit interactive help session
print function
print('next we will print a mix of string and numbers each separated by a coma')
print(1,'one',2,'two') #several mix data types string and int
print('note the output, all arguments are printed but they are each separated by 1 blank')
we will use the print function with several arguments, each separated by a comma
1 2 3 4 5
next we will print a mix of string and numbers each separated by a coma
1 one 2 two
note the output, all arguments are printed but they are each separated by 1 blank
1<??<one<??<2<??<two
the above print output had <??< as a separator between each argument
print() #print an empty line
print(1,'one',2,'two',sep=' --> ', end=' , ')
print('the above print output had <??< as a separator between each argument')
1 --> one --> 2 --> two , the above print output had <??< as a sepaarator between each argument
print('1+2=',1+2)
print(‘3+2=‘,3+2,' and 2 * 3 = ',2*3)
1+2= 3
3+2= 5 and 2 * 3 = 6
variables & assignment statement
variable = expression
shereef omar
variables & assignment statement
num1 = 2
num2 = 3
sum = num1 + num2
print(num1, '+', num2, ' = ', sum)
2 + 3 = 5
variables & assignment statement
1 + 2 + 3 = 6
variables & assignment statement
1 + 1 + 1 = 3
variables & assignment statement
1
shereef
variables & assignment statement
#you can use the type function to determine the type of the variable
var = 1
print(type(var))
var = ‘shereef’
print(type(var))
<class ‘int’>
<class ‘str’>
input function
if you run the above line then the message will display on the screen
Hi, what is your name?
if you type your name then press enter, you will see nothing happens because
the program ended.
What would be nice is to remember what the user entered.
We remember things by using variables and storing values in them.
input function &
variables & assignment statement
age = int( input('How old are you? ‘)) #convert string to int with int() function
nextYearAge = age + 1
lastYearAge = age - 1