No history yet

Python Basics

Getting Started with Python

Python is a popular, powerful programming language known for its simple, readable syntax. It’s a great choice for beginners because it reads a lot like plain English, which lets you focus on learning programming concepts instead of getting tangled up in complex rules.

First, you'll need to install Python on your computer. The best place to get it is from the official website, python.org. Download the latest stable version for your operating system—whether it's Windows, macOS, or Linux. The installer will guide you through the steps. On Windows, make sure to check the box that says "Add Python to PATH" during installation. This makes it easier to run Python from your command line.

Once installed, Python comes with a simple built-in development environment called IDLE (Integrated Development and Learning Environment). It’s a good place to start, but many developers prefer using a more powerful code editor like Visual Studio Code, PyCharm, or Sublime Text. These editors offer features like syntax highlighting and code completion that make writing code much easier.

Lesson image

Your First Lines of Code

Every programming language has its own set of rules, called syntax. Think of it as the grammar of the language. Python's syntax is designed to be clean and uncluttered. For example, it uses indentation (whitespace at the beginning of a line) to group statements, which enforces a readable code style.

Let's write your first program. It's a tradition in programming to start with a program that simply displays "Hello, World!" on the screen. In Python, this is incredibly straightforward.

print("Hello, World!")

That’s it! The print() function is a built-in Python command that outputs text to the console. Whatever you put inside the parentheses and quotes will be displayed. To run this, you can open IDLE, type the line of code, and press Enter. Or, you can save it in a file (e.g., hello.py) and run it from your terminal by typing python hello.py.

Storing Information

Programs need to work with information, like numbers, text, or other kinds of data. To keep track of this information, we use variables. A variable is like a labeled box where you can store a value. You give it a name and then you can refer to that value later using its name.

# Assign the text "Alice" to a variable named 'user_name'
user_name = "Alice"

# Assign the number 25 to a variable named 'age'
age = 25

# Now we can use the variables
print(user_name)
print(age)

The values you store in variables have different types. The most common basic data types in Python are:

Data TypeDescriptionExample Code
String (str)A sequence of characters, used for text.greeting = "Hello"
Integer (int)A whole number, without a decimal point.score = 100
Float (float)A number with a decimal point.pi_value = 3.14

Python automatically figures out the data type when you assign a value to a variable. You can use the type() function to check the type of any variable.

greeting = "Hello"
print(type(greeting)) # Output: <class 'str'>

Operators and Expressions

To do useful things with your data, you need operators. Operators are special symbols that perform operations on values and variables. The most familiar ones are the arithmetic operators.

OperatorNameExampleResult
+Addition5 + 38
-Subtraction5 - 32
*Multiplication5 * 315
/Division15 / 35.0

When you combine variables, values, and operators, you create an expression. Python evaluates an expression and computes a result.

# An expression calculating the area of a rectangle
width = 10
height = 5
area = width * height # This is an expression

print(area) # Output: 50

Operators aren't just for numbers. The + operator can also be used to combine, or concatenate, strings.

first_name = "Grace"
last_name = "Hopper"
full_name = first_name + " " + last_name

print(full_name) # Output: Grace Hopper

Interacting with the User

So far, our programs have just displayed information. To make them interactive, you need a way to get input from the user. Python's input() function does exactly that. It prompts the user to type something and waits for them to press Enter.

# The text in the parentheses is the prompt shown to the user
user_name = input("What is your name? ")

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

When you run this code, it will first display "What is your name? ". After you type your name and press Enter, it will greet you personally.

One important thing to know: the input() function always returns the user's input as a string, even if they type a number. If you need to perform math with that input, you'll have to convert it to a number type first using int() or float().

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

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

Now you have the basic building blocks of Python: writing code, storing data in variables, using operators, and interacting with users. This foundation is the first step toward building much more complex and interesting programs.