You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
4.8 KiB
Python

# [SECTION] User Input
# Python allows input from the user.
# The input() function lets the user provide input to the program.
username = input("Please enter your name:\n")
print(f"Hello {username}! Welcome to the Python short course!")
# Result: Hello John! Welcome to the Python short course!
num1 = input("Enter 1st number:\n")
num2 = input("Enter 2nd number:\n")
print(f"The sum of num1 and num2 is {num1 + num2}")
# Result: The sum of num1 and num2 is 1015
# To solve this, the input values can be typecast to the appropriate data type.
num1 = int(input("Enter 1st number:\n"))
num2 = int(input("Enter 2nd number:\n"))
print(f"The sum of num1 and num2 is {num1 + num2}")
# Result: The sum of num1 and num2 is 15
# [SECTION] Control Structures
# Control structures can be divided into selection and repetition structures.
# Selection control structures allow a program to choose between options and execute specific code blocks based on the condition.
# Repetition control structures enable a program to repeat certain blocks of code as long as a starting condition is met and until a terminating condition is reached.
# Selection Control Structures
# If-else statements are used to choose between two or more code blocks depending on the condition.
test_num1 = 75
if test_num1 >= 60:
print("Test passed")
else:
print("Test failed")
# Result: Test passed
# If-else chains can also be used to provide more than two choices for the program.
test_num2 = int(input("Please enter the test number:\n"))
if test_num2 > 0:
print("The number is positive")
elif test_num2 == 0:
print("The number is zero")
else:
print("The number is negative")
# Result:
# (15): The number is positive
# (0): The number is zero
# (-15): The number is negative
# Mini Exercise 1:
# Create an if-else statement that determines if a number is divisible by 3, 5, or both.
# If the number is divisible by both 3 and 5, print "The number is divisible by both 3 and 5."
# If the number is divisible only by 3, print "The number is divisible by 3."
# If the number is divisible only by 5, print "The number is divisible by 5."
# If the number is not divisible by either, print "The number is not divisible by 3 or 5."
# Solution:
test_div_num = int(input("Please enter a number to test:\n"))
if test_div_num % 3 == 0 and test_div_num % 5 == 0:
print(f"{test_div_num} is divisible by both 3 and 5")
elif test_div_num % 3 == 0:
print(f"{test_div_num} is divisible by 3")
elif test_div_num % 5 == 0:
print(f"{test_div_num} is divisible by 5")
else:
print(f"{test_div_num} is not divisible by both 3 or 5")
# Repetition Control Structure
# Repetition control structures, also known as loops, allow you to execute a block of code repeatedly.
# While loops are used to repeatedly execute a block of code as long as a specified condition is true.
print("While Loop:")
i = 1
while i <= 5:
print(f"Current count {i}")
i += 1
# Result:
# Current count 1
# Current count 2
# Current count 3
# Current count 4
# Current count 5
# For Loops in Python are used to iterate over a sequence.
print("For Loop:")
fruits = ["apple", "banana", "cherry"]
for indiv_fruit in fruits:
print(indiv_fruit)
# Result:
# apple
# banana
# cherry
# To use a for loop to iterate through values, the range() function can be used.
# The range() function is a built-in function in Python used to generate a sequence of numbers.
print("For Loop with range() function:")
for x in range(6):
print(f"The current value is {x}")
# Result:
# The current value is 0
# The current value is 1
# The current value is 2
# The current value is 3
# The current value is 4
# The current value is 5
# range() function with start and stop values
for x in range(6, 10):
print(f"The current value is {x}")
# Result:
# The current value is 6
# The current value is 7
# The current value is 8
# The current value is 9
# range() function with start, stop, and step values
for x in range(6, 20, 2):
print(f"The current value is {x}")
# Result:
# The current value is 6
# The current value is 8
# The current value is 10
# The current value is 12
# The current value is 14
# The current value is 16
# The current value is 18
# [SECTION] Break Statement
# The break statement is used to stop the loop.
print("Break Statement in a While Loop:")
j = 1
while j < 6:
print(j)
if j == 3:
break #
j += 1
# Result:
# 1
# 2
# 3
# Continue Statement
# The continue statement returns control to the beginning of the loop and continues with the next iteration.
print("Continue Statement in a While Loop:")
k = 1
while k < 6:
k += 1
if k == 3:
continue
print(k)
# Result:
# 2
# 4
# 5
# 6
# This results in an infinite loop, preventing the program from finishing.
k = 0
while k < 6:
if k == 3:
continue
k += 1
print(k)
# Result:
# 1
# 2
# 3
# .. infinite Loop