-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.py
78 lines (56 loc) · 1.86 KB
/
index.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
print("hello python")
def add_numbers(a, b):
print(a + b)
add_numbers(5, 5)
# To convert foloat to int or vise versa
# call a funciton with prepended name of datatype
print("Convert 3.14 to integer")
print(int(3.14))
print("Convert integer 10 to float")
print(float(10))
name = "Vladimir"
profession = "coder"
# string format function
print(f"I'm {name}. My lifestyle is a {profession}")
# check if truthy
if name == "Vladimir" and profession == "coder":
print("this will execute")
# ternary operator in python --- in JavaScript it would be
# console.log(name.length > profession.length ? "bigger" : "smaller")
print("bigger" if len(name) > len(profession) else "smaller")
# List manipulation/delete from right and left(-)
list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
# for loop(number is just a variable)
for number in list:
print(f"number {number}")
# for loop with range - first argument defines = start interation index, secont argument defines = finish iteration index, third argument defines = increment(default == 1
some_number = 0
for number in range(1, 7, 2):
print(f"for loop with start index = 1, finish index = 7, iterator = 2 {number}")
# add if condition in for loop and break which is going to stop loop after match
for number in list:
if number == 5:
print(f"Found number {number}")
break
print(f"Currently number is {number}")
# define Dictionary(equivalent of object in JavaScript)
student = {
'name': "Vladimir",
'language': "Phyton",
'city': "Tampa"
}
# exeption block
try:
last_name = student["last_name"]
# key error handling
except KeyError:
print("Error finding last name")
print("eception works!!!!")
try:
int_plus_string = 3 + name
# type error handling
except TypeError:
print("can't add diff data types")
# except Exceptions handles all types of errors
except Exception:
print("Unknown error")