DEV Community

Cover image for Python- Basics, Fundamental concepts
Mary Ngure
Mary Ngure

Posted on

Python- Basics, Fundamental concepts

What is Python?

First things first, Python is a high level programming language that allows us to give instructions to a computer in a way that is relatively easy for humans to read and understand.

A simple way of defining programming is:
Programming is like writing a recipe. You provide clear, step-by-step instructions, and the computer follows those instructions exactly.

We break a problem into instructions that a computer can understand and execute.

Python makes this process easier for beginners because its syntax is relatively readable.

For example:

print("Hello, Python!")

Even if you're completely new to programming, you can probably guess what this does.

And that's one of Python's biggest advantages.

Why Python?

Python is beginner-friendly, but that's not the only reason it's popular.

Python is used for:

  • Data analysis
  • Artificial intelligence and machine learning
  • Web development
  • Automation
  • Scientific computing
  • General software development

It's also free and open source.

So learning Python isn't just about learning a programming language.

It's learning a tool that can eventually be applied to many different areas of technology.

But before getting there, let's start with the basics.

1. print() — Getting Python to Talk

The first thing I learned was print().

print() is Python's way of displaying something on the screen.

Think of it as telling Python:

"I want you to show this."

For example:

print("Hello, World!")

Python responds:

Hello, World!

Anything we want Python to display goes inside the parentheses.

Text needs quotation marks

print("I am learning Python")

The quotation marks tell Python:

This is text.

We can also use single quotes:

print('I am learning Python')

Both work.

Numbers don't need quotation marks

print(25)
print(100)
print(3.14)

But there's an important difference between:

print(25)

and:

print("25")

The first is a number.

The second is text.

They may look identical when printed, but Python treats them differently.

And that brings us to our next concept.

2. Variables — Giving Information a Name

Imagine I have a box.

Inside the box is the name:

Fatuma

Instead of carrying that box around and saying "the box containing Fatuma," I can put a label on it:

name

That's essentially what a variable does.

name = "Fatuma"

I've created a variable called name and stored "Fatuma" in it.

Now I can use the variable instead of repeatedly writing the name:

print(name)

Output:

Fatuma

We can store many different things in variables:

name = "Fatuma"
age = 25
salary = 50000
city = "Nairobi"

Then:

print(name)
print(age)
print(salary)
print(city)

This is one of the fundamental ideas behind programming:

Store information → work with that information → produce an output.

A Small Detail About =

One thing beginners often misunderstand is the equals sign.

In:

age = 25

= doesn't mean "age is mathematically equal to 25."

It means:

Assign the value 25 to the variable age.

This is called assignment.

Naming Variables in Python

Python has a few rules for variable names.

This is valid:

first_name = "Fatuma"
age = 25
monthly_salary = 50000

This isn't:

2name = "Fatuma"

because a variable cannot start with a number.

This also isn't valid:

first name = "Fatuma"

because spaces aren't allowed in variable names.

Instead:

first_name = "Fatuma"

Python variable names are also case-sensitive.

That means:

age
Age
AGE

are three different variable names.

3. f-Strings — A Cleaner Way to Display Variables

Once we have variables, we'll often want to combine them with text.

For example:

name = "Fatuma"
age = 25

print("My name is", name, "and I am", age, "years old.")

This works, but Python gives us a cleaner and more readable option: f-strings.

An f-string allows us to insert variables directly inside a string.

We add an f before the quotation mark and place the variable inside curly braces {}.

name = "Fatuma"
age = 25

print(f"My name is {name} and I am {age} years old.")

Output:

My name is Fatuma and I am 25 years old.

The {name} tells Python:

Insert the value stored in the name variable here.

And {age} tells Python:

Insert the value stored in age here.

Why are f-strings useful?

They make output easier to read and make our code cleaner, especially when we're working with several variables.

Compare:

print("Hello", name, "your balance is", balance)

with:

print(f"Hello {name}, your balance is {balance}.")

The second version reads much more naturally.

We can also put expressions inside f-strings

For example:

price = 100
quantity = 3

print(f"Total cost: {price * quantity}")

Output:

Total cost: 300

This becomes particularly useful when building interactive programs.

4. Data Types — Python Cares About What Your Data Is

