Skip to content

Commit 4afbda2

Browse files
committed
Large Update, Dec 9
1 parent ef743d9 commit 4afbda2

33 files changed

+2970
-131
lines changed

"if" statements/main.py

+106-53
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,106 @@
1-
##
2-
# "If" Statements Lecture
3-
##
4-
5-
# -*- coding: utf-8 -*-
6-
7-
8-
# if condition:
9-
# Action
10-
11-
click = False
12-
13-
Like = 0
14-
15-
click = True
16-
17-
if click == True:
18-
Like = Like + 1
19-
click = False
20-
21-
# print(Like)
22-
23-
Temperature = 20
24-
Thermo = 15
25-
# print(Thermo)
26-
if Temperature <= 15:
27-
Thermo = Thermo + 5
28-
29-
# print(Thermo)
30-
31-
if Temperature >= 20:
32-
Thermo = Thermo - 3
33-
34-
# print(Thermo)
35-
36-
Time = "Day"
37-
Sleepy = False
38-
Pajamas = "Unknown"
39-
InBed = True
40-
41-
print(Pajamas)
42-
43-
if Time == "Night" or Sleepy == True:
44-
Pajamas = "On"
45-
46-
elif Time == "Morning":
47-
Pajamas = "On"
48-
49-
else:
50-
Pajamas = "Off"
51-
52-
53-
print(Pajamas)
1+
##
2+
# "If" Statements Lecture
3+
##
4+
5+
# -*- coding: utf-8 -*-
6+
7+
'''
8+
Syntax:
9+
if condition:
10+
Action
11+
'''
12+
click = False #set variable click to False
13+
Like = 0 #initialize Like equal to 0
14+
15+
if click == True: #Check if click is True
16+
Like = Like + 1 #Increment Like by 1
17+
click = False #set click to False
18+
19+
print(Like) #print the value
20+
Temperature = 20 #set a variable Temperature to 20
21+
Thermo = 15 #set a variable Thermo to 15
22+
if Temperature < 15: #Check if Temperature is less than 15
23+
Thermo = Thermo + 5 #if yes increment variable Thermo by 5
24+
25+
print(Thermo) #print the value Thermo
26+
27+
Temperature = 14 #set variable Temperature to 14
28+
Thermo = 15 #set variable Thermo to 15
29+
if Temperature < 15: #Check if Temperature is less than 15
30+
Thermo = Thermo + 5 #if True increment Thermo by 5
31+
32+
print(Thermo) #print the value of Thermo
33+
34+
Temperature = 15 #set variable Temperature to 15
35+
Thermo = 15 #set variable Thermo to 15
36+
if Temperature <= 15: #Check if Temperature is less than or equal to 15
37+
Thermo = Thermo + 5 #if True increment Thermo by 5
38+
39+
print(Thermo) #print the value of Thermo
40+
41+
if Temperature > 20: #Check if Temperature is greater than 20
42+
Thermo = Thermo - 3 #if True decrement Thermo by 3
43+
44+
print(Thermo) #print the value of Thermo
45+
Temperature = 20 #set the value of Temperature to 20
46+
Thermo = 15 #set Thermo equals to 15
47+
if Temperature <= 15: #Check if Temperature is less than or equal to 15
48+
Thermo = Thermo + 5 #if True increment Thermo by 5
49+
50+
print(Thermo) #print value of Thermo
51+
52+
if Temperature >= 20: #Check if Temperature is greater than or equal to 20
53+
Thermo = Thermo - 3 #if True decrement Thermo by 3
54+
55+
print(Thermo) #print value of Thermo
56+
57+
58+
Time = "Day" #set variable Time to Day
59+
Sleepy = False #set Sleepy equals to False
60+
Pajamas = "Off" #initialize Pajamas equal to Off
61+
'''
62+
If Time equals to Night and Sleep is True then set Pajamas equal to On
63+
'''
64+
if Time == "Night" and Sleepy == True: #Check two condition and are ANDed
65+
Pajamas = "On" #if both condition are True then set Pajamas = On
66+
print(Pajamas) #print the value of Pajamas
67+
68+
Pajamas = "Off" #initialize Pajamas equal to Off
69+
if Time == "Night" or Sleepy == True: #Check two condition and are ORed
70+
Pajamas = "On" #if anyone of the condition is True set Pajamas equal to On
71+
print(Pajamas) #print the value of Pajamas
72+
73+
Time = 'Night' #set variable Time to Night
74+
Sleepy = 'True' #set variable Sleep equals to True
75+
Pajamas = "Off" #initialize Pajamas equal to Off
76+
if Time == "Night" or Sleepy == False: #Check two condition and are ORed
77+
Pajamas = "On" #if anyone of the condition is True set Pajamas equal to On
78+
print(Pajamas) #print the value of Pajamas
79+
80+
'''
81+
intialize Time equals to Day, Sleepy equals to False, Pajamas to off and InBed equals to True
82+
Check if Time equals to Night, set Pajamas equals to On, else if Time equals to Morning is True
83+
set Pajamas equals to On and then print its value
84+
'''
85+
Time = 'Day'
86+
Sleepy = 'False'
87+
Pajamas = "off"
88+
InBed = True
89+
if Time == "Night":
90+
Pajamas = "On"
91+
elif Time == "Morning":
92+
Pajamas = "On"
93+
print(Pajamas)
94+
95+
Time = 'Morning' #set Time equals to Morning
96+
Sleepy = 'False' #Set Sleepy to False
97+
Pajamas = "Unknown" #set Pajamas to Unknown
98+
InBed = True #set InBed variable to True
99+
print(Pajamas) #print value of Pajamas
100+
if Time == "Night": #if Time is equal to Night
101+
Pajamas = "On" #set pajamas to On
102+
elif Time == "Morning": #else if Time equal to Morning
103+
Pajamas = "On" #set pajamas to On
104+
else: #otherwise if any of the above two statement are not true
105+
Pajamas = "Off" #set Pajamas Off
106+
print(Pajamas) #print the value of Pajamas

