Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
Module 1 - Python Fundamentals Python Python Basics Python Programming: From Zero to Real-World Applications

Your First Python Program - Hello World

Reviewed & accurate
AI Summary

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, Anita

The 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-separated
Comments 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
Previously: Lesson 02 installed Python.
Today: You learned: Write and run your first Python program.
Next: Lesson 04 covers variables.
Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments