Learn Python-100 Days Of Code with Khemlall -DAY 2- Understanding Data Types and String manipulation

Khemlall Mangal
9 min readOct 13, 2021

Goals for day 2: Data types, Numbers, Operations, Type Conversion, f-string and whole lot of more. By the end of the day you would learn enough to build a tip calculator

Alright lets get started!

Welcome to DAY2!!! by the end of today, you will learn enough to build a tip calculator.

In order to build this you will need to learn to some new skills including working with number, converting data types from one type to another and whole a lot more.

In Day1 lesson you saw that we could use the len() function to get the number of character in a string. For example len(“hello”) we get 5 character.

Instead if we want to know how much digit in this number how can we do this. On Day 1 we explore datatype called string. Today we will explore more about string, integer, float and Boolean.

Python Privative Data Types

Data-type: String

print(“hello”) you will get → hello

Subscripting — -Pulling out a character within a String
Note: Programmer starts counting with 0 (zeroes) we would with binary 0,1.
So if i say print me out the first character what number will be startwith? you guess it 0

print(“Hello”[0]) → What letter do you think we will output? You guess it. H

The number between the square brackets determine which letter you will pull out.

challenge: can you print out o? change the code so that o is printed

ok solution: you guess it print(“Hello”[4]) …count the letter from 0 and o stops at 4

Question: if i have print(“123” + “345”) what do you think will be printed?

Remember: anything between the quote is treated as a string. 123 is a number but its between a quote so it will be treated as a string and will not be treated as a number.

All whole number regardless weather its positive or negative is called integers. If it has decimal place, then it will be a FLOAT data type.

Think of 3141.59 this is a float.

Final data type is boolean, two possible value, True or False.

Awesome, lets test your understanding.

  1. Which statement is incorrect?
    a) 932 is an Integer
    b) “False” is a boolean
    c) 857.25 is a Float
    d) “523” is a string
  2. What is the data type of the mystery variable? mystery = 734_529.678

Answer
Even though the underscore is there to make the large number more readable, because there is a decimal point it is still a Float. If you printed the number in your code, it would be stripped down to 734529.678

3. I’ve put a spell on you. You are now a computer. If I give you the following code, what will you print out?

u print out?

street_name = "Abbey Road"print(street_name[4] + street_name[7])

The answer is yo space is counted as a character.

Lesson 2:

Lets talk about data type and functions

Remember we used len function in DAY1 to get the number of character in a string. So say we use that same function but pass it in a number….We get an error right?

len(4832)

Here is an analogy.

We got a machine that produce fries. So we have machine that processes potatoes and produces fires as the final product. Now if we decide to feed it a rock we will get an error and it will not process anything.

So as you can see we get a type error: Lets discuss type.

remember in day 1 we had the following below and it gave a type error. Lets talk about type and how to get this working

numb_char = len(input(“what your name?”))
print(“Your name has “ +numb_char + “ characters”)

in python we have a type() function that can tell us the data type of our variable. Example:

print(type(numb_char))

type() function in Python

In this case we will get type error, we can only concatenate string and not integer….. How can we prevent these type errors or how can we see the type we are working with? that the type function()

Now lets do type conversion or type casting to change one particular data type to another. If we want to turn it into a string so the line of code break anymore, we can use str() function

new_numb_char = str(numb_char)
Now plug that into your code.
numb_char = len(input(“what your name?”))
print(“Your name has “ +new_numb_char+ “ characters”)

You can covert a whole different type of data type. if we have something like this, a = 123 which is a number you will see that the type will print out as int. We can now cast that or covert it to a string data type by simply print(str(a)) and if we do a type(a) you will see its now a string data type.

Number data type example

if we do something like print(type(str(a))) you will get the output of

we can turn the whole number into a float for example.

print(70 + float(“103.5”)) you guess it 173.5

Whats happening is that we are converting 103.5 to a float and adding 70 to it. You can use the type() function to figure out what data type you are working with.

Fork my code here for the challenge:

https://replit.com/@KhemlallMangal/day-2-1-exercise#main.py

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

e.g. When you hit run, this is what should happen:

