Python Coding Exercises 1

Python Coding Exercises 1

By Ifeanyi omeata

Tasks -
Task 1 Task 2 Task 3 Task 4 Task 5 Task 6 Task 7 Task 8 Task 9 Task 10 Task 11 Task 12 Task 13 Task 14 Task 15 Task 16 Task 17 Task 18 Task 19 Task 20


Task 1 -
I. Create a variable "message_1" with value = Bobby's World. (Use an Escape String)
II. Create a variable "message_2" with value = Jonny's World. (Use a Double Quote)
III. Using the variables, print out the String using the 2 ways of string formatting:
"Bobby's World and Jonny's World."
>>Return to Menu


#Task 1 -
#I. Create a variable "message_1" with value 'Bobby's World'. (Use an Escape String)
#II. Create a variable "message_2" with value 'Jonny's World'. (Use a Double Quote)
#III. Using the variables, print out the String using the 2 ways of string formatting: 
#"Bobby's World and Jonny's World."

message_1 = 'Bobby\'s World'
message_2 = "Jonny's World"

print("{} and {}.".format(message_1, message_2))
print(f"{message_1} and {message_2}.")

image.png


Task 2 -
I. Create a variable "my_firstname" with value as your First Name.
II. Create a variable "my_lastname" with value as your Last Name.
III. Create a thrid variable "my_course" with value as "Python".
IV. Using the variables, print out the String using concatenation method:
"Hello (my_firstname) (my_lastname), your course is (my_course)."
>>Return to Menu


#Task 2 -
#I. Create a variable "my_firstname" with value as your First Name. <br>
#II. Create a variable "my_lastname" with value as your Last Name. <br>
#III. Create a thrid variable "my_course" with value as "Python". <br>
#IV. Using the variables, print out the String using concatenation method:
#"Hello (my_firstname) (my_lastname),  your course is (my_course)."

my_firstname = "Ifeanyi"
my_lastname = "Omeata"
my_course = "Python"

print("Hello" + " " + my_firstname + " " + my_lastname + ", " + "your course is " + my_course + ".")

image.png


Task 3 -
Print out the datatypes for the following:
I. "Hello, welcome to Python."
II. 110332
III. True
IV. 1.2246
V. None
>>Return to Menu


#Task 3 -
#Print out the datatypes for the following:<br>
#I. "Hello, welcome to Python."
#II. 110332
#III. True
#IV. 1.2246
#V. None

print(type("Hello, welcome to Python."))
print(type(110332))
print(type(True))
print(type(1.2246))
print(type(None))

image.png


Task 4 -
Convert the following datatypes:
I. "Welcome!" From String to Boolean
II. "10.633" From String to Float
III. 110332 From Integer to String
IV. 1.2246 From Float to Integer
V. False From Boolean to Integer
>>Return to Menu


#Task 4 -
#Convert the following datatypes:
#I. "Welcome!" From String to Boolean
#II. "10.633" From String to Float
#III. 110332 From Integer to String
#IV. 1.2246 From Float to Integer
#V. False From Boolean to Integer

#String to Boolean
my_string = "Welcome!" 
print(type(my_string))
string_to_boolean = bool(my_string)
print(type(string_to_boolean))
print(string_to_boolean)
#String to Float
my_string = "10.633"
print(type(my_string))
string_to_float = float(my_string)
print(type(string_to_float))
print(string_to_float)
#Integer to String
my_integer = 110332
print(type(my_integer))
integer_to_string = str(my_integer)
print(type(integer_to_string))
print(integer_to_string)
#Float to Integer
my_float = 1.2246
print(type(my_float))
float_to_integer = int(my_float)
print(type(float_to_integer))
print(float_to_integer)
#Boolean to Integer
my_boolean = False
print(type(my_boolean))
boolean_to_integer = int(my_boolean)
print(type(boolean_to_integer))
print(boolean_to_integer)

image.png


Task 5 -

message = "I love Python always"

Print out the message as follows:
I. Make all the characters be lowercase.
II. Make all the characters be uppercase.
III. Make only the first character be uppercase.
IV. Make only the first characters of words in the string be uppercase.
>>Return to Menu


