Day-2#100DaysOfCodePython
Understanding Data Types and Manipulating the F-Strings
Hello Everyone, Welcome to our Day-2
Pledge: Take a book and pen before you start your course, Write down all the concepts you have learned.
Today's Concepts:
- Data Types
- Type Checking, Type Error and Type Conversion
- Mathematical Operations
- Numbers calculation and F-Strings in Python
DataTypes:
STRINGS:
Strings should always in "double quotes"
- "Hello"
Note: Strings are start with index[0] . If you see "Hello" index of "H" is 0.
print("Hello"[0]) ### print this you can see the Output of this is "H" because we call index[0].
INTEGERS:
123456
FLOAT NUMBERS
3.14569
Note: Decimal can be anywhere in the numbers .
Boolean Value
True
False
Note: True (or) False can't be used as String.
Let's have some Quiz here:
Question:
street_name = "Abbey Road"
print(street_name[4] + street_name[7])
Hint : Answer is "yo"
TYPE ERROR/TYPE Conversion/Type Checking
- Let's have a look at the Type Error how it prints.
name = len(input("what is your name?"))
print("your name has" + name + "characters")
Output
what is your name?sandeep
Traceback (most recent call last):
File "main.py", line 2, in <module>
print("your name has" + name + "characters")
TypeError: can only concatenate str (not "int") to str
Note: You cannot convert the integer with Strings .
Type Checking
Example:-1
name = len(input("what is your name?"))
#To check the type check for the variable "name" . You should use type()
print(name) # It prints the length of the string from the input.
print(type(name)) # It prints the type of the variable
Output
what is your name?sandeep
7
<class 'int'>
Type Conversion
Example-2
name = len(input("what is your name?"))
new_name = str(name)
##Type conversion
print("your name has " + new_name + " characters")
Note: You can convert Int to String . Once it is converted you can concatenate.
Output:
what is your name?sandeep
your name has 7 characters
Quiz:
Data Types
Instructions
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
Warning. Do not change the code on lines 1-3. Your program should work for different inputs. e.g. any two-digit number.
Example Input
39
Example Output
3 + 9 = 12
12
You can find the solution in my Git Repo:
Mathematical Operators
###Adding two numbers
2 + 2
### Multiplication
3 * 2
### Subtraction
4 - 2
### Division
6 / 3
### Exponential
When you want to raise a number
2 ** 2 = 4
2 ** 3 = 8
Note: Order of Mathematical operators to execute is like "PEMDAS" P - Parentheses - () E - Exponents - * M - Multiplication - D - Division - / A - Addition - + S - Subtraction -
Example : Rule of PEMDAS
print( 3 * 3 + 3 / 3 - 3) #Find the output.
Quiz:
Instructions
Write a program that calculates the Body Mass Index (BMI) from a user's weight and height.
The BMI is a measure of some's weight taking into account their height. e.g. If a tall person and a short person both weigh the same amount, the short person is usually more overweight.
The BMI is calculated by dividing a person's weight (in kg) by the square of their height (in m):
Warning you should convert the result to a whole number.
Example Input
weight = 80
height = 1.75
Example Output
80 ÷ (1.75 x 1.75) = 26.122448979591837
26
Hint:
- Check the data type of the inputs.
- Try to use the exponent operator in your code.
- Remember PEMDAS.
- Remember to convert your result to a whole number (int).
Solution : day-2-2-exercise
4. Numbers calculation and F-Strings in Python
## Division
print(3/2 ) # output = 1.5
##If you want to round of the output of floating value to Int -- Floored division method
print(3//2) # output = 1
###Other way instead floored Division method, we can use round() function
print(round(3/2))
# Arthematic operators
score = 1
score += 1 # This is adding the value of score (1+1)
print(score)
F- Strings
score = 2 # Int
height = 1.8 # Float
isWinning = True # Boolean
result = print("your winning is: " + score + height + isWinning) #
Output
Traceback (most recent call last):
File "main.py", line 20, in <module>
result = print("your winning is: " + score + height + isWinning)
TypeError: can only concatenate str (not "int") to str
Note:
We cannot concatenate String with Int In previous excerises we converted the string value to Int and Int to String , Float to String , Float to Int etc..
#Here we can use "F-String" method instead of type() method to convert the variables.
score = 2 # Int
height = 1.8 # Float
isWinning = True # Boolean
result = print(f"your score is {score}, your height is {height}, your winning is {isWinning}")
Output
your score is 2, your height is 1.8, your winning is True
Quiz:
Instructions
I was reading this article by Tim Urban - Your Life in Weeks and realised just how little time we actually have.
https://waitbutwhy.com/2014/05/life-weeks.html
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.
It will take your current age as the input and output a message with our time left in this format:
You have x days, y weeks, and z months left.
Where x, y and z are replaced with the actual calculated numbers.
Warning your output should match the Example Output format exactly, even the positions of the commas and full stops.
Example Input
56
Example Output
You have 12410 days, 1768 weeks, and 408 months left.
Hint
- There are 365 days in a year, 52 weeks in a year and 12 months in a year.
- Try copying the example output into your code and replace the relevant parts so that the sentence is formated the same way.
Solution: Git
That's all Pythian ~~ Yayyy~~ we have completed our Day-2!!!