Primitive data types
#string = string of characters and need to be created with “”. You can use [ ] to show the first character and use 0 such as print(“Hello”[0]) = H. Even numbers in “ “ will be counted as characters instead of number so print(“123” + “345”) = 12345
#Integer = Makes the code as numbers. With large numbers you can use _ instead of commas to make large numbers more easier to look at e.g 123_456_789.
Print(123 + 345) = 468
#Float = Number with a decimal point will become a floating point number. Even if there is an underscore to make a large number more readable, because there is a decimal point it is still a float.
e.g 739_529.829 is a float
#Boolean = Only has two possible answers, truth or false. True or False always start with Capital letters and without “ “.
Type Error, Type checking and Type conversion
You can only concatenate a string and not a integer.
You can use the Type function to tell you what type of code you are using.
e.g num_char = len(input(“What is your name?”)
Print(type(num_char)) = <class ‘int’>
Therefore the code print(“Your name has ” + num_char + “ characters.”) does not work
You can convert the integer into a string by writing a code new_num_char = str(num_char).
You can convert other codes as well such as float.
e.g a = float(123) / print(type(a)) <class ‘float’>
e.g print(70 + float(“100.5”)) = 170.5
However if you use a code print(str(70)+ str(100)) = 70100 since it is a string rather than a integer.
Data type Exercise
Write a program that adds the digits in a 2 digit number. e.g. if the input was 35, then the output should be 3 + 5 = 8.
two_digit_number = input()
# 🚨 Don't change the code above 👆
####################################
# Write your code below this line 👇
# Since two_digit_number is a string we can use [ ] to find out the corresponding input
First_digit = int(two_digit_number[0])
Second_digit = int(two_digit_number[1])
# Add the two interger numbers together
two_digit_number = first_digit + Second digit
Print(two_digit_number)
or chat GPT method
num = int(two_digit_number)
total = sum(int(digit) for digit in str(num))
print(total)
Mathematical Operations in Python
For addition python uses +
For subtraction its -
For multiplication its *
For division its / but only for division the answer will always float
** is the power of something
Even for Python the rules of maths is applied. The PEMDAS rule for math priorities
Parentheses ()
Exponents **
Multiplication *
Division /
Addition +
Subtraction -
Making a BMI calculator
Write a program that calculates the Body Mass Index (BMI) from a user's weight and height.
# 1st input: enter height in meters e.g: 1.65
height = input()
# 2nd input: enter weight in kilograms e.g: 72
weight = input()
# 🚨 Don't change the code above 👆
# Write your code below this line 👇
# At first there was an syntax error so I checked weight and height with print(type())
# After checking the type i would change the height from str to float because of the decimal point
height1 = float(height)
weight1 = int(weight)
bmi = int(weight1 / (height1 * height1)) or bmi = int(weight1 / height1 ** 2)
print(bmi)
Number manipulation and F strings
When dividing numbers in Python the answer will come out as whole number or float number however when we change the code to print(int(8 / 3)) it would just come out as 2 instead of 2.666.
Therefore we can use the round function to get the whole number. Print(round(8 / 3) = 3
In addition you can choose which decimal point to round it from in the code such as
Print(round(8 / 3, 2)) -> 2 meaning from the 2 decimal point = 2.67
If you want a whole number (int) instead of a float number when dividing you can use //
Print(8 // 3) = 2 and when you check using print(type(8 // 3)) it will come out as an int. You can also use short code with the same effects such as score = 0, score += 1 -> print(score) = 1
F-String
F-String allows you to code different types of code such as Boolean, float, integer etc in a string.
f needs to be in front of the string to become a f-string.
score = 0
height = 1.8
isWinning = true
e.g print(f”score is {score}, your height is {height}, you are winning is {isWinning}”)
Life in weeks exercise
Create a program using maths and f-Strings that tells us how many weeks we have left, if we live until 90 years old. It will take your current age as the input and output a message with our time left in this format: You have x weeks left.
age = input()
# 🚨 Don't change the code above 👆
# Write your code below this line 👇
weeks = (52)
Years = (90)
x = ((Years - int(age)) * weeks)
print(f"You have {x} weeks left.")
other solution (course)
years = 90 -int(age)
weeks = years * 52
print(f”you have {weeks} left.”)
Tip Calculator Project
#my code
print("Welcome to the tip calculator")
total_bill = int(input("What was the total bill? $\n"))
tip = int(input("What percentage tip would you like to give? 10, 12 or 15?\n"))
split = int(input("How many people to split the bill?\n"))
bill = (total_bill / split) * (1 + (tip / 100))
final_bill = round(bill, 2)
print(f"Each person should pay ${final_bill}")
Course code
print("Welcome to the tip calculator")
bill = float(input("What was the total bill? $"))
tip = int(input("What percentage tip would you like to give? 10, 12 or 15? "))
people = int(input("How many people to split the bill?"))
bill_with_tip = (tip / 100 * bill + bill) / people
final_amount = "{:.2f}".format(bill_with_tip)
print(f"Each person should pay $ {final_amount}")
'Coding : Python' 카테고리의 다른 글
Python_Day 6 (0) | 2023.11.30 |
---|---|
Python_Day 5 (1) | 2023.11.23 |
Python_Day 4 (0) | 2023.11.17 |
Python_Day 3 (0) | 2023.11.15 |
Python_Day 1 (0) | 2023.11.10 |