#Task 5 -
# message = "I love Python always"
# Print out the message as follows:
# I. Make all the characters be lowercase.
# II. Make all the characters be uppercase.
# III. Make only the first character be uppercase.
# IV. Make only the first characters of words in the string be uppercase.

message = "I love Python always"
#All the characters be lowercase
print(message.lower())
#All the characters be uppercase
print(message.upper())
#Only the first character be uppercase
print(message.capitalize())
#Only the first characters of each word in the string be uppercase.
print(message.title())

image.png


Task 6 -

message = "I love Python always"

Perform the following functions on the message as follows:
I. Find the total number of characters in the string.
II. Find how many times the letter "a" appears in the string.
III. Find how many times the word "Python" appears in the string.
IV. Find the index position of the first letter "o" in the string.
V. Find the index position of the word "always" in the string.
VI. Replace "I" with "We" in the string.
VII. Replace the word "always" with "sometimes" in the string.
>>Return to Menu


# Task 6 -
# message = "I love Python always"
# Perform the following functions on the message as follows:
# I. Find the total number of characters in the string.
# II. Find how many times the letter "a" appears in the string.
# III. Find how many times the word "Python" appears in the string.
# IV. Find the index position of the first letter "o" in the string.
# V. Find the index position of the word "always" in the string.
# VI. Replace "I" with "We" in the string.
# VII. Replace the word "always" with "sometimes" in the string.

message = "I love Python always"

#Total number of characters in the string
print(len(message))
#How many times the letter "a" appears in the string
print(message.count("a"))
#How many times the word "Python" appears in the string
print(message.count("Python"))
#The index position of the first letter "o" in the string
print(message.find("o"))
#The index position of the word "always" in the string
print(message.find("always"))
#Replace "I" with "We" in the string
print(message.replace("I", "We"))
#Replace the word "always" with "sometimes" in the string
print(message.replace("always","sometimes"))

image.png


Task 7 -

message = "I love Python always"

Perform the following functions on the message as follows:
I. Print out the first character in the string.
II. Print out the sixth character in the string.
III. Print out the last character in the string.
IV. Print out the third from the last character in the string.
V. Print out everything from the forth character in the string.
VI. Print out everything from the fifth character to the tenth character in the string.
VII. Print out everything from the first character to the ninth character in the string.
VIII. Print out everything from the first character to the tenth character with a step of 2.
IX. Print out everything from the tenth character to the first character backwards.
X. Print out everything from the last character to the fifth character backwards, with a step of 2.
XI. Print out everything from the last character to the first character backwards (reverse).
>>Return to Menu


# Task 7 -
#message = "I love Python always"
# Perform the following functions on the message as follows:
# I. Print out the first character in the string.
# II. Print out the sixth character in the string.
# III. Print out the last character in the string.
# IV. Print out the third from the last character in the string.
# V. Print out everything from the forth character in the string.
# VI. Print out everything from the fifth character to the tenth character (inclusive) in the string.
# VII. Print out everything from the first character to the ninth character (inclusive) in the string.
# VIII. Print out everything from the first character to the tenth character (inclusive) with a step of 2.
# IX. Print out everything from the tenth character to the first character (inclusive) backwards.
# X. Print out everything from the last character to the fifth character (inclusive) backwards, with a step of 2.
# XI. Print out everything from the last character to the first character backwards (reverse).

message = "I love Python always"

#The first character in the string.
print(message[0])
#The sixth character in the string.
print(message[5])
#The last character in the string.
print(message[-1])
#The third from the last character in the string.
print(message[-3])
#Everything from the forth character in the string.
print(message[3:])
#Everything from the fifth character to the tenth character (inclusive) in the string.
print(message[4:10])
#Everything from the first character to the ninth character (inclusive) in the string.
print(message[:9])
#Everything from the first character to the tenth character (inclusive) with a step of 2.
print(message[:10:2])
#Everything from the tenth character to the first character (inclusive) backwards.
print(message[9::-1])
#Everything from the last character to the fifth character (inclusive) backwards, with a step of 2.
print(message[-1:5:-2])
#Everything from the last character to the first character backwards (reverse).
print(message[-1::-1])

image.png


Task 8 -

message = "I love Python always"

