Python- Day2
3 min readJul 12, 2024
Data Types:
- Numeric data types: int, float, complex.
- String data types: str.
- Sequence types: list, tuple, range.
- Binary types: bytes, bytearray, memoryview.
- Mapping data type: dict.
- Boolean type: bool.
- Set data types: set, frozenset.
#Data Type
#String
print("Hello" [3])
print("Hello" + "world")
#Integer
print(123 + 258)
#Flot
print(1.23 + 25.8)
print(1.23 - 25.8)
#Boolean = TRUE/FALSE
Project 1: To Take input of Name and print number as characters.
no_char = len(input("What is your name?"))
#Converting integer into string
new_no_char = str(no_char)
print("Your name has "+ new_no_char + " characters.")
OR
no_char = str(len(input("What is your name?")))
print("Your name has "+ no_char + " characters.")
>>
What is your name?Nidhi
Your name has 5 characters.
Data Type: Adding Number + float
a= float(123)
print(type(a))
print(70 + float("100.25"))
>>
<class 'float'>
170.25
#This is string
print(str(152) + str(528))
>>
152528
Let's check the Maths
print(3+5)
print(8-6)
print(6*4)
print(86/2)
print(2**5)
>>
8
2
24
43.0
32
Order in which the Maths will execute: ⬇️
#PEMDASLR
#PRIORITY 1 : ()
#PRIORITY 2: **
#PRIORITY 3: * /
#PRIORITY 4: + -
print(3*3+3/3-3)
>>
7.0
print(3*(3+3)/3-3)
>>
3.0
Project 2: Calculating BIM (Body Mass Index)
height = input("What is your height?")
weight = input("What is your weight?")
height_as_flot = float(height)
weight_as_int = int(weight)
bmi = str(int(weight_as_int / height_as_flot ** 2 ))
print("Your Body Mass Index is " + bmi)
>>
What is your height?6.4
What is your weight?48
Your Body Mass Index is 1
- To Round the Number
print(8/3)
>>
2.6666666666666665
Difference
print(round(8/3))
>>
3
2. To Make this above Calculation into an integer
print(8//3)
>>
2
3. Manipulat value based on its previous value
score= 0
score += 1
print(score)
>>
1
score= 0
score += 1
score += 1
print(score)
>>
2
f-string :
f-strings (formatted string literals) are a way to embed expressions inside string literals in Python, using curly braces {}.
score= 0
score += 1
height = 1.8
is_winning = True
print(f"Your score is {score}, your height is {height}, and your are winning is {is_winning}.")
>>
Your score is 1, your height is 1.8, and your are winning is True.
Project 3: Life is Weeks
age = input("What is your age?")
years = 90 - int(age)
weeks = years*52
days = years*365
print(f"You have {weeks} weeks and {days} days left.")
>>
What is your age?42
You have 2496 weeks and 17520 days left.
To check data type:
a = int("28") / int(52.85)
print(type(a))
>>
<class 'float'>
Project 4: Welcome to Tip Calculator!
print("Welcome to tip calculator!\n")
bill = float(input("What was the total bill? \n$"))
tip = int(input ("How much tip would you like to give? 10,12 or 15?\n%"))
no_of_people = int(input("How many people to split the bill? \n"))
total_and_tip = tip / 100 * bill + bill
final_bill = total_and_tip / no_of_people
print(f"Each person should pay: {final_bill}")
>>
Welcome to tip calculator!
What was the total bill?
$100
How much tip would you like to give? 10,12 or 15?
%10
How many people to split the bill?
2
Each person should pay: 55.0
If you want 2 decimal values:
print("Welcome to tip calculator!\n")
bill = float(input("What was the total bill? \n$"))
tip = int(input ("How much tip would you like to give? 10,12 or 15?\n%"))
no_of_people = int(input("How many people to split the bill? \n"))
total_and_tip = tip / 100 * bill + bill
bill_final_bill = total_and_tip / no_of_people
final_bill = "{:.2f}".format(bill_final_bill) #WE ARE FORMATING
print(f"Each person should pay: {final_bill}")
>>
Welcome to tip calculator!
What was the total bill?
$100
How much tip would you like to give? 10,12 or 15?
%10
How many people to split the bill?
2
Each person should pay: 55.00
If you found this guide helpful then do click on 👏 the button.
Follow for more Learning like this 😊
If there’s a specific topic you’re curious about, feel free to drop a personal note or comment. I’m here to help you explore whatever interests you!