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 e086973

Browse files
Add files via upload
1 parent e20aab9 commit e086973

27 files changed

+430
-0
lines changed

‎Tutorials/Dictionary_Calculations.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
stocks = {
2+
'GOOG' : 434,
3+
'AAPL' : 325,
4+
'FB' : 54,
5+
'AMZN' : 623,
6+
'F' : 32,
7+
'MSFT' : 549
8+
}
9+
10+
print(min(zip(stocks.values(), stocks.keys())))
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from operator import itemgetter
2+
3+
users = [
4+
{'fname': 'Bucky', 'lname': 'Roberts'},
5+
{'fname': 'Tom', 'lname': 'Roberts'},
6+
{'fname': 'Bernie', 'lname': 'Zunks'},
7+
{'fname': 'Jenna', 'lname': 'Hayes'},
8+
{'fname': 'sally', 'lname': 'Jones'},
9+
{'fname': 'Amanda', 'lname': 'Roberts'},
10+
{'fname': 'Tom', 'lname': 'Williams'},
11+
{'fname': 'Dean', 'lname': 'Hayes'},
12+
{'fname': 'Bernie', 'lname': 'Barbie'},
13+
{'fname': 'Tom', 'lname': 'Jones'}
14+
]
15+
16+
# How to sort when there are more than 1 keys
17+
18+
for x in sorted(users, key = itemgetter('fname')): # keyword 'sorted' takes in 3 values: 1. the dictionary 2. the key 3. reverse = True/False
19+
# 'itemgetter' allows us to fetch the keyword with which we want to sort the items in the given dictionary
20+
21+
print(x) # But the problem here is it sorts the 1st name accordingly bt the last names aren't sorted
22+
23+
print('______________________________________\n')
24+
25+
# How to fix this problem
26+
27+
for x in sorted(users, key = itemgetter('fname', 'lname')): # Here we can even use 2 keys, but the 1st one will be given preference
28+
print(x)

‎Tutorials/bitwise_operators.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
2+
# ------ Binary AND ------
3+
4+
a = 50 # 110010
5+
b = 25 # 011001
6+
c = a & b # 010000 = 16
7+
# '&' is a binary operator
8+
9+
print(c)
10+
11+
# ------ Binary RIGHT SHIFT ------
12+
13+
x = 240 # 11110000
14+
y = x >> 2 # 00111100 = 60
15+
# Every bit shifted 2 places to the right
16+
17+
print(y)
18+
19+
# ------ Binary LEFT SHIFT ------
20+
z = x << 4 # 00001111
21+
print(z)
22+
23+
# ------ Binary OR ------
24+
25+
d = a | b # 111111
26+
print(d)
27+
28+
# ------ Binary compliment ------
29+
30+
e = ~x
31+
print(e)
32+
33+
# ------ Binary XOR ------
34+
35+
f = a ^ b #101011
36+
print(f)

‎Tutorials/break_continue.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'''
2+
this is used to add a comment line
3+
or a set of lines that the computer ignores
4+
'''
5+
# this is used to add a single comment line
6+
7+
print("this is used to" + " concatenate 2 strings")
8+
9+
'''
10+
a string and a number cannot be concatenated this way
11+
instead we can use a comma to separate the two different data types'''
12+
13+
print(9, ' aousnik')
14+
15+
#BREAK AND CONTINUE::
16+
17+
magicNumber = 26
18+
for x in range(101):
19+
if x is magicNumber:
20+
print(x," is a magic number")
21+
#once 26 is found it prints the number and breaks the loop
22+
break
23+
else:
24+
print(x)
25+
#prints the number anyways
26+
27+
'''numbers till 100 divisibe by 4'''
28+
29+
print('\n')
30+
31+
for i in range(1, 101):
32+
x = i%4
33+
if x is 0:
34+
print(i)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Girl:
2+
3+
gender = 'female' # class variable which is common to all
4+
5+
def __init__(self, name):
6+
self.name = name # instance variable which is unique to a name
7+
8+
a = Girl('Rachel') # this is the way to assign a value to the function when a parameter is present
9+
b = Girl('Stanky')
10+
11+
print(a.gender)
12+
print(b.gender)
13+
print(a.name)
14+
print(b.name)
15+
16+
17+

‎Tutorials/classes_objects.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Enemy attack game
2+
3+
class Enemy:
4+
life = 3
5+
6+
def attack(self):
7+
print("Enemy says - Ouch !!")
8+
self.life -= 1 # to access variables inside the class itself
9+
10+
def checklife(self):
11+
if self.life <= 0:
12+
print("Enemy says - I'm dead")
13+
else:
14+
print(str(self.life) + " life left")
15+
16+
# now to access anything that's inside this class, we need to create an object
17+
18+
enemy1 = Enemy() # Object initialisation
19+
#objects are basically the copies of a class
20+
enemy2 = Enemy()
21+
22+
enemy1.attack()
23+
enemy1.attack() # basically the functions on enemy 1 doesnt affect enemy 2
24+
enemy1.checklife()
25+
26+
enemy2.checklife()
27+
28+
# so by writing just once, we can create 2 instances to run the class twice 'parallelly and independently'
29+

‎Tutorials/continue.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
numbers = [2, 5, 12, 13, 17]
2+
3+
for x in range(1, 20):
4+
if x in numbers:
5+
continue
6+
print(x)

‎Tutorials/default_values.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
def get_gender(gender = 'Unknown'):
2+
if gender is 'm':
3+
gender="male"
4+
elif gender is 'f':
5+
gender='female'
6+
print("Gender is ", gender)
7+
8+
get_gender('m')
9+
get_gender('f')
10+
get_gender()
11+
12+
def sentence(name = 'Aousnik', gender = 'male', category = 'general'):
13+
print(name, gender, category)
14+
15+
sentence('abcd', 'female', 'SC')
16+
sentence(category = 'ST')
17+
sentence(gender = 'other')
18+
sentence()

‎Tutorials/dictionary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
classmates = {'Rono':' ghaora', 'Sohom':' Chutiya', 'Rudra':' Derchokho'}
2+
# classmates is the dictionary with some words with values
3+
4+
print(classmates) # this prints the whole thing as it is
5+
print(classmates['Rono']) # it prints the value 'Rono' assigned
6+
7+
for k in classmates:
8+
#k->keywords, v->values
9+
print(k)
10+
# prints all the keywords spaced with values
11+
12+
''' .items() keyword is used to go through all the items in the dictionary created'''

‎Tutorials/download_files.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# to download data/csv files from the internet
2+
import urllib.request
3+
4+
goog_url = "http://insight.dev.schoolwires.com/HelpAssets/C2Assets/C2Files/C2ImportCalEventSample.csv"
5+
6+
def data_analysis(csv_url):
7+
response = urllib.request.urlopen(csv_url) # response opens the data
8+
csv = response.read() # reads the data in the url
9+
csv_str = str(csv) # converts all the data into string
10+
lines = csv_str.split("\\n") # \\n splits the whole string into different lines accordingly
11+
# of course we dont want all the string in the url in one whole line
12+
destination = r'goog.csv' #destination of the file
13+
fx = open(destination, "w") #to open up the file
14+
15+
for line in lines: # to write in the file
16+
fx.write(line + "\n")
17+
18+
fx.close()
19+
20+
data_analysis(goog_url)
21+

0 commit comments

Comments
(0)

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