Perform the following functions on the message as follows:
I. Find out if "love" is found in the string.
II. Find out if "cake" is found in the string.
III. Find out if "banana" is Not found in the string.
IV. Find out if "always" is Not found in the string.

>>Return to Menu


# Task 8 -
# message = "I love Python always"
# Perform the following functions on the message as follows:
# I. Find out if "love" is found in the string.
# II. Find out if "cake" is found in the string.
# III. Find out if "banana" is Not found in the string.
# IV. Find out if "always" is Not found in the string.

message = "I love Python always"

#Find out if "love" is found in the string.
print("love" in message)
#Find out if "cake" is found in the string.
print("cake" in message)
#Find out if "banana" is Not found in the string.
print("banana" not in message)
#Find out if "always" is Not found in the string.
print("always" not in message)

image.png


Task 9 -
Perform the following functions:
I. Take an Input for the name of a person as a variable name.
II. Take an Input for the age of a person as a variable age.
III. Take an Input as x for the number of years you want to add to the person's age.
IV. Using the input variables, print out the string as follows:
Hello (name), in (x) years time you will be (total) years old.
>>Return to Menu


name = input("What is your name: ")
age = int(input("How old are you: "))
x = int(input("How many years time: "))
print(f"Hello {name}, in {x} years time you will be {age + x} years old.")

image.png


Task 10 -
Perform the following functions:
I. Create a distance converter converting Km to miles.
II. Take two inputs from user: their name (name) and the distance in km (distance_km).
III. Print:
"Hello (name), you have covered (distance_km)km, which is the same as (distance_mi) miles."
IV. Note: 1 mile is 1.609 kilometers.
>>Return to Menu


# Task 10 -
# Perform the following functions:
# I. Create a distance converter converting Km to miles.
# II. Take two inputs from user: their name (name) and the distance in km (distance_km).
# III. Print:
# "Hello (name), you have covered (distance_km)km, which is the same as (distance_mi) miles."
# IV. Note: 1 mile is 1.609 kilometers.

name = input("What is your name: ")
distance_km = float(input("What is your distance in Km: "))
distance_mi = round(distance_km/1.609, 2)
#OR
#distance_mi = distance_km/1.609
#distance_mi = "{:.2f}".format(distance_mi)

print(f"Hello {name}, you have covered {distance_km}km, which is the same as {distance_mi} miles.")

image.png


Task 11 -

people = ["Mary", "Ashley", "Mike", "Femi", "Adam", "Chloe"]

Perform the following functions to the list:
I. Print out the third person in the list.
II. Print out the last person in the list.
III. Print out everyone from the second person in the list.
IV. Print out everyone from the third person to the fifth person (inclusive) in the list.
V. Print out everyone from the first person to the fourth person (inclusive) in the list.
VI. Print out everyone from the first person (with a step of 2) in the list.
VII. Print out the list in reverse.
VIII. Print out the list in reverse (with a step of 2).

>>Return to Menu


# Task 11 -
# people = ["Mary", "Ashley", "Mike", "Femi", "Adam", "Chloe"]
# Perform the following functions to the list:
# I. Print out the third person in the list.
# II. Print out the last person in the list.
# III. Print out everyone from the second person in the list.
# IV. Print out everyone from the third person to the fifth person (inclusive) in the list.
# V. Print out everyone from the first person to the fourth person (inclusive) in the list.
# VI. Print out everyone from the first person (with a step of 2) in the list.
# VII. Print out the list in reverse.
# VIII. Print out the list in reverse (with a step of 2).

people = ["Mary", "Ashley", "Mike", "Femi", "Adam", "Chloe"]

#The third person in the list.
print(people[2])
#The last person in the list.
print(people[-1])
#Everyone from the second person in the list.
print(people[1:])
#Everyone from the third person to the fifth person (inclusive) in the list.
print(people[2:5])
#Everyone from the first person to the fourth person (inclusive) in the list.
print(people[:4])
#Everyone from the first person (with a step of 2) in the list.
print(people[::2])
#The list in reverse.
print(people[::-1])
#The list in reverse (with a step of 2).
print(people[::-2])

image.png


Task 12 -

people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]

