GK SOLUTIONS
AI โ€ข IoT โ€ข Arduino โ€ข Projects & Tutorials
DEFEAT THE FEAR
โ† Back to All Lessons

๐Ÿ“ฆ Lesson 2: Python Variables & Data Types

โฑ๏ธ Estimated time: 20 minutes | Difficulty: Beginner

Dynamic Suffix

Python is dynamically typed, meaning you don't need to specify the type of data when you create a variable. Python handles it for you!

๐ŸŽฏ Your Goal: Write "Hello, World!"

Every programmer writes this program as their first one. It's a rite of passage! ๐ŸŽ“

Step 1: Open Your Text Editor

  1. Open VS Code (or Notepad if you don't have VS Code)
  2. Create a new file
  3. Go to File โ†’ New File

Step 2: Write Your First Program

Type (don't copy-paste!) the following code:

print("Hello, World!")

That's it! Just one line of code. Here's what it means:

  • print() = a command that displays text
  • "Hello, World!" = the text you want to display
  • ( ) = parentheses tell Python this is a function

Step 3: Save Your File

  1. Press Ctrl + S (or File โ†’ Save)
  2. Name it: hello.py (the .py is important!)
  3. Save it somewhere you'll remember (like your Desktop)

Step 4: Run Your Program!

  1. Open Command Prompt (CMD) or PowerShell
  2. Navigate to your file's folder (type: cd Desktop)
  3. Type: python hello.py
  4. Press Enter

๐ŸŽ‰ You should see: Hello, World!

Let's Get Fancier!

Now try this code:

print("Hello, World!")
print("My name is Python!")
print("I'm awesome!")
print("")
print("Welcome to programming!")

Run this program and you'll see multiple lines of output! Each print() creates a new line.

More Examples to Try:

Example 1: Using Numbers

print(5 + 3)
print(10 * 4)
print(20 / 5)

Output: 8 40 4.0

Example 2: Combining Text and Numbers

print("I have", 5, "apples")

Output: I have 5 apples

Example 3: Getting Input from Users

name = input("What is your name? ")
print("Hello,", name)

When you run this, it asks for your name, then says hello!

Common Beginner Mistakes:

โŒ Mistake 1: Forgetting parentheses

print "Hello" (WRONG) print("Hello") (CORRECT)

โŒ Mistake 2: Forgetting quotes around text

print(Hello World) (WRONG) print("Hello World") (CORRECT)

โŒ Mistake 3: Wrong file extension

hello.txt (WRONG) hello.py (CORRECT)

๐Ÿ”‘ Key Points

  • print() shows text or values on the screen.
  • Strings (text) must be wrapped in quotes: "hello" or 'hello'.
  • Numbers can be used directly: 5 + 3 returns 8.
  • Use input() to read user input from the console.
  • Save your scripts with the .py extension and run with python file.py.
  • Common beginner errors: missing parentheses, wrong quotes, wrong file extension.

๐Ÿงช Try It Online

Quick online editors to experiment with code (safe and instant):

๐Ÿ“ Practice Exercises

  1. Create a program that asks for the user's age and prints "You are X years old".
  2. Write a small calculator that asks for two numbers and prints their sum, difference, product, and quotient.
Show sample answers
# Exercise 1
age = input("How old are you? ")
print("You are", age, "years old")

# Exercise 2
x = float(input("Enter number 1: "))
y = float(input("Enter number 2: "))
print("Sum:", x + y)
print("Diff:", x - y)
print("Product:", x * y)
print("Quotient:", x / y if y != 0 else "undefined")

๐Ÿ“š Further Reading

Learn more reference-style material and examples from the originals:

โœ… Quick Quiz - Test Your Knowledge!

โ“ Question 1: What command displays text in Python?

โ“ Question 2: What is the correct file extension for Python programs?

โ“ Question 3: What does this code do? print(5 + 3)

๐ŸŽฏ Challenge

Try writing a program that:

  1. Asks the user for their favorite color
  2. Displays "Your favorite color is [color]"
  3. Save as favorite_color.py and run it

Hint: Use input() to get the user's input and print() to display it.

โ† Previous: What is Python? Next: Variables โ†’