Python is a popular programming language.
It was created by Guido van Rossum, and
released in 1991.
It is used for:
● web development (server-side),
● software development,
● mathematics,
● system scripting.
What can Python do?
● Python can be used on a server to create web applications.
● Python can be used alongside software to create workflows.
● Python can connect to database systems. It can also read and modify files.
● Python can be used to handle big data and perform complex mathematics.
● Python can be used for rapid prototyping, or for production-ready software
development.
Why Python?
● Python works on different platforms (Windows, Mac, Linux, Raspberry Pi, etc).
● Python has a simple syntax similar to the English language.
● Python has syntax that allows developers to write programs with fewer lines than
some other programming languages.
● Python runs on an interpreter system, meaning that code can be executed as
soon as it is written. This means that prototyping can be very quick.
● Python can be treated in a procedural way, an object-oriented way or a functional
way.
● Python was designed for readability, and has some similarities to the English
language with influence from mathematics.
● Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
● Python relies on indentation, using whitespace, to define scope; such as the
scope of loops, functions and classes. Other programming languages often use
curly-brackets for this purpose.
Python Indentation
Indentation refers to the spaces at the beginning of a code line.
Where in other programming languages the indentation in code is for readability only,
the indentation in Python is very important.
Python uses indentation to indicate a block of code.
Python Comments
Comments can be used to explain Python code.
Comments can be used to make the code more readable.
Comments can be used to prevent execution when testing code
Variables
Variables are containers for storing data values.
Creating Variables
Python has no command for declaring a variable.
A variable is created the moment you first assign a value to it.
Variables do not need to be declared with any particular type, and can even change
type after they have been set
.x = 5
y = "John"
print(x)
print(y)
Casting
If you want to specify the data type of a variable, this can be done with casting.
x = str(3) # x will be '3'
y = int(3) # y will be 3
z = float(3) # z will be 3.0
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age,
carname, total_volume).
Rules for Python variables:
● A variable name must start with a letter or the underscore character
● A variable name cannot start with a number
● A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
● Variable names are case-sensitive (age, Age and AGE are three different
variables)
● A variable name cannot be any of the Python keywords.
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
Iilegal variable
2myvar = "John"
my-var = "John"
my var = "John"
Remember that variable names are case-sensitive
Python allows you to assign values to multiple variables in one line:
Example
Get your own Python Serve
rx, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
: Make sure the number of variables matches the number of values, or else you will get
an error.
One Value to Multiple Variables
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
Output Variables
The Python print() function is often used to output variables.
Example
Get your own Python Server
x = "Python is awesome"
print(x)
In the print() function, you output multiple variables, separated by a comma:
Example
x = "Python"
y = "is"
z = "awesome"
print(x, y, z)
You can also use the + operator to output multiple variables:
Example
x = "Python "
y = "is "
z = "awesome"
print(x + y + z)
Notice the space character after "Python " and "is ", without them the result would be
"Pythonisawesome".
For numbers, the + character works as a mathematical operator:
Example
x = 5
y = 10
print(x + y)
In the print() function, when you try to combine a string and a number with the +
operator, Python will give you an error:
Example
x = 5
y = "John"
print(x + y)
The best way to output multiple variables in the print() function is to separate them
with commas, which even support different data types:
Example
x = 5
y = "John"
print(x, y)
Built-in Data Types
In programming, data type is an important concept.
Variables can store data of different types, and different types can do different things.
What is a data type in Python? —
Think of a data type as the label that tells Python what kind of thing a value is — like
whether it’s a whole number, some words, true/false, or a collection of items. Just like
you put fruits, books, and toys in different boxes, Python keeps different kinds of
information in different types so it knows what you can do with them.
Basic built-in data types (with simple
examples)
1. Integer (int)
Whole numbers with no decimal part: -5, 0, 42
a = 12
print(type(a)) # <class 'int'>
What you can do: add, subtract, multiply, use in loops and counts.
2. Float (float)
Numbers with decimals: 3.14, -0.5, 0.0
b = 3.5
print(type(b)) # <class 'float'>
Used when you need fractions or decimals (measurements, averages).
3. String (str)
Text — letters, words, sentences. Always put text inside quotes: 'hello' or "Hi!".
name = "Diksha"
print(type(name)) # <class 'str'>
You can join strings (+), get characters, measure length.
4. Boolean (bool)
Only two values: True or False. Used for yes/no questions and conditions.
is_raining = False
print(type(is_raining)) # <class 'bool'>
Often appears from comparisons: 5 > 3 gives True.
5. List (list)
An ordered collection of items (can mix types). Written with square brackets [].
fruits = ["apple", "banana", "mango"]
print(type(fruits)) # <class 'list'>
print(fruits[0]) # "apple" (indexing starts at 0)
Lists are mutable — you can change items: fruits[1] = "orange".
6. Tuple (tuple)
Like a list but immutable (cannot change after creation). Written with parentheses ().
coords = (10, 20)
print(type(coords)) # <class 'tuple'>
Good when values must stay fixed.
7. Set (set)
An unordered collection of unique items (no duplicates). Written with {} but different
from dict.
letters = {"a", "b", "c"}
print(type(letters)) # <class 'set'>
Useful for removing duplicates or doing math-like operations (union, intersection).
8. Dictionary (dict)
Stores pairs of key: value. Use curly braces {} with colons.
student = {"name": "Asha", "age": 13}
print(type(student)) # <class 'dict'>
print(student["name"]) # "Asha"
Keys let you look up values quickly (like a real dictionary: word → meaning).
9. NoneType (None)
Represents “nothing” or “no value”. Used when something has no value yet.
x = None
print(type(x)) # <class 'NoneType'>
Mutable vs Immutable (important idea)
● Mutable (can change): list, dict, set
● Immutable (cannot change): int, float, str, tuple, bool
Example: strings are immutable — you can’t change a character inside a string; you
make a new string instead.
How to check a value’s type
Use the type() function:
print(type(5)) # <class 'int'>
print(type("hello")) # <class 'str'>
Converting between types (casting)
Sometimes you need to change a value from one type to another:
num_str = "10"
num = int(num_str) # converts to integer 10
f = float(num) # now 10.0
s = str(3.14) # "3.14"
Important: int("3.5") will cause an error; you must convert to float first:
int(float("3.5")).
Common pitfalls (for beginners)
● input() in Python returns a string. If you want a number, convert it:
age = int(input("Enter your age: "))
● Indexing starts at 0: first item is list[0].
● Strings use quotes. Forgetting quotes causes errors: name = John → error;
name = "John" → OK.
● Mixing types in arithmetic: "5" + 3 causes an error. Convert first: int("5") +
3.