Written by PIRATE KING
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.
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
full_name is meaningful, but fn is not.FULL_NAME isn't the Python norm._. While JavaScript uses camelCase fullName or PascalCase FullName, Python uses snake_case full_name.=. 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.
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:
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 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.
Cmd + S or click the Format Code button at the top of the editor to automatically format your code.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:
my_name and assigns it your name as a stringmy_nameFor example, if your name is "PK", it should also output PK