No history yet

Python Basics

The Building Blocks of Python

Every language, whether spoken or for programming, has its own grammar and structure. Python is known for having a syntax that's clean and easy to read, which makes it a great language for beginners. Let's start with one of the most fundamental commands: displaying information.

To print something to the screen, we use the print() function. You just put whatever you want to show inside the parentheses. If you're printing text, you need to wrap it in quotes.

print("Hello, World!")

Running that single line of code will display Hello, World! on your screen. It's a classic tradition for a reason—it's your first successful program.

Storing Information

Writing programs would be pretty limited if we could only work with information one line at a time. We need a way to store data so we can use it later. In programming, we do this with variables.

Think of a variable as a labeled box where you can keep a piece of information. You give the box a name, and you can look inside it or change its contents whenever you need to.

Creating a variable is called assignment. You use the equals sign (=) to assign a value to a variable name.

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

# Print the value stored in the 'greeting' variable
print(greeting)

The information you store in variables comes in different forms, known as data types. Python needs to know what type of data it's working with, whether it's text, a number, or something else. Here are the most common ones:

string

noun

A sequence of characters, used to store text. Strings are enclosed in single (' ') or double (" ") quotes.

integer

noun

A whole number, without any decimal points. It can be positive, negative, or zero.

float

noun

A number that contains a decimal point. The name comes from "floating-point number."

boolean

noun

A value that can only be either True or False. Booleans are essential for making decisions in code.

One of Python's friendly features is that you don't have to explicitly tell it what type of data a variable holds. It figures it out on its own.

# A string for text
user_name = "Alex"

# An integer for a whole number
user_age = 25

# A float for a number with a decimal
account_balance = 100.50

# A boolean for a true/false value
is_active = True

Operators and Expressions

Once you have data in variables, you can perform operations on it using operators. An expression is a combination of values, variables, and operators that Python evaluates to produce a new value.

Let's start with the basic arithmetic operators you already know.

OperatorNameExampleResult
+Addition5 + 27
-Subtraction5 - 23
*Multiplication5 * 210
/Division5 / 22.5

You can use these operators with numbers directly or with variables that hold numbers.

apples = 10
oranges = 5

# You can store the result in a new variable
total_fruit = apples + oranges

print("Total fruit:")
print(total_fruit)

The addition operator (+) does something different with strings. Instead of adding them mathematically, it joins them together. This is called concatenation.

first_name = "Grace"
last_name = "Hopper"

# Concatenate the strings with a space in between
full_name = first_name + " " + last_name

print(full_name)

Getting User Input

Programs are much more interesting when they can interact with a user. Python's input() function lets you pause your program and wait for the user to type something in.

user_name = input("What is your name? ")

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

When you run this code, the message "What is your name? " will appear. The program will wait for you to type a response and press Enter. Whatever you type is then stored as a string in the user_name variable.

This is important: 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 input string to an integer
age_number = int(age_string)

# Now you can do math with it
years_until_100 = 100 - age_number

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

In the example above, int() converts the string to an integer. You can also use float() to convert a string to a float.

These are the fundamentals. With variables, data types, and operators, you have the core tools to start writing simple but useful Python programs.