0% found this document useful (0 votes)
6 views7 pages

Simple Data Types Programming Exercises

The document contains a series of programming exercises focused on simple data types and string manipulation in Python. Each exercise provides a specific task, such as displaying messages, performing arithmetic operations, and handling user input. The exercises are designed to help users practice and understand basic programming concepts.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views7 pages

Simple Data Types Programming Exercises

The document contains a series of programming exercises focused on simple data types and string manipulation in Python. Each exercise provides a specific task, such as displaying messages, performing arithmetic operations, and handling user input. The exercises are designed to help users practice and understand basic programming concepts.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SIMPLE DATA TYPES

Exercise 1
Write a program that displays the string Hello World! on the screen.
print("Hello World!")

Exercise 2
Write a program that stores the string Hello World! in a variable and then displays the content of the variable on the screen.
Hello World!
print(mensaje)

Exercise 3
Write a program that asks the user's name in the console and after the user enters it, displays the string on screen.
Hello <name>!
nombre =input("Introduce tu nombre: ")
print("Hello " + name + "!")

Exercise 4

Write a program that displays the result of the following arithmetic operation on the screen .
print(((3+2) / (2*5)) ** 2)

Exercise 5
Write a program that asks the user for the number of hours worked and the cost per hour. Then it should display the pay on the screen.
what is due to him/her.
hours = float(input("Enter your working hours: "))
cost = float(input("Enter your hourly rate: "))
paga = horas * coste
Your pay is

Exercise 6
Write a program that reads a positive integer, n, entered by the user and then displays on screen the sum of all integers from
1 to n. The sum of the first n positive integers can be calculated as follows:

n = int(input("Enter an integer: "))


sum = n * (n + 1) / 2
print("The sum of the first integers from 1 to " + str(n) + " is " + str(suma))

Exercise 7
Write a program that asks the user for their weight (in kg) and height (in meters), calculates the body mass index, and stores it in a variable.
and display on the screen the phrase Your body mass index is <bmi> where <bmi> is the calculated body mass index rounded to two
decimals.
peso =input("¿Cuál es tu peso en kg? ")
estatura =input("¿Cuál es tu estatura en metros?")
bmi = round(float(weight) / float(height) ** 2, 2)
Your body mass index is

Exercise 8
Write a program that asks the user for two integers and displays on the screen the <n> divided by <m> gives a quotient <c> and a remainder <r> where
<n> and <m> are the numbers entered by the user, and <c> and <r> are the quotient and the remainder of the integer division respectively.
n = input("Enter the dividend (integer): ")
m = input("Introduce the divisor (integer): ")
print(n + ' between ' + m + ' gives a quotient ' + str(int(n) // int(m)) + ' and a remainder ' + str(int(
n) % int(m)))

Exercise 9
Write a program that asks the user for an amount to invest, the annual interest rate, and the number of years, and displays the capital on the screen.
obtained in the investment.
cantidad = float(input("¿Cantidad a invertir? "))
interes = float(input("¿Interés porcentual anual? "))
years = int(input('Years?'))
print("Final capital: " + str(round(amount * (interest / 100 + 1) ** years, 2)))

Exercise 10
A toy store is very successful with two of its products: clowns and dolls. It usually does mail-order sales and the logistics company charges them.
by the weight of each package so they must calculate the weight of the clowns and dolls that will go out in each package on demand. Each clown weighs
112 g for each clown and 75 g for each doll. Write a program that reads the number of clowns and dolls sold in the last order and calculates the total weight.
package that will be sent.
peso_payaso =112
peso_muñeca =75
clowns = int(input("Enter the number of clowns to send: "))
dolls = int(input("Enter the number of dolls to send: "))
peso_total = peso_payaso * payasos + peso_muñeca * muñecas
The total weight of the package is

Exercise 11
Imagine that you have just opened a new savings account that offers you 4% interest per year. These savings due to interest, which do not
They charge until the end of the year, and they are added to the final balance of your savings account. Write a program that starts by reading the amount of
money deposited in the savings account, entered by the user. Then the program must calculate and display on the screen the amount of
savings after the first, second, and third years. Round each amount to two decimal places.
inversion = float(input("Introduce la inversión inicial: "))
interes =0.04
balance1 = inversion * (1 + interest)
print("Balance after the first year:" + str(round(balance1,2)) )
balance2 = balance1 * (1 + interest)
print("Balance after the second year:" + str(round(balance2, 2)))
balance3 = balance2 * (1 + interest)
Balance after the third year:

