-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.py
169 lines (128 loc) · 3.08 KB
/
demo.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
x = 5
print(str(x) + " AAA")
exit()
str()
float()
# function in python
def func():
# returns 0
return 0
# Declare a variable
x = 0
def func1():
x = 2
# Print with formatting
# %d -> integer
# %s -> string
# %f -> float
print("FUNC1: VALUE OF X AT END OF FUNC1 : %d"%x)
def func2():
# Use the x variable outside the function
global x
x = 3
print("FUNC2: VALUE OF X AT END OF FUNC2 : %d"%x)
print("VALUE OF X BEFORE FUNC1: %d"%x)
func1()
print("VALUE OF X AFTER FUNC1: %d"%x)
# call function
func2()
print("VALUE OF X AFTER FUNC2: %d"%x)
# set x equal to string
x = "AAA"
print("VALUE OF X %s %d"%(x,5))
# set x equal to function
x = func1
# call the function stored in x
x()
x = func2
print("VALUE OF X BEFORE FUNC2"+str(x))
x()
print("VALUE OF X AFTER FUNC2: %d"%x)
# define array
arr = []
#add 4 to the end of an array
arr.append(4)
# set array to contain 42 and 3
arr = [42,3]
print(arr)
# see length of array
print(len(arr))
# access each element of array
for el in arr:
print(x)
# Loop:
i = 0
while i < 5:
print(i)
i+=1
# if/else
if i > 3:
print("LARGE")
elif i<0:
print("NEGATIVE")
else:
print("SMALL")
i = 0
while i<6:
if i==2:
#end loop early
break
print(i)
# will do i = 0 then 1 then 2 ... and then 5 (at 6 stops)
for i in range(6):
if i%2 ==0:
# don't do rest of loop and just restart it
continue
print("ODD!")
# sort array
arr.sort()
# import package (library) and use it as np
import numpy as np
# create a an array of elements from 1 to 100 with a skip of 5 (so 1, 6, 11, 16...)
arr = np.arange(1,100,5)
print(arr)
# see shape of array
print(arr.shape)
# make array into 2d array with 4 rows and 5 columns
arr = arr.reshape((4,5))
print(arr)
# rotate array -> columns become rows, rows become columns
arr = np.transpose(arr)
print(arr)
# define class
class Person(object):
#constructor:
def __init__(self, name, age, height):
# class contains attribute name now:
self.name = name
# class contains attribute age now:
self.age = age
# class contains attribute visochina now:
self.visochina = height
# define function for class (ALWAYS PASS self AS FIRST PARAMETER)
def height_v_metri(self):
return self.visochina/100
def predstavq(self):
# Access elements of class
print(self.name)
print(self.age)
print(self.height_v_metri())
# Create instance of class
p1 = Person("Nikolay", 22, 183)
# call its function
p1.predstavq()
# access its attribute
print(p1.name)
p2 = Person("Ime","12b",176)
p2.predstavq()
# make 7 by 6 board of 0s (all ints)
game_board = np.zeros((7,6), dtype=np.int32)
# get user value
choice = input("ENTER VALUE: ")
# try to interpret user input as integer
try:
choice= int(choice)
if choice == 3:
print("YOu ARE CORRECT")
except ValueError:
print("INPUT INTEGER")