Perform the following functions to the list:
I. Print out the number of items in the list.
II. Print out the Index position of person 'Femi'.
III. Print out the Index position of person 'Adam'.
IV. Print out the number of times 'Mike' is found in the list.
V. Print out the number of times 'Charles' is found in the list.

>>Return to Menu


# Task 12 -
# people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
# Perform the following functions to the list:
# I. Print out the number of items in the list.
# II. Print out the Index position of person 'Femi'.
# III. Print out the Index position of person 'Adam'.
# IV. Print out the number of times 'Mike' is found in the list.
# V. Print out the number of times 'Charles' is found in the list.

people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]

#The number of items in the list.
print(len(people))
#The Index position of person 'Femi'.
print(people.index("Femi"))
#The Index position of person 'Adam'.
print(people.index("Adam"))
#The number of times 'Mike' is found in the list.
print(people.count("Mike"))
#The number of times 'Charles' is found in the list.
print(people.count("Charles"))

image.png


Task 13 -

people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32,16,41,30,47,5,11,30]

Perform the following functions to the lists:
I. Print out the Sort list for people in ascending order.
II. Print out the Sort list for ages in ascending order.
III. Print out the Sort list for people in descending order.
IV. Print out the Sort list for ages in descending order.
V. Print out the Reversed list for people.
VI. Print out the Reversed list for ages.

>>Return to Menu


# Task 13 -
# people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
# ages = [32, 16, 41, 30, 47, 5, 11, 30]
# Perform the following functions to the lists:
# I. Print out the Sort list for people in ascending order.
# II. Print out the Sort list for ages in ascending order.
# III. Print out the Sort list for people in descending order.
# IV. Print out the Sort list for ages in descending order.
# V. Print out the Reversed list for people.
# VI. Print out the Reversed list for ages.

#Print out the Sort list for people in ascending order.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
people.sort()
print(people)
#Print out the Sort list for ages in ascending order.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
ages.sort()
print(ages)
#Print out the Sort list for people in descending order.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
people.sort(reverse=True)
print(people)
#Print out the Sort list for ages in descending order.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
ages.sort(reverse=True)
print(ages)
#Print out the Reversed list for people.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
people.reverse()
print(people)
#Print out the Reversed list for ages.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
ages.reverse()
print(ages)

image.png


Task 14 -

people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32,16,41,30,47,5,11,30]

Perform the following functions to the lists:
I. Print out the lowest value in the ages list.
II. Print out the highest value in the ages list.
III. Print out the sum of values in the ages list.
IV. Add the values 'Bob' and 80 respectively to the people and ages list.
V. Add the values "Charles" and 25 respectively to the lists both at index position 2.
VI. Remove the values "Ashley" and "Femi" from the people list.
VII. Remove the value at index position 2 from the ages list.

>>Return to Menu


# Task 14 -
# people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
# ages = [32, 16, 41, 30, 47, 5, 11, 30]
# Perform the following functions to the lists:
# I. Print out the lowest value in the ages list.
# II. Print out the highest value in the ages list.
# III. Print out the sum of values in the ages list.
# IV. Add the values 'Bob' and 80 respectively to the people and ages list.
# V. Add the values "Charles" and 25 respectively to the lists both at index position 2.
# VI. Remove the values "Ashley" and "Femi" from the people list.
# VII. Remove the value at index position 2 from the ages list.

#Print out the lowest value in the ages list.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
print(min(ages))
#Print out the highest value in the ages list.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
print(max(ages))
#Print out the sum of values in the ages list.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
print(sum(ages))
#-Add the values 'Bob' and 80 respectively to the people and ages list.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
ages = [32, 16, 41, 30, 47, 5, 11, 30]
people.append("Bob")
ages.append(80)
print(people)
print(ages)
#-Add the values "Charles" and 25 respectively to the lists both at index position 2.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
ages = [32, 16, 41, 30, 47, 5, 11, 30]
people.insert(2, "Charles")
ages.insert(2, 25)
print(people)
print(ages)
#Remove the values "Ashley" and "Femi" from the people list.
people = ["Adam", "Ashley", "Mike", "Femi", "Adam", "Chloe", "Mike", "Mary"]
people.remove("Ashley")
people.remove("Femi")
print(people)
#Remove the value at index position 2 from the ages list.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
ages.pop(2)
print(ages)