Exercise 12
A bakery sells loaves of bread at €3.49 each. Bread that is not sold on the day has a 60% discount. Write a program that starts
reading the number of loaves sold that are not from the day. Then the program must show the usual price of a loaf of bread, the discount.
what is done to her for not being fresh and the final total cost.
bars = int(input("Enter the number of sold bars that are not fresh: "))
precio =3.49
descuento =0.6
coste = barras * precio * (1- descuento)
The cost of a fresh loaf is
The discount on a non-fresh bar is
The final cost to pay is

EXERCISES WITH CHAINS


Exercise 1
Write a program that asks the user's name in the console and an integer and prints the name on separate lines.
from the user as many times as the number entered.
nombre =input("¿Cómo te llamas? ")
n = input("Enter an integer: ")
print((name + "\n") * int(n))
Exercise 2
Write a program that asks for the user's full name in the console and then displays the user's full name on the screen.
three times, once with all lowercase letters, another with all uppercase letters and another with only the first letter of the first name and the last names
in uppercase. The user can enter their name combining uppercase and lowercase letters as they wish.
name =input("¿Cómo te llamas? ")
print([Link]())
print([Link]())
print([Link]())

Exercise 3
Write a program that asks for the user's name in the console and after the user inputs it, displays <NAME> on the screen.
it has <n> letters, where <NOMBRE> is the username in uppercase and <n> is the number of letters in the name.
name = input("What is your name? ")
print([Link]() + ' has ' + str(len(name)) + ' letters')

Exercise 4
The phones of a company have the following format prefix-number-extension where the prefix is the country code +34, and the extension
it has two digits (for example +34-913724710-56). Write a program that asks for a phone number in this format and shows
Display the phone number without the prefix and the extension.
tel =input("Introduce un número de teléfono con el formato +xx-xxxxxxxxx-xx: ")
print('The phone number is ', tel[4:-3])

Exercise 5
Write a program that asks the user to enter a sentence in the console and displays the reversed sentence on the screen.
frase =input("Introduce una frase: ")
print(frase[::-1])

Exercise 6
Write a program that asks the user to enter a phrase in the console and a vowel, and then displays the same phrase on the screen but
with the vowel introduced in uppercase.
frase =input("Introduce una frase: ")
vocal = input("Enter a lowercase vowel: ")
print([Link](vowel, [Link]()))

Exercise 7
Write a program that asks the user's email in the console and displays another email on the screen with the same one.
name (the part before the @ symbol) but with domain [Link].
email =input("Introduce tu correo electrónico: ")
print(email[:[Link]('@')] +'@[Link]')

Exercise 8
Write a program that asks for the price of a product in euros with two decimal places in the console and displays the number of euros on the screen.
and the number of cents of the entered price.
precio =input("Introduce el precio del producto con dos decimales: ")
print(price[:[Link]('.')], 'euros and', price[[Link]('.') + 1:], 'cents.')

Exercise 9
Write a program that asks the user for their birth date in the format dd/mm/yyyy and displays the day, month, and year on the screen.
Adapt the previous program so that it also works when the day or month is entered with a single character.
date = input("Enter your birth date in the format dd/mm/yyyy: ")
print('Day', date[:2])
print('Month', date[3:5])
print('Year', date[6:])

Adapt the previous program so that it also works when the day or month is entered with a single character.
fecha =input("Introduce la fecha de tu nacimiento en formato día/mes/año: ")
day = date[:[Link]('/')]
mesaño = fecha[[Link]('/')+1:]
month = table year[:table [Link]('/')]
year = month[[Link]('/')+1:]
print('Day', day)
print('Month', month)
print('Year', year)

Exercise 10
Write a program that asks in console for the products in a shopping basket, separated by commas, and displays each one on the screen.
one of the products in a different line.
basket = input('Enter the products in the shopping basket separated by commas: ')
print([Link](',', '\n'))

Solution 2
basket = input('Enter the products in the shopping basket separated by commas: ')
print('\n'.join([Link](',')))

Exercise 11
Write a program that asks for the name of a product, its price, and a number of units, and displays a string on the screen with the
nombre del producto seguido de su precio unitario con 6 dígitos enteros y 2 decimales, el número de unidades con tres dígitos y el coste total
with 8 integer digits and 2 decimals.
producto =input('Introduce el nombre del producto: ')
precio = float(input('Introducde el precio unitario: '))
units = int(input('Enter the number of units: '))
print('{product}: {units:3d} units x{price:9.2f}€={total:11.2f}€'.format(product = product, unit
es = unidades, precio = precio, total = unidades * precio))

You might also like