
Embarking on the journey of learning to code in Python is an exciting step towards building everything from simple scripts to complex applications. While the idea of creating software might seem distant when you’re just starting, every powerful program is built layer by layer upon a solid foundation of fundamental concepts.
🎁 Exclusive for Free Subscribers:
Unlock a Free Jupyter Notebook featuring a complete sample project plus full Basic Python Code implementation with step-by-step guidance to help you get started with ease!
📥 Download available at the end of this article — only on SkillWisor!
This article is your essential guide to these core building blocks of Python. We will explore the basic ideas that underpin every line of Python code you will write. While seeing these concepts come to life in a demo application can be incredibly illustrative – showing how user interfaces, data processing, and interactive elements are powered by these very basics – our focus here will be on gaining a deep and thorough understanding of the fundamentals themselves.
Think of this as mastering the alphabet, words, and basic sentence structure before you start writing stories or novels. By the end of this deep dive, you will have a clear picture of Python’s most crucial elements, equipping you with the knowledge needed to confidently read, write, and understand introductory Python code, no matter where it’s applied.
We’ll cover the essential concepts: how Python handles information using variables and data types, how it performs actions using operators, how programs communicate through input and output, how code can make decisions and repeat actions using control flow, how to organize collections of data with data structures, and how to package code into reusable units with functions.
Let’s lay that strong foundation!
Chapter 1: Variables and Data Types – The Building Blocks of Information
At its heart, programming is about processing information. To do this, your program needs to store, refer to, and manipulate data. This is where variables come in.
Variables: Labeled Containers for Values
A variable in Python is essentially a name that refers to a value stored in the computer’s memory. You can think of it as a labeled box where you place a piece of data. The label (the variable name) allows you to easily access and manipulate the data inside the box later in your code.
Creating a variable and putting data into it is called assignment, and it’s done using the single equals sign (=).
Snippet 1.1: Simple Variable Assignment
user_greeting = "Hello there!" # Assigns the string "Hello there!" to user_greeting
number_of_attempts = 3 # Assigns the integer 3 to number_of_attempts
pi_value = 3.14159 # Assigns the float 3.14159 to pi_value
is_logged_in = False # Assigns the boolean False to is_logged_in
user_greeting = "Hello there!": Here, we create a variable nameduser_greetingand store the text"Hello there!"in it.number_of_attempts = 3: This creates a variablenumber_of_attemptsholding the whole number3.pi_value = 3.14159: A variablepi_valueis created to hold the number3.14159, which has a decimal point.is_logged_in = False: A variableis_logged_inis created to hold the truth valueFalse.
Once a variable is created, you can use its name to access the value it holds. You can also reassign a new value to an existing variable, and the old value is discarded.
# Snippet 1.2: Reassigning Variable Values
score = 100
print(score) # Output: 100
score = score + 50 # Calculate 100 + 50, then store the new value 150 back into score
print(score) # Output: 150
player_name = "Guest"
print(player_name) # Output: Guest
player_name = "Hero" # Reassign a new string value
print(player_name) # Output: Hero
Variable names have certain rules: they must start with a letter (a-z, A-Z) or an underscore (_), followed by letters, numbers (0-9), or underscores. They are case-sensitive (age is different from Age). It’s a standard convention in Python (from PEP 8) to use lowercase words separated by underscores ( snake_case ) for variable names (e.g., total_count, user_id). Choosing descriptive names makes your code much easier to understand.
Data Types: Classifying Information
The kind of value a variable holds determines its data type. Python automatically manages data types, but understanding them is vital because different types support different operations and behave differently.
Here are the most fundamental built-in data types in Python:
- Strings (
str): Used for sequences of characters, essentially text. Strings are enclosed in single quotes ('), double quotes ("), or even triple quotes ('''or""") for multi-line strings.
# Snippet 1.3: String Data Type
single_quoted_string = 'Hello'
double_quoted_string = "World"
sentence = "Python is fun!"
# Multi-line string
poem = """This is a poem
It spans multiple lines
How neat!
"""
empty_string = "" # An empty string is still a string type
print(single_quoted_string)
print(sentence)
print(poem)
Strings are incredibly versatile. You can find their length using the len() function and perform various operations and use methods on them (which we’ll touch on briefly later and are explored more deeply after basics).
Integers (int): Represent whole numbers (positive, negative, or zero) without any fractional part.
# Snippet 1.4: Integer Data Type
count = 10
year = -2023
zero = 0
big_number = 1000000000
Integers can be arbitrarily large (limited only by your computer’s memory), which is a nice feature of Python compared to some other languages. They are used for counting, indexing, and whole-number quantities.
Floating-Point Numbers (float): Represent numbers that have a decimal point or are written using exponential notation (e.g., 1.2e5 for $1.2 \times 10^5$). Used for measurements, calculations involving fractions, etc.
# Snippet 1.5: Float Data Type
pi = 3.14159
temperature = 98.6
gravity = 9.81
small_number = 0.001
scientific_notation = 2.5e-3 # Represents 0.0025
Even a number like 5.0 is a float, even though its value is a whole number, because it has a decimal point. Be mindful that floating-point arithmetic can sometimes have tiny precision differences due to how computers represent them in binary.
Booleans (bool): Represent one of two truth values: True or False. These are fundamental for making decisions and controlling the flow of your program. They are the result of comparison operations (like checking if two values are equal) and are used directly in logical checks.
# Snippet 1.6: Boolean Data Type
is_active = True
has_permission = False
is_student = True
- Boolean values are often used to track states (on/off, yes/no, valid/invalid).
Python is a dynamically typed language. This means you don’t have to explicitly declare the data type of a variable when you create it (like int age = 30; in some other languages). Python figures out the type based on the value you assign. You can even change the type of data a variable holds by reassigning it a value of a different type, though it’s often clearer practice to use different variable names if the concept represented changes fundamentally.
You can always check the type of any variable or value using the built-in type() function:
# Snippet 1.7: Checking Data Types
name = "Python"
count = 200
average = 15.6
print(type(name)) # Output: <class 'str'>
print(type(count)) # Output: <class 'int'>
print(type(average)) # Output: <class 'float'>
print(type(True)) # Output: <class 'bool'>
Understanding variables and data types is the very first step. They are the nouns of your programming language – the things you work with.
Chapter 2: Operators and Expressions – Performing Actions on Data
Once you have data stored in variables with specific types, you need to do things with it. This is where operators and expressions come into play. Operators are symbols that perform operations on values or variables (called operands), and an expression is a combination of variables, values, and operators that Python evaluates to produce a single result.
Arithmetic Operators: Math in Python
These are the most common operators for working with numbers:
+: Addition-: Subtraction*: Multiplication/: Division (result is always a float)//: Floor Division (division that discards the fractional part, result is an integer for integer inputs)%: Modulo (returns the remainder of a division)**: Exponentiation (raises the first operand to the power of the second)

# Snippet 2.1: Arithmetic Operations
x = 10
y = 3
print(x + y) # Output: 13 (Addition)
print(x - y) # Output: 7 (Subtraction)
print(x * y) # Output: 30 (Multiplication)
print(x / y) # Output: 3.3333333333333335 (Division)
print(x // y) # Output: 3 (Floor Division - 10 divided by 3 is 3 with remainder 1, discard .333...)
print(x % y) # Output: 1 (Modulo - the remainder when 10 is divided by 3)
print(x ** y) # Output: 1000 (Exponentiation - 10 to the power of 3)
Expressions like x + y or x ** y are evaluated by Python to produce a single value. This value can then be used, for instance, by assigning it to another variable, as we saw in the basic operations section.
Python follows standard operator precedence rules (similar to PEMDAS/BODMAS in mathematics) to determine the order of operations in more complex expressions (e.g., multiplication before addition). Parentheses () can be used to explicitly control the order.
# Snippet 2.2: Operator Precedence
result1 = 5 + 3 * 2 # Multiplication happens first: 5 + (3 * 2) = 5 + 6 = 11
print(result1) # Output: 11
result2 = (5 + 3) * 2 # Parentheses force addition first: (5 + 3) * 2 = 8 * 2 = 16
print(result2) # Output: 16
String Operators: Working with Text
We already saw the + operator for string concatenation. Another useful operator for strings is *.
+: Concatenation (joins strings)*: Repetition (repeats a string a specified number of times)
# Snippet 2.3: String Operators
word = "Hello"
space = " "
name = "User"
full_message = word + space + name # Concatenation
print(full_message) # Output: Hello User
repeated_word = word * 3 # Repetition
print(repeated_word) # Output: HelloHelloHello
line = "=" * 20 # Repeat the "=" character 20 times
print(line) # Output: ====================
Comparison Operators: Checking Relationships
These operators compare two values and always return a boolean result (True or False). They are fundamental for making decisions in your code.

# Snippet 2.4: Comparison Operators
a = 10
b = 20
c = 10
print(a == b) # Output: False (10 is not equal to 20)
print(a == c) # Output: True (10 is equal to 10)
print(a != b) # Output: True (10 is not equal to 20)
print(a > b) # Output: False (10 is not greater than 20)
print(a < b) # Output: True (10 is less than 20)
print(a >= c) # Output: True (10 is greater than or equal to 10)
print(b <= a) # Output: False (20 is not less than or equal to 10)
print("apple" == "orange") # Strings can be compared alphabetically
# Output: False
print("apple" < "orange")
# Output: True (a comes before o)
Comparison operators are the basis for the conditions used in control flow structures like if statements (coming next!).
Logical Operators: Combining Conditions
These operators are used to combine boolean values or the results of comparison operations.
and:Trueif both operands areTrue.or:Trueif at least one operand isTrue.not: Negates a boolean value (not TrueisFalse,not FalseisTrue).

# Snippet 2.5: Logical Operators
is_sunny = True
is_warm = False
is_weekend = True
print(is_sunny and is_weekend) # Output: True (both are True)
print(is_sunny and is_warm) # Output: False (is_warm is False)
print(is_sunny or is_warm) # Output: True (is_sunny is True)
print(is_warm or is_warm) # Output: False (both are False)
print(not is_warm) # Output: True (negation of False)
age = 25
is_student = True
print(age > 18 and is_student) # Combine comparison and boolean variable
# Output: True (25 > 18 is True, and is_student is True)
Logical operators are powerful for building complex conditions that control your program’s behavior.
Assignment Operators: Shorthands for Updates
We’ve used = for assignment. Python also offers combined assignment operators that perform an operation and assignment in one step.
+=: Add and assign (x += 5is equivalent tox = x + 5)-=: Subtract and assign (x -= 3is equivalent tox = x - 3)*=: Multiply and assign (x *= 2is equivalent tox = x * 2)/=: Divide and assign (x /= 4is equivalent tox = x / 4)- And others like
//=,%=,**=.

# Snippet 2.6: Assignment Operators
counter = 0
counter += 1 # Equivalent to counter = counter + 1
print(counter) # Output: 1
balance = 1000
balance -= 50 # Equivalent to balance = balance - 50
print(balance) # Output: 950
message = "Hello"
message += " World" # Equivalent to message = message + " World" (String concatenation)
print(message) # Output: Hello World
These operators are simply conveniences for updating the value of a variable based on its current value.
Operators are the verbs and conjunctions of your programming language, allowing you to express actions and relationships between your data.
Chapter 3: Input and Output – Communicating with the User
Programs aren’t usually isolated; they need to interact with the outside world. The most basic form of interaction is getting data into the program (Input) and displaying results or messages out of the program (Output).
Output: Displaying Information (print())
The primary function for displaying output to the console in Python is print(). It can display strings, numbers, the values of variables, and more.
# Snippet 3.1: Basic print() usage
print("Hello, Python!") # Print a string literal
print(123) # Print a number literal
message = "Learning is fun."
print(message) # Print the value of a variable
# Print multiple items separated by commas (adds spaces by default)
print("My age is", 30) # Output: My age is 30
print("Name:", "Alice", "City:", "London") # Output: Name: Alice City: London
The print() function automatically adds a newline character at the end of the output, so each subsequent print() call starts on a new line. You can change the separator between items (default is space) and the ending character (default is newline) using the sep and end arguments:
# Snippet 3.2: print() with sep and end
print("Item1", "Item2", "Item3", sep=" - ")
# Output: Item1 - Item2 - Item3
print("Loading...", end=" ") # Don't end with a newline
print("Done.")
# Output: Loading... Done. (on the same line)
A powerful and modern way to format output strings is using f-strings (formatted string literals), available in Python 3.6 and later. They allow you to embed the values of variables or even simple expressions directly inside a string by prefixing the string with f and placing variables/expressions in curly braces {}.
# Snippet 3.3: Using f-strings
name = "Bob"
age = 28
temperature = 21.5
print(f"User: {name}, Age: {age}")
# Output: User: Bob, Age: 28
print(f"Next year, {name} will be {age + 1} years old.") # Embed an expression
# Output: Next year, Bob will be 29 years old.
print(f"The temperature is {temperature:.1f}°C") # Format the float to one decimal place
# Output: The temperature is 21.5°C
F-strings make creating complex output messages much cleaner and more readable.
Input: Getting Data from the User (input())
The input() function is used to get text input from the user via the console. When input() is called, the program pauses, waits for the user to type something and press Enter, and then returns the typed text as a string.
# Snippet 3.4: Basic input() usage
# Ask the user for their name and store the result
user_name = input("Please enter your name: ")
# Print a greeting using the input
print(f"Hello, {user_name}!")
The string message you pass inside the parentheses of input() is displayed as a prompt to the user.
A critical point to remember about input() is that it always returns the user’s input as a string, regardless of what they type. If you need the input to be treated as a number for calculations, you must explicitly convert it using int() or float().
# Snippet 3.5: Converting input to numbers
# Get age input (it comes in as a string)
age_str = input("Enter your age: ")
print(f"Input type of age: {type(age_str)}") # Output example: Input type of age: <class 'str'>
# Convert the string to an integer using int()
age_int = int(age_str)
print(f"After conversion type: {type(age_int)}") # Output example: After conversion type: <class 'int'>
# Now you can perform math
years_later = age_int + 5
print(f"In 5 years, you will be {years_later} years old.")
# Example converting to float
height_str = input("Enter your height in meters: ")
height_float = float(height_str)
print(f"Your height is {height_float:.2f} meters.")
If the user types something that cannot be converted (e.g., text when you use int()), this will cause a ValueError and crash the program if not handled (error handling is a more advanced topic, typically covered after basics).
In applications with user interfaces (like web apps or desktop apps), input isn’t usually handled by input() in the console. Instead, the UI library (like Gradio or Streamlit) manages taking data from text boxes, number fields, dropdowns, etc., and provides those values to your Python code (often as function arguments or variable values) in the appropriate data types. Similarly, output goes to specific areas of the UI, not the console. However, the core concepts of getting data in and showing data out are universal.
Input and output functions are how your program interacts with the user, allowing it to receive instructions or data and provide feedback or results.
Chapter 4: Control Flow – Making Decisions
Subscribe to continue reading
Subscribe to get access to the rest of this post and other subscriber-only content.
