What You Will Learn
Beginner
- How to write and run a Python program
- How to use print() and comments
- The interactive shell vs .py files
Method 1: Interactive Shell (REPL)
Open a terminal and type python. You get the interactive shell (REPL). Type Python code and press Enter to see results immediately.
Terminalbash
$ python
>>> print("Hello, World!")
Hello, World!
>>> 2 + 3
5
>>> exit()Method 2: Create a .py File
Create a file called hello.py:
hello.pypython
# This is a comment - Python ignores it
print("Hello, World!")
print("I am learning Python!")
# You can print multiple things
name = "Anita"
print("Hello,", name)Run it:
Terminalbash
$ python hello.py
Hello, World!
I am learning Python!
Hello, AnitaThe print() Function
print() displays output. You can print strings, numbers, variables, and more:
Terminalpython
print("Hello") # String
print(42) # Number
print(3.14) # Float
print("Sum:", 2 + 3) # Multiple values
print("A", "B", "C") # Space-separatedComments start with #
Anything after # on a line is ignored by Python. Use comments to explain your code.Practical Exercise
Open a terminal
Type python to enter the REPL
Run: print("Hello, World!")
Exit with exit()
Create hello.py with the code above
Run: python hello.py
Key Takeaways
- Interactive shell: type python, write code, see results instantly.
- Script files: write code in .py files, run with python filename.py.
- print() displays output - strings, numbers, variables.
- Comments start with # - Python ignores them.
- Run a script: python hello.py
Comments
Comments
Post a Comment