In this tutorial, you’ll learn about the different kinds of data you can work with in your Python programs. You’ll also learn how to use variables to represent data in your programs.

It is possible to write Python code straight into the shell, but as soon as you hit [Return] at the end of a line, it will run that line of code. This may be suitable for using Python as a quick calculator. However, this style of inputting is not useful for more complex programs.

It is much better to start a new window, create all the code in the new window, save your code and run it. To create a new window in which to write your code, select File and New . Once you enter your code in this new window you can save it and run it all in one go. This will then run the code in the shell window.

EXO I.1

Question: Ask for the user’s first name and display the message: Hello [First Name].

Answer :  

firstname = input("Your first name is ? ")
print("Hello", firstname)

EXO I.2

Question: Ask for the user’s first name and then ask for their surname and display the message: Hello [First Name] [Surname].

Answer :  

firstname = input("Your first name is ?")
surname = input("Your surname is ?")
print("Hello", firstname, surname)

EXO I.3

Question: Write a program that asks the user to type the width and length of a field and displays its perimeter and area.

Answer :  

width = int(input("Enter the width of the filed :"))
lenght = int(input("Enter the lenght of the field :"))

area = width*lenght
perimeter = 2 * (width + lenght)

print("The area is : ", area)
print("The perimeter is : ", perimeter)

EXO I.4

Question: Write a program that asks the user to type five integers and displays their average.

Answer :  

total = 0
total = total + int(input("Enter the value 1 : "))
total = total + int(input("Enter the value 2 : "))
total = total + int(input("Enter the value 3 : "))
total = total + int(input("Enter the value 4 : "))
total = total + int(input("Enter the value 5 : "))

average = total/5.0

print("The average is : ", average)

EXO I.5

Question: Ask the user to enter three numbers. Add the first two numbers, then multiply that total by the third and display the result.

Answer :  

number1 = int(input("Enter the first number: "))
number2 = int(input("Enter the second number: "))
number3 = int(input("Enter the third number: "))

total = (number1 + number2)*number3

print("The total is : ", total)

EXO I.6

Question: Write a program that asks the user to type in the price of one kilo of tomatoes, the number of kilos of tomatoes purchased, the VAT rate (Example 10%, 20%, ...). The program then displays the price of the goods including VAT.

Answer :  

price = int(input("Enter the price excluding VAT of a kilo of tomatoes : "))
weight = int(input("How many kilos did you buy : "))
vat = int(input("What is the VAT rate : "))

total = (1 + vat/100)*price*weight

print("The price including VAT is : ",total)

EXO I.7

Question: Ask for the total price of the bill, then ask how many diners there are. Divide the total bill by the number of diners and show how much each person must pay.

Answer :  

bill = int(input("Enter the total cost of the bill :"))
people = int(input("Enter the number of people who are invited :"))

each = bill/people

print(f'Each person should pay {each}$')

EXO I.8

Question: Write a program that will ask for a number of days and then will show how many hours, minutes and seconds are in that number of days.

Answer :  

days = int(input("Enter the number of days :"))

hours = days*24
minutes = hours*60
seconds = minutes*60

print(f'In {days} days there are :')
print(hours, "h")
print(minutes, "m")
print(seconds, "s")

 

0 comment

There are no comments yet.

Log in to leave a reply

Related posts

Developing a Web Application with Django Part 1: Getting Started

1/12/2021

Developing a Web Application with Django Part 2: Templates

15/12/2021

Developing a Web Application with Django Part 3 : Models

7/1/2022