DEV Community

Cover image for The Foundations of Python
PHILIP KAPLONG
PHILIP KAPLONG

Posted on

The Foundations of Python

Introduction

Python is a simple, powerful, and widely used programming language. For anyone starting their programming journey, understanding its foundations is essential.

In this article, we’ll cover the basic building blocks of Python, including variables, data types, operators, conditions, loops, and functions. These concepts form the foundation you need before moving into more advanced areas like data analysis, automation, and AI.

What is Python?

Python is a high level programming language that is used to give the computer instructions.
"Think of it like writing a recipe. You write clear, step-by-step instructions and the computer follows them exactly”

Why is python good for beginners?

  • Reads almost like English (easier to understand)
  • Can build websites, analyse data, build AI models, automate tasks
  • Free and open source
  • One of the most in-demand skills in tech jobs today

1. Printing Output with print()

print() is Python’s way of showing you something on the screen

“Think of it like sending a text message - you tell Python what to say and it says it”

Everything you want to display/show goes INSIDE the brackets ().

Text must go inside quotes. Numbers DO NOT need quotes.

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

print() tells Python to display something on the screen.

The output will be:

Hello World
Enter fullscreen mode Exit fullscreen mode


python

2. Understanding Variables

Variable is a labelled box where you store a piece of information

Instead of writing ‘Philip’ every time you need the name, you store it once in a box called name

Then you use name wherever you need it

You create a variable with = (a single equals sign)
name = ‘Philip’ → means: create a box called name and put ‘Philip’ inside it.

name = "Philip"

print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

Philip
Enter fullscreen mode Exit fullscreen mode

Python variable naming rules:

  • Can contain letter, digits and underscore(_)
  • Must start with a letter or underscore - cannot start with a digit
  • Cannot contain spaces or special character (-, @, !,%)
  • Cannot be reserved keyword
  • Case-sensitive (age , Age and AGE) are three different variables

3. Understanding Data Types

Python treats different kinds of data differently

You can add two numbers: 10 + 5 = 15

You can join two text: ‘Hello’ + ‘World’ = ‘Hello World’

But you CANNOT add a number and text: 5 + ‘hello’ → ERROR

→ Python needs to know WHAT TYPE of data something is so it knows what to do with it

The Four main types of data : *string, integer, float, boolean *

String

name = "Philip"
print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

Philip
Enter fullscreen mode Exit fullscreen mode

Integer

age = 25
print(age)
Enter fullscreen mode Exit fullscreen mode

Output:

25
Enter fullscreen mode Exit fullscreen mode

Float

height = 5.9
print(height)
Enter fullscreen mode Exit fullscreen mode

Output:

5.9
Enter fullscreen mode Exit fullscreen mode

Boolean

is_student = True
print(is_student)
Enter fullscreen mode Exit fullscreen mode

Output:

True
Enter fullscreen mode Exit fullscreen mode

Adding two numbers

print(10 + 5)
Enter fullscreen mode Exit fullscreen mode

Output:

15
Enter fullscreen mode Exit fullscreen mode

Joining two strings

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

Output:

Hello World
Enter fullscreen mode Exit fullscreen mode

Mixing a number and text

print(5 + "Hello")
Enter fullscreen mode Exit fullscreen mode

Output:

TypeError
Enter fullscreen mode Exit fullscreen mode

4. Getting User Input with input()

So far Python has been talking to YOU (print).
input() lets the USER talk to python
When python hits input(), it stops and waits. The user types something and presses** Enter*.

Whatever the user typed is returned as a **STRING ALWAYS
* even if the user types a number. If you need a number, you must convert it: int(input(‘enter a number’))

name = input("What is your name? ")
print(name)
Enter fullscreen mode Exit fullscreen mode

Output:

What is your name? Philip
Philip
Enter fullscreen mode Exit fullscreen mode

input() always returns the user's answer as a string, even when they enter a number.

age = input("How old are you? ")
print(age)
Enter fullscreen mode Exit fullscreen mode

Output:

How old are you? 25
25
Enter fullscreen mode Exit fullscreen mode

If you need the input as a number, convert it using int():

age = int(input("How old are you? "))
print(age + 5)
Enter fullscreen mode Exit fullscreen mode

Output:

How old are you? 25
30
Enter fullscreen mode Exit fullscreen mode

Top comments (0)