Python Basics Selection and Iteration
Python Basics Selection and Iteration
1st Module
a.
my_boolean = True
print(my_boolean)
b.
my_int = 3
print(my_int)
c.
my_string = "This is a string"
print(my_string)
d.
my_float = 3.0
print(my_float)
e.
print("Okay, it is", end=" ")
print("time to learn about operators.")
2nd Module
a.
a = 4 and b = 9
b.
print(not b and not a or not not c)
c.
greeting = "Hello"
place = "world"
d.
result = 7 // 2 # Floor division (//) discards the decimal part
print(result)
e.
a = 3.0
b = int("2")
print(int(a + b))
3rd Module
a.
import sys
x = int(sys.argv[1])
# Check if x is in the specified ranges
if (0 <= x <= 25) or (75 <= x <= 100):
print(str(x) + " is between 0 and 25 or 75 and 100")
b.
import sys
x = int(sys.argv[1])
# Check if x is divisible by 5
if x % 5 == 0:
print(str(x) + " is divisible by 5")
else:
print(str(x) + " is not divisible by 5")
c.
import sys
x = int(sys.argv[1])
# Check if x is divisible by 5 and even
if (x % 5 == 0) and (x % 2 == 0):
print(str(x) + " is divisible by 5 and even")
else:
print(str(x) + " is not divisible by 5 or it is odd")
d.
import sys
x = sys.argv[1]
4th Module
a.
t.forward(100)
t.lt(120)
b.
count = 0
while count < 10:
count = count + 1
c.
sum = 0
i=0
while True:
sum += i
i += 1
if i > 100:
break
print(sum)
d.
for outer_loop in range(4):
for inner_loop in range(4):
t.forward(50)
t.rt(90)
t.forward(100)
for last_loop in range(4):
t.forward(50)
t.rt(90)
e.
for num in range(1,6):
for dots in range(5-num, 0, -1):
print(".", end='')
print(num)