Basics of Python Programming!

| | 5 min read

Quick summary

  • Store and work with data: variables and basic types provide the building blocks of Python programs.
  • Control execution: conditions and loops determine which steps run and when they repeat.
  • Organise reusable logic: functions group code around a specific task.
  • Use files and libraries: reading, writing and importing modules extend what a program can do.

Python is a popular programming language that is known for its simplicity, readability, and versatility. In this post, we’ll cover the fundamental concepts of Python programming that will help you get started with building your own programs.

Data types and Variables

In Python, there are several data types that you can work with, including integers, floating-point numbers, strings, and lists. Variables are used to store data values in a program. Here are some examples:

# Integer variable
age = 25

# Floating-point variable
price = 12.50

# String variable
name = "John"

# List variable
my_list = [1, 2, 3, 4, 5]

Control Flow

Control flow is used to determine the order in which statements are executed in a program. In Python, you can use if/else statements and loops to control the flow of your program. Here are some examples:

# If statement
if age >= 18:
    print("You are an adult")
else:
    print("You are a minor")

# While loop
i = 1
while i <= 5:
    print(i)
    i += 1

# For loop
for item in my_list:
    print(item)

Functions

Functions are used to group together a set of statements that perform a specific task. In Python, you can define your own functions and call them later in your program. Here’s an example:

# Define a function
def greet(name):
    print("Hello, " + name)

# Call the function
greet("John")

File Handling

File handling is used to read and write data from files in a program. In Python, you can use the open() function to open a file, and the read() and write() methods to read and write data to the file. Here’s an example:

# Open a file
file = open("data.txt", "w")

# Write data to the file
file.write("Hello, world!")

# Close the file
file.close()

Modules and Packages

Modules and packages are used to extend the functionality of Python by providing additional features and tools. In Python, you can use the import statement to import modules and packages into your program. Here’s an example:

# Import the math module
import math

# Use the sqrt function from the math module
x = math.sqrt(25)
print(x)

Conclusion

These are the fundamental concepts of Python programming that you need to know to get started with building your own programs. With these concepts, you can start building more complex programs and take advantage of the vast library of modules and packages available for Python. Happy coding!

Turn the basics into one small program

Individual examples become easier to understand when they contribute to a complete task. Try a hypothetical program that reads a few temperatures, converts them and prints a summary. This combines values, collections, loops and functions without requiring a large application. Start with values written directly in the program before adding keyboard input or files.

Give each variable a name that explains its purpose. A name such as temperatures_celsius communicates more than data. Likewise, a function called celsius_to_fahrenheit makes the direction of conversion clear. Descriptive names reduce the amount of explanation needed when you return to the program later.

def celsius_to_fahrenheit(celsius):
    return celsius * 9 / 5 + 32

temperatures_celsius = [0, 10, 20]

for temperature in temperatures_celsius:
    converted = celsius_to_fahrenheit(temperature)
    print(f"{temperature} C = {converted} F")

The function returns a value rather than printing it. That distinction makes the calculation reusable: another part of the program can display the result, save it or compare it with a threshold. Separating calculation from presentation is a small habit that keeps programs understandable as they grow.

Understand types and common mistakes

Python treats a string containing digits differently from a numeric value. Input from input() is text, so a program must convert it before doing arithmetic. A conversion can fail if the user enters something unexpected. Handle the specific failure and explain what input is required instead of hiding every exception behind a generic message.

Indentation is part of Python’s structure. Statements inside a loop or condition must line up consistently. If a statement runs too often or not at all, check which block contains it. Reading the code aloud as a sequence of decisions can reveal an indentation mistake more quickly than adding extra conditions.

Remember that lists are mutable. Appending an item changes the existing list, and two variables can refer to that same list. When an exercise asks you to preserve the original collection, create an appropriate copy or build a separate result. This avoids surprises when another part of the program uses the original data later.

Use files deliberately

The earlier file example demonstrates opening, writing and closing a file. For everyday code, a with statement makes closing the file more reliable when an error occurs. Also choose the file mode carefully: writing with "w" replaces existing contents, while appending with "a" adds to them. Practise with a disposable file so those differences are visible.

When an error appears, read the final exception message and the relevant line in the traceback. Reproduce the problem with the smallest input that still fails. Then check one assumption at a time: the type of a value, the contents of a list or the location of a file. Random edits often hide the cause without improving understanding.

The official Python tutorial provides a structured reference for these concepts. For another small exercise, try squaring a sorted array. Predict the output before running the code, test an empty input and explain each step in your own words. That process builds a stronger foundation than memorising syntax alone.

The post Basics of Python Programming! appeared first on Alpesh Kumar.

Subscribe to Our Newsletter

We don’t spam! Read our privacy policy for more info.