No history yet

Python Basics

Getting Started with Python

Python is a popular programming language known for its clear and readable syntax. It's designed to be straightforward, which makes it a great choice for beginners. Before you can write any code, you need to set up your programming environment.

This involves two main components:

  1. The Python Interpreter: This is the program that reads your Python code and executes it. You can download it for free from the official Python website, python.org.
  2. A Code Editor: This is where you'll write your code. You can use a simple text editor like Notepad or TextEdit, but a dedicated code editor like VS Code, Sublime Text, or an Integrated Development Environment (IDE) like PyCharm will offer helpful features like syntax highlighting and debugging tools. For now, any simple text editor will do the job.
Lesson image

Your First Program

Let's write your first line of Python code. It's a tradition for new programmers to start by making the computer say "Hello, World!". Open your code editor, type the following line, and save the file as hello.py.

print("Hello, World!")

To run this program, open your computer's terminal or command prompt, navigate to the directory where you saved hello.py, and type python hello.py. You should see Hello, World! printed on your screen.

This simple line demonstrates a few key things about Python's syntax. The print() is a built-in function, which is a reusable block of code that performs a specific action. In this case, it displays whatever you put inside the parentheses on the screen. The text inside the quotes is called a string, which is simply a sequence of characters.

Variables and Data Types

Imagine you need to store a piece of information that you'll use later, like a person's name or a score in a game. Instead of rewriting it every time, you can store it in a variable.

A variable is like a labeled box where you can store a value. You give it a name and put something inside. Later, you can refer to the box by its name to get the value back.

Creating a variable is simple. You just choose a name, use the equals sign (=), and provide the value you want to store.

# Storing a name in a variable
user_name = "Alice"

# Storing a number
user_age = 30

# Printing the values stored in the variables
print(user_name)
print(user_age)

When you run this code, it will print "Alice" and then "30" on separate lines. Notice that we didn't put quotes around user_name or user_age in the print() functions. That's because we want to print the value stored inside the variables, not the variable names themselves.

Python has different types of data to handle different kinds of information. The most common ones you'll encounter at first are:

  • Strings (str): Used for text. You create them by wrapping text in single (') or double (") quotes.
  • Integers (int): Used for whole numbers, like -5, 0, or 1500.
  • Floats (float): Used for numbers with decimal points, like 3.14 or -0.5.
  • Booleans (bool): Can only have one of two values: True or False. They are essential for making decisions in your code.
Data TypeExampleDescription
String"Hello, Python"A sequence of characters
Integer42A whole number
Float98.6A number with a decimal point
BooleanTrueA value representing truth or falsehood

Getting Input from a User

Just as print() sends information out, the input() function brings information in. It lets you pause your program and wait for the user to type something.

# Ask the user for their name
name = input("What is your name? ")

# Greet the user with their name
print("Hello, " + name)

When you run this code, it will first display "What is your name? ". The program will then wait. After you type your name and press Enter, the program will store your input in the name variable. Finally, it will print a personalized greeting.

One important thing to know: the input() function always returns the user's input as a string. If you need to treat the input as a number, you'll have to convert it first. We'll explore how to do that as you learn more about Python.

You now have the fundamental building blocks for writing simple Python programs. You can display messages, store information in variables, and interact with users.

Quiz Questions 1/6

What is the primary role of the Python interpreter?

Quiz Questions 2/6

Which line of Python code will correctly print the text Hello, World! to the screen?