We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent e086973 commit 188a59eCopy full SHA for 188a59e
Tutorials/range_while.py
@@ -0,0 +1,15 @@
1
+for x in range(10):
2
+ print('from 0 to 9:')
3
+ print(x)
4
+
5
+print('\n')
6
7
+for x in range(5, 12):
8
+ print('from 5 to 11:')
9
+ print (x)
10
11
12
+# 3 numbers are used to put the range and the last number signifies the amount of plus need to be added to each number
13
+for x in range(10, 40, 5):
14
+ print('from 10 to 40 in plus 5 terms:')
15
Tutorials/return_value.py
@@ -0,0 +1,8 @@
+def allowed(age):
+ girls_age = age/2 + 7
+ return girls_age
+allowed(30) # does not print anything
+limit = allowed(30)
+print('I can date girls with age ', limit, 'or older')
Tutorials/sample_prog.py
@@ -0,0 +1,11 @@
+x = []
+for _ in range(30):
+ x.append(int(input()))
+for i in range(30):
+ print(str(x[i]), " + ", end = " ")
+ if i is 29:
+ print(" = ", end = " ")
+print(sum(x))
Tutorials/sets.py
@@ -0,0 +1,22 @@
+'''
+its something like lists but notated with curly braces and no reps
+asd = {'hey', 'hi', 'wadup', 'hey'}
+print(asd)
+# it will only print 'hey' once
+num1 = 0
+numtemp = 0
+numfinal = 0
+while True:
+ num1 = input("Enter an integer: ") # input is always taken as a string
16
+ if num1.isdigit():
17
+ numfinal = numtemp + int(num1)
18
+ numtemp = numfinal
19
+ else:
20
+ print(numfinal)
21
+ break
22
Tutorials/sorting_custom_objects.py
@@ -0,0 +1,37 @@
+from operator import attrgetter # attribute getter
+class User:
+ def __init__(self, x, y):
+ self.name = x
+ self.user_id = y
+ def __repr__(self): # This is an in built function used to convert an object into a string variable
+ return self.name + " : " + str(self.user_id)
+users = [
+ User('Bucky', 43),
+ User('Sally', 5),
+ User('Tuna', 61),
+ User('Brain', 2),
+ User('Joby', 77),
+ User('Amanda', 9)
+] # above is a ist of class 'User' objects
+for user in users:
23
+ print(user)
24
25
+print('________________________________')
26
27
+# Sorting:
28
29
+print("By User ID:\n")
30
+for user in sorted(users, key = attrgetter('user_id')): # attrgetter is used when there is no key given, but only an object is passed like 'user_id'
31
32
33
34
35
+print("By name:\n")
36
+for user in sorted(users, key = attrgetter('name')): # attrgetter is used when there is no key given, but only an object is passed like 'name'
37
Tutorials/sorting_dics.py
@@ -0,0 +1,21 @@
+stocks = { "GOOG": 520.54,
+ "YHOO": 54.23,
+ "APPL": 300.33,
+ "REDT": 450.02,
+ "AMZN": 500.65
+ }
+# We cannot sort dictionaries together, but we can sort two lists
+# When python sorts lists, it sorts it through the first element of the list
+# While zipping, it either sorts through the name or the price, depending on the one that we put first
+# Sorting through values
+print('Min:', min(zip(stocks.values(), stocks.keys()))) # to get the minimum value in the list
+print('Max:', max(zip(stocks.values(), stocks.keys())))
+# it gave us the minimum price, because the 1st list consisted of values and not keys
+print('\n', 'Sorting by values:')
+print(sorted(zip(stocks.values(), stocks.keys()))) # sorting by values
+print('\n', 'Sorting by keys:')
+print(sorted(zip(stocks.keys(), stocks.values()))) # sorting by keys
Tutorials/struct_.py
+from struct import *
+# Store as bytes data
+# Struct is something that converts something that humans can read to 1's and 0's, and vice versa
+# Converting into byte format:
+packed_data = pack('iif', 6, 19 , 4.73) # It takes in the format of each number followed by the numbers/values
+ # 'iif' means 2 int and 1 float value. If we enter 5 int we need to enter 'iiiii'
+print(packed_data) # It prints it out in byte format
+print(calcsize('i'))
+print(calcsize('f'))
+print(calcsize('iif')) # It prints the amount of memory needed to store the given data
+# Converting bytes to readable format:
+unpacked_data = unpack('iif', packed_data)
+print(unpacked_data)
+print(unpack('iif', b'\x06\x00\x00\x00\x13\x00\x00\x00)\\\x97@')) # It gives the same result
Tutorials/threading_tut.py
@@ -0,0 +1,18 @@
+# Threading - Basically running two programs at the same time
+import threading
+class MyMessenger(threading.Thread):
+ def run(self):
+ for _ in range(1000):
+ print(threading.currentThread().getName()) # Remember the syntax
+# 2 threads to send and receive msgs
+x = MyMessenger(name = 'send out msgs') # naming a thread
+y = MyMessenger(name = 'receive msgs')
+x.start() # its a rule. The start() goes to run(). You cant run the run() directly
+y.start()
+#its cool. It starts executing object 'y' even before finishing object 'x'. Basically runs them parallely, and finishes it faster
Tutorials/unpack_args.py
+def health(age, apples, cigs):
+ life = (100 - age) + (apples * 3.5) - (cigs * 1.5)
+ print(life)
+my_data = [18, 2, 0]
+health(my_data[0], my_data[1], my_data[2])
+the above method is very tedious so the lower method is used which
+effectively reduces coding and puts the values in the given list accordingly
+health(*my_data)
Tutorials/while.py
@@ -0,0 +1,6 @@
+x=5
+while x < 10:
+ x += 1
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル
0 commit comments