Basic Syntax and Control Structures in Python

Python is a popular programming language known for its simplicity and readability. This article will explore the basic syntax and various control structures available in Python, including if-else statements, loops, and more.

1. Python Syntax Basics

Indentation

Unlike many other programming languages that use braces or brackets to define blocks of code, Python uses indentation. Indentation is vital in Python as it determines the grouping of statements. By convention, four spaces or one tab is used for indentation.

if condition:
    # Code block inside the if statement
elif condition:
    # Code block inside the elif statement
else:
    # Code block inside the else statement

Comments

Comments are used to add explanatory notes to the code and are ignored by the interpreter. In Python, single-line comments start with a # symbol, whereas multi-line comments are enclosed between triple quotes (''' or """).

# This is a single-line comment

'''
This is
a multi-line
comment
'''

Variables and Data Types

In Python, variables are created by assigning a value to them without explicitly declaring their type. Python supports various data types, including integers, floats, strings, booleans, lists, tuples, and dictionaries.

# Integer
number = 10

# Float
pi = 3.14

# String
name = "John Doe"

# Boolean
is_valid = True

# List
fruits = ['apple', 'banana', 'orange']

# Tuple
point = (3, 5)

# Dictionary
person = {'name': 'John', 'age': 25}

2. Control Structures

If-Else Statement

The if-else statement allows us to execute a block of code based on a condition. If the condition is true, the code block under the if statement is executed. Otherwise, the code block under the else statement is executed.

age = 18

if age >= 18:
    print("You are eligible to vote!")
else:
    print("You are not eligible to vote yet.")

Loops

Python provides two types of loops: for and while.

For Loop

The for loop is used to iterate over a sequence (such as a list, tuple, or string) or other iterable objects for a specific number of times.

fruits = ['apple', 'banana', 'orange']

for fruit in fruits:
    print(fruit)

While Loop

The while loop repeatedly executes a block of code as long as the specified condition is true.

count = 0

while count < 5:
    print(count)
    count += 1

Break and Continue

The break statement is used to exit a loop prematurely. It is typically used when a certain condition is met.

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        break
    print(number)

The continue statement is used to skip the rest of the code in the current iteration and move to the next one.

numbers = [1, 2, 3, 4, 5]

for number in numbers:
    if number == 3:
        continue
    print(number)

Conclusion

Understanding the basic syntax and control structures in Python is essential for building any program. With the knowledge gained from this article, you are now equipped to start writing Python code using if-else statements, loops, and other control structures. Keep practicing, and you will soon become a proficient Python developer!


noob to master © copyleft