LAB #1 - 2 - Solved
LAB #1 - 2 - Solved
1. Write a program that declares and adds two integer variables (2, 10), then print out the result on the screen.
Input: 2 and 10
Expected output:
# Solution
a = 2
b = 10
result = a + b
print("The result is: "+str(result))
2. Write a program that asks the user to input an integer value, then print out the input value multiplied by 2.
# Solution
value = int(input("Please, enter a number..."))
result = value * 2
print("The result is: "+str(result))
3. Write a program that asks your name and prints out "Hello " #name#
Input: Paul
Expected output:
Hello: Paul
# Solution
name = input("What is your name? ")
print ("Hello: ", name, "How are you?")
9. Write a program that asks the year of birth and prints out your age.
Input: 1983
Expected output:
# Solution
year_birth = int(input("Introduce the year of birth..."))
age = 2023-year_birth
print ("You are "+str(age)+" years old.")
10. Write a program that asks the temperature in Kelvin degress and prints out the temperature in Celsius.
Input: 300
Expected output:
The temperature in Celsius is: 26.850000000000023
# Solution
t_kelvin = float(input("Introduce the temperature in Kelvin..."))
t_celsius = t_kelvin-273.15
print ("The temperature in Celsius is: "+str(t_celsius))
11. Write a program that asks a number and prints True if the number is odd, and False otherwise.
Input: 2020
Expected output:
Input: 2019
Expected output:
# Solution
number = int(input("Introduce an integer number: "))
is_even = (number % 2 != 0)
print("The number "+str(number)+" is odd: "+str(is_even))
12. Write a program that asks for a year and prints True if the year is a leap year, and False otherwise. In the Gregorian
calendar, a leap year follows these rules:
If the year can be evenly divided by 100, it is NOT a leap year, unless;
The year is also evenly divisible by 400. Then it is a leap year.
Input: 2020
Expected output:
Input: 2019
Expected output:
# if not divided by both 400 (century year) and 4 (not century year)
# year is not leap year
else:
print(year, "is not a leap year")
13. Write a program calculates the area of a triangle.
Input: b = 2, h = 3
Expected output:
# Solution
b = 2
h = 3
area = (b*h)/2
14. Write a program to calculate the Euclidean distance between two points.
Syntax: math.sqrt(x)
Parameter:
x is any number such that x>=0
Returns:
It returns the square root of the number
passed in the parameter.
2.8284271247461903
# Solution
import math
x1 = 2.0
y1 = 3.0
x2 = 4.0
y2 = 5.0
distance = math.sqrt((x1-x2)**2 + (y1-y2)**2)
print(distance)
LAB #2 - Solutions
1. Write a program that declares and multiplies two integer variables (4, 15), then print out the result on the screen.
Input: 4 and 15
Expected output:
# Solution
a = 4
b = 15
result = a * b
print("The result is: "+str(result))
Input:
a = 2
b = 3
c = a
char = 'a'
string_value = "hello"
bool_value = True
float_value = 2.3
Expected output:
<class 'int'>
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>
<class 'bool'>
<class 'float'>
# Solution
a = 2
b = 3
c = a
char = 'a'
string_value = "hello"
bool_value = (c==a)
float_value = 2.3
print(a, type(a))
print(type(b))
print(type(c))
print(type(char))
print(type(string_value))
print(type(bool_value))
print(type(float_value))
<class 'int'>
<class 'int'>
<class 'int'>
<class 'str'>
<class 'str'>
<class 'bool'>
<class 'float'>
3. Which is the value of a ?
a = 2*3+1
print(a)
a = (2*3)+1
print(a)
a = 2* (3+1)
print (a)
a = 2*3**2+1
print(a)
a = 2*(3**2)+1
print (a)
a = (2*3)**2+1
print(a)
7
7
8
19
19
37
4. Write a program that asks the temperature in Farenheit and prints out the temperature in Celsius.
Input: 100
Expected output:
# Solution
t = float(input("Temperature: "))
t_celsius = ((t - 32)*5)/9;
print("Temperature in Celsius is " + str(t_celsius));
Input: a = 2, b = 3
Expected output:
a = 3 b = 2
# Solution
a = 2
b = 3
print("a = "+str(a)+" b = "+str(b))
c = a
a = b
b = c
print("a = "+str(a)+" b = "+str(b))
a = 2 b = 3
a = 3 b = 2
# Solution
t = 10.0
v = 5.0
ts = 13.12 + 0.6215*t -(11.37*(v** 0.16))+(0.3965*t*(v**0.16))
print("Temperature = " + str(t))
print("Velocity = " + str(v))
print("Thermal sensation = " + str(ts))
7. Write a program to calculate the values r and theta required to transform cartesian coordinates to polar.
Input: x = 2, y = 3
Expected output:
r = 3.605551275463989
theta = 0.982793723247329
# Solution
import math
x = 2.0
y = 3.0
r = math.sqrt(x*x + y*y)
theta = math.atan2(y, x)
print("r = " + str(r))
print("theta = " + str(theta))
8. Write a program that calculates the roots following the quadratic formula:
Expected output:
# Solution
import math
a = 4
b = 5
c = 1
bac=b**2-4*a*c
#This will not work if the roots are not real
root_positive = (-b + math.sqrt(bac))/(2*a)
root_negative = (-b - math.sqrt(bac))/(2*a)
print("The roots are:")
print (str(root_positive))
print (str(root_negative))
1/3
0.3333333333333333
1//3
5=="5"
False
11. Write a program to test the augmented assignment. What is the result of the following program?
a = 2
a += 3
print(a)
a *= 2
print(a)
5
10
12. Write a program to calculate the area of a circle of radius r . Use the constant math.pi
Input: r = 3
Expected output:
28.274333882308138
# Solution
import math
r = 3
area = math.pi * r**2
print(area)
13. Write a program that asks the user for an integer numbers and prints out a message indicating whether the input is greater
than 10.
Input: 15
Expected output:
# Solution
a = int(input("-->"))
if a > 10:
print("The input {} is greater than 10.".format(a))
14. Write a program that asks the user for two integer numbers and prints out the greater value.
Input: 4 and 15
Expected output:
15. Write a program that asks the user for an integer number and prints the absolute value of the input value. Do not use the
built-in function abs() .
Input: -3
Expected output:
# Solution
value = int(input("Introduce an integer number: "))
abs_value = value
if value < 0 :
abs_value = value * -1
print("The absolute value of {} is {}.".format(value, abs_value))
16. Write a program that asks the user for an integer number, and prints out whether or not it is an even number.
Input: 4
Expected output:
4 is an even number.
# Solution
value = int(input("Introduce a value: "))
if value % 2 == 0:
print("{} is an even number.".format(value))
17. Write a program that asks the user for a day number an prints the day name.
Monday-Tuesday-etc.
# Solution
day = int(input("Introduce the day number: "))
name = "No day"
if day == 1:
name = "Monday"
elif day == 2:
name = "Tuesday"
elif day == 3:
name = "Wednesday"
elif day == 4:
name = "Thursday"
elif day == 5:
name = "Friday"
elif day == 6:
name = "Saturday"
elif day == 7:
name = "Sunday"
print(name)
Introduce the day number: -5
No day
20. Write a Program to extract each digit from an 3-digits integer in the reverse order :
Input: 753
Expected output:
3
5
7
19. Write a program to generate a diagram showing the facesof up to six dice (in 5 rows x 7 cols.). (Note: Use \n to print a
new line):
Select a number:3
-------
| * |
| * |
| * |
-------
Select a number:4
-------
| * * |
| |
| * * |
-------
n = int(input('Select a number:'))-1
s = dice[n]
d = 7*'-'+'\n'
d += '| ' + s[0] + ' |\n'
d += '| ' + s[1] + ' |\n'
d += '| ' + s[2] + ' |\n'
d += 7*'-'+'\n'
print(d)
20. Write a program that asks for a year and prints True if the year is a leap year, and False otherwise. In the Gregorian
calendar, a leap year follows these rules. Important: Do not use if and else keywords.
The year can be evenly divided by 4;
If the year can be evenly divided by 100, it is NOT a leap year, unless;
The year is also evenly divisible by 400. Then it is a leap year.
Input: 2020
Expected output:
The year 2020 is leap: True
Input: 2019
Expected output: