Python Fundamentals Cheat Sheet for Scripting Beginners

A quick reference guide for essential Python scripting concepts, including data structures, control flow, and function definitions.
Python Fundamentals Cheat Sheet for Scripting Beginners
Welcome to the first part of our Python Scripting Series! In this comprehensive guide, we'll cover the basics you need to start scripting with Python. This includes core data structures, control flow, function definitions, and some best practices to help you write clean and efficient code.
Core Data Structures
Lists
Lists are mutable sequences of items. They are defined using square brackets [].
Example
fruits = ['apple', 'banana', 'cherry']
print(fruits)
# Output: ['apple', 'banana', 'cherry']Dictionaries
Dictionaries are unordered collections of key-value pairs. They are defined using curly braces {}.
Example
student_scores = {'John': 90, 'Jane': 95, 'Mike': 85}
print(student_scores)
# Output: {'John': 90, 'Jane': 95, 'Mike': 85}Sets
Sets are unordered collections of unique items. They are defined using the set() constructor.
Example
unique_fruits = set(['apple', 'banana', 'cherry', 'apple'])
print(unique_fruits)
# Output: {'cherry', 'apple', 'banana'}Tuples
Tuples are immutable sequences of items. They are defined using parentheses ().
Example
coordinates = (40.7128, -74.0060)
print(coordinates)
# Output: (40.7128, -74.0060)Control Flow
If/Elif/Else Statements
Use if, elif, and else to create conditional logic in your scripts.
Example
temperature = 25
if temperature < 0:
print("It's freezing!")
elif temperature >= 0 and temperature < 15:
print("It's cold.")
elif temperature >= 15 and temperature < 25:
print("It's warm.")
else:
print("It's hot.")For Loops
for loops are used to iterate over a sequence (such as a list, tuple, dictionary, set, or string).
Example
for fruit in fruits:
print(fruit)
# Output:
# apple
# banana
# cherryWhile Loops
while loops are used when you want to repeat a block of code as long as a certain condition is true.
Example
count = 0
while count < 3:
print("Count is:", count)
count += 1
# Output:
# Count is: 0
# Count is: 1
# Count is: 2Defining Functions
Functions in Python are defined using the def keyword. They allow you to group a set of statements together to perform a specific task.
Example
def greet(name):
print(f"Hello, {name}!")
greet("Alice")
# Output: Hello, Alice!Best Practices for Clean Syntax
- Use Descriptive Variable Names: Choose meaningful names that reflect the purpose of the variable.
- Consistent Indentation: Use consistent indentation (usually 4 spaces) to improve readability.
- Comments: Add comments to explain complex logic or code sections.
- Error Handling: Use
tryandexceptblocks to handle exceptions gracefully. - Docstrings: Document your functions with docstrings to describe their purpose and parameters.
- Modularity: Break down your code into small, reusable functions.
Example
def calculate_area(radius):
"""
Calculates the area of a circle given its radius.
:param radius: The radius of the circle.
:return: The area of the circle.
"""
import math
try:
area = math.pi * (radius ** 2)
return area
except TypeError:
print("Radius must be a number.")
result = calculate_area(5)
print(result)
# Output: 78.53981633974483Conclusion
In this guide, we've covered the fundamentals of Python scripting, including core data structures, control flow, and defining functions. These concepts are the building blocks for more advanced scripting and automation tasks. Keep practicing and experimenting with these basics to enhance your skills.
Stay tuned for the next part of our Python Scripting Series, where we'll dive deeper into more advanced topics and techniques. Happy scripting!
Tutorial series
Python Scripting Series
A step-by-step Python series for self-hosters and developers: from your first script and a clean environment to databases and local AI APIs.
- 1.Python Fundamentals Cheat Sheet for Scripting Beginners
- 3.Mastering Python Virtual Environments with python3 -m venv
7 more parts coming soon.

Written by Robin te Hofstee
Technology enthusiast with a deep love for programming. Sharing practical guides on React, Next.js, Linux and self-hosting.
Related Posts
Mastering Python Virtual Environments with python3 -m venv
Learn how to use python3 -m venv to create, activate, and manage virtual environments, ensuring clean and isolated development spaces.


