#100DaysOfCode: Python Day 2

Data Types & Mathematical Operators

Subscribe to my newsletter and never miss my upcoming articles

#100DaysOfCode Day 2

Yesterday was day two...
I covered Data Types and Mathematical Operators. I can see why this is a good language for machine learning as the syntax for performing mathematical operations are so simple compared to C#.
The Highlight - I built a restaurant bill and tip calculator!

Lessons

  1. Doing all the tutorials make the quizzes so much fun!
  2. Overcoming setting up your dev environment is a huge step! I am now happier that my machine is set up.
  3. I need a template for my posts if I plan to do this daily!

TO THE CODE!!!

dataTypes.py - strings, bools, ints and floats

# Strings
# Subscripting a string 
print("Hello"[0])
print("Hello"[4])
# Just concatenates
print("123" + "345")

#--------------------------------------------------------------
# Interger
# This gets calculated
print(123 + 345)

#--------------------------------------------------------------
# Float - has decimal places
print(123.00)

#--------------------------------------------------------------
# Boolean
print(True)
print(False)

#--------------------------------------------------------------
# Understanding Type Errors
# num_char = len(input("What is your name?"))
# print("Your name has " + num_char + " characters")

# Get the datatype!
num_char = len(input("What is your name?"))
print(type(num_char))

# We need to convert this datatype
new_num_char = str(num_char)
# Use float(var) to convert another datatype to a float 

num_char = len(input("What is your name?"))
print("Your name has " + new_num_char + " characters")
# Input: Rishal
# Output: Your name has 6 characters

#--------------------------------------------------------------
#f-String
# To print all these datatypes can be a pain but we can use f-String
score = 1
height = 1.7
isWinning = True

print(f"Your score is {score}, your height is {height}, you are winning is {isWinning}")
# Output: Your score is 0, your height is 1.7, you are winning is True

mathematicalOperators.py - PEMDAS!!!

# addition
print(3 + 5) 
# subtraction
print(5 - 3) 
# multiplication
print(3 * 2) 
# division - You will always get a float from this operator
print(6 / 2) 
# exponents - 2 to the power of 2
print(2 ** 3) 

#--------------------------------------------------------------
# Operators follow the PEMDAS preference of execution
# ()
# **
# *
# /
# +
# -

print(3 * 3 + 3 / 3 - 3) 
# Output: 7

# Priority on addition because of brackets
print(3 * (3 + 3) / 3 - 3)  
# Output: 3

#--------------------------------------------------------------
# CHALLENGE

# 🚨 Don't change the code below 👇
height = input("enter your height in m: ")
weight = input("enter your weight in kg: ")
# 🚨 Don't change the code above 👆

# Write your code below this line 👇
bmi = float(weight) / float(height) ** 2
print(int(bmi))

#--------------------------------------------------------------
# ROUNDING NUMBERS
print(8 / 3)
# Output: 2.666666666

print(int(8 / 3))
# Output: 2

print(round(8 / 3)) # rounds it to a whole number 
# Output: 3

print(round(8 / 3, 2)) # rounds it to a decimal
# Output: 2.67

print(8 // 3) # floor devision
# Output: 2

#--------------------------------------------------------------
# You can perform further calculations if you store it as a var
result = 4 / 2
result /= 2
print(result)
# Output: 1.0

As much as #100DaysOfCode is supposed to be an hour a day, writing an article adds some more time. I really do enjoy writing and sharing this learning journey with you. With that said, I am unsure if I will be as consistent as daily posts like Daily Dev Tips (My biggest inspiration); some days, I may bundle my learnings and share with you a day range.

Comments (3)

Poojitha Dathathri Naidu's photo

Hey, In the f-string example, I'm not able to output a string value. I'm able to output only an integer value. Do we need to specifically convert it into a string?

Rishal Vallabh's photo

Hi Poojitha.

It should print the integer as a string when using an fstring.

For example: name = 'Rishal' #this is a string age = 29 #this is an Int print(f"Hello, My name is {name} and I'm {age} years old.")

OUTPUT: Hello, My name is Rishal and I'm 29 years old.

Hope this helps

Poojitha Dathathri Naidu's photo

Thank you Rishal, it did. I forgot to mention the apostrophe. Rishal Vallabh