课堂笔记:Data Processing and Visulisation with Python (Python Exercise 4)

Data Processing and Visulisation with Python

Absolute value

Write a Python program to input a float (negative or positive) from user, and output its absolute value.

Note: please do not use absolute functions from other modules. Write your own version.

n=float(input("Please input a float:"))
if n>0:
    print(f"The absolute value of float {n} is: {n}")
else:
    print(f"The absolute value of float {n} is: {-n}")

Better method

n=float(input("Please input a float:"))
print(f"The absolute value of float {n} is: {n if n>=0 else -n}")

在这里插入图片描述

Convert between o ^o oC and o ^o oF

Write a Python program to convert temperature between Celsius and Fehrenheit.

Hint:

  • To extract the last character from string s, you can use s[-1].
  • To extract the characters except the last from string s, you can use s[:-1]
n=input("Please input temperature degrees (ending with C for Celsius and F for Fehrenheit):")
a=n[-1]
b=float(n[:-1])
if a == "F":
    print(f"{b} degrees in Fehrenheit is {(b-32)*5/9} degrees in Celsius.")
else:
     print(f"{b} degrees in Celsius is {(b*9/5+32)} degrees in Fehrenheit.")

在这里插入图片描述

Dog’s years

Write a Python program to calculate a dog’s age in dog’s years.

Note: For the first two years, a dog year is equal to 10.5 human years. After that, each dog year equals 4 human years.

dog_year=float(input("Please input a dog's age in human years:"))
if dog_year<=2:
    print(f"The dog's age in dog's years is {(dog_year*10.5)}")
else:
    print(f"The dog's age in dog's years is {(dog_year-2)*4+10.5*2}")

在这里插入图片描述

Median of three

Write a Python program to find the median of three values.

a=float(input("Input first number:"))
b=float(input("Input second number:"))
c=float(input("Input third number:"))
if a>b>c or c>b>a:
    print(f"The median is {b}")
elif a>c>b or b>c>a:
    print(f"The median is {c}")
else:
    print(f"The median is {a}")
    

在这里插入图片描述

Next second

Write a Python program to input hour, minute and second from user, then print out the time of next second.

h=int(input("Please input hour (0~23):"))
m=int(input("Please input minute (0~59):"))
s=int(input("Please input second (0~59): "))
print(f"Input time is: {h}:{m}:{s}")

if s == 59:
    a=0
    if m == 59:
        b=0
        if h == 23:
            c=0
        else:
            c=h+1
    else:
        b=m+1
            
else:
    a=s+1
    

print(f"The time of next second is:{a}:{b}:{b}")

在这里插入图片描述

Positive, negative or zero

Write a Python program to check if an input number is positive, negative or zero!

n=float(input("Please input a number:"))
if n<0:
    print(f"{n} is negative")
elif n==0:
    print(f"{n} is zero")
else:
    print(f"{n} is positive")

在这里插入图片描述

Around 888

Write a Python program to test if an input number is within 88 of 888.

n = float(input("Please input a number:"))
if 800<=n<=976:
    print(f"{n} is within 88 of 888")
else:
     print(f"{n} is not within 88 of 888")

在这里插入图片描述

Even or odd (new version)

Write a Python program to input an integer, then output if the integer is odd or even.

n=int(input("Please input an integer:"))
if n%2 ==0:
      print(f"{n} is an even integer.")
else:
      print(f"{n} is an odd integer.")

在这里插入图片描述

©️2020 CSDN 皮肤主题: 游动-白 设计师:上身试试 返回首页