Python is a powerful and flexible programming language used in everything from web development and automation to data science and artificial intelligence. Its simplicity and readability make it accessible to beginners, while its depth keeps professionals engaged. But as with any skill, if you don’t practice it regularly, it can begin to fade.
If you’ve been away from Python for a while, this blog post is your quick refresher which covering the most important core concepts through both theory and practical examples. Let’s dive back in and bring your Python skills back to life.
Variables
A variable in Python is simply a name that points to a value in memory. Variables must:
- Start with a letter or underscore
- Only contain letters, numbers, or underscores
- Be case-sensitive (
Name
andname
are different) - Not conflict with reserved keywords like
if
,class
, ordef
Two common naming conventions:
snake_case
: Preferred for variable and function namescamelCase
: Less common, but valid
Numbers
Python supports several numeric types:
- Integers (e.g.,
10
) - Floats (e.g.,
3.14
) - Scientific notation (e.g.,
1.2e3
interpreted as1200.0
) - Binary representation (e.g.,
bin(9)
gives'0b1001'
)
Note: "3320"
is a string, not a number, because it’s enclosed in quotes.
Strings
Strings are ordered sequences of characters and can be manipulated in many ways:
- Indexing starts at
0
- Use
.title()
to capitalize each word:"hello python!" → "Hello Python!"
- Check substrings with
"Candy" not in text
- Use
len()
to get string length - Combine slicing and replacing:
string[:4].replace("Soft", "Skill")
gives"Skill"
from"Software"
Lists
Lists are ordered and mutable, meaning you can change their contents. They can hold any data type — even a mix.
Examples:
- Slicing:
movies[-3:]
gives the last three items - Concatenation:
list1 + list2
combines both into a new list
Lists are great for storing collections, manipulating sequences, and organizing data.
Conditionals
Conditionals control the flow of logic:
- Use
if
,elif
, andelse
- Use the ternary operator:
grade = "A" if score >= 80 else "B"
- Check ranges:
if bp >= 80 and bp <= 120:
- Compare values:
if company != 'banana':
These help in making decisions within programs.
Loops and Control Flow
Loops allow repetitive tasks:
range(1, -6, -2)
gives1, -1, -3, -5
continue
skips the current iterationwhile
loops run as long as a condition is true- Use counters inside loops for tasks like counting non-fraud entries in a dataset
These structures make your code efficient and reduce redundancy.
Functions
Functions are reusable blocks of code that perform specific tasks:
- Defined using
def
keyword - If no
return
, Python returnsNone
by default - Promotes modular and readable code
Example:
def greet(name):
return f"Hello, {name}!"
Dictionaries and Tuples
- Dictionaries store key-value pairs
Use.items()
to iterate through them
Access nested values like:salaries['php']['senior']
- Tuples are similar to lists but immutable
You can’t change their values once created
Dictionaries are great for labeled data, while tuples are useful for fixed collections.
Modules and Imports
Modules let you organize code into multiple files:
- To reuse a function from
calculate.py
:import calculate result = calculate.multiply(3, 5)
- Use the
import
keyword to bring in built-in or custom modules
Modular code is easier to manage, test, and reuse.
File Handling
Python makes reading and writing files simple:
- Use
.close()
to ensure data is saved and the file is released - Open a file in append mode with:
open("file.txt", "a")
- Use
.readlines()
to read multiple lines - Avoid invalid modes like
xr
; use valid ones:
'r'
,'w'
,'a'
,'x'
, and combinations with'b'
or'+'
File handling is essential for data storage and interaction with external files.
Classes and Special Methods
Python supports object-oriented programming through classes:
__init__()
is the constructor__str__()
defines string representation__ne__()
allows customizing!=
self
refers to the current instance of the class
Example:
class Car:
def __init__(self, model):
self.model = model
def __str__(self):
return f"My car is a {self.model}"
Inheritance and Object-Oriented Concepts
Inheritance allows a class to inherit attributes and methods from another:
- A subclass can override parent methods
- Use
super()
to call the parent class constructor
Example behavior:
Starting the car ...
Three Class is being driven..
Stopping the car ...
This promotes code reuse and logical hierarchy.
Exception Handling
Python handles errors using try
, except
, and finally
:
try
: code that might failexcept
: handles the errorfinally
: always runs, ideal for cleanup
Example:
try:
result = x / int(y)
except Exception as e:
print("Error:", e)
finally:
print("Done")
This ensures your programs are robust and user-friendly.
Take a few minutes each day to write small programs, revisit forgotten concepts, and most importantly keep coding. Python is easy to return to, and the more you practice, the more confident and capable you’ll become. You can find practical examples and practice code based on the above concepts in the following GitHub repository.
Explore the examples and start practicing here:
GitHub Repo: https://github.com/jubayer98/refresh-recall-python
These code samples align with each topic covered: from variables and loops to functions, file handling, and object-oriented programming and making it easier to apply what you’ve just refreshed.
Happy Coding!