Python Syntax Guide

1. Basic Syntax

Print Statement:

print("Hello, World!")

Variables and Data Types:

# Variable assignment age = 25 # Different data types name = "John" height = 5.9 is_student = True

2. Control Flow

Conditional Statements:

if condition: # code block if condition is True elif another_condition: # code block if another_condition is True else: # code block if none of the conditions are True

Loops:

for item in iterable: # code block to iterate over iterable while condition: # code block while condition is True

3. Functions

Function Definition:

def greet(name): return "Hello, " + name + "!"

Function Call:

result = greet("Alice") print(result)

4. Data Structures

Lists:

my_list = [1, 2, 3, "Python"]

Dictionaries:

my_dict = {"name": "John", "age": 30}

5. Advanced Concepts

Exception Handling:

try: # code that may raise an exception except ExceptionType as e: # handle the exception

Decorators:

def my_decorator(func): def wrapper(): print("Something is happening before the function is called.") func() print("Something is happening after the function is called.") return wrapper

6. Official Documentation Links

Explore the official Python documentation for an in-depth understanding of the language and its features.

0 Comment:

Post a Comment