No history yet

Python Basics

Your First Python Program

Let's start by writing a simple line of code. In programming, a common first step is to make the computer say "Hello, World!". This tradition is a simple way to confirm that your setup is working correctly. In Python, it's incredibly straightforward.

print("Hello, World!")

Here, print() is a built-in function that tells the computer to display whatever is inside the parentheses on the screen. The text inside the quotation marks is called a string, which is just a sequence of characters. Python executes commands one line at a time, from top to bottom.

You can also leave notes in your code for yourself or other programmers. These are called comments. Python ignores anything on a line that follows the # symbol. Comments are great for explaining what your code does.

# This is a comment. It won't be executed.
print("This line will be printed.") # So will this one.

Storing Information with Variables

Imagine you have a box, and you put a label on it. You can put something inside the box, and whenever you refer to the label, you're really talking about what's inside. Variables in programming work just like that. They are containers for storing data values.

To create a variable, you just need to give it a name and assign it a value using the equals sign (=).

message = "Welcome to Python!"
print(message)

In this example, message is the variable name, and it holds the string value "Welcome to Python!". When we print the variable message, the program displays the value stored inside it. You can change the value of a variable at any time by assigning a new one to it.

There are a few rules for naming variables:

  • They can contain letters, numbers, and underscores.
  • They cannot start with a number.
  • They are case-sensitive, so message and Message are two different variables.

Python's Basic Data Types

Variables can hold different kinds of data. These categories are called data types. Python has several built-in types, but let's start with the four most common ones.

  1. Integers and Floats for numbers.
  2. Strings for text.
  3. Booleans for true or false values.

An integer is a whole number, like 10, -5, or 0. A float, or floating-point number, is a number with a decimal point, like 3.14 or -0.5.

age = 25              # This is an integer
pi_approx = 3.14    # This is a float

print(age)
print(pi_approx)

A string is used for text. You can create a string by enclosing characters in either single quotes (') or double quotes ("). You can even join strings together using the + symbol, a process called concatenation.

first_name = "Ada"
last_name = 'Lovelace'
full_name = first_name + " " + last_name

print(full_name) # Output: Ada Lovelace

Finally, a boolean can only have one of two values: True or False. They are essential for logic and decision-making in programs. Note that they must be capitalized in Python.

is_learning = True
is_finished = False

print(is_learning) # Output: True

Making Things Happen with Operators

Operators are special symbols that perform operations on variables and values. You're already familiar with many of them from math class.

Python has three basic kinds of operators: arithmetic, comparison, and logical.

Arithmetic operators perform mathematical calculations.

OperatorNameExample
+Addition5 + 2 is 7
-Subtraction5 - 2 is 3
*Multiplication5 * 2 is 10
/Division5 / 2 is 2.5
**Exponent5 ** 2 is 25
%Modulo5 % 2 is 1

Comparison operators compare two values and return a boolean result (True or False).

OperatorNameExample
==Equal to5 == 2 is False
!=Not equal to5 != 2 is True
>Greater than5 > 2 is True
<Less than5 < 2 is False
>=Greater than or equal to5 >= 5 is True
<=Less than or equal to5 <= 2 is False

Logical operators (and, or, not) are used to combine boolean values.

OperatorDescriptionExample
andReturns True if both statements are true(5 > 2) and (3 < 4) is True
orReturns True if one of the statements is true(5 < 2) or (3 < 4) is True
notReverses the resultnot(5 > 2) is False

Interacting with the User

So far, our programs have just displayed pre-written information. To make them interactive, we can ask the user for input. Python's input() function does exactly that. It waits for the user to type something and press Enter.

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

When this code runs, it will display "What is your name? " and pause. After you type your name and press Enter, the program will greet you.

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

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

years_to_100 = 100 - age_number

# We must convert the number back to a string to print it with other text
print("You will be 100 in " + str(years_to_100) + " years.")

Here, we get the age as a string, convert it to an integer to do math, and then convert the result back into a string (str()) so we can combine it with our message.

Time to test what you've learned.

Quiz Questions 1/6

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

Quiz Questions 2/6

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

These are the fundamental building blocks of Python. With just variables, data types, and operators, you can already write simple but useful programs.