https://cdn.fs.teachablecdn.com/iyJTPDDRRJCB1gmdVQMS

Hint

  1. Try to find out the data type of two_digit_number.
  2. Think about what you learnt about subscripting.
  3. Think about type conversion.

My Solution:

Lets talk about mathematical operations in Python.

mathematical calculation in python

So in this lesson we can learn about different mathematical operation we have.
Addition: 3 +1 = 4
Subtraction 3–2 = 1
Multiplication 3*2 =6
Division 6/3 = 3

When you divide a number you will get a float result or type back. So example 6/2 = 3.0. Lets print this out in python : print(type(6/3)) — What do you expect the results will be? It will be type of float. Try it

division in python returns a float.

Last operation we will be talking about is the Double Astris ** or the power of or exponent.

Example 2 **3 = 2x2x2 =8

Remember high school we learn about order of operation? there is a word we learn to help us remember the order which is:
PEMDAS — Parentheses first, then exponents then multiplication then division then addition and subtraction.

If we were to execute this what would you expect… Remember it starts from left to right….
result = 3*3+3/3–3
print(result)

The answer is 7. Try to see if you understand why.

Challenge, change this code so instead of getting 7 we get 3

Solution:
3 *(3+3)/3–3

BMI Calculator

https://replit.com/@KhemlallMangal/day-2-2-exercise#main.py

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):

https://cdn.fs.teachablecdn.com/jKHjnLrNQjqzdz3MTMyv

Warning you should convert the result to a whole number.

Example Input

weight = 80height = 1.75

Example Output

80 ÷ (1.75 x 1.75) = 26.122448979591837

26

e.g. When you hit run, this is what should happen:

https://cdn.fs.teachablecdn.com/wmjVjddeSmGj0QVtOUrE

Hint

  1. Check the data type of the inputs.
  2. Try to use the exponent operator in your code.
  3. Remember PEMDAS.
  4. Remember to convert your result to a whole number (int).

Test Your Code

Before checking the solution, try copy-pasting your code into this repl:

Checkout the solution:

Lets learn about f-string in python when you want to insert different data type within your print statement, you can use fstring. This will elimiate the need to keep converting the datatype.

count = 0
floatscore = 1.5
winning = true

f “the count is {count} ”
print(f “the count is {count} and is winning {winning}”)

FINAL CODING CHALLENGE

Your Life in Weeks

Instructions

I was reading this article by Tim Urban — Your Life in Weeks and realized just how little time we actually have.

LIFE CALENDAR

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.

e.g. When you hit run, this is what should happen:

https://cdn.fs.teachablecdn.com/RjqBViZQpyVTv7XY6cfA

Hint

  1. There are 365 days in a year, 52 weeks in a year and 12 months in a year.
  2. Try copying the example output into your code and replace the relevant parts so that the sentence is formatted the same way.

There are more efficient ways to do this but lets just try to walk though this.

  1. Lets Convert the age to integer
  2. Lets calculate the number of years left for us to meet 90 years old
  3. We set a variable that stores the number of years in a year or we can just simply takes the number of years left multiply by 360
  4. store number of weeks by multiplying the number of years left *52 since there is 52 weeks in a year.
  5. and to calculate number of months we know 12 months =1 year so we take 12 Months X number of years left.
  6. To print the various data type without having to do explicit conversions we use the functionality of f string.

Ok now you are ready to build the TIP CALCULATOR!

#If the bill was $150.00, split between 5 people, with 12% tip.

#Each person should pay (150.00 / 5) * 1.12 = 33.6

#Format the result to 2 decimal places = 33.60

#Tip: There are 2 ways to round a number. You might have to do some Googling to solve this.💪

#HINT 1: https://www.google.com/search?q=how+to+round+number+to+2+decimal+places+python&oq=how+to+round+number+to+2+decimal

#HINT 2: https://www.kite.com/python/answers/how-to-limit-a-float-to-two-decimal-places-in-python

For solution here:

https://replit.com/@KhemlallMangal/tip-calculator-start#main.py

--

--

Khemlall Mangal

I am a passionate coder, QA Engineer, and someone who enjoys the outdoors.