Python Basics
Python Basics
# a = int(a)
# b= int(b)
c = a+b
print(c)
'''
from typing import List
# F funtion
'''
f function (To write the string and variable value together
name = "python"
number = int(123)
number2 = int(10)
# Arrays in python
'''
# Arrys in python
in pyhton we can store different type of data in an array
print(names[1])
# Array Slicing
print(names[0:3]) ----> it will give the items from 0 to 2, it excludes
the written index
#Other functions
to show length
len()
to add at the end in array, also works to add an arry in to another array
fruits.append('watermelon')
to remove an item
fruits.remove("apple")
to find index
fruits.index("apple")
multidimention arrays
a = [ 1,2,3,[4,5],6,7,[8,9],10]
print(a[6][1]) ----> at access 9 from the list
to modify array
a = [1,2,3]
a[0:3] = [10,20,30]
Lists(arrays) are modifyable but Tuples are not
#Tuples
fruits = ("mango","apple","banana","grapes")
# Dictionary (it has a key and value, it is not accessed by index, it is accessed
by key) (Dictionary is also changeable)
people = {
"John":32 , -------> (john is key and 32 is value )
"Rob":40 ,
"Tim":20
}
print(people['John'])
people = dict(
John:32,
Ron:40,
Rox:60
)
// to add something in dictionary
del people["John"]
// get method
people.get("John") // it does not crash the program if a certain
value in not presnet in the dictionary, but accessing it with the people["John"],
will crash the program if it is not present in it
prices.keys()
prices.items()
//------------------------Sets in python------------------------
numbers = set([1,2,3,4,5,6])
numbers = {1,2,3,4,5,6}
//a set cannot have a duplicate value, if there is a duplicate value, the set
ignores it and writes it only once
//order in a set is not retained, if you write {1,3,2}, it will set it in
ascending order
s = set()
// write an empty set like {}, it will not be an empty set, it will be a
dictionay which will have key value pair
seta = {1,2,3,4,5}
setb = {5,6,7,8,9}
seta | setb
*// Symmetric difference (minus) ----> it will remove the common elemnts
seta = {1,2,3,4,5}
setb = {5,6,7,8,9}
seta ^ setb
*// To remove a number in a set and prevent the crash if the value is not
present in the set
seta = {1, 2, 3, 4, 5}
seta.discard(50)
print(seta)
if marks>=90:
print("Grade A+")
elif marks>=70:
print("grade b")
//-----------------------Range Function------------------------
//-----------------------For loop------------------------
for x in range(5):
print(x)
for x in range(1,11):
print(x)
fruits = ["Apple","Grapes","Banana"]
for fruit in fruits:
print(fruit)
//-----------------------While loop------------------------
counter = 0
while counter<=10:
print(counter)
counter=counter+1
//-----------------------Adding multiple items in a cart using for
loop------------------------
cart = []
n = int(input("Enter the number of item you want to add: "))
for x in range(n):
item = input("Enter the item you want to add to the cart: ")
cart.append(item)
print(cart)
cart = []
while True:
choice = input("Do you want to add an item to the cart?")
if choice == "yes":
item = input("Add the item to the cart")
cart.append(item)
print(cart)
else:
break
cart =[]
while True:
choice = input("Do you want to continue shopping? ")
if choice == "yes":
for index,product in enumerate(products):
print(f"{index} : {product['name']} : ${product['price']}:
{product['description']}" )
index_id = int(input("Enter the id of the product you want to add to the
cart"))
else:
break
print(f"Thank you for shopping, your cart items are{cart}")
//-----------------------Functions ----------------------
def hello():
print("hi")
print("bye")
hello()
//*Arguements
def add(a,b):
return a+b
print(add(90,78))
//*Keywords
print(speed(distance=100,time=2))
print(speed(time=2, distance=100))
//*Default arguements
area(10)
scores=[1,2,3,4,5]
def add(numbers):
for number in numbers:
return number
print(add(scores))
count = 10
def increment():
global count
count =count+1
print(count)
increment()
words ="kook"
lenght = len(words)
palindrome = True
for i in range(lenght):
if words[i] != words[lenght-i-1]:
palindrome =False
break
else:
palindrome =True
if palindrome:
print("The string is a palindrome")
else:
print("The string is not a palindrome")
print(add(9,2,4))
def product(**kwargs):
for key,value in kwargs.items():
print(key + ":" + value)
product(name='iphone', price='500')
product(name='ipad',price='800',description='this is an ipad')
//*decorator function
def chocolate():
print("Chocolate")
def decorator(func):
def wrapper():
print("Chocolate upper wrapper")
func()
print("chocolate lower wrapper")
return wrapper()
decorator(chocolate)
@decorator
def chocolate():
print("Chocolate")
@decorator
def cake():
def decorator(func):
def wrapper():
print("Chocolate upper wrapper")
func()
print("chocolate lower wrapper")
return wrapper()
@decorator
def chocolate():
print("Chocolate")
@decorator
def cake():
print("cake")
chocolate()
cake()
'''
# class Solution:
# def solve(self,board):
# position_dict = {}
# for position in board:
# position_dict[position] = 0
# class Solution:
# def solve(self, board):
# # Example board: [(row1, col1), (row2, col2), ...]
# for position in board:
# row, col = position
# # Process each position on the board
# print(f"Processing position at row {row}, col {col}")
# # Further logic to solve the problem goes here
#
# # Example usage
# board = [(0, 0), (1, 2), (2, 1)]
# solution = Solution()
# solution.solve(board)