You are on page 1of 36

Data Types

int, float, str


Variable names
• Must start with a letter or _
• Followed by letter(s), digit(s), _
• Variable names are case sensitive
• Use camel case naming convention
Built-in Data Types --- Numbers
• int
• x = 3,
• a = -728
• type(a) à int We can use the type() function to know which class a
variable or a value belongs to
• float
• a = 235.27 ,
• b = 0.000025,
• C = -.25e4 # ßscientific notation, meaning -2500.0, -.25 x 10 4
• d = .25e-4 # .000025 same as b
• type(b) à float
Mixed mode Arithmetic
• When both operands of an expression are of the same numeric type,
the resulting value is also of that type 1+2 à 3
When each operand is of a different type, the resulting value is of the
more general type 1+2.5 à 3.5
Arithmetic operations on numbers
operator meaning syntax
+ Add 1+2 à 3 , 1+2.5 à 3.5
- Subtract 1 – 2 à -1
* Multiply 1*2 à2 , 1.5 * 2à 3.0
/ Divide ½ à 0.5 , 2/2 à 1.0, 3/2à1.5
// Quotient 1 // 2 à 0, 2//2à1, 3//2à1
% Remainder , modules 1 % 2 à 1 , 2%2à 0, 3 %2à1
- Negation -2 à -2, -a
** Exponentiation 2**3è8
Arithmetic operations, precedence rules:

• ** has the highest precedence and is evaluated first


• Unary negation is evaluated next
• *, /, and % are evaluated next
• + and - are evaluated next
• = evaluated last
• Exception: operations of equal precedence are left associative, so they are
evaluated from left to right BUT ** are right associative
• You can use () to change the order of evaluation
Arithmetic operations, precedence rules,
examples

Expression Evaluation Result


7+3*4 7 + 12 19
(7 + 3) * 4 10 * 4 40
7+3*4-2 7 + 12 – 2 , 19 - 2 17
3 * 2 ** 4 3 * 16 48
2 ** 2 ** 3 2 ** 8 256
(2 ** 2) ** 3 4 ** 3 64
9 % 3 , 10% 3, 11%3 ,12% 3, 13%3 0 , 1 , 2, 0, 1
2 % 0 , 2 / 0, 2//0 Error, cant divide by 0
Type conversion functions for numbers
• int (< number or string with digit>)
• int(3.75) à 3 #truncation not rounding
• int(“73”) à 73

• float(<number or string with digit>)


• float(35) à 35.0
• float(‘73.58’)à 73.58

• 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

• round(<number> , n) #default n=0, otherwise n values after decimal point


• round(54.2345) : 54
• round(54.5345) : 55
• round(54.6593,1) : 54.7
• round(54.6493,1) : 54.6
• round(54.6493, 2) : 54.65
• round(54.6413, 2) : 54.64
Built-in Data Types --- Strings
• You can use ‘abc’ or “abc” for string literals
• greeting = “hello there” or greeting = ‘hello there’
• type(greeting) à str
• empty = “”
• numbers = ‘1234’
• initial = ‘a’
String concatenation +
String repeat *
• firstName = ‘shereef’
• lastName = ‘abu al-maati’
• fullName = firstName +” “+lastName

• The * operator allows you to build a string by repeating another


string a given number of times
• “a“ * 20 à ‘aaaaaaaaaaaaaaaaaaaa’
Concatenate only works with strings as both arguments

• 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(value,…, sep=‘ ‘, end =‘\n’)

print function prints arguments then at end goes to new line.

print('Hello csis230 I hope you enjoy coding in Python')


print(‘Hi’)
print(‘How’)
print(‘are you’)

Hello csis230 I hope you enjoy coding in Python


Hi
How
are you
print function

print function prints arguments and if the reserve word end


argument is not new line (\n), then function says, print
arguments and stay on same line.

print('Hello csis230 I hope you enjoy coding in Python')


