Written by PIRATE KING
Starting the video will automatically play it from this chapter.
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.
Instruction:
"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.
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 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:
Instruction:
2 + 3 to a variable named total.total using the print() function.Expected output:
Pirate 5
Each line is a statement. Assignment (using =) and calling print() are both statements.
In Python, indentation is required and affects how code runs. Random or incorrect indentation will cause an IndentationError.
Instruction:
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 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:
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:
fav_color and assigns it the string "blue"fav_colorExpected 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.