Python Basics


Written by PIRATE KING

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.

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 code that evaluates to a value. For example:

"Pirate"
2 + 3

Both "Pirate" and 2 + 3 are expressions. On the other hand, a statement is an operation on a value. For example, there are four statements in this code snippet:

Indentation

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

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:

5. Write a Python program that:

  • Declares a variable my_name and assigns it your name as a string
  • Prints the value of my_name

For example, if your name is "PK", it should also output PK

© PIRATE KING 2025
GOLDSTONE STUDIO LLC
Privacy Policy