PYTHON NOTES | REFERENCE | SOURCE CODE | CODE WITH HARRY





#  WRITING OUT FIRST PROGRAM 😎 😎


print("Hi, Farooq!")

# Creating a variable in python

a = "Farooq"    #String
b = 45          #integer
c = 45.67       #floating point
d = True        #Boolean
e = False       #None

#  Printing the variables

print(a)        #Output Farooq
print(b)        #Output 45
print(c)        #Output 45.67

#  Printing the type of variable

print(type(a))  #<class 'string'>
print(type(b))  #<class 'int'>
print(type(c))  #<class 'float'>


#  Operation in python

a = 4
b = 5

print("The sum of a and b is: ",a + b) #output is 9
print("The subtraction of a and b is: ",a - b) #output is -1
print("The multiplication of a and b is: ",a * b) #output is 45
print("The division of a and b is: ",a / b) #outpu is 0 point something

# Assignment Operator

# == += -= *= /=

# Comparison Operator

l = (14>7)  #True
l = (14<7)  #False
l = (14==7) #True
l = (14!=7) #True
l = (14>=7) #True

print(f"the value of l is: {l}")

# Logical Operator


bool1 = True
bool2 = False

# Printing the bool1 and bool2 with logical operators

print("The value of bool1 and bool2 is", (bool1 and bool2))
print("The value of bool1 and bool2 is", (bool1 or bool2))
print("The value of bool1 and bool2 is", (not bool2))

# Type Casting in Python

# int() str() float() bool() complex()


# type function ()

# It is used to check the data type of a variable


# input() in python

# it take input as a string


# Chapter 3 - Strings

# Concatenation two string

greet = "Good Morning, "
name = "Farooq"

print(greet+name)

# Accessing String

my_name = 'Farooq'

print(my_name[0]) #o/p F
print(my_name[1]) #o/p a
print(my_name[2]) #o/p r
print(my_name[3]) #o/p o

# String Slicing

name_slice = 'Farooq'

print(name_slice[1:4]) #aro

print(name_slice[:])

print(name_slice[:4])

# Negative Indexing

print(name_slice[-1])

# Slicing with skip value

word = "Amazing"

print(word[1:6:2])  #'mzn'

# String Function

# len() --> Counts the number of characters

story = "Farooq"

print(len(story))

# endswith() --> return in boolean whether character is present or not.

story2 = "Farooq"

print(story2.endswith("ooq"))  #True

# count()

story3 = "Once upon a time Once"

print(story3.count('O')) # 2

print(story3.count('Once')) #2

# Capitalize()

story5 = "farooq"

print(story5.capitalize()) #Farooq

# find()

story6 = "farooq"

print(story6.find("rooq")) #2

# Give index location of the first same character

# replace()

story7 = "Farooq"

print(story7.replace("Farooq","farooq")) #farooq

# We can store this replaced version of the string in a different variable but
 the original will remain the same as string are immutable

print(story7)  #Farooq
# which mean it not changed the original value here.

# Escape squence character

# Example :

# \n (newline)
# \t (tab)
# \' (single quote)
# \\  (backlash)

# Chapter 4 List and tuple

# 1 Create a list using []
# 2 List is a set of ordered items
# 3 List is Mutable
# 4 List Element can be access by their index

a = [1,2,3,4,5,6,7]

# Print the list using print () using function

a[0] = 3
# Can't write like this 🚫 a[0] = "Farooq" 🚫

print(a)

# We can create a list with items of different data types

different_type_list = ["farooq", 7, True]

# List slicing

college_friends = ["farooq", "Madesh", "Tamil", "Krishna Keerthan","Sarveshwaran"]

print(college_friends[0:4])

# Output ['farooq', 'Madesh', 'Tamil', 'Krishna Keerthan']

# List Methods

# sort() = sorts the list
# reverse () = reverses the list
# append () = adds element at the end of list
# index() = adds an element at a particular position without removing
element at that position
# pop () = Removes last element in a list
# remove () = remove an element of your wish

l1 = [1,2,3,5,5]
l1.reverse() # Reverse the elements present in l1

l1.append(8) # adds 8 to the end of last element

l1.index(1,3) # Adding an element at particular index

l1.pop() # Removes last element from a list by default if blank

l1.remove(2) # removes the element which name as 2

l1.sort() # sort all the elements in l1

print(l1) # printing l1

# Tuple

# Create tuple using ()

t = (1,2,3,4,5)

# printing tuple elements
print(t)

t1 = ()  #Empty tuple

single_tup = (1,)  #Tuple with single element

