You are on page 1of 5

Programming Logic and Design

Lesson 1:
I. Introduction In Python 2. Packing and Unpacking
1. Python print () statement ➢ x = (“CMU", 20, "Education")
➢ Print (“Hello World”) (Company, emp, profile) = x
✓ Output: Hello World print(company)
➢ Print (“Welcome to COE114”) print(emp)
✓ Output: Welcome to COE114 print(profile)
2. How to print blank lines ✓ Output:
➢ Print (“Hello World”) CMU
Print (2 * ”\n”) 20
Print (“Hello World”) Education
✓ Output: 3. Comparing Tuples
Hello World ➢ A = (5,6)
- B = (1,4)
- if (a>b): print ("a is bigger")
Hello World else: print ("b is bigger")
3. Print End Command ✓ Output: a is bigger
➢ Print (“Python”, end = ‘@’) 4. Using Tuples as keys in dictionaries
✓ Output: Python@ ➢ a = {'x':100, 'y':200}
b = list(a.items())
II. Python Variables: How to Define/Declare String print(b)
Variable Types ✓ Output: [('x', 100), ('y', 200)]
1. How to Declare and Use a Variable 5. Deleting Tuples
➢ a = 250 ➢ del b
print (a) ✓ Output: syntax error, b is undefined
✓ Output: 250 6. Slicing of Tuple
2. Delete a Variable ➢ x = ("a", "b","c", "d", "e")
➢ f = 11 print (x [2:4])
➢ print (f) ✓ Output: (‘c’, ‘d’)
✓ Output: 11
Del f
F= 12 Lesson 3:
Print (f)
✓ Output: 12 IV. Not equal operator in Python
1. Not equal operator
Lesson 2: ➢ C = 12222
X = 12222.0
III. Tuple
Y = "12222"
1. Tuple Assignment
Print (C! = X)
➢ tup1 = ('Robert', 'Carlos','1965','Terminator
print (X! = Y)
1995', 'Actor','Florida');
print (C! = Y)
tup2 = (1,2,3,4,5,6,7);
✓ Output:
print(tup1[0])
False
print(tup2[1:4])
True
✓ Output:
True
Robert
(2, 3, 4)
2. Not equal operator with If statement 4. Insert Elements
➢ X=5 ➢ import array
Y=5 balance = array.array('i', [300,200,100])
if (X! = Y): balance.insert(2, 150)
print ("X is not equal to Y") print(balance)
else: ✓ Output:
print ("X is equal to Y") array ('i', [300, 200, 150, 100])
✓ Output: X is equal to Y 5. Modify Elements
3. How to use equal to (==) operator with while loop ➢ import array as myarr
➢ m=300 a=myarr.array('b',[3,6,4,8,10,12,14,16,18,20])
while m<=305: a[0]=99
m=m+1 print(a)
if m%2 == 0: ✓ Output:
continue Array ('b', [99, 6, 4, 8, 10, 12, 14, 16, 18, 20])
print(m) 6. Concatenation Operations
✓ Output: ➢ import array as myarr
301 first = myarr.array('b', [4, 6, 8])
303 second = myarr.array('b', [9, 12, 15])
305 numbers = myarr.array('b')
4. Finding even numbers by using not equal operator numbers = first + second
➢ m=300 print(numbers)
while m<=305: ✓ Output: array ('b', [4, 6, 8, 9, 12, 15])
m=m+1 7. Pop an element from array in Python (pop or del)
if m%2! = 0: ➢ import array as myarr
continue first = myarr.array('b', [20, 25, 30])
print(m) first.pop(2) / del no[0]
Output: print(first)
302 ✓ Output: array ('b', [20, 25])
304 8. Delete Elements
306 ➢ import array as myarray
first = myarray.array('b', [2, 3, 4])
V. Array in Python first.remove(3)
1. Array print(first)
➢ import array ✓ Output: array ('b', [2, 4])
balance = array.array('i', [300,200,100]) 9. Search and get the index of a value in an Array
print (balance [1]) ➢ import array as myarray
✓ Output: 200 number = myarray.array('b', [2, 3, 4, 5, 6])
2. Accessing Array Item print(number.index(3))
➢ import array as myarray ✓ Output: 1
abc = myarray.array('d', [2.5, 4.9, 6.7]) 10. Reverse an Array in Python
print ("Array first element is:",abc[0]) ➢ import array as myarray
print ("Array last element is:",abc[-1]) number = myarray.array('b', [1,2, 3])
✓ Output: number.reverse()
Array first element is: 2.5 print(number)
Array last element is: 6.7 ✓ Output: array ('b', [3, 2, 1])
3. Access elements by using the ‘:’ 11. Count the occurrence of a Value in Array
➢ import array as myarray ➢ import array as myarr
abc= myarray.array('q',[3,9,6,5,20,13,19,22,30,25]) number = myarr.array('b', [2, 3, 5, 4,3,3,3])
print(abc[1:4]) print(number.count(3))
print(abc[7:10]) ✓ Output: 4
✓ Output:
Array ('q', [9, 6, 5])
Array ('q', [22, 30, 25])
Lesson 4:
VI. Python Conditional Statements 5. Python nested if statement
1. If Statement ➢ total = 100
➢ def main (): country = "US"
x,y =2,8 country = "AU"
if (x < y): if country == "US":
st= "x is less than y" if total <= 50:
print(st) print ("Shipping Cost is $50")
if __name__ == "__main__": elif total <= 100:
main () print ("Shipping Cost is $25")
✓ Output: x is less than y elif total <= 150:
2. else condition print ("Shipping Costs $5")
➢ def main (): else:
x,y =8,4 print("FREE")
if (x < y): if country == "AU":
st= "x is less than y" if total <= 50:
else: print ("Shipping Cost is $100")
st= "x is greater than y" else:
print (st) print("FREE")
if __name__ == "__main__": ✓ Output: Shipping Cost is $25
main () 6. Switch Case statement in Python
✓ Output: x is greater than y ➢ def SwitchExample(argument):
3. Elif condition switcher = {
➢ def main (): 0: " This is Case Zero ",
x,y = 8,8 1: " This is Case One ",
if (x<y): 2: " This is Case Two ",
st="x is less than y" }
elif(x==y): return switcher.get(argument, "nothing")
st="x is same as y" if __name__ == "__main__":
else: argument = 1
st="x is greater than y" print (SwitchExample(argument))
print(st) ✓ Output: This is Case One
if __name__ == "__main__": VII. Python for & While Loops
main () 1. While loop
✓ Output: x is same as y x=0
4. Execute conditional statement with minimal code while (x <4):
➢ def main (): print(x)
x,y = 10,8 x = x+1
st = "x is less than y" if (x < y) else "x is ✓ Output:
greater than or equal to y" 0
print(st) 0
if __name__ == "__main__": 0
main () …
✓ Output: x is greater than or equal to y 2. For loop
x=0
for x in range (2,7):
print(x)
✓ Output:
2
3
4
5
6
3. For loop for string
➢ Months = ✓ Output:
["Jan","Feb","Mar","April","May","June"] Siya
For m in Months: After break statement
print(m) Loop is Terminated
✓ Output: Tiya
Jan After break statement
Feb Loop is Terminated
Mar Guru
April Found the name Guru
May 2. Break statement inside while-loop
June ➢ my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru']
4. Break statements in For loop i=0
➢ for x in range (10,20): while True:
if (x == 15): break print(my_list[i])
#if (x % 2 == 0) : continue if (my_list[i] == 'Guru'):
print(x) print ('Found the name Guru')
✓ Output: 15 break
5. Continue Statement in for loop print ('After break statement')
➢ for x in range (10,20): i += 1
#if (x == 15): break print ('After while-loop exit')
if (x % 5 == 0) : continue ✓ Output:
print(x) Siya
✓ Output: 19 After while-loop exit
6. Enumerate in python Tiya
➢ Months = After while-loop exit
["Jan","Feb","Mar","April","May","June"] Guru
for i, m in enumerate (Months): Found the name Guru
print(i,m) Continue Statement Execution flow
✓ Output: 3. Continue inside for-loop
0 Jan ➢ for i in range (10):
1 Feb if i == 7:
2 Mar continue
3 April print ("The Number is:", i)
4 May ✓ Output:
5 June The Number is: 0
The Number is: 1
Lesson 5: The Number is: 2
The Number is: 3
VIII. Python break, continue, pass statements
The Number is: 4
Break statement Execution flow
The Number is: 5
1. Break statement inside for -loop
➢ my_list = ['Siya', 'Tiya', 'Guru', 'Daksh', 'Riya', 'Guru'] The Number is: 6
for i in range(len(my_list)): The Number is: 8
print(my_list[i]) The Number is: 9
if my_list[i] == 'Guru':
print ('Found the name Guru')
break
print ('After break statement')
print ('Loop is Terminated')
4. Continue inside while-loop
➢ i=0
while i <= 10:
if i == 7:
i += 1
continue
print ("The Number is:", i)
i += 1
✓ Output:
The Number is: 0
The Number is: 1
The Number is: 2
The Number is: 3
The Number is: 4
The Number is: 5
The Number is: 6
The Number is: 8
The Number is: 9
The Number is: 10
5. Pass statement inside a function
➢ def my_func():
print ('pass inside function')
pass
my_func()
✓ Output: pass inside function
6. Pass statement inside the class
➢ Class My_Class:
Print ("Inside My_Class")
Pass
✓ Output: Inside My_Class
7. Pass statement inside the loop
➢ test = “COE114"
for i in test:
if i == 'r':
print ('Pass executed')
pass
print(i)
✓ Output:
C
O
E
1
1
4
8. Pass statement inside if-loop
➢ a=1
if a==1:
print ('pass executed')
pass
✓ Output: pass executed

You might also like