DEV Community

Cover image for Python Basics
Gideon Kiprono
Gideon Kiprono

Posted on

Python Basics

Introduction

Data is the new oil, but Python is the combustion engine. While traditional enterprise tools crash under the weight of modern datasets, Python enables leaders and future professionals to scale operations through automation, predictive analytics, and custom machine learning pipelines.
It is no longer just a programming language, it is the core infrastructure powering algorithmic decision-making in the modern economy.

In this article, we will cover the basics of Python.
This includes;
- Variables
- Data Types
- Input & Output
- Basic Python syntax
- Practical Example

Variables (The Digital Storage Units)

Think of variables as labeled storage tags for data. When you write programs, you constantly need to save information. For example a price, a username, or a calculation result. Variables give you a way to name that information so you can easily find and use it later.
In Python, creating a variable is incredibly straightforward. You simply write the name you want to use, followed by an equals sign (=), and then the data you want to save.

price = 19.09 
username = "Gideon Kiprono"
Enter fullscreen mode Exit fullscreen mode

How It Works

While it is easy to think of a variable as a box holding your data, Python actually treats them like sticky notes.
When you write,
price = 19.99,
Python creates the number 19.99 in the computer's memory, and then sticks the label price onto it. If you decide to change that value later, for example,
price = 24.99,
Python doesn't erase the old number. It simply moves the price label to the new number.

Dynamic Typing

In many other programming languages, you have to tell the computer exactly what kind of data a variable will hold before you can even use it. You have to explicitly state, "This variable will only hold whole numbers," or "This variable will only hold text."
Python is much smarter. It uses a concept called dynamic typing. This means Python looks at the data you provide and figures out what type of information it is automatically.

price = 19.09 #Python automatically knows this is a float number
username = "Gideon Kiprono" #string
Enter fullscreen mode Exit fullscreen mode

This flexibility makes writing code much faster and heavily reduces the amount of setup code you have to type. You can focus purely on solving the problem at hand, rather than managing structural rules.

Data types (The Building Blocks of Information)

Just like we use different tools for different tasks, Python organizes information into different categories called data types. Because Python needs to know how to handle your data, like adding numbers together or combining pieces of text, it assigns a specific type to every piece of information you create.
Here are the four most common data types you will use every day:

Text (Strings)

A string is simply any text data wrapped in quotation marks. It can be a single letter, a full sentence, or even a password. The quotes tell Python to treat the contents as literal text rather than code instructions.

welcome_message = "Welcome to Python!"
Enter fullscreen mode Exit fullscreen mode

Whole Numbers (Integers)

An integer is any whole number without a decimal point. These can be positive or negative. You use integers whenever you need to count distinct items, like items in a shopping cart or website visits.

items = 3
Enter fullscreen mode Exit fullscreen mode

Decimals (Floats)

A floating-point number (or float) is a number that includes a decimal point. You use floats whenever you need precision, such as dealing with money, percentages, or GPS coordinates.

tax_rate = 0.08
Enter fullscreen mode Exit fullscreen mode

True/False States (Booleans)

A boolean represents binary logic and can only have one of two values: True or False. (Note the capital letters!). These act like digital light switches and are vital for helping your program make decisions.

is_logged_in = True
Enter fullscreen mode Exit fullscreen mode

Input and Output (Talking to Your Program )

A program becomes truly useful when it can interact with the outside world. It needs a way to take in information from a user (Input) and a way to display results back to them (Output). Python makes this exchange incredibly easy using two built-in commands.

Output: Showing Results with print()

To display text or variables on the screen, you use the print() function. It is the tool your program uses to speak to the screen.

Modern Python uses a feature called f-strings (formatted strings) to easily mix text and variables together. You simply put an f outside the quotation marks and place your variables inside curly braces {}.

name = "Alex"
print(f"Hello, {name}!")  # Displays: Hello, Alex!
Enter fullscreen mode Exit fullscreen mode

Input: Gathering Data with input()

To ask a user for information, you use the input() function. This command pauses your program and waits for the user to type something and press Enter.

favorite_color = input("What is your favorite color? ")
print(f"Cool! I like {favorite_color} too.")
Enter fullscreen mode Exit fullscreen mode

Note:

  • Python always captures user input as text (a string).
  • If you ask a user for a number to do math with, Python will treat that number like text letters. To fix this, you must wrap the input inside a conversion command like int() (for whole numbers) or float() (for decimals). This process is called type casting.
