Python from Angela
Day-2
print first character of a string.
# print first character of a string.
print("Hello"[0]) ## this will print 'H'
Typecasting
num_char = len(input("what is your name: "))
new_num_char = str(num_char) ## This is type casting
print("your name has " + new_num_char + " characters.")
output:
what is your name: Donald Trump
your name has 12 characters.
print(70 + float("100.5"))
output: 170.5 print(str(70) + str(100))
output: 70100
Data Types
Ques: 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("Type a two digit number: ")
first_digit_number = two_digit_number[0]
second_digit_number = two_digit_number[1]
result = int(first_digit_number) + int(second_digit_number)
print(result)
## or
two_digit_number = input("Type a two digit number: ")
## Get the first and second digits using subscripting then convert string to int.
first_digit_number = int(two_digit_number[0])
second_digit_number = int(two_digit_number[1])
## Add the two digits together
two_digit_number = second_digit_number + first_digit_number
print(two_digit_number)
Operators in Python
Parentheses---()
Exponents --- **
Multiplication -- *
Division--- /
Addition -- +
Subtraction -- Minus -
BMI Calculator
## BMI Calculator
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
bmi = int(weight) / float(height) ** 2
bmi_int = int(bmi) ## or print(int(bmi))
print(bmi_int)
Round
print(round(8 / 3))
output: 3
print(round(8 / 3, 3)) ## last 3 will make 3 decimal after (.)
output: 2.667
print(round(10 / 3, 5))
output: 3.33333
print(round(14 / 3))
output: 5
print(round(14 / 3, 3))
output: 4.667
print(round(20 / 3))
output: 7
print(round(19 / 3))
output: 6
print(round(19 / 3, 4))
output: 6.3333
It will print integer value
print(8 // 3)
output: 2
print(type(8 // 3))
<class 'int'>
It will print Floating number
print(8 / 3)
output: 2.6666666666666665
print(type(8 / 3))
F-String
## F-string
score = 0
height = 1.8
winner = True
# F-string
print(f"your score is {score}, your height is {height}, your winning is {winner}")
OUTPUT: your score is 0, your height is 1.8, your winning is True
Ques: 1:
Create a program using maths and f-strings that tells us how many Days, weeks, months we have left if we live until 90 years old.
Sol:
age = int(input("enter your current age: "))
years_remaining = 90 - age
days_remaining = years_remaining * 365
weeks_remaining = years_remaining * 52
months_remaining = years_remaining * 12
message = (f"you have {days_remaining} days,{weeks_remaining} weeks, and {months_remaining} months")
print(message)
Make a Tip calculator
print("welcome to the tip calculator.")
bill = float(input("what was the total bill? $ "))
tip = int(input("How much tip would you like to give? 10, 12, or 15? "))
people = int(input("How many people to split the bill? "))
tip_as_percent = tip / 100
total_tip_amount = bill * tip_as_percent
total_bill = bill + total_tip_amount
bill_per_person = total_bill / people
final_amount = round(bill_per_person, 2)
final_amount = "{:.2f}".format(bill_per_person) ## round two decimal places using .2f
print(f"Each person should pay: ${final_amount}")