Skip to main content

Introduction to Python Programming

Python is a versatile and powerful programming language that's easy to learn and widely used in data science, web development, automation, and more. This article provides a beginner-friendly introduction to Python programming, explaining how Python works as an interpreted language, its basic syntax, variables, and input/output.


1. How Python Works: The Interpreter​

Python is an interpreted language, which means that its code is executed line-by-line by the Python interpreter rather than being compiled into machine code first (as is the case with compiled languages like C or Java). When you run a Python script, the interpreter reads each line of code and immediately executes it.

Python as an Interpreted Language​

  • Interactive Execution: You can run Python code directly in an interactive environment (REPL) or shell, where you type a line of code and get immediate feedback.
  • Portability: Because Python is interpreted, you can run Python code on different platforms (Windows, macOS, Linux) without needing to recompile it.
  • Dynamically Typed: Python doesn’t require you to declare variable types, as the interpreter assigns types at runtime.

Running Python Code​

There are two main ways to run Python code:

  1. Interactive Shell: You can open a Python shell by typing python or python3 in your terminal (depending on your setup). This allows you to execute commands one at a time and see the results immediately.

  2. Python Scripts: You can write Python code in a file (e.g., script.py) and then run the entire script using the terminal:

    python script.py

2. Basic Python Syntax​

Python’s syntax is clean and easy to read. One of Python’s strengths is its use of indentation to define the structure of the code, rather than using curly braces {} or keywords.

Comments​

In Python, comments are used to explain what the code is doing. They are ignored by the interpreter and can be useful for explaining code logic.

  • Single-line comments: Begin with a # symbol.
# This is a comment
print("Hello, world!") # This will print 'Hello, world!'
  • Multi-line comments: Use triple quotes ''' or """.
"""
This is a multi-line comment.
It spans multiple lines.
"""
print("This is Python!")

Indentation​

Python uses indentation to define blocks of code, which is critical for functions, loops, and conditionals. Consistent indentation is required, or you will get an error.

if 5 > 2:
print("Five is greater than two!") # This line is indented

In the above example, the indented line belongs to the if block.


3. Variables and Data Types​

Variables​

A variable is a named location in memory where data is stored. You can assign values to variables using the = operator.

# Assign values to variables
x = 10
name = "Alice"
is_active = True

Python automatically determines the type of a variable based on the value you assign to it, so you don’t need to declare the type explicitly.

Basic Data Types​

Python has several basic data types that are commonly used:

  • Integers: Whole numbers (e.g., 5, 100, -23).
  • Floats: Numbers with decimal points (e.g., 3.14, 0.001, -5.0).
  • Strings: Text data, enclosed in quotes (e.g., "Hello, world!" or 'Python').
  • Booleans: True or false values (True, False).

Example:​

age = 25         # Integer
height = 5.9 # Float
name = "John" # String
is_student = False # Boolean

4. Basic Input and Output​

Printing to the Console​

To display output in Python, use the print() function.

print("Hello, world!")
print("The value of x is:", x)

Taking User Input​

To get input from a user, use the input() function. The input() function always returns the input as a string, so if you need a number, you'll have to convert it.

name = input("Enter your name: ")
print(f"Hello, {name}!")

# Convert the input to an integer
age = int(input("Enter your age: "))
print(f"You are {age} years old.")

5. Basic Arithmetic and Operators​

Python supports all the basic arithmetic operations:

a = 10
b = 3

addition = a + b # 13
subtraction = a - b # 7
multiplication = a * b # 30
division = a / b # 3.33 (float division)
floor_div = a // b # 3 (integer division)
modulus = a % b # 1 (remainder)
power = a ** b # 1000 (exponentiation)

6. Conditional Statements​

Python allows decision-making using if, elif, and else statements to control the flow of the program based on conditions.

x = 10
y = 5

if x > y:
print("x is greater than y")
elif x == y:
print("x is equal to y")
else:
print("x is less than y")

7. Loops: Repeating Code​

Python provides two types of loops for repeating code: for loops and while loops.

7.1. For Loops​

A for loop is used to iterate over a sequence like a list or range.

# Print numbers from 0 to 4
for i in range(5):
print(i)

7.2. While Loops​

A while loop repeats code as long as a condition is True.

i = 1
while i <= 5:
print(i)
i += 1

8. Functions: Organizing Code​

Functions are reusable blocks of code that perform specific tasks. They are defined using the def keyword and allow you to write modular code.

Defining and Calling Functions​

# Define a function
def greet(name):
print(f"Hello, {name}!")

# Call the function
greet("Alice") # Output: Hello, Alice!

Functions can accept parameters and return values.

def add(a, b):
return a + b

result = add(5, 10)
print(result) # Output: 15

Conclusion​

This article introduced the foundational concepts of Python programming, including how Python works as an interpreted language, basic syntax, variables, input/output, arithmetic, conditionals, loops, and functions. These are the essential building blocks for further exploration into more advanced topics like data structures, file handling, and time-series analysis. Python’s simplicity and readability make it an ideal choice for beginners and an excellent tool for data science.