single_tup = (1) #🚫 This will throw an error


# Can not Update the tuple(immutable)
#t[0] =78   #This will throw an error 🚫

# Tuple Method

count_tup = (1,2,3,4,5,6,7,1)

print(count_tup.count(1))
# 2 times 1 is appeared in the count_tup

print(count_tup.index(5)) # 4


# <--- Chapter 5 ----->


myDict = {
    "Fast": "In a Quick Manner",
    "Harry": "A Coder",
    "Marks" :[1,2,3,4] ,
    "anotherdict" : {"farooq": 'player'}
}

# Printing the dictionaries values through keys

print(myDict['Fast'])  # In a Quick Manner
print(myDict['Harry'])  # A Coder

# Changing the value of Marks

myDict['Marks'] = [45,56]
# This will change the value of 'Marks'

print(myDict['Marks']) # [45, 56]

# Printing the nested dictionary

print(myDict['anotherdict']['farooq']) #player

# Dictionary Method

myDict = {
    "Fast": "In a Quick Manner",
    "Harry": "A Coder",
    "Marks" :[1,2,3,4] ,
    "anotherdict" : {"farooq": 'player'},
    1:2
}

# Prints the keys of the dictionary
print((myDict.keys()))
# dict_keys(['Fast', 'Harry', 'Marks', 'anotherdict', 1])

# Prints the values of the dictionary
print(myDict.values())
# dict_values(['In a Quick Manner', 'A Coder', [1, 2, 3, 4], {'farooq': 'player'}, 2])

# Prints the (key,value) for all contents of the dictionary

print(myDict.items())

# dict_items([('Fast', 'In a Quick Manner'), ('Harry', 'A Coder'),
('Marks', [1, 2, 3, 4]), ('anotherdict', {'farooq': 'player'}), (1, 2)])

# Updating Dictionary

updateDict = {
    "lovish":"Friend",
    "Divya":"Friend",
    "Shubham":"Friend"
}

# myDict.update(updateDict) #Updates the dictionary by adding key-value pairs from
updateDict  type: ignore
print(myDict)
# {'Fast': 'In a Quick Manner', 'Harry': 'A Coder', 'Marks': [1, 2, 3, 4],
'anotherdict': {'farooq': 'player'}, 1: 2, 'lovish': 'Friend',
 'Divya': 'Friend', 'Shubham': 'Friend'}


print(myDict.get("Harry2"))  #Returns 'None' as harry2 is not present in
the dictionary
print(myDict.get("Harry2"))  #This will throw an 'error' as harry2 is not present
 in the dictionary


# set in python

a = {1,2,3,5,1}
print(type(a))  # <class 'set'> but empty set {} is <class 'dict'>
print(a)  # {1,2,3,5} 1 is repetative so neglected

# Empty set can be created using this syntax

b = set()
print(type(b))

# Set Method

# Adding values to an empty set

b.add(5)
b.add(6)
b.add(7)
b.add((1,2,3))  #tuple can be added
b.add([1,2,4])  #List is Hashable cannot be added
b.add({4:5})  #Dictionary is Hashable cannot be added
print(b)

# len ()

print(len(b))  #print the length of this set

b.remove(5) #Remove 5 from set b
b.remove(15)  #throws an error while trying to remove 15
#  (which is not present in the set)

print(b.pop())  #Removes last element
print(b)

# Conditional Statement in python

# if else and elif

age = int(input("Enter your age: "))

if age> 18:
    print("You can drive")
else:
    print("You can not drive")

# You can use elif for multiple conditions

a = None

if a is None:
    print("Yes")
else:
    print("No")

l = [45, 56, 6]
print(435 in l)  #False

# for loop in python

# <____Code Will Be Written Here____>


# File I / O

# Use open function to read the content of a file!

open_file = open("practice_set.py", 'r')
open_file = open("practice_set.py", 'r')   #By default the mode is 'r'

# Use read funtion to read the file

data = open_file.read()
data = open_file.read(5)  #reads the first 5 in the file

# Use close function to close the file

open_file.close()

print(data)

# Readline

f = open("sample.txt", 'r')

# Read first line
data = f.readline()
print(data)

# reads second line
data = f.readline()
print(data)

# reads third line
data = f.readline()
print(data)

# reads fourth line
data = f.readline()
print(data)


# Writing file

fw = open("another.txt", 'w')
fw.write("This is written by me!")
fw.close()

# with statement

# Definition: #This with statement is very helpful because we don't need to
write close()


# OOPs in Python

# To be Continued

Post a Comment

0 Comments