Data Processing and Visulisation with Python
Python Exercise 3
import datetime
print(datetime.datetime.now())
Height and Length
Write a Python program to convert height or length (in feet and inches) to centimeters
1 ft = 12 in
1 in = 2.54 cm
Feet=float(input("Feet:"))
Inches=float(input("Inches:"))
print("Your height is :",(Feet*12+Inches)*2.54,"cm.")
Hypotenuse
Write a Python program to calculate the hypotenuse of a right angled triangle
HInt: you can use sqrt from math module.
from math import *
print("Input lengths of shorter triangle sides:")
a=float(input("a:"))
b=float(input("b:"))
print("The length of the hypotenuse is",sqrt(a**2+b**2))
BMI
Write a Python program to calculate body mass index
BMI = w e i g h t h e i g h t 2 \frac{weight}{height^2} height2weight
height=float(input("Input your height in meters: "))
weight=float(input("Input your weight in meters: "))
print("Your body mass index is: ",weight/(height)**2)
height=float(input("Input your height in meters: "))
weight=float(input("Input your weight in meters: "))
print("Your body mass index is: ",round(weight/(height)**2,2))
###注:函数round()
Swapping two variables
Write a Python program to swap two variables
Please write your code in Your Code
section. Please don’t change any code outside
x = 3
y = 7
print(f'before swapping: x = {x}, y = {y}')
#------------------Your Code begins here------------------------
#--------------------End of Your Code---------------------------
print(f'after swapping: x = {x}, y = {y}')
x = 3
y = 7
print(f'before swapping: x = {x}, y = {y}')
a=x
b=y
x=b
y=a
print(f'after swapping: x = {x}, y = {y}')
Better method
x = 3
y = 7
print(f'before swapping: x = {x}, y = {y}')
x,y=y,x
print(f'after swapping: x = {x}, y = {y}')
Last digit
Write a Python program to display the last digit of the input integer.
n=int(input("Please input an integer:"))
print("The last digit of 54667 is",n%10)
Second last digit
Write a Python program to display the second last digit of the input integer.
n=int(input("Please input an integer:"))
print(f"The second last digit of {n} is",int(n/10)%10)
Better method
n=int(input("Please input an integer:"))
print(f"The second last digit of {n} is {n//10%10}")
nth digit from right
Write a Python program to display the nth digit from right of the input integer.
a=int(input("Please input an integer: "))
b=int(input("Please input n:"))
print(f"The {b}th digit from right of integer {a} is ",int(a/10**(b-1)%10))
Better method
a=int(input("Please input an integer: "))
b=int(input("Please input n:"))
print(f"The {b}th digit from right of integer {a} is {(a//10**(b-1)%10)}")
nth digit from right of binary form
Write a Python program to input a positive integer k, and check what is the nth digit from right of k in binary form.
a=int(input("Please input a positive integer:"))
b=int(input("Please input n:"))
print(f"The binary form of integer {a} is",bin(a),f". Its {b}th digit from right is ",int(a/2**(b-1))%2)
Better method
a=int(input("Please input a positive integer:"))
b=int(input("Please input n:"))
print(f"The binary form of integer {a} is {bin(a)}. Its {b}th digit from right is {a>>(b-1)&1}")