๐ Introduction to Python
Python is a high-level, interpreted programming language known for its readability and versatility. It's widely used for web development, data analysis, automation, and more.
Installation Steps:
- Visit python.org/downloads/
- Download the latest version
- Run installer and add Python to PATH
- Verify:
python --version
๐ข Basic Data Types
Integer (int)
Whole numbers without decimal points
Float
Numbers with decimal points
Boolean
True or False values
String
Sequences of characters
List
Ordered collections of items
โก Variables, Expressions & Statements
Variables
Store data values and are created when you assign a value to them.
Expressions
Combinations of values, variables, and operators that compute a value.
Statements
Instructions that the Python interpreter can execute.
Operator Precedence
- Parentheses ()
- Exponentiation **
- Multiplication, Division *, /, //, %
- Addition, Subtraction +, -
- Comparison ==, !=, >, <, >=, <=
- Logical and, or, not
๐ฌ Comments
Comments explain code and are ignored by the interpreter.
๐ Indentation in Python
Python uses indentation to define code blocks. The recommended style is 4 spaces per level. Be consistent and avoid mixing tabs and spaces to prevent IndentationError.
Correct indentation (4 spaces)
hostname = "Switch01"
port_count = 48
if port_count > 24:
print(f"{hostname} is a high-density switch")
for vlan in [10, 20, 30, 40]:
print(f"Configuring VLAN {vlan}")
print("Done")
Incorrect indentation (raises IndentationError)
hostname = "Switch01" if True: print(hostname) # โ Not indented under if
Practical snippet (well-indented)
# Examples Related to Cisco Network Topology
# Example 1: Displaying Switch Details
hostname = "Switch01"
ip_address = "192.168.1.1"
port_count = 48
vlans = [10, 20, 30, 40]
is_active = True
print(f"Hostname: {hostname}")
print(f"IP Address: {ip_address}")
print(f"Port Count: {port_count}")
print(f"VLANs: {vlans}")
print(f"Is Active: {is_active}")
๐ง MidโSession Quiz
- Which data type is a sequence of characters?
- Which line creates a variable named
ip?
- Which is an expression?
- Which is a proper singleโline comment in Python?
- Indentation per level should typically be:
๐ฎ Interactive Demo: Data Types
Try different data types:
๐ง Functions
What is a Function?
A function is a block of code that performs a specific task. Functions help in reusability and organization of code.
Defining a Function
Calling a Function
Parameters and Arguments
๐ Flow of Execution
The flow of execution refers to the order in which statements are executed. In Python, execution starts from the first line of the script and proceeds sequentially. When a function is called, the flow jumps to the function, executes its code, and then returns to the next line after the function call.
Example: Network Topology
โถ๏ธ Run the code (Python in your browser)
You can execute Python directly below using Pyodide. Edit the code and click Run.