Variables and Assignments:Data Processing and Visulisation with Python (Python Exercise 3)

Data Processing and Visulisation with Python

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}")

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值