🐍 Python Basics: Modes, Variables & Comments
Interactive Mode, Script Mode, Error Correction, and Variables
print() statement.
What is Interactive Mode?
When you launch Python IDLE, you see the Python Shell with the >>> prompt. This is Interactive Mode, which allows individual Python commands to be typed directly and carried out immediately.
>>> print("Hello world")
Hello world
>>> (7+6)*2
26
>>>
print, not Print). Also, you do not need the print command for basic calculations in the shell.
Essential Vocabulary
- Interactive mode: The Python shell allows commands to be entered and run immediately.
- Python shell: The interactive mode environment where commands are typed directly.
- Print statement: A Python command used to output text or values onto the screen.
- Case sensitive: Able to distinguish between capital and small letters.
- Interpreter: Translates Python code into machine code line by line.
- Machine code: The binary language a computer uses to carry out instructions.
Interactive Mode Practice Tasks
- Enter:
print("Welcome to the Python Shell") - Enter a command to print your name:
print("Your Name") - Fix the error:
print("Hello World)(Missing closing quotation mark). - Fix the calculation:
(7+3*2is missing a bracket. Correct it to(7+3)*2to get 20. - Try four of your own math commands (remember
*is for multiplication). - Get Python to add 150 and 23, then multiply by 3:
(150 + 23) * 3
What is Script Mode?
In Interactive Mode, code cannot be saved. Script Mode uses a text editor (like Notepad) to write, save, and run multiple lines of code as a complete program.
Steps to Create & Run:
- Create: In the Python shell, select File → New File.
- Type: Enter your code (e.g.,
print("Hello world")). - Save: Select File → Save As. Name it
HelloWorld.py(the.pyextension is required). - Run: Select Run → Run Module (or press F5). The output appears in the Shell window.
Correcting Errors in Script Mode
If there is an error, Python highlights it in red and provides an error message.
Print("Hello World")
# Error: NameError: name 'Print' is not defined
How to fix: Open the program, correct the mistake (change Print to print), save, and run again.
Adding Comments
Comments explain what the code does. The Python interpreter ignores them. Start a comment with a hash (#) symbol.
# This is an Adventure Game about The Digital Sweet Shop
# The program was written by me
print("Welcome to The Digital Sweet Shop")
print() # This prints a blank line
Script Mode Vocabulary
- Script mode: Python's text editor, allowing programmers to enter a list of commands executed together.
- Execute: Another word for running a program.
- Comments: Text entered by a programmer to improve code readability; starts with
#in Python.
Hands-On Practice Tasks
- Create a New File in IDLE.
- Type:
print("Welcome to my first program in Python") - Add a line to print your favourite hobby.
- Save as
My First Program.pyand Run (F5). - Add these lines:
print("Did you know Python can do calculations? The line below displays 10") print(7+3) print("How about this? The calculation below should display 25") print(7+3*2+5) - Fix the error: The last line outputs 18, not 25. Correct it to
print((7+3)*2+5)and add a#comment above it explaining the BIDMAS/PEMDAS fix. - New Program: Create
Maths Favourites.pythat prints your name, favourite number, and favourite calculation (e.g., "Name: Adnan", "Favourite number: 7", "Favourite calculation: multiplication").
What is a Variable?
A variable is a named location in the computer's memory that stores data of a particular type. Data can be assigned directly in the code or from user input.
name = "Ardeel"
age = 12
address = "Station Road"
colour = "Red"
Printing with Variables
To print a variable alongside text, do not put the variable name in quotation marks. Use a comma to join the text and the variable.
print(name)
# Output: Ardeel
print("Your name is", name)
# Output: Your name is Ardeel
print("Your name is", name, ". Nice to meet you.")
# Output: Your name is Ardeel . Nice to meet you.
- Variable: A named memory location used to store data of a given type during program execution; a variable can change value as the program runs.
Variable Practice Tasks
- Create a new file and type:
name = "Ardeel" age = 12 address = "Station Road" colour = "Red" print("Your name is", name) - Add code to display the age:
print("You are", age, "years old") - Complete the program to say:
print("You live at", address) - Complete the program to say:
print(colour, "is your favourite colour") - Final Task: Create a new program showing your name, age, address, and favourite colour. Save and run it.