Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 188a59e

Browse files
Add files via upload
1 parent e086973 commit 188a59e

File tree

11 files changed

+182
-0
lines changed

11 files changed

+182
-0
lines changed

‎Tutorials/range_while.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
print('\n')
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+
print(x)

‎Tutorials/return_value.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def allowed(age):
2+
girls_age = age/2 + 7
3+
return girls_age
4+
5+
allowed(30) # does not print anything
6+
7+
limit = allowed(30)
8+
print('I can date girls with age ', limit, 'or older')

‎Tutorials/sample_prog.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
x = []
3+
for _ in range(30):
4+
x.append(int(input()))
5+
6+
for i in range(30):
7+
print(str(x[i]), " + ", end = " ")
8+
if i is 29:
9+
print(" = ", end = " ")
10+
11+
print(sum(x))

‎Tutorials/sets.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
its something like lists but notated with curly braces and no reps
3+
'''
4+
5+
asd = {'hey', 'hi', 'wadup', 'hey'}
6+
print(asd)
7+
8+
# it will only print 'hey' once
9+
10+
'''
11+
num1 = 0
12+
numtemp = 0
13+
numfinal = 0
14+
while True:
15+
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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
from operator import attrgetter # attribute getter
2+
3+
4+
class User:
5+
6+
def __init__(self, x, y):
7+
self.name = x
8+
self.user_id = y
9+
10+
def __repr__(self): # This is an in built function used to convert an object into a string variable
11+
return self.name + " : " + str(self.user_id)
12+
13+
users = [
14+
User('Bucky', 43),
15+
User('Sally', 5),
16+
User('Tuna', 61),
17+
User('Brain', 2),
18+
User('Joby', 77),
19+
User('Amanda', 9)
20+
] # above is a ist of class 'User' objects
21+
22+
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+
print(user)
32+
33+
print('________________________________')
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+
print(user)

‎Tutorials/sorting_dics.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
stocks = { "GOOG": 520.54,
2+
"YHOO": 54.23,
3+
"APPL": 300.33,
4+
"REDT": 450.02,
5+
"AMZN": 500.65
6+
}
7+
8+
# We cannot sort dictionaries together, but we can sort two lists
9+
# When python sorts lists, it sorts it through the first element of the list
10+
# While zipping, it either sorts through the name or the price, depending on the one that we put first
11+
12+
# Sorting through values
13+
print('Min:', min(zip(stocks.values(), stocks.keys()))) # to get the minimum value in the list
14+
print('Max:', max(zip(stocks.values(), stocks.keys())))
15+
# it gave us the minimum price, because the 1st list consisted of values and not keys
16+
17+
print('\n', 'Sorting by values:')
18+
print(sorted(zip(stocks.values(), stocks.keys()))) # sorting by values
19+
20+
print('\n', 'Sorting by keys:')
21+
print(sorted(zip(stocks.keys(), stocks.values()))) # sorting by keys

‎Tutorials/struct_.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from struct import *
2+
3+
# Store as bytes data
4+
5+
# Struct is something that converts something that humans can read to 1's and 0's, and vice versa
6+
7+
# Converting into byte format:
8+
packed_data = pack('iif', 6, 19 , 4.73) # It takes in the format of each number followed by the numbers/values
9+
# 'iif' means 2 int and 1 float value. If we enter 5 int we need to enter 'iiiii'
10+
11+
print(packed_data) # It prints it out in byte format
12+
13+
print(calcsize('i'))
14+
print(calcsize('f'))
15+
print(calcsize('iif')) # It prints the amount of memory needed to store the given data
16+
17+
# Converting bytes to readable format:
18+
19+
unpacked_data = unpack('iif', packed_data)
20+
print(unpacked_data)
21+
print(unpack('iif', b'\x06\x00\x00\x00\x13\x00\x00\x00)\\\x97@')) # It gives the same result

‎Tutorials/threading_tut.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Threading - Basically running two programs at the same time
2+
3+
import threading
4+
5+
class MyMessenger(threading.Thread):
6+
7+
def run(self):
8+
for _ in range(1000):
9+
print(threading.currentThread().getName()) # Remember the syntax
10+
11+
# 2 threads to send and receive msgs
12+
x = MyMessenger(name = 'send out msgs') # naming a thread
13+
y = MyMessenger(name = 'receive msgs')
14+
15+
x.start() # its a rule. The start() goes to run(). You cant run the run() directly
16+
y.start()
17+
18+
#its cool. It starts executing object 'y' even before finishing object 'x'. Basically runs them parallely, and finishes it faster

‎Tutorials/unpack_args.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def health(age, apples, cigs):
2+
life = (100 - age) + (apples * 3.5) - (cigs * 1.5)
3+
print(life)
4+
5+
my_data = [18, 2, 0]
6+
health(my_data[0], my_data[1], my_data[2])
7+
'''
8+
the above method is very tedious so the lower method is used which
9+
effectively reduces coding and puts the values in the given list accordingly
10+
'''
11+
health(*my_data)

‎Tutorials/while.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
x=5
2+
3+
while x < 10:
4+
print(x)
5+
x += 1
6+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /