Introduction to Python
Your programming journey starts here! Learn what Python is and write your first code.
🎯 What You'll Learn
What is Python?
The language that powers AI, web, and more
Variables
Store and retrieve data easily
Naming Rules
7 rules for valid variable names
print() & id()
Display output and check memory
📘 Same topic in the course notebook
Session_1 in the course source has Introduction_to_Python and Variables in Python notebooks: same ideas (what is Python, variables, naming, print, id). Run those cells to practice.
🐍 What is Python?
Imagine you have a robot butler. You want it to make you breakfast. But you can't just say "make breakfast" - the robot doesn't understand English!
You need to give it very specific instructions:
- Go to the refrigerator
- Open the door
- Pick up 2 eggs
- Close the door
- Walk to the stove...
Python is the language we use to give these instructions to computers!
Your robot butler needs step-by-step instructions in a language it understands!
Python was created by Guido van Rossum in 1991
He named it after "Monty Python's Flying Circus" (a comedy show, not the snake! 🐍)
Python is designed to be:
- Easy to read - Code looks almost like English
- Easy to write - Less typing than other languages
- Powerful - Can do AI, web apps, games, and more!
Three Important Terms to Understand
Real-Life Analogy: Think of a UN translator. When someone speaks French, the translator converts it to English line by line, in real-time. They don't wait for the entire speech!
Python works the same way - it reads your code one line at a time and executes it immediately.
Real-Life Analogy: Think of a car. A car has:
- Properties (data): color, model, speed
- Actions (behaviors): start, stop, accelerate
In Python, we can create "objects" that bundle data and actions together!
Real-Life Analogy: When you drive a car, you just press the gas pedal - you don't manually inject fuel into each cylinder!
Python handles all the complex computer operations for you. You focus on what you want, not how the computer does it.
📌 In One Sentence Each (So You Never Forget)
Interpreted = Python runs your code line by line as you go (no separate "compile" step). Object-Oriented = We organize code into "objects" that have data and actions (you'll see this in Session 8). High-Level = You write in human-friendly commands; the computer translates them into machine steps for you.
Popular Python IDEs
IDE = Integrated Development Environment (where you write code)
- Jupyter Notebook - Great for learning & data science
- VS Code - Free, powerful, lots of features
- PyCharm - Professional Python IDE
- Google Colab - Free, runs in browser, no installation!
🎉 Your Very First Python Program!
Every programmer's journey starts with the famous "Hello, World!" program. Let's write yours!
print("Hello, World!")
Let's Break This Down:
This is a function - a built-in command that Python understands. Think of it as a megaphone 📢 that displays whatever you give it.
Parentheses tell Python: "Here comes the input for this function!" Functions always need parentheses.
Text inside quotes is called a "string". Quotes tell Python "this is just text, don't try to interpret it as code."
Hello is a variable name! Try print(Hello) and you'll get an error because there's no variable called "Hello".
More print() Examples
# Printing different things
print("I am learning Python!")
print(42) # Numbers don't need quotes
print(3.14159) # Decimals work too
print("The answer is:", 42) # Multiple items
# are comments. Python ignores them completely! They're notes for humans reading the code.
📦 Variables: Your Data Storage System
Imagine you're moving to a new house. You pack your things in boxes and label each box:
When you need something, you look at the labels to find the right box.
Variables in Python work exactly the same way! They're labeled containers that store data.
Definition: Variable
A variable is a named location in memory used to store data. Variables allow programs to store, retrieve, and manipulate values dynamically.
Python is dynamically typed - you don't need to declare what data type a variable will hold!
Creating a Variable
To create a variable, you need three things:
# Creating variables - like labeling boxes!
name = "Alice" # Box "name" contains "Alice"
age = 25 # Box "age" contains 25
height = 5.6 # Box "height" contains 5.6
is_student = True # Box "is_student" contains True
# Python is dynamically typed - no type declaration needed!
growth = 567
Growth = 345 # Different variable! (case sensitive)
🧠 What Happens in Computer Memory:
Using Variables
# Using variables with print()
print(name) # Opens the "name" box, shows what's inside
print(age)
# Combine text and variables
print("Hello,", name, "! You are", age, "years old.")
print("name")→ prints the word "name"print(name)→ prints what's INSIDE the variable name ("Alice")
Quotes = literal text. No quotes = look up the variable!
The id() Function - Memory Address
# id() returns the unique memory address of a variable
growth = 567
Growth = 345
print(id(growth)) # Memory location of growth
print(id(Growth)) # Different location! (different variable)
growth and Growth are two completely different variables because Python is case-sensitive! They're stored in different memory locations.
📝 Variable Naming Rules: The 7 Golden Rules
Imagine writing an address on a package:
- ✅ "123 Main Street" - Clear and deliverable!
- ❌ "###@@@!!!" - The postal service would be confused!
Python has similar rules to avoid confusion!
| Rule | ✅ Correct | ❌ Wrong | Why? |
|---|---|---|---|
| 1. Start with letter or underscore | name, _private |
1name |
Numbers first confuse Python |
| 2. Only letters, numbers, underscores | my_name2 |
my-name, any@ |
Special chars have meanings |
| 3. No spaces allowed | machine_learning |
machine learning |
Space = end of variable |
| 4. Case sensitive! | age ≠ Age ≠ AGE |
- | These are 3 different boxes! |
| 5. No reserved words | else1, my_class |
else, if, for |
Reserved for Python |
| 6. No special characters | data_2025 |
data), name$ |
Only _ is allowed |
| 7. Be descriptive | student_age |
x |
Makes code readable! |
# ✅ VALID variable names
hello1 = "how are you"
machine_learning = 45
_year = 2023
aravind = 12345
else1 = 567 # "else1" is fine, "else" is not
t1234 = "tarun"
__DATA = 45
# ❌ INVALID - These will cause errors!
# 1233 = 2024 # Can't start with number
# machine learning = 344 # No spaces!
# data) = 34 # No special chars!
# any@ = 345 # @ not allowed
# These are THREE DIFFERENT variables!
age = 25 # Variable 1
Age = 30 # Variable 2
AGE = 35 # Variable 3
print(age)
print(Age)
print(AGE)
print(id(age))
print(id(Age))
print(id(AGE)) # All different memory addresses!
Quick Check: Which are valid variable names?
myVariable✅ Valid - letters only2nd_place❌ Invalid - starts with number_secret✅ Valid - underscore at start is OKuser-name❌ Invalid - hyphen not allowedfor❌ Invalid - reserved worddeep_learning45✅ Valid - letters, numbers, underscore
🔄 The Assignment Operator (=)
Important: = Does NOT Mean "Equals" in Python!
=means "put this value into this variable" (assignment)==means "are these equal?" (comparison)
Think of = as an arrow pointing LEFT: take the value on the right and PUT IT INTO the variable on the left
Reassigning Variables
# Variables can change! (That's why they're called "variables")
age = 20
print(age)
age = 21 # Replace the old value with new one
print(age)
age = age + 1 # Take current age, add 1, put result back
print(age)
age = age + 1 work?
- Look up current value of
age→ 21 - Calculate 21 + 1 → 22
- Store 22 back into
age
Multiple Assignment - Python's Superpowers! ✨
# Assign multiple variables at once
rahul, suresh, jhon, tarun = 65, 67, 88, 99
print(jhon)
# Same value to multiple variables
tarun = tanish = tanamyi = 100
print(tanamyi)
a = 5
b = 10
print(f"Before: a={a}, b={b}")
# In other languages, you'd need a temporary variable
# Python makes it elegant!
a, b = b, a
print(f"After: a={a}, b={b}")
Python's Elegant Swap
In other languages like C or Java, swapping requires a temporary variable. Python's multiple assignment makes it a one-liner! This is one reason developers love Python.
Type Conversion (Basic)
age = 56
print(type(age)) # <class 'int'>
age = float(age) # Convert to float (reallocation)
print(age)
print(type(age)) # <class 'float'>
🚫 Common Mistakes (Intro)
- Spaces in variable names — Use
my_namenotmy name; Python will treat it as two things. - Forgetting quotes for text — Strings need quotes:
"hello"or'hello'; without them Python thinks it's a variable name. - Case matters —
Nameandnameare different variables; stay consistent.
💭 Short reflection
In one sentence: why is it useful that Python is interpreted (run line by line) rather than compiled, when you are first learning?
✅ CORE (Must know)
- Python: interpreted, high-level; variables as named containers;
name = value. print()for output;id()to see object identity.- Naming: start with letter/underscore, no spaces, case sensitive.
📚 NON-CORE (Good to know)
- Multiple assignment; difference between interpreted and compiled.
📋 Session 1 Summary
🐍 Python Is
An interpreted, OOP, high-level language that's easy to learn and powerful!
📦 Variables Are
Named containers that store data. Create with name = value
🖨️ print()
Displays output. Quotes for text, no quotes for variables.
🔍 id()
Returns the memory address of a variable.
📝 Naming Rules
Start with letter/underscore, no spaces, case sensitive!
🔄 Assignment
= puts value in variable. Multiple assignment is powerful!
🎉 Congratulations!
You've completed Session 1! You now know what Python is, how to create variables, and the rules for naming them. You're officially a Python programmer!
Complete code from course notebook: Introduction_to_Python (1).ipynb
Every line of code from the course notebook (verbatim).
# --- Code cell 4 ---
c=123456789234567892345678
# --- Code cell 5 ---
c
# --- Code cell 7 ---
Popular Python IDEs and Editors:
PyCharm:
A comprehensive IDE developed by JetBrains, offering powerful features like intelligent code completion, on-the-fly error checking, debugging, and robust integration with various tools and frameworks. It comes in a free Community Edition and a paid Professional Edition.
Visual Studio Code (VS Code):
A free, open-source code editor from Microsoft, highly extensible through a vast marketplace of extensions. Its Python extension provides excellent features for development, debugging, and linting.
Jupyter Notebook/JupyterLab:
Primarily used for data science and interactive computing, allowing for the creation and sharing of documents containing live code, equations, visualizations, and narrative text.
Spyder:
An open-source IDE specifically designed for scientific computing and data analysis, offering features like a variable explorer, interactive console, and plotting capabilities.
IDLE:
The default IDE bundled with Python, suitable for beginners due to its simplicity and ease of use.
Thonny:
Another beginner-friendly IDE with a clean interface and helpful debugging tools, focusing on ease of learning.
Sublime Text:
A highly customizable and lightweight text editor known for its speed and powerful features, enhanced for Python development via packages.
Atom:
An open-source, hackable text editor developed by GitHub, supporting Python development through various packages.
Eclipse with PyDev:
Eclipse, a popular open-source IDE for multiple languages, can be extended for Python development using the PyDev plugin.
Choosing an IDE:
Beginners:
Thonny or IDLE offer a gentle introduction to Python development.
General-purpose development:
VS Code or PyCharm (Community Edition) provide a robust environment for various projects.
Data Science/Scientific Computing:
Jupyter Notebook/JupyterLab or Spyder are tailored for these specific needs.
Web Development:
PyCharm Professional Edition offers advanced features for web frameworks like Django and Flask.
# --- Code cell 17 ---
a = 2
b = 3
sum = a + b
print(sum)
# --- Code cell 21 ---
a = 15
b = 27
print(f'Before swapping : a, b = {a}, {b}')
a, b = b, a
print(f'After swapping : a, b = {a}, {b}')
Complete code from course notebook: Variables__in_Python (1) (1).ipynb
Every line of code from the course notebook (verbatim).
# --- Code cell 1 --- # Variable---is a named location in memory used to store data. # variables allow programs to store,retrieve and manupluate values dynamically. # --- Code cell 2 --- # python is a dynamically typed---You need not to define what data type is a variable # --- Code cell 3 --- # Defining a variables--Rules 1@ Variable name start with a letters or a underscore(_) 2@ variable can contain alpha-numeric 3@ variable name cannot be an keyword/Reserved words in python (if,else,while,elif,True..) 4@ variable name are case sensitive(Data,DATA and data are different) 5@ Variable name cannot contain spaces 6@ Variable name cannot contain special characters 7@ Variable name should not start with a number # --- Code cell 4 --- growth=567 # --- Code cell 5 --- Growth=345 # --- Code cell 6 --- Growth # --- Code cell 7 --- growth # --- Code cell 8 --- id(growth) # --- Code cell 9 --- id(Growth) # --- Code cell 10 --- _=34563 # --- Code cell 11 --- _ # --- Code cell 12 --- hello1="how are you" # --- Code cell 13 --- hello1 # --- Code cell 14 --- elsea=5678 # --- Code cell 15 --- machine_learning=45 # --- Code cell 16 --- aravind=12345 # --- Code cell 17 --- hello=678 # --- Code cell 18 --- else1=222 # --- Code cell 19 --- a12=123 # --- Code cell 20 --- a12=23 # --- Code cell 21 --- a12 # --- Code cell 22 --- _=22 # --- Code cell 23 --- id(_) # --- Code cell 24 --- _reeal2345=2025 # --- Code cell 25 --- reeal2345=67 # --- Code cell 26 --- else1=7865 # --- Code cell 27 --- else1 # --- Code cell 28 --- year=2017 # --- Code cell 29 --- 1233=2024 # --- Code cell 30 --- import string spl_char=string.punctuation print(spl_char) # --- Code cell 31 --- _ # --- Code cell 32 --- 45=2033 # --- Code cell 33 --- year # --- Code cell 34 --- =----used for assigning a value ==---used for comparing two values # --- Code cell 35 --- year # --- Code cell 36 --- _year=2023 # --- Code cell 37 --- _year # --- Code cell 38 --- t1234="tarun" # --- Code cell 39 --- t1234 # --- Code cell 40 --- year # --- Code cell 41 --- else1=567 # --- Code cell 42 --- Else1=567 # --- Code cell 43 --- id()--used to give identity of any variable # --- Code cell 44 --- id(else1) # --- Code cell 45 --- id(Else1) # --- Code cell 46 --- machine learning=344 # --- Code cell 47 --- machine_learning=344 # --- Code cell 48 --- machine_learning # --- Code cell 49 --- data)=34 # --- Code cell 50 --- __DATA=45 # --- Code cell 51 --- id(DATA) # --- Code cell 52 --- id(data) # --- Code cell 53 --- id(DATA) # --- Code cell 54 --- DATA=45 # --- Code cell 55 --- DATA # --- Code cell 56 --- id(DATA) # --- Code cell 57 --- artifical_intelligence=56 # --- Code cell 58 --- deep_learning45=34.56 # --- Code cell 59 --- deep_learning45 # --- Code cell 60 --- machine learning=4.15 # --- Code cell 61 --- any@=345 # --- Code cell 62 --- 123="rahul" # --- Code cell 63 --- a123="rahul" # --- Code cell 64 --- # reassigning a variable # --- Code cell 65 --- age=20 # --- Code cell 66 --- age=21 # a variable value can change at any point of time # --- Code cell 68 --- # assigning multiple variables in a single line # --- Code cell 69 --- rahul,suresh,jhon,tarun=65,67,88,99 # --- Code cell 70 --- print(jhon) # --- Code cell 71 --- # assigning multiple variables of same value # --- Code cell 72 --- tarun=tanish=tanamyi=100 # --- Code cell 73 --- tanamyi # --- Code cell 74 --- tarun=tanish=tanamyi=100 # --- Code cell 75 --- print(tanamyi) # --- Code cell 76 --- # Memory allocation in variable assignment Q--How variables are stored in memory? A---python stores variables as objects in memory # --- Code cell 77 --- The id() function returns a unique id for the specified object. # --- Code cell 78 --- age=56 # --- Code cell 79 --- type(age) # --- Code cell 80 --- age=float(age) # reloaction # --- Code cell 81 --- age # --- Code cell 82 --- type(age)