# Converting text input into a whole number so we can do math
age = int(input("Enter your age: "))
next_year = age + 1
print(f"Next year you will be {next_year} years old.")
Enter fullscreen mode Exit fullscreen mode

Basic Python Syntax

Every language has grammar rules, and programming languages are no different. The rules that govern how you write code are called syntax. Python is famous for having one of the cleanest, most minimalist syntax designs in the world, making it look almost like plain English.
Here are the basic rules you need to know to write valid Python code:

The Power of Whitespace (Indentation)

In many programming languages, you have to use messy curly braces {} or keywords like END to show where a block of code begins and finishes. Python simplifies this by using spaces or tabs (called indentation).
By indenting your code, you visually show Python which lines belong together.

if user_is_happy:
    print("Keep doing what you're doing!")  # Indented line belongs to the 'if' statement
Enter fullscreen mode Exit fullscreen mode

Note: Keeping your spacing consistent is vital. If your lines do not align correctly, Python will pause and show an IndentationError.

Case Sensitivity Matters

Python reads code very literally and pays close attention to capitalization. To Python, a lowercase letter and an uppercase letter are entirely different.

username = "Alex"
Username = "Sam"  # Python treats this as a completely separate variable!
Enter fullscreen mode Exit fullscreen mode

Comments

Sometimes you want to leave notes inside your code to explain how a certain part works. You can do this by using the hashtag symbol (#). Anything you type after a # is completely ignored by the computer.

# This is a comment. Python will skip this line entirely.
price = 10.00  # You can also add comments at the end of a line
Enter fullscreen mode Exit fullscreen mode

Practical example

Now that we have covered variables, data types, inputs, outputs, and basic syntax, let’s bring them all together into a real, working program.

The Digital Receipt Calculator

This simple script acts as a receipt calculator. It asks for a product's price, calculates the sales tax, determines the grand total, and prints a clean summary for the user.

# --- DIGITAL RECEIPT CALCULATOR ---

# 1. Inputs & Data Types
# We ask the user for data. We convert the price to a float (decimal) 
# and the quantity to an int (whole number) so we can do math.
product_name = input("Enter the item name: ")
price = float(input("Enter the item price: $"))
quantity = int(input("Enter the quantity purchased: "))

# 2. Variables & Basic Math
# We create variables to calculate and store the totals.
subtotal = price * quantity
tax_rate = 0.08  # Representing an 8% sales tax
calculated_tax = subtotal * tax_rate
grand_total = subtotal + calculated_tax

# 3. Basic Python Syntax (Decision Making)
# We use an 'if' statement with indentations to check for a discount.
if grand_total > 100.00:
    discount_applied = True
    grand_total = grand_total * 0.90  # Apply a 10% discount
else:
    discount_applied = False

# 4. Output
# We use f-strings to display a cleanly formatted receipt back to the user.
print("\n--- FINAL RECEIPT ---")
print(f"Item: {product_name}")
print(f"Subtotal: ${subtotal:.2f}")
print(f"Tax (8%): ${calculated_tax:.2f}")

if discount_applied:
    print("A 10% discount was applied to your order!")

print(f"Grand Total: ${grand_total:.2f}")
print("---------------------")
Enter fullscreen mode Exit fullscreen mode

Code explanation:

  • Variables: product_name, price, and grand_total hold our information as it moves through the program.
  • Data Types: We mix strings (text names), floats (prices and taxes), integers (quantities), and booleans (True/False for the discount status).
  • Input/Output: input() captures what the user typed, and print() cleanly formats and shares the mathematical results.
  • Basic Syntax: We use hashtags for comments to explain the code, capital letters for True and False, and strict indentation under the if and else lines to control exactly when the discount rules should trigger.

Conclusion

Writing your first lines of Python code is more than just an academic exercise, it is the moment you stop merely consuming technology and start creating it. By understanding how to manipulate variables, manage data types, handle inputs, and structure clean syntax, you have unlocked the foundational blueprint used by software engineers, data scientists, and creators worldwide.
The beauty of Python lies in its scalability. The exact same basic logic you used to build a simple receipt calculator is what powers complex artificial intelligence models, massive data pipelines, and global web applications.

You now have the keys to the language. The best way to master it is to keep building, experimenting, and turning your everyday ideas into working code.

Happy programming!

Top comments (0)