Recap Python Language Syntax and Functions

My Python Documentation:-

#       /''''''''''''''''''''''''''''''''''''''''''/
#      / Doc: My Python Documentation             /
#     /  By & For : Huzaifa Asim                /
#    /   CodeWithHarry ♡♡♡                   /
#   /__________________________________________/

# MODULES AND IMPORT STATEMENST
from ast import operator
import cv2
import math

from setuptools import sic
from symbol import argument

# First print
print("Hello World")
print(5+8)

# COMMENT IN PYTHON. just press Ctrl + / in VS Code or type
'''
Multiline
Comment
'''

#VARIABLES AND DATATYPES
a = 45 #integer
b = "Huzaifa" #string
c = 45.5454 #float
print(a+c)
print(type(a))

# TYPE CASTING
d = "31"
d_convert_int = int(d)
d_convert_float = float(d)
d_convert_string = str(d)

# STRING
sentence = "Huzaifa is a good boy."
print(sentence[1]) #print u
print(sentence[2:6]) #print zaif - starts with 2 and end with less than 6

spaces_sentence = "      Huzaifa is the best boy."
print(spaces_sentence) #       Huzaifa is the best boy.
print(spaces_sentence.strip()) # remove extra spaces from start and end / Huzaifa is the best boy.

print(len(sentence)) #print length /22

print(sentence.lower()) #convert into lowercase / huzaifa is a good boy.
print(sentence.upper()) #convert into uppercase / HUZAIFA IS A GOOD BOY.

replace_sentence = sentence.replace("zaif", "taiq") # replace
print(replace_sentence) # Hutaiqa is a good boy.

# usecase
names_collab = "Atif Aslam, Neha Kakkar, Ava Max, Dua Lipa"
print(names_collab) # Atif Aslam, Neha Kakkar, Ava Max, Dua Lipa
names_collab = names_collab.replace(', ', '\n')
print(names_collab) #print like below
# Atif Aslam
# Neha Kakkar
# Ava Max
# Dua Lipa

str1_name = "Huzaifa "
str2_movie = "Spiderman No Way Home"
print(str1_name + str2_movie) # Huzaifa Spiderman No Way Home

# usecase
#also you can specify with numbers like {0}, {1}, and you can moved position as well.
template = "His name is {}, and he is watching {} movie.".format(str1_name, str2_movie)
print(template) # His name is Huzaifa , and he is watching Spiderman No Way Home movie.

template2 = f"His name is {str1_name}, and he is watching {str2_movie} movie."
print(template2) # His name is Huzaifa , and he is watching Spiderman No Way Home movie.

# SOME ADVANCED OPERATORS
# 1. ** / Exponentiation operator / 2 to the power 3 will be printed
# 2. // / Floor division operator / It divides but return an integer
# 2. % Modulo operator / Remainder


# PYTHON COLLECTIONS
# 1. list - []
# 2. tuple - ()
# 3. set - {}
# 4. dictionary {:}

# 1. LIST
list1 = [1, 2, 5, 45, 65, 12]
print(type(list1))
list1Slicing = list1[2]
print(list1Slicing) # 5

list1Slicing1 = list1[2:5]
print(list1Slicing1) # [5, 45, 65]

print(len(list1)) # 6

# Functions of lists
list2 = [1, 2, 3, 4, 5]

list2.append(100)
print(list2) # [1, 2, 3, 4, 5, 100]

list2.insert(1, 200)
print(list2) # [1, 200, 2, 3, 4, 5, 100]

list2.remove(3)
print(list2) # [1, 200, 2, 4, 5, 100]

list2.pop()
print(list2) # [1, 200, 2, 4, 5] / remove from end

del list2[3]
print(list2) # [1, 200, 2, 5]

list2.clear()
print(list2) # []

del list2 # List has been deleted, Now list2 is not available

# 2. TUPLE - Unchangable
tuple_a = ("Babar Azam", "Shaheen Afridi", "Eion Morgan", "Moeen Ali")
print(type(tuple_a))

# tuple_a[1] = "Naseem Shah" # Cannot do this because it's unchangable

# You can apply most of the funcions of Lists here

# 3. SET - no repeat element
s1 = {14, 2, 3, 3, 3, 3, 45, 65, 3, 3, 3, 3, 3, 3, 4}
print(s1) # {65, 2, 3, 4, 45, 14}

s1.add(789)
print(s1) # {65, 2, 3, 4, 789, 45, 14}

s1.update([798, 654, 4654, 5786, 565765, 454, 22, 55, 89])
print(s1) # {65, 2, 3, 4, 565765, 454, 654, 14, 789, 22, 89, 5786, 798, 45, 4654, 55}
print(len(s1)) # 16

s1.discard(6666666666) # Remove is stricked and discard() don't through error
print(s1)

# 4. DICTIONARY
paperDict = {
    'Name' : 'Huzaifa',
    'Class' : '5th',
    'Percentage' : 32.45,
    'Grade' : 'Fail'
}
print(paperDict) # {'Name': 'Huzaifa', 'Class': '5th', 'Percentage': 32.45, 'Grade': 'Fail'}

# print(idDict[1]) # Error
print(paperDict['Percentage']) # 32.45

paperDict['Percentage'] = 98.97
paperDict['Grade'] = 'A+'
print(paperDict) # {'Name': 'Huzaifa', 'Class': '5th', 'Percentage': 98.97, 'Grade': 'A+'}
# del, clear, pop, update, etc... can be used

# CONDITION - IF ELSE
# input
# age = input("Enter your age\n") # as string
age = int(input("Enter your age\n")) # as string
print(age)

if(age > 18):
    print('You can drive.')
elif(age == 18):
    print('Wait... ok, You can drive')
else:
    print('You cannot drive')

# LOOPS
# FOR
# WHILE

# FOR  
# Scenario: If you have to print number from 1 to 1000
for i in range(0, 1000):
    print(i)

# Scenario: get all items from list or collection
list_for_for = [1212, 4545, 'dfsadsa', 4545, 0]
for item in list_for_for:
    print(item)

# WHILE
i = 0
while(i < 30):
    i = i + 1
    if i == 10:
        continue # it will skip 10
    if i == 20:
        break # it'll stops loop at 19
    print(i)

# FUNCTIONS
def greeting():
    print("Hello, Good Morning! Subah men")
    print("Hello, Good Evening! Shaam men")
    print("Hello, Good Night! Raat men")
greeting()

# Functions with arguments and return
# We can also set default arguments as well like sum_func(a = 5, b)
def sum_func(a, b):
    c = a + b
    return c
# sum_func(2, 5) # nothing will happen directly because we're returning not printing here
d = sum_func(2, 5) # It'll work
print(d) # 7

# OOPs - is like a template
class Employee:
    def __init__(self, hname, hsalary): #constructor
        self.name = hname
        self.salary = hsalary

printClass = Employee("Huzaifa", 45)
print(printClass.name) # Huzaifa
print(printClass.salary) # 45


Post a Comment

0 Comments

© Copyright HunerOn Codes. All right reserved

HunerOn®