DEV Community

Cover image for Python Fundamentals
Mugendi Mung'athia
Mugendi Mung'athia

Posted on

Python Fundamentals

Introduction

I recently started learning Python, and like most beginners, I started with the basics.

At first, it was just print(), variables, and taking input from the user. Then I started doing calculations and building small programs from what I had learned.

In this article, I'll share some of those early Python exercises and explain what I learned along the way. I'll also finish with a small Kiosk Receipt program that brings several of these concepts together.

1. print()

The first thing I wrote was print().

It is used to display something on the screen:

print("Hello World!")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!
Enter fullscreen mode Exit fullscreen mode

We can also print multiple values:

print("Noon", "Moon")
Enter fullscreen mode Exit fullscreen mode

Output:

Noon Moon
Enter fullscreen mode Exit fullscreen mode

Python automatically adds a space between the two values.

We can also use \n to move to a new line:

print("Hello World!\n")
print("Welcome to Python")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!

Welcome to Python
Enter fullscreen mode Exit fullscreen mode

The \n simply tells Python to start a new line.

We can also add a new line by printing an empty line using print():

print("Hello World!")
print()
print("Welcome to Python")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello World!

Welcome to Python
Enter fullscreen mode Exit fullscreen mode

Here, print() without anything inside the brackets simply prints a blank line.

2. Variables

After learning how to display things with print(), the next thing was how to store information.

In Python, we do this using variables.

For example:

name = 'John'
age = 30
track = 'Data Science'
Enter fullscreen mode Exit fullscreen mode

Here, name stores the text 'John', while age stores the number 30.

We can then use these variables with print():

print(name)
print(age)
print(track)
Enter fullscreen mode Exit fullscreen mode

Output:

John
30
Data Science
Enter fullscreen mode Exit fullscreen mode

We can also use variables together:

print(name, 'is', age, 'years old.')
Enter fullscreen mode Exit fullscreen mode

Output:

John is 30 years old.
Enter fullscreen mode Exit fullscreen mode

For variables, we don't have to keep writing the actual values. We can give the value a name and use that name whenever we need it.

3. Changing the Value of a Variable

One thing I quickly learned is that a variable doesn't have to keep the same value.

For example, I started with 8 sessions remaining:

sessions_remaining = 8

print(sessions_remaining)
Enter fullscreen mode Exit fullscreen mode

Output:

8
Enter fullscreen mode Exit fullscreen mode

After completing one session, I can update the variable:

sessions_remaining = sessions_remaining - 1

print(sessions_remaining)
Enter fullscreen mode Exit fullscreen mode

Output:

7
Enter fullscreen mode Exit fullscreen mode

Python takes the current value of sessions_remaining, subtracts 1, and stores the new value back in the same variable.

Variables are not just containers for information, they can also change as our program runs.

4. Strings and Numbers

Python treats text and numbers differently.

For example:

number = '7'

print(number + number)
Enter fullscreen mode Exit fullscreen mode

Output:

77
Enter fullscreen mode Exit fullscreen mode

Why 77 instead of 14?

Because '7' is a string, not a number. Python joins the two pieces of text together.

But if we remove the quotation marks:

number = 7

print(number + number)
Enter fullscreen mode Exit fullscreen mode

Output:

14
Enter fullscreen mode Exit fullscreen mode

Now 7 is an integer, so Python performs normal addition.

The quotation marks make a big difference:

'7'   # string
7     # integer
Enter fullscreen mode Exit fullscreen mode

Sometimes code can look almost identical but behave completely differently depending on the data type.

5. Getting Input from the User

So far, I was giving Python the information myself. But what if I want the user to enter something?

That's where input() comes in.

name = input('What is your name? ')

print('Hello', name)
Enter fullscreen mode Exit fullscreen mode

If I enter:

John
Enter fullscreen mode Exit fullscreen mode

The output will be:

Hello John
Enter fullscreen mode Exit fullscreen mode

input() pauses the program and waits for the user to type something.

There is one important thing to remember: input() always gives us a string.

For example:

age = input('What is your age? ')

print(age)
Enter fullscreen mode Exit fullscreen mode

Even if I enter 30, Python receives it as the string '30'.

This becomes important when we want to perform calculations with the user's input.

6. Converting User Input with int()

Since input() gives us a string, we need to convert it when we want to work with numbers.

We can use int() for whole numbers.

For example:

age = int(input('What is your age? '))

print('You are', age, 'years old')
print('Next year you will be', age + 1, 'years old')
Enter fullscreen mode Exit fullscreen mode

If I enter:

30
Enter fullscreen mode Exit fullscreen mode

The output is:

You are 30 years old
Next year you will be 31 years old
Enter fullscreen mode Exit fullscreen mode

The important part is:

int(input())
Enter fullscreen mode Exit fullscreen mode

input() gets the value from the user, while int() converts that value from a string into an integer.

We can do something similar with decimal numbers using float():

height = float(input('Your height in metres: '))

