Mastering Python: Day 2 — Python Fundamentals

Risky Mulya Nugraha
5 min readSep 1, 2023

--

Welcome to Day 2 of your Python learning journey! Today, we’ll dive into the fundamental building blocks of Python. We’ll cover variables and data types, mathematical operations, and string operations.

Part 1: Variables and Data Types

  1. Variables

In Python, variables are essential components that enable you to store and manipulate data. They serve as placeholders or labels for data values, making it easier to work with information in your programs. To use a variable, you can freely choose a name for it and then assign a value to it.

2. Data Types

Python supports various data types, including:

  • Strings (str): Used for text data, enclosed in single (‘ ‘) or double (“ “) quotes, e.g., “Hello, Python!”
  • Integers (int): Integers are used for whole numbers, like 5, -10, or 1000. You can convert other data types, such as strings, to integers using the int() function. For example, int("42") would convert the string "42" into the integer 42.
  • Floats (float): Floats are used for decimal numbers, such as 3.14, -0.5, or 2.0. When receiving user input as text, you can convert it to a floating-point number using the float() function.
  • Booleans (bool): Used for representing boolean values, which can be either Trueor False. Booleans are typically used in conditional statements and logical operations.

Example: Variable declaration and assignment

# An integer variable
my_integer = 5

# A floating-point variable
my_float = 3.14

# A string variable
my_string = "Hello, Python!"

# Boolean variable
is_true = True
is_false = False

Note: Python has comments; code written in comments will not be executed when the program is run.

Example: Python comments

# This is a single-line comment (started with #)

'''
This is
a multi-line comment
(starts and ends with triple quotes)
'''

Part 2: Mathematical Operations

Python provides a set of operators to perform basic mathematical operations. Let’s take a closer look at these operators:

  1. Addition (+)

Use the plus operator to add two numbers, e.g., 5 + 3 results in 8.

You can also use the += operator for addition and assignment, e.g., x += 3 is equivalent to x = x + 3.

2. Subtraction (-)

The minus operator subtracts one number from another, e.g., 10 - 4 results in 6.

For subtraction and assignment, you can use the -= operator, e.g., x -= 2 is equivalent to x = x - 2.

3. Multiplication (*)

The asterisk operator is used for multiplication, e.g., 4 * 6 results in 24.

You can use the *= operator for multiplication and assignment, e.g., x *= 5 is equivalent to x = x * 5.

4. Division (/)

The forward slash operator performs division, e.g., 8 / 2 results in 4.0 (a float).

For division and assignment, you can use the /= operator, e.g., x /= 2 is equivalent to x = x / 2.

5. Modulus (%)

  • The modulus operator (%) returns the remainder of a division operation, e.g., 7 % 3 results in 1.
  • For modulus and assignment, you can use the %= operator, e.g., x %= 4 is equivalent to x = x % 4.

6. Exponentiation (**)

  • The double asterisk operator (**) is used for exponentiation, e.g., 2 ** 3 results in 8.
  • You can use the **= operator for exponentiation and assignment, e.g., x **= 2 is equivalent to x = x ** 2.

Comparison Operators:

In addition to these mathematical operators, Python also provides comparison operators, which allow you to compare values. Here are some common comparison operators:

  • Greater Than (>): Used to check if one value is greater than another, e.g., 5 > 3 evaluates to True.
  • Less Than (<): Checks if one value is less than another, e.g., 2 < 7 evaluates to True.
  • Greater Than or Equal To (>=): Checks if one value is greater than or equal to another, e.g., 5 >= 5 evaluates to True.
  • Less Than or Equal To (<=): Checks if one value is less than or equal to another, e.g., 4 <= 5 evaluates to True.
  • Equal To (==): Used to test if two values are equal, e.g., 3 == 3 evaluates to True.
  • Not Equal To (!=): Checks if two values are not equal, e.g., 5 != 7 evaluates to True.

These comparison operators are essential for making decisions in your Python programs, as you’ll see when we explore conditional statements later in this guide.

Now, let’s try an example,

please give it a try in your code editor:

result = 5 + 3
print("The result is:", result)

When you run this code, the result will be displayed as follows:

The result is: 8

Congratulations if you’ve successfully displayed the result; it means you’ve tried it out!

Again:

result = 5 > 3
print("Is 5 greater than 3?", result)

When you run this code, the result will be displayed as follows:

Is 5 greater than 3? True

Part 3: String Operations

Python’s string operations allow you to work with text data efficiently. Here are some essential string operations:

  1. Combining Strings:

You can concatenate (join) two or more strings together using the + operator. For example:

first_name = "Bella"
last_name = "Hadid"
full_name = first_name + " " + last_name
print(full_name)

result: Bella Hadid

2. Slicing Strings:

You can extract specific portions of a string using slicing. For instance:

text = "Python is amazing!"
substring = text[7:13]
print(substring)

result: is ama

3. Formatting Strings:

Python provides various methods for formatting strings. One common method is using f-strings (formatted string literals):

name = "Alice"
age = 18
formatted_text = f"Hello, my name is {name} and I am {age} years old."
print(formatted_text)

result: Hello, my name is Alice and I am 18 years old.

Now that you’ve learned about variables, data types, mathematical operations, and string operations, you’re equipped with the fundamental knowledge needed to start writing more complex Python programs. These concepts serve as the foundation for your Python journey, and you’ll find them essential as you progress. Stay tuned for Day 3, where we’ll explore control flow and decision-making in Python.

--

--