Python.notes
Python.notes
Variables in Python are used to store information . Variables are containers that are used to store
data.
Example
Comments in Python
In Python, comments are used to explain code and make it more readable. Comments are
ignored during execution.
In Python, data types are used to define the type of a value stored in a variable.
In Python, data types are a classification that dictates what type of value a variable can hold and
what operations can be performed on it
String - Bigya , Kathmandu , Nepal, coding king - It in enclosed in “ ”.
int - 5 , 6 , 7 , 8 , 9
float - 3.4 , 4.5 , 6.6
type function in Python is used to return the data type of the object / variable.
a = 10
b = 3.8
converted_a = float(a)
converted_b = int(b)
print(converted_a)
print(converted_b)
a = int(3.8)
b = float(3)
print(a)
print(b)
print(type(a))
print(type(b))
Boolean
In general, a Boolean variable can have only two values - True or False
In Python, the Boolean data type is used to represent two values, and it has two possible values:
True and False Boolean in Python.
print(3>2) - True
print(5==5) - True
print(5>7) - False
print(3>11) - False
print(4==5) - False
if statement
In computer programming, the if statement is a conditional
statement. It is used to execute a block of code only when a specific
condition is met.
Syntax
if condition:
# code to execute if the condition is true
if else
An if statement can have an optional else clause. The else
statement executes if the condition in the if statement evaluates to
False.
if (3>2):
print(“This gets executed if condition is true”)
else:
print(“I am executed if the above condition is false”)
elif in Python
‘elif’ stands for ‘else if’ and is used in Python programming to test
multiple conditions. It is written following an if statement in Python
to check an alternative condition if the first condition is false. The
code block under the elif statement will be executed only if its
condition is true.
if condition1:
statement to execute if condition1 is true
elif condition2:
statement to execute if condition2 is true
else :
statement to execute if both conditions are false
User Input -
User Input. Python allows for user input. That means we are able to ask the user for input. For
String use . input( )
For integer use: int(input( ))
Operators
In Python, operators are special symbols or keywords used to perform operations on variables
and values.
-, + , * , / , % ,
print(5+3)
print(5-3)
print(3*2)
print(10%2)
print(10/2)
Comparison operators in Python are used to compare two values, variables, or expressions. The
result of a comparison operation is always a Boolean value
print(3==3)
print(2!=3)
print(5<3)
print(5>5)
print(5>=5)
Assignment Operators
= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
Python Membership Operators
Operator Description
List
fruits = [“apple”,”litchi”,”mango”,”orange”]
print(fruits)
To add an item to the end of the list, use the append( ) method:
For Loops
In computer programming, a loop is a sequence of instruction that is continually repeated until a
certain condition is reached.
A loop in programming is a control structure that repeatedly executes a block of code as long as a
specified condition remains true.
A for loop is used for iterating over a sequence (that is either a list, a tuple, , a set, or a string).
for i in range(0,5):
print(“this is python class”)
break statement in python - The break statement in python is used to forcefully end a loop. We
use break statement to end the loop.
for i in range(10):
print(i)
if i == 2:
break
While Loops
In most computer programming languages, a while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean condition
i=1
while i < 5:
print(i)
i += 1
Infinite Loop
An infinite loop in Python is a loop that continues to execute indefinitely because its condition
always evaluates to True or there is no condition that stops it.
running = True
while(running):
print(“I am a example of infinite loop”)
Python Functions
A function is a block of code which execute only when it gets called.
To define a function in python we use def keyword.
Advantages of functions
Example
def myfunction( ):
print(“Hello this is my function”)
myfunction( )
Practise problem
print("|-----------------------------------|")
print("|Welcome to our Airplane game|")
print("| Press g to land the plane |")
print("| Press l to go left |")
print("| Press r to go right |")
print("|-----------------------------------|")
def land_plane( ):
print("Airbus A380 ready to land")
print("Plane is landing.........")
def go_left( ):
print("Airbus A380 ready to turn left")
print("Plane is turning left.........")
def go_right( ):
print("Airbus A380 ready to turn right")
print("Plane is turning right.........")
choice = input("Enter your choice captain:")
if(choice == "g"):
land_plane()
elif(choice == "l"):
go_left()
elif(choice == "r"):
go_right()
else:
print("Invalid choice! Please enter 'g', 'l', or 'r'.")
Practise problems:
def calculate_volume():
l = int(input("Enter length:"))
b = int(input("Enter breadth:"))
h = int(input("Enter height:"))
volume = l*b*h
print(volume)
def calculate_area():
l = int(input("Enter length:"))
b = int(input("Enter breadth:"))
area = l*b
print(area)
if(choice == 1):
calculate_volume()
elif(choice == 2):
calculate_area()
else:
print("Invalid")
The return statement in Python is used with function and is used to send a value back to the
caller.
Example
def addition( ):
a=5
b = 10
return a+b
print(addition( ))
Python file handling refers to the process of working with files on the filesystem.
File handling in Python allows you to read from and write to files on
your filesystem.
Creating a file:
file = open("info.txt","w")
file.write("Hello world ")
file.close()
Reading from a File
file = open("example.txt","r")
content = file.read()
print(content)
file.close()
books = ["Science","Maths","Social","Grammar"]
def display_books( ):
print(books)
def add_books( ):
b = input("Which book you want to add:")
books.append(b)
print(books)
def remove_books( ):
b = input("Which book you want to delete:")
books.remove(b)
print(books)
if(choice == 1):
display_books()
elif(choice == 2):
add_books()
elif(choice == 3):
remove_books()
else:
print("Invalid choice...")
© 2024 codingguruba. All Right Reserved