Python Notes
Python Notes
INTRODUCTION
SET - TUPLES
Python is a programming language, just like java or javascript. It is very easy to learn and
very easy to get started with.
Advantage -
• Easy to learn
- Simple syntax
- Easy to setup
• Large Ecosystem
- large community
- many community
Usage -
Famous Usages -
Mobile Development
Game Development
Desktop Development
https://www.python.org/downloads/
Installation on Windows
Run the python installer once downloaded
***Very Important - Make sure to select “Add Python 3.10 to PATH” & “Install
launcher for all user“ checkbox
Press Windows+X to open power users menu and then click “Command Prompt” or
“Command Prompt (Admin)”
Installation on MAC is fairly simple, Run the python installer once downloaded and just
click on continue and install once asked
Once Installed, Open “Terminal” through spotlight search, and type “python —version”
For "python —version” you will get “Python 2.x” as result as this is the default python
installed on Mac. For "python3 —version” you will get “Python 3.x”, which we just
installed.
You can use any other code editor like Visual Studio code as well, Or even write python
code without any code editor but Pycharm is the most popular and highly used code
editor for whiting python code, so we will use PyCharm for this tutorial.
Download the free and open source community version, as that is more than enough for us.
Installation of pycharm is very simple on both Windows and MAC, just click on the installer
once it is downloaded and follow the screens to install. For Windows, change the Install
Location if needed. And for MAC, Once installed move PyCharm to Applications.
Thats it! We are all set to create our First Python Application with PyCharm.
Enter your project name in the next screen, and change you project location to your preferred
location. For MAC, make sure Python3 is selected in Base Interpreter.
Our Code
Interpreter
Machine Code
NOTE*** Base Interpreter - Executes our Python program by converting our python code to
machine readable binary code.
print(“Hello World”)
- output
Hello World
Variables - A variable is a container that holds information that you can access later.
age = 20
name = “Rahul”
print(age)
print(name)
- output
20
Rahul
age = 30
- output
30
String - Strings can hold any text like your name, address
name = “Rahul”
address = “34th Street, 5th Avenue”
Int - Int can hold any number like your age, score in game
age = 20
score = 100
bool - bool can hold true/false value like weather you are vegetarian or not
is_vegetarian = True
is_non_vegetarian = False
There are many other data type, which we will later on.
- output
What is your name? Rahul
Hi Rahul
IF Else Condition - To take decision based on some condition we use if else condition in
programming.
is_veg = False
is_egg = True
if is_veg:
print(name + " is vegetarian")
elif is_egg:
print(name + " is eggetarian”)
else:
print(name + " is not vegetarian")
- output
What is your name? Rahul
Rahul is eggetarian
Problem Statement : We will ask user three question and check it with correct answer.
if answer1 == "Lion"
print("Correct Answer!!"
else
print("Game Over"
quit(
if answer2 == "Giraffe"
print("Correct Answer!!"
else
print("Game Over"
quit(
if answer3 == "Ostrich"
print("Correct Answer!!"
else
print("Game Over"
quit(
Syntax -
while condition:
// Code that executes in loop until the while condition is true
else:
// code that executes once, when while condition is false
Ex -
i=0
while i < 5:
I=I+1
print(f“Hello {I}”) // This is how we do string formatting in python
else:
print(“Number is bigger then 5”)
- Output
Hello 1
Hello 2
Hello 3
Hello 4
Hello 5
Number is created then 5
PRACTICE : GUESS THE NUMBER - Make a game where user need to guess a number, and
user will be given 3 attempts to guess the correct number.
number = 1
attempt_limit =
i =
if int(guess_number) == number
print("You Win!!"
else
i = i +
else
)
:
List - List data type is use to store multiple items, list is changeable, ordered and allow duplicate
values
For Loop - In python we can use for loop to iterate through each item in any List or sequence.
Ex -
for fruit in fav_fruits:
print("I like " + fruit)
- Output -
I like Grapes
I like Banana
I like Mango
for i in "PYTHON":
print(i)
- Output -
P
Y
T
H
O
N
Tuples - This also stores multiple values. Tuples are unchangeable, immutable and ordered.
Dictionary - With dictionary also we store multiple values, the bene t is we can store key and
value in dictionary, and it does not allow duplicate keys.
Ex -
person_dict = {
“Name” : “Rahul” ,
“Age” : 30,
“Occupation” : “Software Engineer”
}
output -
{'name': 'Rahul', 'age': 30, 'Occupation': 'Software Engineer', 'address': '34th Street'}
output -
{'name': 'Rahul', 'age': 30, 'Occupation': 'Software Engineer'}
output -
name Rahul
age 30
Occupation Senior Software Engineer
question_dict =
user_answer = input(question
if user_answer == answer
print("Correct Answer!!"
else
print("Game Over"
brea
"
Functions - As the name suggest, function is task. Like eating, running and jumping in real world
is a task.
To perform any task in programming we use function. A function is block of code which is written
to perform speci c task.
Ex -
def function_name():
print("Hello from a function")
function_name()
- Output -
Hello from a function
print(square_root(3))
- Output -
9
def quiz_game(question_dict)
user_answer = input(question
if user_answer == answer
print("Correct Answer!!"
else
print("Game Over"
brea
animal_question_dict =
quiz_game(animal_question_dict
capitals_question_dict =
quiz_game(capitals_question_dict
Package - A package is group of module and a special le __init__.py. Package can also
have sub packages as well. Purpose of package is to group similar modules with same
agenda and distribute it to developers for reusability.
Ex - Django - Third Party Package to make web application
How can we install third party packages? We can check all the third party package from
https://pypi.org/ and install them using the instruction given. Like to install Django package
we can use below command -
pip install django
Excel Automation - For excel automation we need to use a third party package
“openpyxl”. So rst, we need to install this package using - pip install openpyxl
import openpyxl as x
wb = xl.load_workbook('employee.xlsx'
sheet = wb['Sheet1'
cell = sheet.cell(row, 3
else
increased_salary_cell = sheet.cell(row, 4
increased_salary_cell.value = increased_salar
wb.save('employee2.xlsx'
fi
]
)
)
fi
)
fi
)
Object Oriented Programming, also knows as OOPS, is a programming paradigm which many
programming languages follow. In this paradigm, everything in software design revolves around
class and objects.
def ClassName:
def any_function_name(self):
print(“function de nition”)
Object -
object_name = ClassName(parameter_value)
object_name.any_function_name()
Example
def User:
Object -
user = User(“Rahul”)
user.change_user_name(“Rohit”)
- Output -
Username changed to Rohit
Once installed you should be able to see Anaconda Navigator in your launchpad or start menu.
Click on that and you should see this below window -
Anaconda, installs everything that you may require for data analysis, machine learning and also
for data science. We will only work with Jupiter Notebook. So click on the launch button to launch
the Jupiter notebook or you can open command prompt/ terminal and run the command - “jupiter
notebook”, and this should open Jupiter notebook in your preferred browser.
Now in browser, select the folder where you want to create your project. and then click on “New”,
to create new project, and select python3 notebook .
Simple chart -
Bar Chart -