?print-pdf
' Created for
$ python -m this
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
numbers = [1, 2, 3, 4]
product = 1
for i in numbers:
product *= i
total = sum(numbers)
print(total, product)
const numbers = [1, 2, 3, 4];
let product = 1;
let total = 0;
for (let i of numbers) {
product *= i;
total += i;
}
console.log(total, product);
That code will lead to
TabError: inconsistent use of tabs and spaces in indentation
That code will lead to
IndentationError: unexpected indent
Can you spot the bug?
print( 1 + 2 ) # 3
print( '1' + '2' ) # 12
print( True + True ) # 2
print( 3 * 4 ) # 12
print( 3 * '4' ) # 444
we can use positive and negative integers:
# positive integer literal:
>>> 42
42
>>> +42
42
# negative integer literal:
>>> -42
-42
we can use positive and negative floating point numbers
>>> 3.1234
3.1234
>>> -0.255
-0.255
# leading zero can be skipped:
>>> .44
0.44
>>> -.55
-0.55
>>>
>>> 1_000_000 + 2_000_000
3000000
>>> 5-3
2
>>> 5*3
15
>>> 5/3
1.6666666666666667
>>> 5//3
1
>>> 5%3
2
/
" in python2 returns the integer part(i.e. works like Floor division), while in python3 it returns the float result
>>> 5/3
1
>>> 5/3
1.6666666666666667
>>> 5//3
1
print(0.1+0.2)
# OUTPUT:
# 0.30000000000000004
math
module
import math
# Example: Calculate square root of a number
number = 16
sqrt_value = math.sqrt(number)
print("Square root of", number, "is", sqrt_value)
>>> import math
>>>
>>> math.pi
3.141592653589793
>>> math.floor(math.pi)
3
>>> math.pow(2,3)
8.0
>>> math.sqrt(9)
3.0
>>> math.ceil(2.9)
3
>>> math.ceil(2.1)
3
>>> math.floor(2.9)
2
>>> math.floor(2.1)
>>> round(2.51)
3
>>> round(2.49)
2
>>> max(1,2,3)
3
>>> min(1,2,3)
1
>>> abs(2-5)
3
>>> "this is a single line string"
'this is a single line string'
>>> 'another single line string with UTF charactes like 🍷'
'another single line string with UTF charactes like 🍷'
>>> 'but can not be spread in multiple lines
File "<stdin>", line 1
'but can not be spred in multiple lines
^
SyntaxError: EOL while scanning string literal
>>> """infact you can -
... if you use these triple quotes"""
'infact you can - \nif you use these triple quotes'
>>> '''or these triple quotes
... can separate multiline without errors'''
'or these triple quotes\ncan separate multiline without errors'
+
The operation is defined only when both operands are string.
Python can not concatenate "apples with oranges":
#string concatenation with '+':
>>> "ala" + "bala"
'alabala
>>> "1" + "2"
'12'
>>> "1" + 3
Traceback ...
TypeError: cannot concatenate 'str' and 'int' objects
*
One of the operands must be string, the other - integer
>>> "-" * 10
'----------'
>>> "1" * 10
'1111111111'
>>> ">hello<" * 3
'>hello<>hello<>hello<'
>>> "a" * "3"
Traceback ...
TypeError: can't multiply sequence by non-int of type 'str'
# string methods:
>>> "ada".capitalize()
'Ada'
>>> "alabala".count("a")
4
>>> "Alabala".count("a")
3
>>> "AlabAla".find("a")
2
>>> "alabala".replace("a", "o")
'olobolo'
>>> "one,two,three".split(",")
['one', 'two', 'three']
What a method is will be discussed further!
Reference: String Methods @docs.python.org
x = 99
y = 3.141516
first_name = "ada"
sur_name = "byron"
num_list = [1,2,3,4,5]
x = 99
print("x = ", x)
first_name = "ada"
print("first_name = ", first_name)
print("first_name = ", First_name) #NameError: name 'First_name' is not defined
sur-name = "byron" #SyntaxError: can't assign to operator
variable = expression
expression
is evaluated, and then its result is assigned to variable
.
### Multiple Assignments
x, y = 10, 20 # x=10 and y=20.
### Chain Assignment:
x = y = 10 # x = 10 and y = 10
### Shorthand Assignment with Operators:
x += 10 # Equivalent to x = x + 10
x **= 10 # Equivalent to x = x ** 10
assignment
statement (=
)id()
built-in function.
a = 2
print(id(a))
# 9413216
a = 5
print(id(a))
# 9413312
# Now we create a new object, and bind it to 'a'
a = 6
print(id(a))
# 9413344
# the object [id:9413312, value:5] will be deleted by the garbage collector, as nothing points to it
#
#
(hash tag) is a comment and is ignored py Python interpreter
# this is a just a comment: no print("whatever") will happens
print("this will be printed, of course")
### a more semantic example for comment:
# check if a triangle with sides (3,4,5) is a Pythagorean:
print(3**2 + 4**2 == 5**2)
Total duration in astronomical hours: 48.00
BMI = W / (H*H)
HW/BMI.py
file
>>> round(2.1457, 2)
2.15
>>> round(1.4234,2)
1.42
>>> round(1.4284,2)
1.43
These slides are based on
customised version of
framework