Đổi độ f sang độ c python

Bài viết này Phú tổng hợp những chương trình Python cơ bản, giúp người học có cài nhìn tổng quát hơn về ngôn ngữ lập trình này.

Danh sách các chương trình

# store input number
num1 = input("Nhap so thu 1: ")
num2 = input("Nhap so thu 3: ")
# add two numbers
sum = float(num1) + float(num2)
# display the sum
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
# chương trình rút gọn
print('The sum is %.2f' %(float(input('Enter first number: ')) + float(input('Enter second number: '))))
# %.2f làm tròn đến chữ số thập phân thứ 2

Chương trình tính diện tích hình vuông

import cmath

# num = 1+2j

# To take input from the user
num = eval(input('Enter a number: ')) # eval để tính số thực có dạng (a + bj)

num_sqrt = cmath.sqrt(num)
print('The square root of {0} is {1:0.3f}+{2:0.3f}j'.format(num ,num_sqrt.real,num_sqrt.imag))


# num_sqrt.real = phần thực
# num_sqrt.imag = phần ảo

Chương trình tính diện tích tam giác bất kỳ

# tính diện tích hình tam giác
# Heron's formula
# s = (a+b+c)/2
# area = √(s(s-a)*(s-b)*(s-c))

canh2 = float(input('Nhập cạnh 1: '))
canh2 = float(input('Nhập cạnh 2: '))
canh3 = float(input('Nhập cạnh 3: '))

s = (canh2 + canh2 + canh3) / 2

dien_tich = (s * (s - canh2) * (s - canh2) * (s - canh3)) ** 0.5

print("Dien tích hình tam giác {0} {1} {2} là: {3} ".format(canh2, canh2, canh3, dien_tich))
# print("Dien tich hinh tam giac: %0.1f" %dien_tich)

Chương trình tính nghiệm của phương trình bậc 2

import cmath

a = float(input("Nhập a: "))
b = float(input("Nhập b: "))
c = float(input("Nhập c: "))

# tính số phân biệt
d = (b**2) - (4*a*c)

# tính kết quả
sol1 = (-b - cmath.sqrt(d))/(2*a)
sol2 = (-b + cmath.sqrt(d))/(2*a)

print('The solution are {0} and {1}'.format(sol1,sol2))

Chương trình chuyển đổi giá trị 3 biến

a = int(input('Nhập a: '))
b = int(input('Nhập b: '))
c = int(input('Nhập c: '))


a, b, c = b, c, a

print('Gia gia a {}'.format(a))
print('Gia gia b {}'.format(b))
print('Gia gia c {}'.format(c))

Chương trình chuyển đổi năm km sang dặm

# chương trình chuyển đổi km -> dặm

# conversion factor
conv_fac = 0.621371

kilometers = float(input("Input kilometers: "))

# tính ra dặm
miles = kilometers * conv_fac

print("%0.2f kilimeters is equal  to %0.2f miles" %(kilometers, miles))

Chương trình chuyển đổi độ C sang độ F

# chương trình chuyển độ C sang độ F
# celsius * 1.8 = fahrenheit - 32

celsius = float(input("Input oC: "))

fahrenheit = (celsius * 1.8) + 32

print("%0.2f oC to %0.2f oF" %(celsius,fahrenheit))

Chương trình tính năm nhuận

# Một năm nhuận chính xác là chia hết cho 4 
# ngoại trừ năm thế kỷ (năm kết thúc bằng 00). 
# Năm thế kỷ chỉ là năm nhuận nếu nó chia hết cho 400.

year =  int(input("Input year (19xx - 20xx): "))

if (year % 4) == 0:
    if (year % 100) == 0:
        if (year % 400) == 0:
            print("{0} is leap year".format(year))
        else:
            print("{0} is not leap year".format(year))
    else:
        print("{0} is  leap year".format(year))
else:
    print("{0} is not leap year".format(year))

Chương trình tìm số lớn nhất

a = int(input("Input a: "))
b = int(input("Input b: "))
c = int(input("Input c: "))

# max = a
# if a < b:
#     if b > c:
#         max = b
#     elif a < c:
#         max = c
# elif a < c:
    # max = c
# print('Max is ',max)

if (a > b) and (a > c):
    print("Max is ", a)
elif (b > a) and (b > c):
    print("Max is ", b)
else:
    print("Max is ",c)

Chương trình tìm số nguyên tố

# chương trình tìm số nguyên tố
# Program to check if a number is prime or not

# num = 407

# To take input from the user
num = int(input("Enter a number: "))

# prime numbers are greater than 1
if num > 1:
   # check for factors
   for i in range(2,num):
       if (num % i) == 0:
           print(num,"is not a prime number")
           print(i,"times",num//i,"is",num)
           break
   else:
       print(num,"is a prime number")
       
# if input number is less than
# or equal to 1, it is not prime
else:
   print(num,"is not a prime number")

Chương trình tính giai thừa của một số

# tìm giai thừa của một số

num = int(input("Input number: "))
factorial = 1
for i in range(1,num+1):
    factorial = factorial * i

print("Factorial {} is {}".format(num, factorial))

Chương trình tạo bảng cửu chương

# bảng cửu chương

num = int(input("Enter number multiplication table: "))

for i in range(1,11):
    print(num, 'x', i, '=', num*i)

Chương trình in dãy số Fibonacci

# in dãy số fibonacci
# 1 1 2 3 5 8 13 21 34
nterms = int(input('Enter terms: '))
n1, n2 = 0, 1
count = 0

if nterms <= 0:
    print('Please enter a positive interger')
elif nterms == 1:
    print('Fibonacci sequence upto', nterms, ':')
    print(n1)
else:
    print('Fibonacci sequence upto', nterms, ':')
    while count < nterms:
        print(n1, end ='\t')
        n_next = n1 + n2
        #update n1, n2
        n1 = n2
        n2 = n_next
        count += 1

Chương trình