For AI agents: use /llms.txt for the Nakafa content index.
Python is a popular programming language with a strong emphasis on readable code. Its clear syntax makes it a practical first language for someone who is new to programming.
Many Python statements resemble short instructions in English, so you can often follow what a program does before you know every detail of the syntax. Python is also used across web development, data analysis, machine learning, and artificial intelligence.
For details about the language and its standard library, use the official Python documentation. It is the authoritative reference for the Python version you are using.
A common first program displays the text "Hello World!". Running it confirms that Python can execute a file and gives you a small, complete program to inspect.
print("Hello World!")The print() command is a built-in Python function used to display text or data to the screen. Whatever you write inside the parentheses will be displayed as output.
This lesson uses three common ways to run Python code. Each supports a different part of the learning workflow.
Interactive mode allows you to write and run Python code directly in the terminal. This is very useful for testing simple code or quick experiments.
Here's how to use interactive mode. First open a terminal or command prompt, then type python or python3 and press Enter. You'll see the >>> prompt indicating Python is ready to receive commands. Next, write Python code and press Enter to execute it.
$ python3
Python 3.8.5 (default, Sep 4 2020, 02:22:02)
[Clang 10.0.0 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello World!")
Hello World!
>>> 2 + 3
5
>>> name = "Alice"
>>> print(f"Hello, {name}!")
Hello, Alice!
>>>Script mode is a way to run saved Python files. This is suitable for more complex programs or programs you want to save for repeated use.
The steps to run Python scripts are quite simple. First create a file with .py extension using a text editor, then write Python code inside the file and save it. After that, open a terminal and navigate to the folder where the file is saved, then run it with the command python filename.py.
# Simple program to greet users
name = input("What's your name? ")
age = input("How old are you? ")
print(f"Hello {name}!")
print(f"You are {age} years old.")
print("Happy learning Python!")To run the file above:
$ python3 greeting.py
What's your name? John
How old are you? 20
Hello John!
You are 20 years old.
Happy learning Python!Jupyter is an interactive environment often used for data science and teaching. A notebook combines executable code, its output, and written explanation in one document.
Each cell can run independently, and its output appears directly below it. You can also add explanatory text, images, and graphs, which makes notebooks useful for experiments and data analysis.
Install JupyterLab in a dedicated Python environment so its packages do not interfere with unrelated projects:
# Install JupyterLab with the selected Python interpreter
python3 -m pip install jupyterlab
# Or install it from conda-forge in a Conda environment
conda install -c conda-forge jupyterlabSecond, run Jupyter Lab:
# Run Jupyter Lab
jupyter lab
# Will open browser with Jupyter interfaceThird, create a new notebook with .ipynb extension and write code in cells, then press Shift+Enter to execute.
Example usage in Jupyter:
# Cell 1: Import and setup
import math
# Cell 2: Variables and operations
radius = 5
area = math.pi * radius ** 2
print(f"Circle area with radius {radius} is {area:.2f}")
# Cell 3: Create function
def calculate_circle_area(r):
"""Function to calculate circle area"""
return math.pi * r ** 2
# Cell 4: Test function
test_radius = 7
result = calculate_circle_area(test_radius)
print(f"Circle area with radius {test_radius} is {result:.2f}")Let's learn some basic Python concepts that you'll use frequently.
Python has several basic data types. A variable name refers to a value; assigning a new value changes that binding rather than changing every value into a mutable box.
# Basic data types in Python
# String (text)
name = "John Doe"
message = 'Hello Python!'
# Integer (whole numbers)
age = 25
year = 2025
# Float (decimal numbers)
height = 175.5
pi = 3.14159
# Boolean (True/False)
is_student = True
is_graduated = False
# List (list of items)
fruits = ["apple", "orange", "mango", "banana"]
numbers = [1, 2, 3, 4, 5]
# Dictionary (key-value pairs)
person = {
"name": "Alice",
"age": 22,
"major": "Computer Science"
}
# Display data types
print(type(name)) # <class 'str'>
print(type(age)) # <class 'int'>
print(type(height)) # <class 'float'>
print(type(is_student)) # <class 'bool'>
print(type(fruits)) # <class 'list'>
print(type(person)) # <class 'dict'>User interaction is an important part of programming. Like a conversation, your program can ask users questions and provide answers or information.
# Input from user
user_name = input("Enter your name: ")
user_age = input("Enter your age: ")
# Data type conversion
age_number = int(user_age) # Convert string to integer
# Output with neat formatting
print(f"Hello {user_name}!")
print(f"You are {age_number} years old.")
# Mathematical operations
next_year = age_number + 1
print(f"Next year you will be {next_year} years old.")
# Output with different format
print("Name:", user_name)
print("Age:", age_number)
print("Age next year:", next_year)Python supports various mathematical operations:
# Basic mathematical operations
a = 10
b = 3
# Arithmetic operations
addition = a + b # 13
subtraction = a - b # 7
multiplication = a * b # 30
division = a / b # 3.333...
floor_division = a // b # 3 (division result rounded down)
modulus = a % b # 1 (remainder)
exponent = a ** b # 1000 (10 to the power of 3)
print(f"{a} + {b} = {addition}")
print(f"{a} - {b} = {subtraction}")
print(f"{a} * {b} = {multiplication}")
print(f"{a} / {b} = {division}")
print(f"{a} // {b} = {floor_division}")
print(f"{a} % {b} = {modulus}")
print(f"{a} ** {b} = {exponent}")
# Operations with assignment
x = 5
x += 3 # x = x + 3, result is x = 8
x *= 2 # x = x * 2, result is x = 16
print(f"Current value of x: {x}")Let's try some simple exercises to practice what we've learned:
# Simple calculator
print("=== SIMPLE CALCULATOR ===")
# Input from user
number1 = float(input("Enter first number: "))
operator = input("Enter operator (+, -, *, /): ")
number2 = float(input("Enter second number: "))
# Calculation process
if operator == "+":
result = number1 + number2
elif operator == "-":
result = number1 - number2
elif operator == "*":
result = number1 * number2
elif operator == "/":
if number2 != 0:
result = number1 / number2
else:
print("Error: Cannot divide by zero!")
result = None
else:
print("Invalid operator!")
result = None
# Output result
if result is not None:
print(f"Result: {number1} {operator} {number2} = {result}")# Temperature converter from Celsius to Fahrenheit and Kelvin
print("=== TEMPERATURE CONVERTER ===")
# Input temperature in Celsius
celsius = float(input("Enter temperature in Celsius: "))
if celsius < -273.15:
raise ValueError("Temperature cannot be below absolute zero")
# Convert to Fahrenheit and Kelvin
fahrenheit = (celsius * 9/5) + 32
kelvin = celsius + 273.15
# Output conversion results
print(f"\nConversion results from {celsius}°C:")
print(f"Fahrenheit: {fahrenheit:.1f}°F")
print(f"Kelvin: {kelvin:.1f}K")
# Bonus: Additional information
if celsius < 0:
print("This temperature is below water freezing point!")
elif celsius == 0:
print("This temperature is water freezing point!")
elif celsius == 100:
print("This temperature is water boiling point!")
elif celsius > 100:
print("This temperature is above water boiling point!")# Program to collect and display personal information
print("=== STUDENT PROFILE ===")
# Input information
name = input("Full name: ")
student_id = input("Student ID: ")
major = input("Major: ")
semester = int(input("Semester: "))
hobby = input("Favorite hobby: ")
current_year = int(input("Current year: "))
# Estimate using two semesters per academic year
entry_year = current_year - (semester - 1) // 2
estimated_graduation = entry_year + 4
# Display complete profile
print("\n" + "="*40)
print("STUDENT PROFILE")
print("="*40)
print(f"Name : {name}")
print(f"Student ID : {student_id}")
print(f"Major : {major}")
print(f"Semester : {semester}")
print(f"Hobby : {hobby}")
print(f"Entry Year : {entry_year}")
print(f"Est. Graduation: {estimated_graduation}")
print("="*40)
# Motivational message
if semester <= 2:
print("Good luck at the beginning of your studies!")
elif semester <= 6:
print("Keep up your learning spirit!")
else:
print("Good luck finishing your studies!")Your practice programs are correct when: