Read and Interpret Simple Python Scripts
In-Depth Guide with Examples
1. Basic Python Syntax
Indentation and Code Blocks:
Python uses indentation (spaces/tabs) to define blocks, not curly braces.
Example:
Comments (#):
Example:
if 5 > 3:
print("Five is greater than three") # Indented block
# This is a comment
print("Hello, John")
2. Variables and Data Types
| Type | Example |
|---|---|
| int | age = 28 |
| float | height = 1.75 |
| string | name = "John" |
| boolean | is_active = True |
| list | skills = ["Python", "Networking", "Linux"] |
| dictionary | person = {"name": "John", "age": 28} |
3. Operators
| Type | Operators | Example |
|---|---|---|
| Arithmetic | +, -, *, / | x = 10 + 5 y = 7 * 3 |
| Comparison | ==, !=, <, > |
print(5 == 5) # True
print(5 < 3) # False
|
| Logical | and, or, not |
a = True
b = False
print(a and b) # False
print(a or b) # True
print(not a) # False
|
4. Input and Output
print(): Outputs to the screen.
print("Hello, John")
input(): Gets input from the user as a string.
name = input("Enter your name: ")
print("Hello,", name)
5. Control Flow Statements
if, elif, else: Controls execution based on conditions.
age = 28
if age < 18:
print("Minor")
elif age < 65:
print("Adult")
else:
print("Senior")
6. Loops
for loops: Iterate over lists or ranges.
for skill in skills:
print(skill)
for i in range(3):
print(i) # 0, 1, 2
while loops: Repeat as long as a condition is True.
count = 0
while count < 3:
print("Count is", count)
count += 1
7. Functions
Defining and Calling a Function:
def greet(name):
print("Hello,", name)
greet("John")
With Parameters and Return Values:
def add(a, b):
return a + b
result = add(5, 7) # result is 12
8. Basic Error Handling
try-except Block: Handles errors gracefully.
try:
num = int(input("Enter a number: "))
print(10 / num)
except Exception as e:
print("Error:", e)
9. Importing Modules
import Statement: Adds external libraries/functions.
import math
print(math.sqrt(16)) # 4.0
10. Reading and Understanding Code Structure
Example Script:
# Greet user and display their skills
def greet(name, skills):
print("Hello,", name)
print("Your skills are:")
for skill in skills:
print("-", skill)
user = {"name": "John", "skills": ["Python", "Networking"]}
greet(user["name"], user["skills"])
Interpretation:
- The function
greetis defined first. - A
userdictionary is created. - The function is called with values from the dictionary.
11. Common Built-in Functions
| Function | Purpose | Example |
|---|---|---|
| len() | Get length of a list or string | print(len(user["skills"])) # 2 |
| range() | Generate a sequence of numbers |
for i in range(3): print(i) # 0, 1, 2 |
| type() | Check data type | print(type(user)) # <class 'dict'> |
12. Simple Data Manipulation
String Operations:
s = "John"
print(s.upper()) # "JOHN"
print(s.lower()) # "john"
List Indexing and Slicing:
skills = ["Python", "Networking", "Linux"]
print(skills[0]) # "Python"
print(skills[1:3]) # ["Networking", "Linux"]
When and How to Use
- Use Python scripts for automation, data processing, network device configuration, simple tools, and more.
- Reading/interpreting scripts is critical for troubleshooting, code review, and collaborative development.
Key Points and Exam Tips
- Indentation is crucial—never mix tabs and spaces.
- Understand basic data types and operators.
- Be able to read I/O, conditionals, loops, and functions.
- Identify purpose of code blocks and overall script flow.
- Common errors: indentation, type mismatch, division by zero.
- Practice predicting script output and debugging real code.
Practice Exercise:
Answer: Total: 12
numbers = [2, 4, 6]
sum = 0
for n in numbers:
sum += n
print("Total:", sum)
What will this script output?Answer: Total: 12