print('Height:', height, 'm')
Enter fullscreen mode Exit fullscreen mode

Here is the simple step: get the input, convert it when necessary, and then use it in our program.

7. Doing Calculations

Once we get numbers from the user, we could start using Python as a calculator.

For example, I can calculate the total cost of buying several items:

price = int(input('Enter the price (Ksh): '))
quantity = int(input('Quantity: '))

subtotal = price * quantity

print(f'Subtotal: Ksh {subtotal}')
Enter fullscreen mode Exit fullscreen mode

If I enter:

Enter the price (Ksh): 200
Quantity: 5
Enter fullscreen mode Exit fullscreen mode

The output is:

Subtotal: Ksh 1000
Enter fullscreen mode Exit fullscreen mode

Python can also handle other common mathematical operations:

print(10 + 5)   # Addition
print(10 - 5)   # Subtraction
print(10 * 5)   # Multiplication
print(10 / 5)   # Division
Enter fullscreen mode Exit fullscreen mode

Output:

15
5
50
2.0
Enter fullscreen mode Exit fullscreen mode

Instead of just displaying information, Python could calculate something useful.

8. Formatting Output with f-Strings

After using print() a few times, I wanted my output to look cleaner. That's when I came across f-strings.

An f-string lets us put variables directly inside a string using {}.

For example:

name = 'John'
age = 30

print(f'{name} is {age} years old.')
Enter fullscreen mode Exit fullscreen mode

Output:

John is 30 years old.
Enter fullscreen mode Exit fullscreen mode

We can also put calculations inside the {}:

print(f'Next year {name} will be {age + 1} years old.')
Enter fullscreen mode Exit fullscreen mode

Output:

Next year John will be 31 years old.
Enter fullscreen mode Exit fullscreen mode

The f before the quotation marks tells Python that we want to insert variables or expressions into the string.

This is much easier to read than joining lots of separate values with commas.

9. Putting Everything Together

At this point, I had learned enough basics to start combining them into small programs.

I could:

  • Store information in variables
  • Get information using input()
  • Convert input using int() and float()
  • Perform calculations
  • Format the output using f-strings

For example, I put some of these ideas together to create a simple student profile:

print('=== Student Registration ===')

name = input('Full name: ')
age = int(input('Age: '))
city = input('City: ')
track = input('Track (DS or DE): ')

print()

print('=== Profile Created ===')
print(f'Name: {name}')
print(f'Age: {age}')
print(f'City: {city}')
print(f'Track: {track}')
Enter fullscreen mode Exit fullscreen mode

If I enter:

Full name: John
Age: 30
City: Nairobi
Track: DS
Enter fullscreen mode Exit fullscreen mode

I get:

=== Profile Created ===
Name: John
Age: 30
City: Nairobi
Track: DS
Enter fullscreen mode Exit fullscreen mode

10. Building a Simple Kiosk Receipt

Now I wanted to put everything together into something practical.

I decided to build a simple Kiosk Receipt program. It asks for the item, price, and quantity, then calculates the subtotal, a 5% discount, 16% VAT, and the final amount.

print('---------- KIOSK RECEIPT --------')

item = input('Enter name of item: ')
price = int(input('Enter price of item: '))
quantity = int(input('Quantity bought: '))

print()

subtotal = price * quantity
discount = 0.05 * subtotal
discounted_amount = subtotal - discount

vat = 0.16 * discounted_amount
total = discounted_amount + vat

print(f'Item: {item}')
print(f'Subtotal: Ksh {round(subtotal, 2)}')
print(f'Discount: Ksh {round(discount, 2)}')
print(f'VAT (16%): Ksh {round(vat, 2)}')
print(f'TOTAL: Ksh {round(total, 2)}')
Enter fullscreen mode Exit fullscreen mode

For example, if I buy 5 bags of sugar at Ksh 200 each:

---------- KIOSK RECEIPT --------
Enter name of item: Sugar
Enter price of item: 200
Quantity bought: 5

Item: Sugar
Subtotal: Ksh 1000
Discount: Ksh 50.0
VAT (16%): Ksh 152.0
TOTAL: Ksh 1102.0
Enter fullscreen mode Exit fullscreen mode

The calculation is:

  • Subtotal: 200 × 5 = Ksh 1,000
  • Discount (5%): 1,000 × 0.05 = Ksh 50
  • After discount: 1,000 − 50 = Ksh 950
  • VAT (16%): 950 × 0.16 = Ksh 152
  • Final total: 950 + 152 = Ksh 1,102

The calculation happens in a simple flow:

Price × Quantity
       ↓
    Subtotal
       ↓
    Discount
       ↓
 Discounted Amount
       ↓
      VAT
       ↓
   Final Total
Enter fullscreen mode Exit fullscreen mode

It's a small program that combines almost everything I've learned so far: variables, user input, type conversion, calculations, and f-strings.

Conclusion

These first exercises have given me a good starting point. I've learned how to use variables, take user input, perform basic calculations, and format output.

Top comments (0)