]Week 1, college 1
● Install newest Python 2!
● Prepare practicals beforehand
Bits
● If you have 32 bits you can write 232 different codes.
Number types we can manipulate
● Int (Integer: geheel getal (7,8 bijvoorbeeld))
○ How do you write an integer in Python:
○ Exactly the way you just type them: 7
○ But you can only write positive numbers like that.
■ -4 Python sees that as a positive 4 with a - that says make it negative
operation.
○ 0123 Python sees as 83
■ Because Python sees the 0 as an sign for octal number.
■ In the octal system 1238 is 1x84 for example.
○ Only remember that you don’t begin with a number beginning with a 0.
● Long (long integer)
○ If you have very long numbers you need to use long.
○ Notation: If you write 7 as a long you write 7L.
○ You never need the type long in this course, you only need int!!!!
Sequence types
● Str (string)
○ Rows
○ Contains rows of elements
○ String is a row of characters.
■ Characters are symbols
■ All the things you can type on the keyboard are characters.
○ Notation: “abc”
■ or ‘abc’
■ Double quotes or single quotes are not part of the string.
■ Python prefers single quote so it will change it to single quote in the
answer, but you can use double quote.
○ Strings are used to manipulate text.
■ You can put a sentence in a string.
○ Strings are really useful to print text on the screen.
○ But what if you have ‘ab’cd’, this won't work because the second quote will
already close the string.
■ Then you can wrote a double quote: “ab’cd”
○ But what if you have “ab’cd’”ef”
■ You use a special escape character which is the backslash \
● Slash / (wrong)
● backslash \ (right you use this).
, ● backslash takes special meaning away of next character
● So you can say ‘ab\’cd’, then the ‘ in the middle is no special
character but just a character.
● If you print something it won't be in the output.
● But you cannot do ‘\’
○ You can write ‘\\’
○ Backslash takes away the special meaning of the
backslash.
■ Backslash can also give special meaning to next character
sometimes.
● \t is a tab
○ You write that if you do tabs in a string and python will
give that also as answer if you do tabs in a string.
● \n is a new line or return (enter).
○ So you can say ‘ab\ncd’ if you want ab on one rule and
cd on the next line if you print it.
○ If a string has a length of 0 it’s an empty string (0 characters).
● List
○
Rows
○
They contain rows of elements
○
Is a row of anything.
■ Either an int or an long or a float
■ These things you can put in a list.
○ You write a list by opening wit [
○ [ ] empty list.
○ [a] one element etc
○ [a, b, c], you write komma’s in between.
○ You can also have a list inside a list [1, 2, 3, 5, ‘abc’, [1,2]]
● The number types are elements, so no lists.
● One billion is een miljard in Nederlands
Week 1, college 2
2 types of data
● Number types
○ Int (3)
○ Long (3L)
○ Float (Decimals)
■ 1,5 is 1.5 (American style with dot)
■ Is called fixed point notation
■ Floating Point notation (wetenschappelijke notatie je weet wel):
● Is used if you have many 0’s after the decimal point.
● for example (1.6x1019)
○ The decimal point has to move 19 places to the left.
● In python you write (1.6e-19)
, ○ If you write a big E, it can but Python changes it to a
small e in the answer.
● The number of bits that is used to code float numbers is limited.
● So how does Python store all those numbers
● They don’t store it exact.
○ If you use 13.4 than it’s maybe not exact, it’s as close to the real value as
possible.
○ For small numbers it doesn’t matter but for large numbers you will get errors.
● You have a float if you have a decimal point in the number and you have at least one
digit.
○ 3. is a float
○ 3.0 is also a float
○ .5 is 0.5
○ You can also write a half as 5e-1.
Calculations with values
● If you write 1+2 you call the + the operator.
○ The values are operands.
○ 1 is the left operand and 2 is the right operand.
● Arithmetic operators
Math Python
+ (1 operand) +
- (1 operand) -
+ (2 operands, add) +
- (2 operands, substract) -
x ( multiplication) *
/ (divide) /
Modulo %
24 for example 2**4
● But you can only divide same type of numbers (int, long, float) to each other and not
across each other.
○ Also if Python will give the answer in the same type of number.
○ If you do 22/5 if will tell you 4 because you can take 5 4 times out of 22.
○ But you can do 22%5 than it tells you 2, so it tells you how much there is left
after you take out the 5’s.
○ If both operands are ints then the answer will be a int (and with the others).
○ 1 + 2. cannot be calculated.
, ■But the first one can be transformed to the nearest float, so 1.+2. can
be calculated.
■ You change ints into floats, not the other way around.
■ If you do 13/0.5 the answer will always be a float.
■ Remember, floats are not exact.
● Relational operators
Math Python
< <
> <
<= (left is smaller or equal to right). <=
= ==
not = !=
● If you type 3<4 you get a boolean.
○ A boolean is a truth value, has 2 values which are True and False (note the
uppercase letters).
○ Python is uppercase and lowercase sensitive.
○ 3<4 will get the output true.
○ 3>4 will get the output false.
● Boolean operators
○ Were the operators are of the type boolean.
Math Python
and
∧
or
∨
not
¬
Some truth tables
a b a and b
f f F
f t F
t f F
t t T
● Install newest Python 2!
● Prepare practicals beforehand
Bits
● If you have 32 bits you can write 232 different codes.
Number types we can manipulate
● Int (Integer: geheel getal (7,8 bijvoorbeeld))
○ How do you write an integer in Python:
○ Exactly the way you just type them: 7
○ But you can only write positive numbers like that.
■ -4 Python sees that as a positive 4 with a - that says make it negative
operation.
○ 0123 Python sees as 83
■ Because Python sees the 0 as an sign for octal number.
■ In the octal system 1238 is 1x84 for example.
○ Only remember that you don’t begin with a number beginning with a 0.
● Long (long integer)
○ If you have very long numbers you need to use long.
○ Notation: If you write 7 as a long you write 7L.
○ You never need the type long in this course, you only need int!!!!
Sequence types
● Str (string)
○ Rows
○ Contains rows of elements
○ String is a row of characters.
■ Characters are symbols
■ All the things you can type on the keyboard are characters.
○ Notation: “abc”
■ or ‘abc’
■ Double quotes or single quotes are not part of the string.
■ Python prefers single quote so it will change it to single quote in the
answer, but you can use double quote.
○ Strings are used to manipulate text.
■ You can put a sentence in a string.
○ Strings are really useful to print text on the screen.
○ But what if you have ‘ab’cd’, this won't work because the second quote will
already close the string.
■ Then you can wrote a double quote: “ab’cd”
○ But what if you have “ab’cd’”ef”
■ You use a special escape character which is the backslash \
● Slash / (wrong)
● backslash \ (right you use this).
, ● backslash takes special meaning away of next character
● So you can say ‘ab\’cd’, then the ‘ in the middle is no special
character but just a character.
● If you print something it won't be in the output.
● But you cannot do ‘\’
○ You can write ‘\\’
○ Backslash takes away the special meaning of the
backslash.
■ Backslash can also give special meaning to next character
sometimes.
● \t is a tab
○ You write that if you do tabs in a string and python will
give that also as answer if you do tabs in a string.
● \n is a new line or return (enter).
○ So you can say ‘ab\ncd’ if you want ab on one rule and
cd on the next line if you print it.
○ If a string has a length of 0 it’s an empty string (0 characters).
● List
○
Rows
○
They contain rows of elements
○
Is a row of anything.
■ Either an int or an long or a float
■ These things you can put in a list.
○ You write a list by opening wit [
○ [ ] empty list.
○ [a] one element etc
○ [a, b, c], you write komma’s in between.
○ You can also have a list inside a list [1, 2, 3, 5, ‘abc’, [1,2]]
● The number types are elements, so no lists.
● One billion is een miljard in Nederlands
Week 1, college 2
2 types of data
● Number types
○ Int (3)
○ Long (3L)
○ Float (Decimals)
■ 1,5 is 1.5 (American style with dot)
■ Is called fixed point notation
■ Floating Point notation (wetenschappelijke notatie je weet wel):
● Is used if you have many 0’s after the decimal point.
● for example (1.6x1019)
○ The decimal point has to move 19 places to the left.
● In python you write (1.6e-19)
, ○ If you write a big E, it can but Python changes it to a
small e in the answer.
● The number of bits that is used to code float numbers is limited.
● So how does Python store all those numbers
● They don’t store it exact.
○ If you use 13.4 than it’s maybe not exact, it’s as close to the real value as
possible.
○ For small numbers it doesn’t matter but for large numbers you will get errors.
● You have a float if you have a decimal point in the number and you have at least one
digit.
○ 3. is a float
○ 3.0 is also a float
○ .5 is 0.5
○ You can also write a half as 5e-1.
Calculations with values
● If you write 1+2 you call the + the operator.
○ The values are operands.
○ 1 is the left operand and 2 is the right operand.
● Arithmetic operators
Math Python
+ (1 operand) +
- (1 operand) -
+ (2 operands, add) +
- (2 operands, substract) -
x ( multiplication) *
/ (divide) /
Modulo %
24 for example 2**4
● But you can only divide same type of numbers (int, long, float) to each other and not
across each other.
○ Also if Python will give the answer in the same type of number.
○ If you do 22/5 if will tell you 4 because you can take 5 4 times out of 22.
○ But you can do 22%5 than it tells you 2, so it tells you how much there is left
after you take out the 5’s.
○ If both operands are ints then the answer will be a int (and with the others).
○ 1 + 2. cannot be calculated.
, ■But the first one can be transformed to the nearest float, so 1.+2. can
be calculated.
■ You change ints into floats, not the other way around.
■ If you do 13/0.5 the answer will always be a float.
■ Remember, floats are not exact.
● Relational operators
Math Python
< <
> <
<= (left is smaller or equal to right). <=
= ==
not = !=
● If you type 3<4 you get a boolean.
○ A boolean is a truth value, has 2 values which are True and False (note the
uppercase letters).
○ Python is uppercase and lowercase sensitive.
○ 3<4 will get the output true.
○ 3>4 will get the output false.
● Boolean operators
○ Were the operators are of the type boolean.
Math Python
and
∧
or
∨
not
¬
Some truth tables
a b a and b
f f F
f t F
t f F
t t T