Python Full Notes Apna College
Python Full Notes Apna College
Translator
(Compiler /Interpreter)
Machine Code
What is Python?
Python is simple & easy
Free & Open Source
Portable
Our First Program
print("Hello World")
Python Character Set
Letters – A to Z, a to z
Digits – 0 to 9
name = "Shradha"
age = 23
price = 25.99
Memory
name = "Shradha"
age = 23
price = 25.99
Rules for Identifiers
Data Types
Integers
String
Float
Boolean
None
Data Types
Keywords
Keywords are reserved words in python.
"""
Multi Line
Comment
"""
Types of Operators
An operator is a symbol that performs a certain operation between operands.
Arithmetic Operators ( +, - , * , / , % , * * )
a, b = 1, 2.0
sum = a + b
#error
a, b = 1, "2"
sum = a + b
Type Casting
a, b = 1, "2"
c = int(b)
sum = a + c
Type Casting
Input in Python
input( ) statement is used to accept values (using keyboard) from user
Basic Operations
concatenation
length of str
len(str)
Indexing
A p n a _Co l l e g e
0 1 2 3 4 5 6 7 8 9 10 11
str = “Apna_College”
str = “ApnaCollege”
str[ 1 : 4 ] is “pna”
Appl e
-5 -4 -3 -2 -1
str = “Apple”
str[ -3 : -1 ] is “pl”
String Functions
str = “I am a coder.”
if(condition) :
Statement1
elif(condition):
Statement2
else:
StatementN
Conditional Statements
Grade students based on marks
tup1 = ( )
tup2 = ( 1, )
tup3 = ( 1, 2, 3 )
Tuple Methods
tup = (2, 1, 3, 1)
WAP to check if a list contains a palindrome of elements. (Hint: use copy( ) method)
Store the above values in a list & sort them from “A” to “D”.
Dictionary in Python
Dictionaries are used to store data values in key:value pairs
“key” : value
student[”score”][”math”]
Dictionary Methods
myDict.keys( ) #returns all keys
nums = { 1, 2, 3, 4 }
set2 = { 1, 2, 2, 2 }
#repeated elements stored only once, so it resolved to {1, 2}
You are given a list of subjects for students. Assume one classroom is required for 1
subject. How many classrooms are needed by all students.
Figure out a way to store 9 & 9.0 as separate values in the set.
(You can take help of built-in data types)
Loops in Python
Loops are used to repeat instructions.
while Loops
while condition :
#some work
Continue : terminates execution in the current iteration & continues execution of the loop
with the next iteration.
for Loops
for el in list:
#some work
for el in list:
#some work
else:
else used as it doesn’t execute
#work when loop ends when break is used
Let‘s Practice
using for
Print the elements of the following list using a loop:
for el in range(10):
pass
print( )
len( )
type( )
range( )
Default Parameters
Assigning a default value to parameter, which is used when no argument is passed.
Let‘s Practice
WAF to print the length of a list. ( list is the parameter)
WAF to print the elements of a list in a single line. ( list is the parameter)
#prints n to 1 backwards
Base case
Recursion
#returns n!
Let‘s Practice
Write a recursive function to calculate the sum of first n natural numbers.
data = f.read( )
f.close( )
Reading a file
import os
os.remove( filename )
Let‘s Practice
Create a new file “practice.txt” using python. Add the following data in it:
Hi everyone
we are learning File I/O
using Java.
I like programming in Java.
WAF that replace all occurrences of “java” with “python” in above file.
From a file containing numbers separated by comma, print the count of even numbers.
OOPin Python
To map with real world scenarios, we started using objects in code.
#creating class
class Student:
name = “karan kumar”
s1 = Student( )
print( s1.name )
Class & Instance Attributes
Class.attr
obj.attr
__init_ _Function
Constructor
All classes have a function called __init__(), which is always executed when the object is being
initiated.
class Student:
@staticmethod #decorator
def college( ):
print( “ABC College” )
Encapsulation
Wrapping data and functions into a single unit (object).
Let‘s Practice
Create Account class with 2 attributes -balance & account no.
Create methods for debit, credit & printing the balance.