print('Hi‘ , end=’’) #print and stay on same line
print('How‘ , end=’’) #print and stay on same line
print('are you')

Hello csis230 I hope you enjoy coding in Python


HiHoware you
print function

print function prints arguments and if the reserve word end


argument is not new line (\n), then function says, print
arguments and stay on same line.

print('Hello csis230 I hope you enjoy coding in Python')


print('Hi‘ , end=’ / ’) #print and stay on same line
print('How‘ , end=’ / ’) #print and stay on same line
print('are you')

Hello csis230 I hope you enjoy coding in Python


Hi / How / are you
print('Next, we will display HI in big letters') Next, we will display HI in big letters
print() #print empty line
print(' | |') | |
print(' | |') | |
print(' | |') | |
print(' |---------|') |---------|
print(' | |') | |
print(' | |') | |
print(' | |') | |
print() #print empty line
print(' --- ') ---
print(' | ') |
print(' | ') |
print(' | ') |
print(' | ') |
print(' | ') |
print(' | ') |
print(' --- ') ---
print('Next, we will display HI in big letters') Next, we will display HI in big letters
print(‘print function with several arguments') print function with several arguments
print() #print empty line
#print 2 pieces of data each seperated by comma | | ---
print(' | |' , ' --- ') | | |
print(' | |' , ' | ') | | |
print(' | |' , ' | ') |---------| |
print(' |---------|' , ' | ') | | |
print(' | |' , ' | ') | | |
print(' | |' , ' | ') | | ---
print(' | |' , ' --- ')
print('we will use the print function with several arguments, each separated by a comma')
print(1,2,3,4,5) #several data in one print
print() #print an empty line

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')

print() #print an empty line


print(1,'one',2,'two',sep='<??<') #sep='string'
print('the above print output had <??< as a sepaarator between each argument')

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

#variable names are case sensitive;


#The = is the assignment operator
name = ‘shereef’
Name = ‘omar’
Print(name,Name)

shereef omar
variables & assignment statement

num1 = 2
num2 = 3
sum = num1 + num2
print(num1, '+', num2, ' = ', sum)

2 + 3 = 5
variables & assignment statement

#multiple values to multiple variables


num1, num2, num3 = 1,2,3
sum = num1 + num2 + num3
print(num1, '+', num2, ‘+’, num3, ' = ', sum)

1 + 2 + 3 = 6
variables & assignment statement

#multiple variables assigned the same value


num1 = num2 = num3 = 1
sum = num1 + num2 + num3
print(num1, '+', num2, ‘+’, num3, ' = ', sum)

1 + 1 + 1 = 3
variables & assignment statement

#Note, you do not need to declare a variable’s data type, it is


#determined dynamically when the statement is executed.
var = 1
print(var)
var = ‘shereef’
print(var)

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

String ß--- input(message)

input is a function that takes in string argument (the question),


then displays it on the screen and waits for the user to type
something on the keyboard and press enter. Once enter is pressed the
function returns a string representation of what was typed.

input('Hi, what is your name? ')

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

#name is a variable referring to a data of type string


name = input('Hi, what is your name? ')
print('Hello ‘ , name , ' I hope you enjoy coding in Python')

Hi, what is your name? shereef abu al-maati


Hello shereef abu al-maati I hope you enjoy coding in Python
input function &
variables & assignment statement &
int function & int variables
name = input('Hi, what is your name? ')
print('Hello ',name,' I hope you enjoy coding in Python')

ageTemp = input('How old are you? ')


age = int(ageTemp) #int function converts string digits to a number data type
nextYearAge = age + 1
lastYearAge = age - 1

print(name,' next year you will be ',nextYearAge,' years old, yaaaay')


print(name,' last year you were ‘,lastYearAge,' years old')

Hi, what is your name? Shereef abu al-maati


Hello Shereef abu al-maati I hope you enjoy coding in Python
How old are you? 35
Shereef abu al-maati next year you will be 36 years old, yaaaay
Shereef abu al-maati last year you were 34 years old
input function &
variables & assignment statement & int
function & math operators
name = input('Hi, what is your name? ')
print('Hello ',name,' I hope you enjoy coding in Python')

age = int( input('How old are you? ‘)) #convert string to int with int() function
nextYearAge = age + 1
lastYearAge = age - 1

print(name,' next year you will be ',nextYearAge,' years old, yaaaay')


print(name,' last year you were ‘,lastYearAge,' years old')

Hi, what is your name? Shereef abu al-maati


Hello Shereef abu al-maati I hope you enjoy coding in Python
How old are you? 35
Shereef abu al-maati next year you will be 36 years old, yaaaay
Shereef abu al-maati last year you were 34 years old
Float function & float variables

>>> deposit1 = float(input("deposit 1 in KD: "))


deposit 1 in KD: 250.750
>>> deposit2 = float(input("deposit 2 in KD: "))
deposit 2 in KD: 12.750
>>> deposit1+deposit2
263.5
>>>

You might also like