No history yet

Python Basics

Your First Lines of Code

Let's jump right in. The most traditional way to start with any programming language is to make it say "Hello, World!". In Python, this is remarkably simple.

print("Hello, World!")

That's it. The print() command, called a function, tells the computer to display whatever you put inside the parentheses. Since we're displaying text, we wrap it in quotation marks. This is a core part of Python's syntax—the set of rules that define how a Python program is written and interpreted.

Python's syntax is designed to be clean and readable. You'll often find that the code looks a lot like plain English.

Storing Information in Variables

Writing a program often involves working with data. Instead of typing the same piece of information over and over, we can store it for later use. We do this with variables.

Think of a variable as a labeled box where you can keep something. You give the box a name, and you can put something inside it. The = symbol is used to put data into a variable. This is called an assignment.

# Assign the text "Welcome to Python!" to a variable named 'greeting'
greeting = "Welcome to Python!"

# Now, print the content of the variable
print(greeting)

Once we've stored the text in the greeting variable, we can use the variable's name anywhere we would have used the original text.

You have a lot of freedom when naming variables, but there are a few rules:

  • They must start with a letter or an underscore (_), not a number.
  • They can only contain letters, numbers, and underscores.
  • They are case-sensitive, so message and Message are two different variables.

The Building Blocks - Data Types

Python handles different kinds of data in different ways. These categories of data are called data types. Let's look at the most common ones.

String

noun

A sequence of characters used to represent text. In Python, strings are enclosed in either single quotes ('') or double quotes ("").

We've already been using strings. "Hello, World!" is a string.

first_name = "Ada"
last_name = 'Lovelace'

Next are numbers. Python has two main types for numbers: integers for whole numbers and floats for numbers with a decimal point.

Integer

noun

A whole number, without a fractional part.

apples = 5
number_of_students = 23

Float

noun

A number that contains a decimal point.

price = 4.99
pi_approx = 3.14159

You can perform math operations with numbers, just like on a calculator.

item_price = 12.50
quantity = 2
total_cost = item_price * quantity # The '*' symbol means multiply

print(total_cost)

The final basic data type is the boolean. It can only have one of two values: True or False. Booleans are the foundation of logic in programming, used to answer true-or-false questions.

is_learning = True
is_finished = False

print(is_learning)

Interacting with Your Program

A program isn't very useful if it can't interact with the outside world. We already know how to send information out using print(). To get information from a user, we use the input() function.

# The text inside input() is the prompt shown to the user.
name = input("Please enter your name: ")

print("Hello, " + name + "!")

When you run this code, the program will pause and wait for you to type something and press Enter. Whatever you type is then stored as a string in the name variable.

It's important to remember that input() always gives you back a string, even if the user types a number. If you need to treat the input as a number, you have to convert it first.

age_string = input("How old are you? ")

# Convert the string to an integer
age_number = int(age_string)

years_until_100 = 100 - age_number

print("You will be 100 in", years_until_100, "years!")

Here, int() is a function that takes a string and tries to convert it into an integer. There's also a float() function for converting to floats. This ability to get input, process it, and provide output is the basis of almost every program you'll ever write.

Time to check what you've learned.

Quiz Questions 1/5

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

Quiz Questions 2/5

Which of the following is an invalid variable name in Python?

You now have the fundamental building blocks of Python: how to display output, store data in variables, understand basic data types, and get input from a user. With these concepts, you're ready to start building more complex programs.