100 days of code — Python

Khadija Batool Gardezi
3 min readMar 9, 2023

--

Continuing my journey on day 2.

In Python, there are several primitive data types that are used to store values.

These include:

  1. Integers — used to represent whole numbers, such as 1, 2, 3, etc.
  2. Floats — used to represent decimal numbers, such as 1.0, 2.5, 3.14, etc.
  3. Booleans — used to represent True or False values.
  4. Strings — used to represent text values, such as “hello”, “world”, etc.
  5. 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:

  1. Mathematical such as abs(), round(), min(), and max() operate on numeric data types like integers and floats.
  2. String such as len(), split(), join(), and upper() operate on string data types.
  3. Boolean such as bool(), all(), and any() operate on boolean data types.
  4. Data type conversion such as int(), float(), str(), bool(), and list() 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:

  1. Addition: The + operator adds two numbers together.
  2. Subtraction: The - operator subtracts the second number from the first.
  3. Multiplication: The * operator multiplies two numbers together.
  4. Division: The / operator divides the first number by the second.
  5. Modulo: The % operator returns the remainder of the first number divided by the second.
  6. Exponentiation: The ** operator raises the first number to the power of the second.
  7. 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.

--

--

Khadija Batool Gardezi

Dev — SignalWire |GitHub Campus Expert // Google DSC // MLSA |