[go: up one dir, main page]

0% found this document useful (0 votes)
14 views10 pages

LAB #1 - 2 - Solved

Ejercicios Lab1 resueltos
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)
14 views10 pages

LAB #1 - 2 - Solved

Ejercicios Lab1 resueltos
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
You are on page 1/ 10

LAB #1 - Solutions

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:

The result is: 12

# 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.

Input: an integer number, 4


Expected output:

The result is: 8

# 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:

You are 39 years old.

# 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:

The number 2020 is odd: False

Input: 2019

Expected output:

The number 2019 is odd: True

# 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:

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:

The year 2019 is leap: False

# To get year (integer input) from the user


year = int(input("Enter a year: "))

# divided by 100 means century year (ending with 00)


# century year divided by 400 is leap year
if (year % 400 == 0) and (year % 100 == 0):
print(year, "is a leap year")

# not divided by 100 means not a century year


# year divided by 4 is a leap year
elif (year % 4 ==0) and (year % 100 != 0):
print(year, "is a leap year")

# 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:

The area is: 3.0

# Solution
b = 2
h = 3
area = (b*h)/2

print("The area is: "+str(area))

14. Write a program to calculate the Euclidean distance between two points.

Use the function math.sqrt(x) to calculate the root square of a number.

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.

Input: (2.0, 3.0) (4.0, 5.0)


Expected output:

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:

The result is: 60

# Solution
a = 4
b = 15
result = a * b
print("The result is: "+str(result))

2. Check the type of different variables using the function type() .

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:

Temperature in Celsius is 37.77777777777778

# Solution
t = float(input("Temperature: "))
t_celsius = ((t - 32)*5)/9;
print("Temperature in Celsius is " + str(t_celsius));

5. Write a program to swap the values of two variables.

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

6. Write a program to calculate the thermical sensation.

Input: t = 10.0, v = 5.0


Expected output:
Temperature = 10.0
Velocity = 5.0
Thermal sensation = 9.755115709161835

# 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:

Use the function math.sqrt(x)


Input: a = 4, b = 5, c = 1

Expected output:

The roots are:


-0.25
-1.0

# 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))

9. Write and check the difference between 1/3 and 1//3?

1/3
0.3333333333333333

1//3

10. Write and check the result of comparing 5 == "5".

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:

The input 15 is greater than 10.

# 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:

The result is: 15


# Solution
a = int(input("-->"))
b = int(input("-->"))
if a > b:
print("a is greater..."+str(a))
else:
print("b is greater..."+str(b))

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:

The absolute value of -3 is 3.

# 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.

Input: between 1-7


Expected output:

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):

Input: Select a number:


Expected output:

Select a number:3
-------
| * |
| * |
| * |
-------

Select a number:4
-------
| * * |
| |
| * * |
-------

dice = [[' ',' * ', ' '],


['* ',' ', ' *'],
['* ',' * ', ' *'],
['* *',' ', '* *'],
['* *',' * ', '* *'],
['* *','* *', '* *']]

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:

The year 2019 is leap: False

year = int(input("Introduce a year: "))


is_leap_year = (year % 4 == 0)
is_leap_year = is_leap_year and (year % 100 != 0)
is_leap_year = is_leap_year or (year % 400 == 0)
print("The year "+str(year)+" is leap: "+str(is_leap_year))

You might also like