100 days of code — Python
Continuing my journey on day 2.
In Python, there are several primitive data types that are used to store values.
These include:
- Integers — used to represent whole numbers, such as 1, 2, 3, etc.
- Floats — used to represent decimal numbers, such as 1.0, 2.5, 3.14, etc.
- Booleans — used to represent True or False values.
- Strings — used to represent text values, such as “hello”, “world”, etc.
- None — used to represent a null value or absence of value.
# Integer
my_int = 15
# Float
my_float = 3.14
# Boolean
my_boolean = True
# String
my_string = "Hello, Dj!"
# None
my_none = None
The type of data that a function can operate on is often determined by the function’s implementation.
Functions:
- Mathematical such as
abs()
,round()
,min()
, andmax()
operate on numeric data types like integers and floats. - String such as
len()
,split()
,join()
, andupper()
operate on string data types. - Boolean such as
bool()
,all()
, andany()
operate on boolean data types. - Data type conversion such as
int()
,float()
,str()
,bool()
, andlist()
are used to convert one data type to another.
# Using math functions with integers and floats
abs(-5) # returns 5
round(3.14159, 2) # returns 3.14
min(1, 2, 3, 4) # returns 1
max(1, 2, 3, 4) # returns 4
# Using string functions with strings
len("hello") # returns 5
"hello world".split() # returns ['hello', 'world']
" ".join(["hello", "world"]) # returns "hello world"
"hello".upper() # returns "HELLO"
# Using boolean functions with booleans
bool("") # returns False
all([True, True, False]) # returns False
any([False, False, True]) # returns True
# Using data type conversion functions
int("5") # returns 5
float("3.14") # returns 3.14
str(123) # returns "123"
bool(1) # returns True
list("hello") # returns ['h', 'e', 'l', 'l', 'o']
The concept of data types in Python is important because it can impact the behavior of our code. For instance, if we try to perform an operation between 2 values of different data types, we can encounter a Type Error.
Type Error occurs when an operation or function is applied on of the incompatible data type such as concatenate string with integer.
Lets say; if we have a string that is containing a number and we want to perform a mathematical operation on it — we can int()
function to convert its type to integer data type
my_string = "45"
my_int = int(my_string)
result = my_int + 10
# Output: 55
print(result)
Overall, understanding of data types, type checking and data conversion in Python is important for writing robust and error-free code.
Python supports many mathematical operations, including:
- Addition: The
+
operator adds two numbers together. - Subtraction: The
-
operator subtracts the second number from the first. - Multiplication: The
*
operator multiplies two numbers together. - Division: The
/
operator divides the first number by the second. - Modulo: The
%
operator returns the remainder of the first number divided by the second. - Exponentiation: The
**
operator raises the first number to the power of the second. - Floor Division: The
//
operator performs integer division, discarding any remainder.
F-strings:
F-strings (or formatted strings) is a feature in Python 3.6 and above that allows us to embed expressions inside string literals, using curly braces {}
.
name = "Jennifer"
age = 21
# {name} > string literals
greeting = f"Hello, my name is {name} and I am {age} years old."
print(greeting)
In day 2, we learned about data types, type checking and data conversion. we also learned F-strings a convenient way to format our strings in Python that enable us to embed expressions and variables into strings literals.