No history yet

Introduction to Python

What is Python?

Python is a powerful and versatile programming language, known for being relatively easy to learn. Its design philosophy emphasizes code readability, which means you can write programs that are clear and logical. Think of it less like a cryptic code and more like a set of structured instructions in English.

Lesson image

It was created in the late 1980s by Guido van Rossum. One of its key features is that it's an interpreted language. This means you don't have to go through a complicated compiling step to run your code. The Python interpreter reads your code line by line and executes it directly, which makes testing and debugging much faster.

Because of its simplicity and power, Python is used everywhere: from web development and data science to artificial intelligence and task automation.

Lesson image

Getting Started

To run Python code, you need the Python interpreter installed on your computer. You can download it for free from the official website, python.org. Once installed, you can write code in any plain text editor, but many programmers use an Integrated Development Environment (IDE), which is software that provides helpful tools like syntax highlighting and debugging.

Let's write our first program. It's a tradition in programming to start by making the computer say "Hello, World!".

print("Hello, World!")

That's it. This single line tells the computer to display the text inside the parentheses. The print() function is a built-in command for showing output on the screen.

Building Blocks

Programs need to work with information, and they do this using variables. A variable is like a labeled box where you can store a piece of data. You give it a name and assign it a value using the equals sign (=).

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

# Assigning the value 25 to the variable 'age'
age = 25

The kind of data a variable holds is called its data type. Python has several basic data types.

Data TypeDescriptionExample
String (str)A sequence of text characters."Hello" or 'Python'
Integer (int)A whole number, without a decimal.10, -5, 0
Float (float)A number with a decimal point.3.14, -0.5
Boolean (bool)Represents one of two values: True or False.True, False

Python is smart enough to figure out the data type on its own when you assign a value to a variable. You don't have to declare it explicitly.

Interacting with Your Program

We've already seen how to output information using the print() function. But what if you want your program to get information from the user? For that, you use the input() function.

The input() function prompts the user to type something and then returns whatever they entered as a string.

Let's combine everything we've learned to create a simple interactive program. It will ask for the user's name and then greet them.

# Ask the user for their name and store it in a variable
user_name = input("What is your name? ")

# Create a greeting message
greeting = "Hello, " + user_name + "!"

# Print the final greeting
print(greeting)

When you run this code, it will first display "What is your name? ". After you type your name and press Enter, it will print a personalized greeting. Notice how we used the + operator to combine strings. This is called concatenation.

Time to check your understanding.

Quiz Questions 1/5

What is the primary role of the print() function in Python?

Quiz Questions 2/5

Python is known as an "interpreted" language. What does this mean?

You now have a solid grasp of the absolute fundamentals of Python. You can create variables, understand the basic data types, and write simple programs that interact with a user.