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

Data Processing and Visulisation with Python

Equilateral, isosceles or scalene

Write a Python program to check a triangle is equilateral, isosceles or scalene.

Note :

  • An equilateral triangle is a triangle in which all three sides are equal.
  • A scalene triangle is a triangle that has three unequal sides.
  • An isosceles triangle is a triangle with (at least) two equal sides.
print("Input lengths of the triangle sides:")
x=int(input("x:"))
y=int(input("y:"))
z=int(input("z:"))
if x ==y == z :
    print("equilateral triangle")
elif x != y and x != z and y != z:
    print("scalene triangle")
else :
    print("isosceles triangle")

在这里插入图片描述

Area of triangle (new version)

Write a Python program to input the lengths of three line segments (a, b and c). If they can form a triangle, then output the triangle’s area. Otherwise output “No triangle can be formed with these segments.”.

Hint:

area = s ( s − a ) ( s − b ) ( s − c ) \sqrt{s(s-a)(s-b)(s-c)} s(sa)(sb)(sc)
in which s = a + b + c 2 \frac{a+b+c}{2} 2a+b+c

from math import *
a = float(input("Please input length of segment a:"))
b = float(input("Please input length of segment b:"))
c = float(input("Please input length of segment c:"))
s = ( a + b + c)/2
if a + b > c or a + c > b or b + c > a :
    print(f"The area of triangle is: {sqrt(s*(s-a)*(s-b)*(s-c))}")
else :
    print("No triangle can be formed with these segments.")

在这里插入图片描述

Divisibility

Write a Python program to input two integers x and y, and output if x is divisible by y.

x = int(input("Please input integer x:"))
y = int(input("Please input integer y:"))
if 9%5 ==0:
    print(f"{x} is divisible by {y}")
else:
    print(f"{x} is not divisible by {y}")

在这里插入图片描述

Right triangle

Write a Python program to check whether three given lengths (integers) of three sides form a right triangle. Print “Yes” if the given sides form a right triangle otherwise print “No”.

a=int(input("Please input side a:"))
b=int(input("Please input side b:"))
c=int(input("Please input side c:"))
if a**2 + b**2 == c**2 or a**2 + c**2 == b**2 or b**2 + c**2 == a**2 :
    print("Yes")
else:
    print("No")

在这里插入图片描述

Contain, intersect or disjoint

There are two circles C1 with radius r1, central coordinate (x1, y1) and C2 with radius r2 and central coordinate (x2, y2)

Write a Python program to test the followings:

  • “C1 and C2 coincide" if C1 and C2 coincide
  • “C2 is in C1” if C2 is in C1
  • “C1 is in C2” if C1 is in C2
  • “Circumference of C1 and C2 intersect” if circumference of C1 and C2 intersect, and
  • “C1 and C2 do not overlap” if C1 and C2 do not overlap.
##C1
r1 = float(input("Radius r1 of C1:"))
x1 = float(input("Radius x1 of C1:"))
y1 = float(input("Radius y1 of C1:"))

##C2
r2 = float(input("Radius r2 of C2:"))
x2 = float(input("Radius x2 of C2:"))
y2 = float(input("Radius y2 of C2:"))

##计算圆心距离
from math import *
l = sqrt((x1-x2)**2 + (y1-y1)**2) 

if r1 + r2 < l:
    print("C1 and C2 do not overlap")
    
elif r1 + r2 == l :
    print("C1 and C2 coincide")
    
elif r1 - r2 > l:
    print("C2 is in C1")
    
elif r2 - r1 > l:
    print("C1 is in C2")
    
else:
    print("Circumference of C1 and C2 intersect")

在这里插入图片描述

Real roots of quatratic equation

Write a Python program to solve the quatratic equation:
a x 2 + b x + c = 0 ax^2+bx+c=0 ax2+bx+c=0

###Have problems
import sympy as sym
a=float(input("a="))
b=float(input("b="))
c=float(input("c="))
print(f"The quatratic equation {a}x^2 + {b}x + {c} = 0")

x=sym.Symbol('x')
sym.solveset(a*x**2 + b*x + c,x)
from math import *
a ,b ,c = float(input("a=")),float(input("b=")),float(input("c="))
print(f"The quatratic equation {a}x^2+{b}x+{c}=0")

if b**2-4*a*c > 0 :
    root1 = (-b+sqrt(b**2-4*a*c))/2/a 
    root2 = (-b-sqrt(b**2-4*a*c))/2/a 
    print("has two real root: ",root1,root2)
elif b**2-4*a*c == 0 :
    root0 = -b/2/a
    print("has one real root: ",root0)
else:
    print("has no real root")

在这里插入图片描述

Chinese Zodiac

Write a Python program to display the sign of the Chinese Zodiac for a given year.
在这里插入图片描述

