top of page
Home: Blog2
Home: Instagram_Widget

Python - Beginner - Data Types 002

Data Types : String, Integer, Float, Boolean

Subscripting : use [] with the location of the character to pull out the item

String : "ABC" "123"

Integer : 123 or 123_456_789 (_ in liue of commas)

Float : 2.1279

Boolean : True or False

Concatenation : You can only concatenate strings not integer.


Type Conversion / Type casting

Convert a string to an integer by creating a new name new_num_char = str(numchar)

convert a integer to string (you need this for contacating'0

num_char = len(input("what is your name? "))
new_num_char = str(num_char)
print("Your name has " + new_num_char + " characters.")
print(str(70)+ str(100))
# result wil be 70100, here we are concatenating two strings 
print(70 + float("100.5"))
# result will be 170.5, we are first converting the string to a float and then adding

Exercise : Program which adds the digits of two numbers which are entered.

input_number = input("Type a two digit number : ")
input_a = input_number[0]
input_b = input_number[1]
new_input_a = int(input_a)
new_input_b = int(input_b)
final_output = new_input_a + new_input_b
print(final_output)
#BMI Calculator
weight = input("whats your weight in kg: ")
height = input("whats your height in m: ")

int_weight = float(weight)
int_height = float(height)

BMI = int_weight / (int_height ** 2)

BMI_as_int = int(BMI)
print(BMI_as_int)

if BMI < 30 : print("Healthy")
else : print("Obese")

print(round(BMI, 2))

# floor division is  a way to get the the integer values and drop everything after the decimal. e.g 8 // 3
# f-String is a function which allows you to stich up different data types together

print(f"your weight is {int_weight}, your height is {int_height}, and your BMI is {BMI_as_int}")

# Life Left Calculator
input_age = input("what is your current age? ")
max_age = 90
months_in_year = 12
weeks_in_year = 52
days_in_year = 365

#convert input_age from string to integer
int_input_age =  int(input_age)

#get the result of life left
life_left_weeks = (max_age * weeks_in_year) - int_input_age * (weeks_in_year)
life_left_days = life_left_weeks * (days_in_year / weeks_in_year)
life_left_months = (life_left_days * months_in_year) / days_in_year

#convert float to Integer
int_life_left_days = int(life_left_days)
int_life_left_months = int(life_left_months)

print(f"Your have {int_life_left_days} days, {life_left_weeks} weeks and {int_life_left_months} months left,")

*SUMMARY:

  • Programmers always count starting from zero

  • the method of pulling out a character from the string is called Subscripting

  • use len() to count in the length in strings, it doesnt work with integers

  • if your not sure what the type of the data is then put it into the type check e.g. print(type(num_char))

  • you can use the type function to interrogate the differnent data types that your working with.**

Mathematical Operators

  • When dividing 6 / 3 the result is 2.0 which is a float even though its a clean division, if you do a type check it will show as a float.

  • Exponent handling is built into python and thats why data scientists like it. eg : 2 to the power of 3 is expressed as 2**3

  • priority of formula calculation is PEMDAS : Parenthesis, exponents, multiplications, division, addition, subtraction. . Eg : (3 * 3 + 3 / 3 - 3) will be 7.0

Comments


Home: Subscribe

©2019 by CodingWithSuj. Proudly created with Wix.com

bottom of page