0% found this document useful (0 votes)
2 views

Exercise For Py

Uploaded by

enockprober
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Exercise For Py

Uploaded by

enockprober
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#Exercise :1

# Create two variables:


# one for your age and another for your name. Print them

# Integer variable
age = 24

# String variable
name = ("name : David")

# Float variable
height = 5.9

# Boolean variable
cyberprober = True

# output for all above.


print(name ,", age: " , age ,", height:" , height ,", is he cyberprober:",
cyberprober,)

# Exercise :2
# Simple Calculator
# Task: Create a simple calculator that adds two numbers.

# integer numbers assinged to inputs


frist_number = float(input("frist_number to add "))# Convert input to float
second_number =float(input("second_number to add "))# Convert input to float
# assing final ans to added numbers
final_ans = frist_number + second_number # Output the sum of the numbers
print("The sum is frist_number + second_number = ", final_ans )
# or try this but change all mathamical operations
print (frist_number ,"+" ,second_number , "=" ,final_ans)

# Exercise :3
# Get user input for the number
number = int(input("Enter a number: ")) # Convert input to integer

# Check if the number is even


if number % 2 == 0: # If the remainder when divided by 2 is 0
print(number, "is even") # Output that the number is even
else:
print(number, "is odd") # Output that the number is odd

# reversed method

# If you enter something like "seven" or "7a",


# Python will not know how to convert that to an integer,
# and you'll see a ValueError.
# To prevent this,
# you can add error handling with try and except to manage incorrect inputs
gracefully

try:
number = int(input("Enter a number: "))
if number % 2 != 0:
print(number, "is odd")
else:
print(number, "is even")

except ValueError:
print("That's not a valid number. Please enter an integer.")

# Exercise 4: List Operations


# Task: Create a list of numbers and print the sum and average.
list=[1, 2, 3, 4,]
len(list)
average= (sum(list))/4
print("Average:",average , " Sum:",sum(list)," Len(list):",len(list))

# or
# This program calculates the sum and average of a list of numbers

# Create a list of numbers


numbers = [1, 2, 3, 4,]

# Calculate the sum of the numbers


sum_of_numbers = sum(numbers) # Use the built-in sum function

# Calculate the average


average = sum_of_numbers / len(numbers) # Divide sum by the number of elements in
each list

# Print the results


print("Sum:", sum_of_numbers) # Output the sum
print("Average:", average) # Output the average
print("len(numbers)",len(numbers) ) # output the len(numbers)

# Exercise 6: Simple If-Else


# Task: Write a program that checks if a user is eligible to vote.

voters = int(input ("plz, how old are you ? Numbers only. "))
if voters <= 17:
print("SO sorry, plz wait until 18 years old")
else:
print("congratulations our beloved voter !! ")

# or
# This program checks if a user is eligible to vote

# Get user input for age


age = int(input("Enter your age: ")) # Convert input to integer

# Check voting eligibility


if age >= 18: # If age is 18 or older
print("You are eligible to vote.") # Output eligibility message
else:
print("You are not eligible to vote.") # Output ineligibility message
# Exercise 7: Looping Through a List
# Task: Loop through a list of fruits and print each fruit.

# Create a list of fruits


fruits = ["mango","orange","banana","apple","berry","cherry"] # fruits data sorted
in list
# Loop through the list and print each fruit
for fruit in fruits: # For each fruit in the fruits list
print(fruit)

# Exercise 8: Function Definition


# Task: Write a function that takes two numbers and returns their product.

# Creating our function


def productions(number1,number2):
return number1*number2 # finished function created and " using return
function "asked to return
# to give back data to programmer or results to me so as to use them in next.
# parts

# For user input your can assinge your function to user input.

number1 = int(input("your frist number "))

number2 = int(input("your secord number "))


print(productions(number1,number2))

# or use fix values


print(productions(4,10))

# This program defines a function to multiply two numbers

def multiply(num1, num2):


"""Return the product of num1 and num2."""
return num1 * num2 # Return the product

# Call the function and print the result


result = multiply(5, 10) # Call the multiply function
print("The product is:", result) # Output the product

# Exercise 9: Counting Vowels


# Task: Write a program that counts the number of vowels in a string.

You might also like