Python Programming: Module 2 - Control Flow, Operators, and Data Structures, Exams of Advanced Education

A concise overview of fundamental python programming concepts, including control flow structures (if, while, for), operators (comparison, bitwise), and data structures (lists). It presents code examples and explanations to illustrate these concepts, making it a valuable resource for beginners learning python.

Typology: Exams

2024/2025

Available from 02/06/2025

faith-wambua-1
faith-wambua-1 🇬🇧

83 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Computer Programming: Python - Module 2
(Code) Questions Answers
if x == y:
→something #if x equals y do something
#can be >, <, >=, <=, != (not equal to)
while x == y:
→something #while x is equal to y do something
#can be >, <, >=, <=, != (not equal to)
while True:
→something
→if x == y:
→→break
while True:
→something
→if x == y:
→→continue
for i in range (x):
→something #does "something" "x" times, starts counting at "0"
for i in range (x,y):
→something #starts counting at x and ends at y
#x>y
for i in range (x,y,z):
→something #starts counting at x by increments of z and ends at y
pass an empty instruction
x > y and q == z checks if x is greater then y and if q equals z before continuing
(also can be done with or)
not (Variable == 0) same as Variable != 0
&
|
~
^ (ampersand) bitwise conjunction
(bar) bitwise disjunction
(tilde) bitwise negation
pf3

Partial preview of the text

Download Python Programming: Module 2 - Control Flow, Operators, and Data Structures and more Exams Advanced Education in PDF only on Docsity!

Computer Programming: Python - Module 2

(Code) Questions Answers

if x == y: →something ✅#if x equals y do something #can be >, <, >=, <=, != (not equal to) while x == y: →something ✅#while x is equal to y do something #can be >, <, >=, <=, != (not equal to) while True: →something →if x == y: →→break ✅ while True: →something →if x == y: →→continue ✅ for i in range (x): →something ✅#does "something" "x" times, starts counting at "0" for i in range (x,y): →something ✅#starts counting at x and ends at y #x>y for i in range (x,y,z): →something ✅#starts counting at x by increments of z and ends at y pass ✅an empty instruction x > y and q == z ✅checks if x is greater then y and if q equals z before continuing (also can be done with or) not (Variable == 0) ✅same as Variable != 0 & | ~ ^ ✅(ampersand) bitwise conjunction (bar) bitwise disjunction (tilde) bitwise negation

(caret) bitwise exclusive or (xor) the arguments of these operators must be integers Var = 17 VarRight = Var >> 1 VarLeft = Var << 2 print(Var, VarLeft, VarRight) ✅17 68 8 #17 // 2 → 8 (shifting to the right by one bit is the same as integer division by two) 17 * 4 → 68 (shifting to the left by two bits is the same as integer multiplication by four) numbers = [ 10, 5, 7, 2, 1 ] numbers[2] = 111 ✅background: [ 10, 5, 111, 2, 1 ] numbers = [ 10, 5, 7, 2, 1 ] len(numbers) ✅#len determines the lists length numbers = [ 10, 5, 7, 2, 1 ] del numbers[1] ✅background: [ 10, 7, 2, 1 ] numbers = [ 10, 5, 7, 2, 1 ] print(numbers[-1]) ✅ 1 numbers = [ 10, 5, 7, 2, 1 ] numbers.append(4) ✅background: [ 10, 5, 7, 2, 1, 4 ] numbers = [ 10, 5, 7, 2, 1 ] numbers.insert (1, 33) ✅background: [ 10, 33, 5, 7, 2, 1, ] list = [ 8, 10 , 6, 2, 4 ] list.sort( ) print(list) ✅[2, 4, 6, 8, 10] list1 = [ 1 ] list2 = list1 [ : ] list1 [ 0 ] = 2 print(list2) ✅[1] #[ : ] creates a new list for list 2 instead of assigning list2 to the same location as list list = [10, 8, 6, 4, 2] new_list = list [1:4] print (new_list) ✅[8, 6, 4]