In python, every value has a data type, which tells Python how it can be used.
Basic Data Types:
- String - a piece of text ("Hello", "apple", ...)
- Int - an integer number (-10, -2, 0, 1, 7, 500, ...)
- Float - a real number (-10.5, 4.0, 3.3333, 20.16, ...)
- Bool - a boolean value (True, False)
It’s important to understand what you can and cannot do with each data type. For example, adding a number to a boolean value usually doesn’t make sense and can lead to errors.
To check the type of a value, you can use the type() function.
type(5)Obviously this on its own won't show you anything, you can pair it with the print() function.
print(type(5)) # Output: <class 'int'>You can also use variables with this function:
number = 5
print(type(number)) # Output: <class 'int'>Or even store the output of the type() function itself in a variable:
number = type(5)
print(number) # Output: <class 'int'>Casting is the proccess of converting a value from one data type to another.
There is a set of Python functions that allows you to do that:
int()- turns the value to an integerfloat()- turns the value to a floatstr()- turns the value to a stringbool()- turns the value to a boolean
You can do a few things with strings.
You can concatenate them:
print("Hello " + "world!") # Output: Hello world!But what if you want to concatenate a string with a number?
print("Number " + 5) # This will throw an errorYou can either wrap your integer in quotes and it will turn into a string:
print("Number " + "5") # Output: Number 5Or you can cast your integer into a string:
print("Number " + str(5)) # Output: Number 5It will also work with variables:
my_text = "Number "
my_number = 5
print(my_text + str(my_number)) # Output: Number 5What if it is the other way around:
number_1 = "5"
number_2 = "10"
print(number_1 + number_2) # Output: 510You can just cast them to integers:
number_1 = "5"
number_2 = "10"
print(int(number_1) + int(number_2)) # Output: 15Or even floats:
number_1 = "5"
number_2 = "10"
print(float(number_1) + float(number_2)) # Output: 15.0Something useful you can also do is multiply a string with an integer n:
print("a"*10) # Output: aaaaaaaaaaIt will just repeat the string n times.
Ints and floats are different data types, but when python is doing calculations with them, it just converts them to the correct data type.
Take this example:
print(5/2) # Output: 2.5Despite using two integers, Python knows the output needs to be a float so it allows and converts it automatically.
Another example:
print(5 + 2.0) # Output: 7.0We are doing the sum of an integer and a float, but Python just converts the integer to float, this is what is called implicit type conversion.
Next topic: ArithmeticOperators