image.png image.png


Task 15 -

people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32,16,41,30,47,5,11,30]

Perform the following functions to the lists:
I. Remove all the values once from the ages and people list.
II. Ensure that the people and ages lists are removed and not found in memory.
III. Change the value "Ashley" to "Tom" in the people list.
IV. Change the value 47 to 20 in the ages list.
V. Put all the values in ages list into people list.
VI. Print the 3 ways to copy the people list to a client list.
VII. Create a string from the people list to look like this:
Adam-Ashley-Mike-Femi-Adam-Chloe-Mike-Mary
VIII. Create a list from the string:
msg = "Honda, Toyota, Mazda, Mecedes, Tata"
IX. Replace 'Javascript' with 'Python' in the string:
msg ='Welcome to Javascript class'

>>Return to Menu


# Task 15 -
# people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
# ages = [32,16,41,30,47,5,11,30]
# Perform the following functions to the lists:
# I. Remove all the values once from the ages and people list.
# II. Ensure that the people and ages lists are removed and not found in memory.
# III. Change the value "Ashley" to "Tom" in the people list.
# IV. Change the value 47 to 20 in the ages list.
# V. Put all the values in ages list into people list.
# VI. Print the 3 ways to copy the people list to a client list.
# VII. Create a string from the people list to look like this:
# Adam-Ashley-Mike-Femi-Adam-Chloe-Mike-Mary
# VIII. Create a list from the string:
# msg = "Honda, Toyota, Mazda, Mecedes, Tata"
# IX. Replace 'Javascript' with 'Python' in the string:
# msg ='Welcome to Javascript class'

#Remove all the values once from the ages and people list.
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32,16,41,30,47,5,11,30]
people.clear()
ages.clear()
print(people)
print(ages)
#the people and ages lists are removed and not found in memory.
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32,16,41,30,47,5,11,30]
del people
del ages
#Change the value "Ashley" to "Tom" in the people list.
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
pos = people.index("Ashley")
people[pos] = "Tom"
print(people)
#Change the value 47 to 20 in the ages list.
ages = [32, 16, 41, 30, 47, 5, 11, 30]
pos = ages.index(47)
ages[pos] = 20
print(ages)
#Put all the values in ages list into people list.
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
ages = [32, 16, 41, 30, 47, 5, 11, 30]
people.extend(ages)
print(people)
#Print the 3 ways to copy the people list to a client list.
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
client = people[:]
print(people)
client = people.copy()
print(people)
client = list(people)
print(people)
#Create a string from the people list to look like this:
# Adam-Ashley-Mike-Femi-Adam-Chloe-Mike-Mary
people = ["Adam","Ashley","Mike","Femi","Adam","Chloe","Mike","Mary"]
people = "-".join(people)
print(people)
#Create a list from the string:
msg = "Honda,Toyota,Mazda,Mecedes,Tata"
msg = msg.split(",")
print(msg)
#Replace 'Javascript' with 'Python' in the string:
msg ="Welcome to Javascript class"
msg = msg.replace("Javascript", "Python")
print(msg)

image.png

image.png


Task 16 -

sales_w1 = [7,3,42,19,15,35,9]
sales_w2 = [12,4,26,10,7,28]

You sell lemonade in 2 weeks. The lists show number of lemonades sold per week. The profit for each lemonade sold is $1.5.
I. Add another day (7th day) to week 2 list (sales_w2) by capturing a number as input for how many lemonade were sold on that day.
II. Combine the two lists into a list called "sales".
III. Calculate and print how much you have earned on the best day, worst day, and in total on both best and worst days.

>>Return to Menu


# Task 16 -
# sales_w1 = [7,3,42,19,15,35,9]
# sales_w2 = [12,4,26,10,7,28]
# You sell lemonade in 2 weeks. The lists show number of lemonades sold per week. The profit for each lemonade sold is $1.5.
# I. Add another day (7th day) to week 2 list (sales_w2) by capturing a number as input for how many lemonade were sold on that day.
# II. Combine the two lists into a list called "sales".
# III. Calculate and print how much you have earned on the best day, worst day, and in total on both best and worst days.

sales_w1 = [7,3,42,19,15,35,9]
sales_w2 = [12,4,26,10,7,28]

