Skip to main content

Chapter 1: Getting started with Python Language

✏️ Section 1.3: Python Basics

Creating Variables and Assigning Values

In Python, variables are created the moment you assign a value to them, and they don't need to be declared with any particular type. Python is dynamically typed, which means that you can reassign variables to different data types as needed. Here’s how you can create variables and assign values:

# Integer assignment
number = 10

# Floating point
pi = 3.14159

# String
message = "Hello, Python!"

Variables in Python are like labels that you can stick on values to refer to them later. When you assign a value to a variable, you're creating a reference to the data stored in memory.

Understanding Python Syntax and Block Indentation

Python syntax is clean and its readability is a fundamental aspect of the design of the language. One of the unique features of Python is its use of indentation to define blocks of code. Unlike other programming languages that use curly braces {} to define blocks of code, Python uses whitespace indentation.

Here is an example to illustrate how Python uses indentation:

# If statement example
if number > 5:
    print("Number is greater than five.")
    if number > 7:
        print("Number is also greater than seven.")
else:
    print("Number is less than or equal to five.")

In this example, the blocks of code inside if and else are defined by their indentation level. All statements with the same distance to the right belong to the same block of code. The code’s readability is significantly enhanced by this simplicity in syntax.

Python’s approach encourages programmers to write clean and readable code, which is essential for maintaining and scaling complex applications.

For a more comprehensive understanding of Python basics, consider exploring the following resources:

By mastering these fundamental concepts, you set a strong foundation for advanced Python programming and application development.