No history yet

Python Basics

The Grammar of Python

Every language has rules. In English, we put periods at the end of sentences and capitalize proper nouns. Programming languages have rules, too, which we call syntax. Python's syntax is known for being clean and readable, which makes it a great language for beginners.

Let's start with the most classic first step in programming: telling the computer to say hello. In Python, we use a built-in command called print().

# This is a comment. Python ignores lines starting with #.
# The print() function displays text to the screen.
print("Hello, World!")

When you run this code, the output will simply be Hello, World!. The text inside the parentheses is what gets printed. The quotation marks tell Python that this is a piece of text.

Variables for Storage

Imagine you have a set of labeled boxes. You can put things in the boxes and use the labels to remember what's inside. In programming, these boxes are called variables. They are containers for storing data values.

To create a variable in Python, you just need to give it a name and assign it a value using the equals sign =. This is called an assignment.

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

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

Now, instead of using the data directly, we can use the variable name. The computer will look up what's stored in the variable and use that value.

print(name)
print(age)

Variable names can contain letters, numbers, and underscores, but they cannot start with a number. For example, user_age is a valid name, but 2nd_user is not.

Meet the Data Types

Variables can hold different kinds of information. Python automatically understands the data type based on the value you assign. Let's look at the four most common basic types.

Integer

noun

A whole number, positive or negative, without decimals.

In Python, integers are represented by the int type.

year = 2024
number_of_likes = 150

Float

noun

A number, positive or negative, containing one or more decimals.

The name "float" comes from "floating-point number." In Python, this type is called float.

pi_approx = 3.14
temperature = -5.5

String

noun

A sequence of characters, such as letters, numbers, and symbols, enclosed in quotes.

Strings are used for all text data. You can use single quotes (') or double quotes ("). Python calls this type str.

greeting = "Hello, Python!"
user_input = 'Yes'

Boolean

noun

A data type that has one of two possible values, True or False.

Booleans are the foundation of logic and decision-making in programming. In Python, they are represented by True and False (with a capital first letter). The type is called bool.

is_active = True
has_permission = False

You can check the type of any variable using the type() function. This can be useful when you're not sure what kind of data you're working with.

score = 95
print(type(score))  # Output: <class 'int'>

Here's a quick summary of these fundamental types.

Data TypePython NameDescriptionExample
IntegerintWhole numbers42, -100
FloatfloatNumbers with decimals3.14, -0.001
StringstrTextual data"hello", '@'
BooleanboolLogical true/false valuesTrue, False

Now that you've seen the basic building blocks, let's test your understanding.

Quiz Questions 1/5

What is the output of the following Python code?

print("Hello, Python!")
Quiz Questions 2/5

In programming, a named container for storing a data value is called a __________.

With variables and data types, you have the tools to store and categorize information. This is the first essential step in writing any program.