sold_w2_last = int(input("Number of lemonades sold on last day: "))
sales_w2.append(sold_w2_last)

sales = list(sales_w1)
sales.extend(sales_w2)

best_day = max(sales) * 1.5
worst_day = min(sales) * 1.5
total = "{:.2f}".format(best_day + worst_day)
print(f"You earned ${best_day} on the best day, ${worst_day} on the worst day, and in total ${total}.")

image.png


Task 17 -

csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = []

From the list "csv", fill a list "friends_list" properly with the names of all the friends, one per slot.
>>Return to Menu


Method 1:

# Task 17 -
# csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
# friends_list = []
# From the list "csv", fill a list "friends_list" properly with the names of all the friends, one per slot.

csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = []
csv = csv.replace(":",",")
print(csv)
csv = csv.replace(";",",")
print(csv)
csv = csv.split(",")
print(csv)

image.png

Method 2:

# Task 17 -
# csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
# friends_list = []
# From the list "csv", fill a list "friends_list" properly with the names of all the friends, one per slot.

csv = 'Eric,John,Michael,Terry,Graham:TerryG;Brian'
friends_list = []
# join, split
csv = csv.split(":")
print(csv)
csv = ",".join(csv)
print(csv)
csv = csv.split(";")
print(csv)
csv = ",".join(csv)
print(csv)
csv = csv.split(";")
print(csv)
friends_list = list(csv)
print(friends_list)

image.png


Task 18 -

friends_set = {'John','Michael','Terry','Eric','Graham'}
people_set = {'Eric','Reg','Loretta','Colin','Graham'}

From the sets, perform the following:
I. Find out items that are found in both sets.
II. Find out items that are found in first but not second set.
III. Find out items that are only found in one set.
IV. Join the two sets together.
>>Return to Menu


# Task 18 -
# friends_set = {'John','Michael','Terry','Eric','Graham'}
# people_set = {'Eric','Reg','Loretta','Colin','Graham'}
# From the sets, perform the following:
# I. Find out items that are found in both sets.
# II. Find out items that are found in first but not second set.
# III. Find out items that are only found in one set.
# IV. Join the two sets together.

friends_set = {'John','Michael','Terry','Eric','Graham'}
people_set = {'Eric','Reg','Loretta','Colin','Graham'}

#Find out items that are found in both sets.
print(friends_set.intersection(people_set))
print(friends_set & people_set)
#Find out items that are found in first but not second set.
print(friends_set.difference(people_set))
print(friends_set - people_set)
#Find out items that are only found in one set.
print(friends_set.symmetric_difference(people_set))
print(friends_set ^ people_set)
#Join the two sets together.
print(friends_set.union(people_set))
print(friends_set | people_set)

image.png


Task 19 -
Write a function that does the following:
I. Receives 2 arguments with parameters "name" and "age=28".
II. It takes input values for the arguments.
III. It should print out the following:
Hello (name), you are (age) years old!
IV. Test with (Mark, 32) and (Mary).
>>Return to Menu


# Task 19 -
# Write a function that does the following:
# I. Receives 2 arguments with parameters "name" and "age=28".
# II. It takes input values for the arguments.
# III. It should print out the following:
# Hello (name), you are (age) years old!
# IV. Test with (Mark, 32) and (Mary).

def age_declaration(name, age=28):
    return f"Hello {name}, you are {int(age)} years old!"

print(age_declaration("Mark",32))
print(age_declaration("Mary"))

image.png


Task 20 -
Use the import system module to show the Python system version and executable file path.
>>Return to Menu


# Task 20 -
# Use the import system module to show the Python system version and executable file path.

import sys

print(sys.version)
print(sys.executable)

image.png

#End


Hope you enjoyed this! :) Follow me for more contents...


Get in Touch:
ifeanyiomeata.com

Youtube: youtube.com/c/IfeanyiOmeata
Linkedin: linkedin.com/in/omeatai
Twitter: twitter.com/iomeata
Github: github.com/omeatai
Stackoverflow: stackoverflow.com/users/2689166/omeatai
Hashnode: hashnode.com/@omeatai
Medium: medium.com/@omeatai
© 2021**