Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
October 8, 2018
1 Basic Python
1.1 Print Statements and Comment
In [1]: print("Hello World!")
def a():
"""
comment
input:
output:
"""
return
Hello World!
test
In [171]: x = 10
x
Out[171]: 10
1.2 Variables
1.2.1 Integer, Float, and String
In [172]: people = 100 # integer
people
Out[172]: 100
1
Out[173]: 36.0
1.2.2 List
In [2]: matrix = [[1, 2, 3], [4, 5, 6]]
print(matrix[0])
matrix2 = list(matrix)
matrix2[0][0] = 3
print(matrix)
[1, 2, 3]
[[3, 2, 3], [4, 5, 6]]
In [177]: example_list = ["this is string", 10] # list with different types of elements
example_list
Out[179]: 'apple'
In [180]: fruits[-2]
Out[180]: 'banana'
In [181]: fruits.index("orange")
Out[181]: 2
2
In [182]: "orange" in fruits
Out[182]: True
Out[183]: False
1.2.3 Dictionary
In [185]: customer = {} # empty dictionary
customer = {"name": "John", "age": 30} # dictionary with key -> value pair
customer
Out[186]: {7: 16, 9: 10, 'age': 30, 'email': 'john@example.com', 'name': 'John'}
Out[187]: 'john@example.com'
In [189]: customer.values()
In [190]: customer.items()
3
In [191]: for value in customer.values():
print(value)
10
30
16
John
john@example.com
[{9: 10, 'age': 30, 7: 16, 'name': 'Budi', 'email': 'john@example.com'}, {9: 10, 'age': 30, 'em
[5, 2]
[1, 2]
True
True
{9: 10, 'age': 30, 7: 16, 'name': 'Budi', 'email': 'john@example.com'}
1.2.4 Tuple
In [196]: # tuple is similar to list but immutable once created
coordinate = (0, 5)
coordinate
Out[196]: (0, 5)
4
In [197]: colors = ("red", "green", "blue")
colors
Out[197]: ('red', 'green', 'blue')
In [198]: # colors[1] = "red"
In [199]: len(customer.values())
Out[199]: 6
1.2.5 Set
In [200]: unique_numbers = {1, 2, 3, 3}
unique_numbers = set([1, 2, 3, 3])
unique_numbers
Out[200]: {1, 2, 3}
In [201]: # add element to a set
unique_numbers.add(2)
unique_numbers.add(2)
unique_numbers.add(2)
unique_numbers.add(2)
unique_numbers.add(2)
unique_numbers.add(4)
unique_numbers.update([1, 5])
unique_numbers
Out[201]: {1, 2, 3, 4, 5}
5
1.3 Basic Mathematical Operations
In [207]: 20 + 2
Out[207]: 22
In [208]: 3 + 2 + 1 - 5 + 4 % 2 - 1 / 4 + 6
Out[208]: 6.75
10 / 3 # div
Out[209]: 3.3333333333333335
Out[210]: 3.3333333333333335
In [211]: 10 % 3 # mod
Out[211]: 1
1.3.1 Comparison
In [212]: 5 > 2
Out[212]: True
Out[213]: True
In [214]: 5 - 95 > 0
Out[214]: False
In [215]: 7 == 7.0
Out[215]: True
Out[216]: False
6
1.4 Function
In [217]: # function with no return statement
def print_hello_world():
def print_string():
print("halo")
print("Hello World!")
print(1)
print_string()
print_hello_world()
Hello World!
1
halo
In [218]: a = [1, 2, 3]
def kali2(n):
return n * 2
map(lambda n: n * 3, a)
Out[218]: [3, 6, 9]
In [220]: is_even(3)
Out[220]: False
1.5 IF Statement
In [221]: number = 99
def guess_number(guess):
if guess < number:
print("Wrong guess! try with larger number")
elif is_even(number):
print("even")
elif guess > number:
print("Wrong guess! try with smaller number")
else:
print("Correct! Congratulations!")
print(1)
7
1
In [222]: guess_number(5)
guess_number(100)
guess_number(99)
1.6 Looping
In [223]: range(1, 10)
Out[223]: [1, 2, 3, 4, 5, 6, 7, 8, 9]
1
2
3
4
5
6
7
8
9
5
4
3
2
1
0
-1
-2
-3
-4
8
In [226]: # looping over list
fruits = ["apple", "banana", "orange"]
for fruit in fruits:
print(fruit)
apple
banana
orange
# loop from 0 to 9
# add number to the list if the number is even
for i in range(10):
if is_even(i):
numbers.append(i)
numbers
Out[228]: [0, 2, 4, 6, 8]
i = 2
numbers = []
numbers
2
3
4
9
5
6
7
8
9
Out[229]: [2, 3, 4, 5, 6, 7, 8, 9]
p = Person()
print("Person name: %s" % p.name)
print("Person age: %i" % p.age)
def get_name(self):
return self.name
def __get_age(self):
return self.age
10
Out[231]: 'John'
In [232]: person.edit_age(35)
person.age
Out[232]: 35
11