Skill Training Python

overview

Operators and expressions are fundamental to programming. Here's a simple Python program that uses operators and expressions to perform various calculations.

# Program using operators and expressions

# Arithmetic operators
num1 = 10
num2 = 5

addition = num1 + num2
subtraction = num1 - num2
multiplication = num1 * num2
division = num1 / num2
remainder = num1 % num2
exponentiation = num1 ** num2

print("Arithmetic Operators:")
print(f"{num1} + {num2} = {addition}")
print(f"{num1} - {num2} = {subtraction}")
print(f"{num1} * {num2} = {multiplication}")
print(f"{num1} / {num2} = {division}")
print(f"{num1} % {num2} = {remainder}")
print(f"{num1} ** {num2} = {exponentiation}\n")

# Comparison operators
x = 10
y = 5

equals = x == y
not_equals = x != y
greater_than = x > y
less_than = x < y
greater_than_or_equal = x >= y
less_than_or_equal = x <= y

print("Comparison Operators:")
print(f"{x} == {y} is {equals}")
print(f"{x} != {y} is {not_equals}")
print(f"{x} > {y} is {greater_than}")
print(f"{x} < {y} is {less_than}")
print(f"{x} >= {y} is {greater_than_or_equal}")
print(f"{x} <= {y} is {less_than_or_equal}\n")

# Logical operators
p = True
q = False

logical_and = p and q
logical_or = p or q
logical_not_p = not p
logical_not_q = not q

print("Logical Operators:")
print(f"{p} and {q} is {logical_and}")
print(f"{p} or {q} is {logical_or}")
print(f"not {p} is {logical_not_p}")
print(f"not {q} is {logical_not_q}")

This program demonstrates the use of arithmetic operators (+, -, , /, %, *) and comparison operators (==, !=, >, <, >=, <=), as well as logical operators (and, or, not). It performs various calculations and displays the results.

Program using conditional control structures.

Conditional control structures, like if statements, allow you to make decisions in your programs based on certain conditions. Here's a simple Python program that uses conditional control structures:

# Program using conditional control structures
# Error code

# Input from the user
age = int(input("Enter your age: ")

# Conditional statement
if age < 18:
    print("You are a minor.")
elif age >= 18 and age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")



# Program using conditional control structures

# Input from the user
age = int(input("Enter your age: "))  # Added a closing parenthesis

# Conditional statement
if age < 18:
    print("You are a minor.")
elif 18 <= age < 65:
    print("You are an adult.")
else:
    print("You are a senior citizen.")

# Using conditional operators
num = int(input("Enter a number: "))
if num % 2 == 0:
    print(f"{num} is even.")
else:
    print(f"{num} is odd.")

In this program:

  1. The user is prompted to enter their age.

  2. A conditional control structure is used to determine whether the user is a minor, an adult, or a senior citizen based on their age.

  3. Another input is taken to enter a number.

  4. A conditional control structure is used to check if the number is even or odd using the modulus operator (%).

This program demonstrates the use of if, elif, and else statements to make decisions based on different conditions.

  • Program using looping structures.

    Looping structures in Python, such as for and while loops are used to repeat a block of code multiple times. Here's a simple Python program that uses both for and while loops:

      # Program using looping structures
    
      # For loop
      fruits = ["apple", "banana", "cherry"]
      print("Using a for loop to iterate over a list:")
      for fruit in fruits:
          print(f"I like {fruit}s!")
    
      # While loop
      count = 1
      print("\nUsing a while loop to count from 1 to 5:")
      while count <= 5:
          print(count)
          count += 1
    

    In this program:

    1. A for loop is used to iterate through a list of fruits and print a message for each fruit.

    2. A while loop is used to count from 1 to 5 and print each number.

This program demonstrates the use of looping structures to repeat a set of instructions multiple times.