Zodiac AnimalRecent Years
Rat1924, 1936, 1948, 1960, 1972, 1984, 1996, 2008, 2020
Ox1925, 1937, 1949, 1961, 1973, 1985, 1997, 2009, 2021
Tiger1926, 1938, 1950, 1962, 1974, 1986, 1998, 2010, 2022
Rabbit1927, 1939, 1951, 1963, 1975, 1987, 1999, 2011, 2023
Dragon1928, 1940, 1952, 1964, 1976, 1988, 2000, 2012, 2024
Snake1929, 1941, 1953, 1965, 1977, 1989, 2001, 2013, 2025
Horse1930, 1942, 1954, 1966, 1978, 1990, 2002, 2014, 2026
Goat1931, 1943, 1955, 1967, 1979, 1991, 2003, 2015, 2027
Monkey1932, 1944, 1956, 1968, 1980, 1992, 2004, 2016, 2028
Rooster1933, 1945, 1957, 1969, 1981, 1993, 2005, 2017, 2029
Dog1934, 1946, 1958, 1970, 1982, 1994, 2006, 2018, 2030
Pig1935, 1947, 1959, 1971, 1983, 1995, 2007, 2019, 2031
birth_year = int(input("Input your birth year:"))
if birth_year%12 == 4:
    print("Your Chinese Zodiac sign : Rat")
elif birth_year%12 == 5:
    print("Your Chinese Zodiac sign : Ox")
elif birth_year%12 == 6:
    print("Your Chinese Zodiac sign : Tiger")
elif birth_year%12 == 7:
    print("Your Chinese Zodiac sign : Rabbit")
elif birth_year%12 == 8:
    print("Your Chinese Zodiac sign : Dragon")
elif birth_year%12 == 9:
    print("Your Chinese Zodiac sign : Snake")
elif birth_year%12 == 10:
    print("Your Chinese Zodiac sign : Horse")
elif birth_year%12 == 11:
    print("Your Chinese Zodiac sign : Goat")
elif birth_year%12 == 0:
    print("Your Chinese Zodiac sign : Monkey")
elif birth_year%12 == 1:
    print("Your Chinese Zodiac sign : Rooster")
elif birth_year%12 == 2:
    print("Your Chinese Zodiac sign : Dog")
else :
    print("Your Chinese Zodiac sign : Pig")

在这里插入图片描述

Astrological sign

Write a Python program to display astrological sign for given date of birth.
在这里插入图片描述

Note:

  • Aries: March 21 - April 19
  • Taurus: April 20 - May 20
  • Gemini: May 21 - June 20
  • Cancer: June 21 - July 22
  • Leo: July 23 - August 22
  • Virgo: August 23 - September 22
  • Libra: September 23 - October 22
  • Scorpio: October 23 - November 21
  • Sagittarius: November 22 - December 21
  • Capricorn: December 22 - January 19
  • Aquarius: January 20 - February 18
  • Pisces: February 19 - March 20
birthday = int(input("Input birthday:"))
month_of_birth = input("Input month of birth (e.g. march, july etc):").capitalize()

if month_of_birth == "April":
    if birthday <= 19:
        print("Your Astrological sign is : Aries")
    else:
        print("Your Astrological sign is : Taurus")
if month_of_birth == "May":
    if birthday <= 20:
        print("Your Astrological sign is : Taurus")
    else:
        print("Your Astrological sign is : Gemini")
if month_of_birth == "June":
    if birthday <= 20:
        print("Your Astrological sign is : Gemini")
    else:
        print("Your Astrological sign is : Cancer")
if month_of_birth == "July":
    if birthday <= 22:
        print("Your Astrological sign is : Cancer")
    else:
        print("Your Astrological sign is : Leo")
if month_of_birth == "August":
    if birthday <= 22:
        print("Your Astrological sign is : Leo")
    else:
        print("Your Astrological sign is : Virgo")
if month_of_birth == "September":
    if birthday <= 22:
        print("Your Astrological sign is : Virgo")
    else:
        print("Your Astrological sign is : Libra")
if month_of_birth == "October":
    if birthday <= 22:
        print("Your Astrological sign is : Libra")
    else:
        print("Your Astrological sign is : Scorpio")
if month_of_birth == "November":
    if birthday <= 21:
        print("Your Astrological sign is : Scorpio")
    else:
        print("Your Astrological sign is : Sagittarius")
if month_of_birth == "December":
    if birthday <= 21:
        print("Your Astrological sign is : Sagittarius")
    else:
        print("Your Astrological sign is : Capricorn")
if month_of_birth == "January":
    if birthday <= 19:
        print("Your Astrological sign is : Capricorn")
    else:
        print("Your Astrological sign is : Aquarius")
if month_of_birth == "February":
    if birthday <= 18:
        print("Your Astrological sign is : Aquarius")
    else:
        print("Your Astrological sign is : Pisces")
if month_of_birth == "March":
    if birthday <= 20:   
        print("Your Astrological sign is : Pisces")

在这里插入图片描述

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