No history yet

Python Basics

Getting Started with Python

Think of a programming language as a way to give instructions to a computer. Python is a popular language because its instructions are designed to be clear and readable, a bit like plain English. Before you can write these instructions, you need to set up your programming workspace, often called a development environment.

First, you'll need to install Python from its official website. The installation includes a simple program called IDLE (Integrated Development and Learning Environment), which is a great place for beginners to start. IDLE gives you a place to write your code and a way to run it immediately to see the results.

Lesson image

A long-standing tradition for programmers learning a new language is to start by making the computer say "Hello, World!". This simple task confirms that your setup is working correctly. In Python, it only takes one line of code.

print("Hello, World!")

The print() command is a built-in Python function that displays text or other information to the screen. Whatever you put inside the parentheses and quotes will be printed as output.

The Rules of the Road

Every language has rules for grammar and structure. Programming languages are no different; their rules are called syntax. Python is known for its clean and simple syntax. One of its most distinctive features is how it uses indentation—the spaces at the beginning of a line.

Unlike many other languages that use brackets or keywords to group code, Python uses whitespace. This means the amount of space you leave at the start of a line is crucial. For now, just remember that lines of code in the same block must have the same indentation. This might seem strange at first, but it forces code to be neat and easy to read.

In Python, whitespace isn't just for looks. It's part of the syntax and gives your code structure.

Another key part of writing clean code is adding comments. Comments are notes for humans that the computer completely ignores. They're a way to explain what your code does, which is incredibly helpful when you (or someone else) look at it later. In Python, a comment starts with a hash symbol (#).

# This is a comment. Python will ignore it.
print("This line will be executed.")

Storing Information

To do anything useful, programs need to work with information, like numbers, text, or dates. A variable is like a labeled box where you can store a piece of information. You give the box a name, and you can put data inside it. You can also change what's inside the box later.

greeting = "Hi there!"
user_age = 25

Here, greeting is a variable holding text, and user_age is a variable holding a number. The data you store in variables comes in different forms, called data types. Python is smart and automatically figures out the data type for you.

Data TypeDescriptionExample
String (str)A sequence of characters (text)"Hello"
Integer (int)A whole number42
Float (float)A number with a decimal point3.14159
Boolean (bool)A value that is either True or FalseTrue

Once you have data in a variable, you can use it. For example, you can print it out.

city = "New York"
print(city)

Calculations and Decisions

Computers are excellent at doing math. Python uses symbols called operators to perform calculations. You can combine variables and literal values with operators to create expressions, which are pieces of code that produce a value.

An expression is a combination of values, variables, and operators that Python evaluates to produce a result.

Arithmetic operators work just like they do in math class. They include addition (+), subtraction (-), multiplication (*), and division (/).

apples = 10
oranges = 5

total_fruit = apples + oranges
print(total_fruit) # Output: 15

You can also use comparison operators to compare two values. These expressions always result in a Boolean value: True or False.

OperatorMeaningExample (a=5, b=10)Result
==Equal toa == 5True
!=Not equal toa != bTrue
>Greater thana > bFalse
<Less thanb < 20True
>=Greater than or equal toa >= 5True
<=Less than or equal tob <= aFalse

Finally, we have logical operators (and, or, not), which are used to combine Boolean values.

# Check if age is between 13 and 19
age = 15
is_teenager = age >= 13 and age <= 19
print(is_teenager) # Output: True

Talking to the User

So far, we've only been displaying information. Interactive programs also need to get information from the user. The input() function lets you do this. It prompts the user with a message, waits for them to type something, and then returns whatever they typed as a string.

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

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

One important thing to remember is that input() always gives you a string. If you need to treat the input as a number, you have to convert it first using int() or float().

age_str = input("How old are you? ")
age_num = int(age_str) # Convert the string to an integer

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

With these building blocks—syntax, variables, operators, and I/O—you have the foundation for writing simple but powerful Python programs.