classes/Introduction-to-Classes.py

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
##
2+
# Introduction to Classes Lecture
3+
##
4+
5+
# -*- coding: utf-8 -*-
6+
7+
8+
"""
9+
class is defined by a keyword class
10+
11+
Classname Team in defined
12+
"""
13+
14+
class Team:
15+
def __init__(self): #constructor with no arguments
16+
self.TeamName = 'Name' #self represents the instance of the class and the variables of a class can be accessed using self
17+
self.TeamOrigin = 'Origin' #set an attribute 'TeamOrigin to "Origin"
18+
19+
'''
20+
DefinieTeamName and DefineTeamOrigin represents the methods of a class. Each method takes one
21+
arguments
22+
'''
23+
def DefineTeamName(self, Name):
24+
self.TeamName = Name
25+
26+
def DefineTeamOrigin(self, Origin):
27+
self.TeamOrigin = Origin
28+
29+
Team1 = Team() #create an object of a class Team
30+
'''
31+
Methods and Member of a class can be accessed using a dot operator.
32+
object.membername or object.methodname
33+
'''
34+
print(Team1.TeamName) #Access the member of a class using dot operator and print the value of a member
35+
Team1.DefineTeamName("Tigers") #call methods of a class using a dot operator
36+
print(Team1.TeamName) #print the value of a member TeamName
37+
print(Team1.TeamOrigin) #print the value of a member TeamOrigin
38+
Team1.DefineTeamOrigin("Chicago") #call method DefineTeamOrigin of a class Team
39+
print(Team1.TeamOrigin) #print value of a member TeamOrigin
40+
41+
'''
42+
Again Define a class Team
43+
'''
44+
class Team:
45+
#constructor passes two variables Name and Origin. The
46+
#default value of variable Name is "Name" and that of variable Origin is "Origin".
47+
#If no values are passed through a constructor its default values are Name and Origin.
48+
def __init__(self, Name = "Name", Origin = "Origin"): #constructor
49+
self.TeamName = Name #member assignment
50+
self.TeamOrigin = Origin #member assignment
51+
52+
def DefineTeamName(self, Name): #class method
53+
self.TeamName = Name
54+
55+
def DefineTeamOrigin(self, Origin): #class method
56+
self.TeamOrigin = Origin
57+
58+
Team1 = Team("Tigers", "Chicago") #creating an object of a class Team.
59+
Team2 = Team("Hawks", "Newyork") #creating an instance of a class Team and passing two values.
60+
Team3 = Team() #creating an object of a class with no values passed.
61+
print(Team1.TeamName) #member can be accessed from outside the class using dot operator
62+
Team1.DefineTeamName("Tigers") #call methods from outside the class
63+
print(Team1.TeamName) #print the value of member TeamName
64+
print(Team1.TeamOrigin) #print the value of member TeamOrigin
65+
Team1.DefineTeamOrigin("Chicago") #call method DefineTeamOrigin
66+
print(Team1.TeamOrigin) #print the value of member
67+
print(Team2.TeamName) #print the value of member TeamName of object Team2
68+
print(Team2.TeamOrigin) #print the value of member TeamOrigin of object Team2
69+
print(Team3.TeamName) #print the value of member TeamName of object Team3
70+
print(Team3.TeamOrigin) #print the value of member TeamOrigin of object Team3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
##
2+
# Dictionaries and Sets Lecture
3+
##
4+
5+
# -*- coding: utf-8 -*-
6+
7+
8+
"""
9+
A set is a data structure in python like a list to store a variety of hetrogenous unique elements.
10+
Hetrogenous means a set can contain primitive types integer , string , float in it.
11+
Unique means that each element can occur only once in a set
12+
"""
13+
14+
# Declaring a set 'Sets' with different string values in it
15+
Sets={"Element1","Element2","Element1","Element4"}
16+
# Printing variable 'Sets' using the 'print' function
17+
print(Sets)
18+
# Output
19+
"""
20+
{'Element1', 'Element4', 'Element2'}
21+
"""
22+
# Notice how the output is different from the input in two ways
23+
# 1) Only unique elements are printed
24+
# 2) Elements are not printed in the same order as they were stored because in sets order doesnt matter
25+
26+
27+
# Here 'if' condition is used along with 'in' keyword to check whether the value "Element1" is present in set 'Sets'
28+
if "Element1" in Sets:
29+
# If the value "Element1" is found print "Yes" to console
30+
print("Yes")
31+
# Output
32+
"""
33+
Yes
34+
"""
35+
36+
37+
# Declaring a list variable 'CountryList' and assigning empty list to it by using '[]'
38+
CountryList = []
39+
40+
# For loop is used with 'range(5)' indicating the loop will run 5 times from 0-4
41+
for i in range(5):
42+
# Taking input from user and assigning it to variable named 'Country' using 'input(range_value)' function
43+
Country = input("Please Enter Your Country: ")
44+
# 'append(variable_name)' function is used here which adds a new element into the list 'CountryList'
45+
CountryList.append(Country)
46+
47+
# A new set 'CountrySet' is created using the 'set(variable_name)' function by passing the variable 'CountryList' which will convert the 'CountryList' to a set
48+
CountrySet = set(CountryList)
49+
50+
# Printing list 'CountryList'
51+
print(CountryList)
52+
# Printing set 'CountrySet'
53+
print(CountrySet)
54+
# Output
55+
"""
56+
Please Enter Your Country: US
57+
Please Enter Your Country: France
58+
Please Enter Your Country: India
59+
Please Enter Your Country: Brazil
60+
Please Enter Your Country: France
61+
['US', 'France', 'India', 'Brazil', 'France']
62+
{'France', 'India', 'Brazil', 'US'}
63+
"""
64+
# First the program asks to entry country names 5 times and then the list and set is printed
65+
# Notice how the set has changed order and only prints unique elements which is the property of set
66+
67+
# Here 'if' condition is used along with 'in' keyword to check whether the value "Brazil" is present in set 'CountrySet'
68+
if "Brazil" in CountrySet:
69+
# If the value "Brazil" is found print "attended" to console
70+
print("attended")
71+
# Output
72+
"""
73+
attended
74+
"""
75+
76+
"""
77+
A dictionary is another data structure in python that also supports hetrogenous data to be stored inside it.
78+
Rather than using index like used in list , a dictionary supports key-value structure where the key is used like an index and value is stored besides it like how values are stored in a variable
79+
A dictionary should also contain unique keys and can contain even lists inside of it.
80+
"""
81+
82+
# Declaring a dictonary variable named 'Dictonary' and assigning keys and values to it
83+
# "Key" , "Key1", "Key2" are the keys
84+
# "Value" , "Value1" , "Value2" are the corresponding values
85+
Dictionary={"Key":"Value","Key1":"Value1","Key2":"Value2"}
86+
# Printing the dictonary variable 'Dictionary'
87+
print(Dictionary)
88+
# Output
89+
"""
90+
{'Key': 'Value', 'Key1': 'Value1', 'Key2': 'Value2'}
91+
"""
92+
93+
94+
# Declaring a list variable 'CountryList' and assigning empty list to it by using '[]'
95+
CountryList = []
96+
97+
# For loop is used with 'range(5)' indicating the loop will run 5 times from 0-4
98+
for i in range(5):
99+
# Taking input from user and assigning it to variable named 'Country' using 'input(range_value)' function
100+
Country = input("Please Enter Your Country: ")
101+
# 'append(variable_name)' function is used here which adds a new element into the list 'CountryList'
102+
CountryList.append(Country)
103+
104+
105+
# Declaring a dictionary variable 'CountryDictionary' and assigning empty dictionary to it by using '{}'
106+
CountryDictionary={}
107+
108+
# A for loop is used using 'for in syntax' to access the elements of list 'CountryList' and stored it in local variable named 'Country'
109+
for Country in CountryList:
110+
# If statement is used in order to check if the country name is present as a key in dictionary 'CountryDictionary'
111+
if Country in CountryDictionary:
112+
# upon finding the key the value is accessed using 'DictionaryName[key_name]' synatx and incremented one to it
113+
CountryDictionary[Country] +=1
114+
else:
115+
# if the key is not found then creating a new key with country name and assigning one to it
116+
# Notice how no error will be produced which was occuring in list when tried to access an element whcih didnt existed
117+
CountryDictionary[Country] = 1
118+
119+
# Printing the dictionary variable 'CountryDictionary'
120+
print(CountryDictionary)
121+
# Output
122+
"""
123+
Please Enter Your Country: US
124+
Please Enter Your Country: France
125+
Please Enter Your Country: India
126+
Please Enter Your Country: Brazil
127+
Please Enter Your Country: France
128+
{'France': 2, 'India': 1, 'Brazil': 1,'US': 1}
129+
"""
130+
# No order is mainatined in dictionary as well

0 commit comments

Comments
 (0)