IT-info > Python

Python

Python language info

Example code

x = 2.9

print(round(x)) # 3

print(abs(-2.9)) # 2.9

Example code

import math # imports math modile. Info on math module: https://docs.python.org/3/library/math.html

print(math.ceil(2.9)) # 3
print(math.floor(2.9)) # 2

Example code

is_hot = False
is_cold = False

if is_hot:
    print("It's a hot day")
    print("Drink plenty of water")
elif is_cold:
    print("It's a cold day")
    print("Wear warm clothes")
else:
    print("It's a lovely day")

print("Enjoy your day")

Example code

price = 1000000
has_good_credit = True

if has_good_credit:
    down_payment = 0.1 * price
else:
    down_payment = 0.2 * price

print(f"Down payment: ${down_payment}")

Example code

Example 1

has_high_income = True
has_good_credit = True

if has_high_income and has_good_credit:
    print("Eligible for loan")

Example 2

has_high_income = False
has_good_credit = True

if has_high_income or has_good_credit:
    print("Eligible for loan")

Example 3

has_good_credit = True
has_criminal_record = False

if has_high_income and not has_criminal_record:
    print("Eligible for loan")

Example code

name = "J"

if len(name) < 3:
    print("Name must be at least 3 characters")
elif len(name) > 50:
    print("Name must be a maximum of 50 characters")
else:
    print("Name looks good!")

Example code

weight = int(input('Weight:'))
unit = input('(L)bs or (K)g:')

if unit.upper == 'L'
    converted = weight * 0.45
    print(f"You are {converted} lbs")
else
    converted = weight / 0.45
    print(f"You are {converted} kg")