No history yet

Python Basics

Why Learn Python?

Python is a powerful and versatile programming language, and it's also one of the easiest to learn. Its design philosophy emphasizes code readability, with a syntax that allows programmers to express concepts in fewer lines of code than might be used in languages like C++ or Java.

This simplicity doesn't limit its power. Python is used everywhere: from building websites and automating tasks to conducting complex data analysis and building machine learning models. Companies like Google, Netflix, and NASA all rely on Python for various parts of their operations.

Lesson image

Setting Up Your Workspace

To start writing Python, you need a place to write and run your code. This is called a development environment. You have two main options: installing Python on your computer or using an online editor.

Installing Python locally is straightforward. You can download the official installer from the python.org website. The installation includes Python's standard Integrated Development and Learning Environment, called IDLE, which is a simple editor that lets you write and run code right away.

Lesson image

Online editors are even easier, requiring no setup. Websites like Replit or Google Colab let you write and run Python code directly in your web browser. For now, let's write our first program. It’s a tradition in programming to start by making the computer say "Hello, World!".

print("Hello, World!")

This single line of code uses the built-in print() function. Whatever you put inside the parentheses and quotes will be displayed as output.

Core Building Blocks

All programs, no matter how complex, are built from basic components. In Python, these include variables for storing data and operators for performing actions on that data.

A variable is like a labeled box where you can store information. You create a variable by giving it a name and assigning it a value using the equals sign =.

# Assigning the value 'Alice' to the variable 'name'
name = "Alice"

# Assigning the number 42 to the variable 'age'
age = 42

print(name)
print(age)

The information you store in a variable has a specific type. Python automatically figures out the data type for you. The most common ones are:

  • Strings (str): Text, wrapped in single ' or double " quotes. Example: "Hello".
  • Integers (int): Whole numbers. Example: 10, -5.
  • Floats (float): Numbers with a decimal point. Example: 3.14, -0.5.
  • Booleans (bool): Represents truth values, which can only be True or False.
Data TypeExampleDescription
String"Python"A sequence of characters
Integer25A whole number
Float19.99A number with a decimal
BooleanTrueA value of true or false

Once you have data, you can perform operations on it using operators.

Operators are the symbols that tell the computer to perform a specific mathematical or logical manipulation.

For numbers, you can use standard arithmetic operators.

# Arithmetic Operators
addition = 10 + 5        # Result: 15
subtraction = 10 - 5     # Result: 5
multiplication = 10 * 5  # Result: 50
division = 10 / 5        # Result: 2.0

Python also has operators for more complex math, like exponents (**), floor division (//), and modulus (%).

23 (2 to the power of 3, equals 8)10//3 (division ignoring remainder, equals 3)10%3 (remainder of division, equals 1)\begin{aligned} \\ & 2 ** 3 \quad & \text{ (2 to the power of 3, equals 8)} \\ & 10 // 3 \quad & \text{ (division ignoring remainder, equals 3)} \\ & 10 \% 3 \quad & \text{ (remainder of division, equals 1)} \\ \end{aligned}

For strings, the + operator concatenates (joins) them together.

first_name = "Grace"
last_name = "Hopper"
full_name = first_name + " " + last_name

print(full_name) # Output: Grace Hopper

Interacting with Users

Programs often need to get input from a user. The input() function pauses your program and waits for the user to type something and press Enter.

user_name = input("What is your name? ")
print("Hello, " + user_name)

One important thing to know is that input() always returns the user's entry as a string. If you need to treat it as a number, you have to convert it using int() or float().

# Get input from the user (as a string)
age_str = input("How old are you? ")

# Convert the string to an integer
age_num = int(age_str)

# Now you can do math with it
age_in_ten_years = age_num + 10

print("In ten years, you will be:", age_in_ten_years)

This process of changing a value from one type to another is called type casting. It's a common and essential task in programming.

Now, let's test your understanding of these fundamental concepts.

Quiz Questions 1/6

What is Python's primary design philosophy?

Quiz Questions 2/6

What is the data type of the value returned by the input() function by default?

With these building blocks—variables, data types, operators, and basic I/O—you have the foundation for writing simple but useful Python programs.