Python Basics


Written by PIRATE KING

Starting the video will automatically play it from this chapter.

Variables

Variables are a core concept in programming. They let us store data in a computer's memory using a name. Think of a variable as a box: you can put something inside, check or use what's in the box, or replace it with something else. In this example name = "Pirate", the text "Pirate" is stored in a variable called name. You can display the value of name by passing it to the print function like this: print(name). Give it a try.

Exercise

Instruction:

Assign the string "Pirate" to a variable called name, then display its value by passing it to the print function.

Expected output:

Pirate

Variables are created by putting the name on the left and the value on the right of an = sign. Use the print() function to see the result.

Variable Naming

You can name your variable in any way you want as long as it's composed of characters, numbers, and the _ underscore character and does not start with a number. For example, these variable names are all valid:

All of these are invalid variable names. Running any of them will result in a SyntaxError.

987
name!
number^ten

Best Practices

I said earlier that you can name your variables however you like, but it's important to follow industry style guides. Coding conventions make your code easier to read, understand, and maintain—both for yourself and others. Consistent style helps prevent errors, improves collaboration, and shows professionalism. Since code is read way more often than it's written, clear and uniform style saves time and headaches in the long run. The Python community widely follows Pep 8 , the official style guide, to keep code clean and consistent. Let me go over the four most important ones.
  1. Use descriptive variable names—full_name is meaningful, but fn is not.
  2. Stick to lowercase letters. FULL_NAME isn't the Python norm.
  3. Separate words with underscores _. While JavaScript uses camelCase fullName or PascalCase FullName, Python uses snake_case full_name.
  4. Add spaces around the equals sign =. Instead of full_name="Pirate King", write full_name = "Pirate King"—it's cleaner and easier to read.

Consistency is the key. Develop the habit of writing clean code manually.

Expressions and statements

An expression is any piece of code that evaluates to a single value. For example:

"Pirate"
2 + 3

Both "Pirate" and 2 + 3 are expressions. Astatement, on the other hand, is an instruction that performs an action, such as assigning a value to a variable or printing it. Complete the program to match the expected output:

Exercise

Instruction:

Complete the program by adding two more statements:
- Assign the expression 2 + 3 to a variable named total.
- Print the value of total using the print() function.

Expected output:

Pirate
5

Each line is a statement. Assignment (using =) and calling print() are both statements.

Indentation

In Python, indentation is required and affects how code runs. Random or incorrect indentation will cause an IndentationError.

Exercise

Instruction:

Fix this code by removing the indentation before print(name) so it aligns with the variable assignment.

Expected output:

Pirate

Code at the same level should start at the same column. Remove the tab or spaces before the print function.

Indented lines mark a code block—such as those for control statements, functions, or classes. You'll learn more about these blocks later.

Code Formatting

Code formatting is as important as writing code. There are multiple ways to write the same code, like x=1, x= 1, and x = 1, or even x = 1.

Notice the different spacing, but all are valid and produce the same result. However, as your code grows and you collaborate with others, inconsistent formatting can make the code hard to read. That's why maintaining a consistent style is crucial. I want you to become a great coder, and writing clean, well-formatted code is a key part of that. As we move forward, please pay close attention to how I use spaces and indentation. Following these formatting practices aligns with industry standards and sets you up with the best habits from day one.

Pro Tip: Press Cmd + S or click the Format Code button at the top of the editor to automatically format your code.

Quiz

1. Which of the following are valid Python variable names?

2. According to Python's PEP 8 style guide, choose all recommended best practices when naming variables:

3. What is the output of the following code?

name = "Pirate"
print(name)

4. Fix the indentation error in the following code so it runs without error:

Expected output:

Pirate

In Python, code should be aligned correctly. Remove the spaces before the variable assignment.

5. Write a Python program that fulfills these requirements:

  • Declares a variable fav_color and assigns it the string "blue"
  • Prints the value of fav_color

Expected output:

blue

Proper variable assignment involves placing the variable name (fav_color) on the left, an = sign in the middle, and the value you want to store on the right.

© PIRATE KING 2026
GOLDSTONE STUDIO LLC
Privacy Policy