Here's something important I learned:

Not all data is the same.

For example:

age = 25

and:

age = "25"

look almost identical.

But Python sees them differently.

The first is a number.

The second is text.

Python has different data types that tell it what kind of information it's dealing with.

Four basic types beginners should know are:

str — String

Text.

name = "Mary"

int — Integer

Whole numbers.

age = 25

float — Floating-point number

Numbers containing decimals.

price = 99.99

bool — Boolean

Either True or False.

is_logged_in = True

We can even ask Python what type something is:

age = 25

print(type(age))

Python gives us: <class 'int'>

Why Data Types Matter

This is where things get interesting.

Python can add numbers:

10 + 5

Result:

15

But strings behave differently.

"Hello" + "World"

Result:

HelloWorld

Here, + doesn't mean mathematical addition.

It joins the two pieces of text together.

This is called string concatenation.

But what happens here?

5 + "Hello"

Python raises a TypeError.

Why?

Because we're trying to perform an operation between an integer and a string.

Python needs to know what we're working with before it can determine what operation makes sense.

And this becomes particularly important when we start taking information from users.

5. input() — Making Programs Interactive

Until now, we've been telling Python exactly what information to use.

But what if we want the user to provide the information?

That's where input() comes in.

For example:

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

When the program runs, Python asks:

What is your name?

The user might enter:

Mary

Python stores that answer in the variable name.

We can then use it with an f-string:

print(f"Hello {name}!")

Output:

Hello Mary!

This is much cleaner than manually joining several pieces of text.

⚠️ The input() Trap Beginners Should Know

There's an important detail here.

input() returns a string.

Consider:

age = input("How old are you? ")

If the user enters:

25

Python sees:

"25"

not:

25

That matters if we want to perform calculations.

For example:

age = input("How old are you? ")

print(age + 5)

This will cause an error.

Why?

Because Python is essentially trying to do:

"25" + 5

Text + number = problem.

The solution is type conversion.

age = int(input("How old are you? "))

Now the input is converted from a string into an integer.

For decimal numbers, we can use float():

price = float(input("Enter the price: "))

6. Arithmetic — Turning Python Into a Calculator

Python can perform basic mathematical operations.

Addition

total = 10 + 5

Subtraction

difference = 10 - 5

Multiplication

product = 10 * 5

Division

result = 10 / 5

Exponents

result = 2 ** 3

Modulo %

The % operator gives us the remainder after division.

result = 10 % 3

print(result)

Output:

1

Because 10 divided by 3 leaves a remainder of 1.

Modulo becomes particularly useful later when working with conditions and loops.

Putting Everything Together

Now let's combine the concepts we've learned into a small savings calculator.

name = input("What is your name? ")
monthly_savings = float(input("How much do you save per month? "))
months = int(input("How many months will you save? "))

total_savings = monthly_savings * months

print(f"\nHello {name}!")
print(f"If you save {monthly_savings} per month for {months} months,")
print(f"your total savings will be {total_savings}.")

A possible interaction:

What is your name? Mary
How much do you save per month? 5000
How many months will you save? 6

Hello Mary!
If you save 5000.0 per month for 6 months,
your total savings will be 30000.0.

Now we've combined:

input() to collect information
Variables to store information
Data types to represent different kinds of values
Type conversion using int() and float()
Arithmetic to calculate the result
f-strings to create readable output
print() to display the result

That's a lot of programming concepts packed into a very small program.

🧠 The Mental Model I'm Taking Away

One thing I'm realizing while learning Python is that programming isn't really about memorising syntax.

It's about learning how to break a problem down.

When faced with a programming problem, I can ask:

  1. What information do I need?

Maybe a name, price, age, score, or quantity.

  1. Where will I store it?

Variables.

  1. What type of information is it?

str, int, float, or bool.

  1. What do I need to do with it?

Maybe calculate, compare, combine, or transform it.

  1. What should the user see?

print() and f-strings can help create the output.

This gives me a simple programming pattern:

Input → Store → Process → Output

And that pattern is much more valuable than memorising isolated Python commands.

The code gets longer as you progress, but the fundamental idea remains the same:

Give the computer clear instructions, one step at a time.

Top comments (0)