Why to program
- To do tasks that require a lot of computation
- To automate repetitive tasks
- To improve user experience
- To simplify a complicated task
- To perform tasks that humans are not good at or don’t
want to perform.
What is a program
- A program is a set of instructions that a computer follow
to perform a specific task.
For Example:
- Adding two numbers.
- Computing mean and variance of a distribution
- Recommending movies to watch that you might be
interested in.
- coming up with best route on a map between two
locations.
How to program
- You program a computer with a programming language. It
is used to communicate instructions to a computer.
Languages:
- Low level: Machine code, Assembly
- Middle level: C
- High level: C++, Python, Java, etc
Low level languages are closer to hardware and we have
more control over them, but they are less human readable.
Compiler Vs Interpreter
- A compiler translates an entire high-level program into
machine code before execution, producing an executable
file. Ex, C, C++, Java etc.
- An interpreter translates and executes the code line by
line during runtime, without generating an executable file.
Ex, Python, JavaScript etc.
Installing python
- Visit [Link]/downloads and download the version
you want to use. For example, python 3.13.5 (major,
minor, patch)
- Install the executable file and be sure to tick “Add python
to PATH”.
- To check installation, write “python --version” in terminal.
Where to write python
program
- In terminal using python REPL (Read-Eval-Print Loop). Use
when you want to quickly test a short program.
Where to write python
program
- In a text editor like Visual Studio Code, Atom, Sublime, etc
- In an Integrated development environment (IDE) like
pycharm, spyder, etc
- In Jupyter Notebook (pip install jupyter notebook)
- On online services like google colab or Kaggle notebooks.
Note that python file has “.py” as extension, but python
notebooks has “.ipynb” as extension.
Applications of python
Python can do almost anything that you can think of.
- AI, Machine Learning, Deep Learning
- Game Development (pygame)
- Visualization (Matplotlib, seaborn)
- Web Development (Flask, Django, pyscript)
- Dashboards for data visualization (plotly)
- GUI application (Tkinter)
- And much more…
Constants
- A constant is a named value that is intended to remain
unchanged throughout the execution of a program.
- Constants are typically defined using all uppercase letters,
with words separated by underscores, For Example, PI,
MAX_LIMIT, etc.
- Note that python does not enforce immutability for
constants!
Variables
- A variable is a named place in the memory where a
programmer can store data and later retrieve the data
using the variable’s name.
- You get to choose a variable’s name, For Example, speed,
x_position, model, etc.
- As the name suggests, the value of a variable can change
throughout the execution of the program
- Variable name must start with letter or underscore. It can
contain numbers after 1st letter or underscore. Variable
names are case sensitive.
Variables
Reserved Words
- If we use reserved words, we must use them to mean the
thing python expects them to mean.
Reserved Words
Arithmetic Operators
Arithmetic Operators
Order of evaluation
- When we have a bunch or operators in an expression,
python must know which one to do first. This is called
“operator precedence”.
- Parenthesis -> Exponentiation (raised to a power) ->
Multiplication/Division/Modulus/floor division ->
Addition/Subtraction.
- If the expression has same precedence, then evaluate
from left to right.
- For ex, 1+8*6/3 -> 17
Questions
Answers
Assignment Operators
Assignment Operators
Comparison Operators
Logical Operators
Identity Operators
Membership Operators
Data Types
Variables can store data of different types and these types
can do different things. The most common data types are:
- Text type (string) -> str
- Numeric type -> int, float
- Sequence type -> list, tuple
- Mapping type -> dict
- Set type -> set
- Boolean type -> bool
- None type -> NoneType
Data Type conversions
You can convert one data type into another using built-in
python functions.
- str() -> tries to convert input type into string type
- int() -> tries to convert input into integer
- float() -> tries to convert input into float
- bool()
Note that if the function can’t convert input then it’ll give an
error.
Conditionals (one way)
- Conditionals are used to make choices. The program will
execute that block of code for which the condition is True.
Conditionals (one way)
Nested Conditionals
Conditionals (two way)
Conditionals (multi way)
Question
Answer
else block in left code and “elif x < 10” block in right code
Loops and Iterations
- With loops, we can make computers do repetitive tasks.
- Python has two loops, while loop and for loop.
- While loop keeps on executing the code present in it’s block
until the condition provided is True. Once the condition
becomes False, the loop breaks and program gets out of the
loop.
- For loop requires a iteration variable initialization that traverses
over a container or sequence. Once the container or sequence
is exhausted, the loop breaks and program gets out of the loop.
- Whatever you can code with “for” loops, you can code with
“while” loops. It’s just that for some problems “for” loop is
easier to use while for others “while” loop is easier.
while loops
- Each iteration, there must be some change in the
condition, so that it eventually becomes False.
Infinite loops
- If you don’t change anything In the condition, the
program will go in an infinite loop, which can freeze your
system
Breaking out of loops
- You can use “break” keyword inside a loop and whenever
the program executes it, it’ll get out of loop immediately.
Skipping an iteration
- You can use “continue” keyword inside a loop and
whenever the program executes it, it’ll get out of the
current iteration immediately.
For loops
- For loop traverses through a finite amount of elements.
These elements can be of any sequence or container type
like strings, list, tuple, etc.
For loops
For loops