python QA.
python QA.
Input() Method:
Input() method ka upyog user se input lena hai. Yah method user ko ek prompt deta
hai aur user ka input string ke roop mein return karta hai.
variable_name = input(prompt)
Print() Method:
Print() method ka upyog user ko output dikhana hai. Yah method apne arguments ko
string ke roop mein convert karta hai aur unhein console par print karta hai.
print("Hello, World!")
print("Hello", "World!")
print("Hello", "World!", sep="-")
Input() aur Print() Method ka Sanyojan:
Input() aur print() method ka sanyojan karke hum user se input le sakte hain aur
unhein output de sakte hain.
Udaaharan:
name = input("Please enter your name: ")
age = input("Please enter your age: ")
print("Hello, " + name + "! You are " + age + " years old.")
5 Lists have several built-in methods. Tuples have fewer built-in methods.
Lists are more prone to unexpected Tuples, being immutable are less
6
changes and errors. error prone.
Udaaharan:
# Empty tuple
my_tuple = ()
Note:
- Tuple mein elements ko immutable rakhna hota hai, jiska matlab hai ki tuple mein
elements ko add, remove aur modify nahin kiya ja sakta hai.
- Tuple mein elements ko indexing ke through access kiya ja sakta hai.
- Tuple mein elements ko slicing ke through access kiya ja sakta hai.
What do you mean by list comprehension?
List comprehension ek prakaar ka syntax hai jo Python mein list ko create karne ke liye
upyog kiya jata hai. Yah syntax list ko create karne ke liye ek concise aur readable tareeka
pradaan karta hai.
Udaaharan:
numbers = [1, 2, 3, 4, 5]
double_numbers = [x * 2 for x in numbers]
print(double_numbers) # Output: [2, 4, 6, 8, 10]
L-Value:
L-value ek aisa value hota hai jo memory mein ek specific location ko represent karta hai.
Yah value ek variable, array, structure, ya class ka member ho sakta hai. L-value ko assign
karna sambhav hai, jiska matlab hai ki ismein ek naya value assign kiya ja sakta hai.
R-Value:
R-value ek aisa value hota hai jo kisi bhi expression ki evaluation ke baad prapt hota hai.
Yah value ek constant, literal, ya ek expression ka result ho sakta hai. R-value ko assign
nahin kiya ja sakta hai, jiska matlab hai ki ismein ek naya value assign nahin kiya ja sakta hai.
Udaaharan:
Antar:
1. Assignability: L-value ko assign kiya ja sakta hai, jabki R-value ko assign nahin kiya ja sakta
hai.
2. Memory Location: L-value ek specific memory location ko represent karta hai, jabki R-
value ek value ko represent karta hai jo memory mein nahin hota hai.
3. Usage: L-value ko variables, arrays, structures, aur classes mein upyog kiya jata hai, jabki
R-value ko expressions, constants, aur literals mein upyog kiya jata hai.
What is difference between mutable and immutable data types?
Mutable Data Types:
Mutable data types woh hote hain jinke values ko badalaya ja sakta hai. Yah data types ek
baar create hone ke baad apne values ko modify kar sakte hain.
Udaaharan:
1. List: List ek mutable data type hai jismein elements ko add, remove aur modify kiya ja
sakta hai.
2. Dictionary: Dictionary ek mutable data type hai jismein key-value pairs ko add, remove
aur modify kiya ja sakta hai.
3. Set: Set ek mutable data type hai jismein elements ko add aur remove kiya ja sakta hai.
Udaaharan:
1. Integer: Integer ek immutable data type hai jiska value badalaya nahin ja sakta hai.
2. Float: Float ek immutable data type hai jiska value badalaya nahin ja sakta hai.
3. String: String ek immutable data type hai jiska value badalaya nahin ja sakta hai.
4. Tuple: Tuple ek immutable data type hai jiska value badalaya nahin ja sakta hai.
Antar:
1. Modifiability: Mutable data types ko modify kiya ja sakta hai, jabki immutable data types
ko modify nahin kiya ja sakta hai.
2. Memory Allocation: Mutable data types ke liye memory allocation dynamic hota hai,
jabki immutable data types ke liye memory allocation static hota hai.
3. Thread Safety: Immutable data types thread-safe hote hain, jabki mutable data types
thread-safe nahin hote hain.
What are the different types of errors in a python program?
1. Syntax Errors:
Syntax errors woh errors hote hain jo Python interpreter ko code ko parse karne mein rok
dete hain. Yah errors tab hoti hain jab code mein syntax ki galati hoti hai.
Udaaharan:
2. Runtime Errors:
Runtime errors woh errors hote hain jo code ko execute karte samay hoti hain. Yah errors
tab hoti hain jab code mein logical galati hoti hai ya jab code mein koi unexpected situation
aati hai.
Udaaharan:
Udaaharan:
x=5
y=x*2
print(y) # Output: 10, lekin yadi hum x ko 10 kar dete hain, to output 20 hona chahiye
4. Logical Errors:
Logical errors woh errors hote hain jo code ko execute karte samay nahin dikhai deti hain,
lekin code ka output galat hota hai. Yah errors tab hoti hain jab code mein logical galati hoti
hai.
Udaaharan:
x=5
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10") # Output: x is less than or equal to 10, lekin yadi hum
x ko 15 kar dete hain, to output "x is greater than 10" hona chahiye
5. Exception Errors:
Exception errors woh errors hote hain jo code ko execute karte samay aati hain aur jo code
ko rok deti hain. Yah errors tab hoti hain jab code mein koi unexpected situation aati hai.
Udaaharan:
try:
x=5/0
except ZeroDivisionError:
print("Cannot divide by zero")
unit 1
A) how many types data types using in python?
Python mein kai prakaar ke data types hote hain:
Reserved Keywords:
Python mein kuch reserved keywords hote hain jo variable naam ke roop mein nahin
use kiye ja sakte hain:
1. and
2. as
3. assert
4. break
5. class
6. continue
7. def
8. del
9. elif
10. else
11. except
12. finally
13. for
14. from
15. global
16. if
17. import
18. in
19. is
20. lambda
21. nonlocal
22. not
23. or
24. pass
25. raise
26. return
27. try
28. while
29. with
30. yield
Udaaharan:
Blocks ke Prakaar:
1. Conditional Blocks: Conditional blocks ka upyog if-else statements mein kiya jata
hai.
2. Loop Blocks: Loop blocks ka upyog for aur while loops mein kiya jata hai.
3. Function Blocks: Function blocks ka upyog functions mein kiya jata hai.
4. Class Blocks: Class blocks ka upyog classes mein kiya jata hai.
Udaaharan:
if x > 5:
print("x is greater than 5")
y=x*2
print("y is", y)
Blocks ke Fayde:
1. Code ko Organize Karta Hai: Blocks code ko organize karte hain aur isse padhne aur
samajhne mein aasan banate hain.
2. Code ko Reusable Banata Hai: Blocks code ko reusable banate hain, jisse hum ek hi
block ko alag-alag jagah par use kar sakte hain.
3. Code ko Debug Karne Mein Madad Karta Hai: Blocks code ko debug karne mein
madad karte hain, jisse hum blocks ke andar ke errors ko aasan se find aur fix kar
sakte hain.
b) How many types of operators in python?
Python mein kai prakaar ke operators hote hain:
1. Arithmetic Operators:
Arithmetic operators ka upyog mathematical operations ke liye kiya jata hai.
- + (Addition)
- - (Subtraction)
- * (Multiplication)
- / (Division)
- ** (Exponentiation)
- // (Floor Division)
- % (Modulus)
2. Comparison Operators:
Comparison operators ka upyog do values ko compare karne ke liye kiya jata hai.
- == (Equal)
- != (Not Equal)
- > (Greater Than)
- < (Less Than)
- >= (Greater Than or Equal)
- <= (Less Than or Equal)
3. Logical Operators:
Logical operators ka upyog conditional statements mein kiya jata hai.
4. Assignment Operators:
Assignment operators ka upyog variables ko assign karne ke liye kiya jata hai.
- = (Assignment)
- += (Addition Assignment)
- -= (Subtraction Assignment)
- *= (Multiplication Assignment)
- /= (Division Assignment)
- **= (Exponentiation Assignment)
- //= (Floor Division Assignment)
- %= (Modulus Assignment)
5. Bitwise Operators:
Bitwise operators ka upyog binary numbers ke saath kiya jata hai.
6. Membership Operators:
Membership operators ka upyog check karne ke liye kiya jata hai ki ek value kisi
sequence mein hai ya nahin.
- in (Membership)
- not in (Non-Membership)
7. Identity Operators:
Identity operators ka upyog check karne ke liye kiya jata hai ki do values ek hi object
ko represent karte hain ya nahin.
- is (Identity)
- is not (Non-Identity)
unit 2
a) Define the use of pass, continue, break and else with suitable
example?
Pass, continue, break, aur else statements ka upyog loop aur conditional statements
mein kiya jata hai.
Pass Statement:
Pass statement ka upyog ek empty block ya ek function mein kiya jata hai jab hum
kuch bhi nahin karna chahte hain.
Udaaharan:
for i in range(5):
if i == 3:
pass
print(i)
Output:
0
1
2
3
4
Continue Statement:
Continue statement ka upyog loop mein kiya jata hai jab hum ek iteration ko skip
karna chahte hain aur agle iteration par jaana chahte hain.
Udaaharan:
for i in range(5):
if i == 3:
continue
print(i)
Output:
0
1
2
4
Break Statement:
Break statement ka upyog loop mein kiya jata hai jab hum loop ko puri tarah se exit
karna chahte hain.
Udaaharan:
for i in range(5):
if i == 3:
break
print(i)
Output:
0
1
2
Else Statement:
Else statement ka upyog loop ke saath kiya jata hai jab hum loop ke baad ek block ko
execute karna chahte hain jab loop puri tarah se execute ho gaya ho.
Udaaharan:
for i in range(5):
print(i)
else:
print("Loop puri tarah se execute ho gaya")
Output:
0
1
2
3
4
Loop puri tarah se execute ho gaya
Udaaharan 2:
Else statement ka upyog loop ke saath kiya jata hai jab hum loop ko break kar dete
hain aur else block ko execute nahin karna chahte hain.
for i in range(5):
if i == 3:
break
print(i)
else:
print("Loop puri tarah se execute ho gaya")
Output:
0
1
2
B) How to use range function using for loop with suitable example?
Range function ka upyog for loop ke saath kiya jata hai taaki hum ek sequence ke
sabhi elements par iterate kar sakein.
Parameters:
- start: Sequence ki starting value.
- stop: Sequence ki ending value.
- step: Sequence mein increment ya decrement ki value.
Udaaharan:
Udaaharan 1:
Ek simple range function ka upyog karke 1 se 5 tak ke numbers print karna.
Output:
1
2
3
4
5
Udaaharan 2:
Ek range function ka upyog karke 2 se 10 tak ke numbers print karna, lekin 2 ke step
se.
Output:
2
4
6
8
10
Udaaharan 3:
Ek range function ka upyog karke 10 se 1 tak ke numbers print karna, lekin -1 ke step
se (reverse order).
Output:
10
9
8
7
6
5
4
3
2
1
or
a) How to use if,elif and else for decision macking with suitable example?
if condition
The if condition is considered the simplest of the three and makes a decision based on whether
the condition is true or not. If the condition is true, it prints out the indented expression. If the
condition is false, it skips printing the indented expression.
if condition:
expression
POWERED BY
Example of if
Suppose you have a variable z, equal to 4. If the value is 'even', you will print z is 'even'. You will use
modulo operator 2, which will return 0 if z is 'even'. As soon as you run the below code, Python will check if
the condition holds. If True, the corresponding code will be executed.
z=4
if z % 2 == 0: # True
print("z is even")
POWERED BY
z is even
POWERED BY
z=4
if z % 2 == 0:
print("checking" + str(z))
print("z is even")
POWERED BY
checking 4
z is even
POWERED BY
z=5
if z % 2 == 0: # False
POWERED BY
POWERED BY
if-else condition
The if-else condition adds an additional step in the decision-making process compared to the
simple if statement. The beginning of an if-else statement operates similar to a simple if statement;
however, if the condition is false, instead of printing nothing, the indented expression under else will be
printed.
if condition:
expression
else:
expression
POWERED BY
Example of if-else
Continuing our previous example, what if you want to print 'z is odd' when the if condition is false? In this
case, you can simply add another condition, which is the else condition. If you run it with z equal to 5, the
condition is not true, so the expression for the else statement gets printed out.
z=5
if z % 2 == 0:
print("z is even")
else:
print("z is odd")
POWERED BY
z is odd
POWERED BY
if-elif-else condition
The most complex of these conditions is the if-elif-else condition. When you run into a situation where
you have several conditions, you can place as many elif conditions as necessary between the if condition
and the else condition.
if condition:
expression
elif condition:
expression
else:
expression
POWERED BY
Here, since z equals 3, the first condition is False, so it goes over to the next condition. The next condition
does hold True. Hence, the corresponding print statement is executed.
z=3
if z % 2 == 0:
elif z % 3 == 0:
else:
POWERED BY
z is divisible by 3
POWERED BY
In the first condition, you check if you are looking in the kitchen, elif you are looking in the
bedroom, else you are looking around elsewhere. Depending on the value of the room variable, the satisfied
condition is executed.
Similarly, for the area variable, you write an if and else condition and check whether the area is greater
than 15 or not.
# Define variables
room = "bed"
area = 14.0
if room == "kit":
else:
print("Big place!")
else:
print("Pretty small.")
POWERED BY
क क आ क क ( while(condition) ) र र कई क
while
while
while
number = 1
table = 5
while number <= 10 :
print(number, ' * ', table, ' = ', number *table )
number = number + 1
while
क आ Python Nested While Loop क कर क , While
Loop क दर एक और While Loop.
while
x = 1
while x <= 10 :
y = 1
print(y, end=' ')
while y < x :
y = y+1
print(y, end=' ')
print('')
x = x+1
unit 3
String ka Syntax:
String Operations:
1. Concatenation: Do ya adhik strings ko concatenate kiya ja sakta hai.
2. Repetition: Ek string ko repeat kiya ja sakta hai.
3. Indexing: Ek string ke characters ko index ke through access kiya ja sakta hai.
4. Slicing: Ek string ke characters ko slicing ke through access kiya ja sakta hai.
String Methods:
1. lower(): Ek string ko lowercase mein convert karta hai.
2. upper(): Ek string ko uppercase mein convert karta hai.
3. title(): Ek string ko title case mein convert karta hai.
4. strip(): Ek string ke starting aur ending whitespace ko remove karta hai.
5. split(): Ek string ko ek separator ke basis par split karta hai.
6. join(): Ek list ke strings ko ek separator ke basis par join karta hai.
7. find(): Ek string mein ek substring ki index ki jaanch karta hai.
8. replace(): Ek string mein ek substring ko replace karta hai.
9. count(): Ek string mein ek substring ki frequency ki jaanch karta hai.
Udaaharan:
# Concatenation
print(my_string + " How are you?") # Output: Hello, World! How are you?
# Repetition
print(my_string * 2) # Output: Hello, World!Hello, World!
# Indexing
print(my_string[0]) # Output: H
# Slicing
print(my_string[0:5]) # Output: Hello
# lower()
print(my_string.lower()) # Output: hello, world!
# upper()
print(my_string.upper()) # Output: HELLO, WORLD!
# title()
print(my_string.title()) # Output: Hello, World!
# strip()
print(my_string.strip()) # Output: Hello, World!
# split()
print(my_string.split(",")) # Output: ['Hello', ' World!']
# join()
print(",".join(["Hello", "World!"])) # Output: Hello,World!
# find()
print(my_string.find("World")) # Output: 7
# replace()
print(my_string.replace("World", "Universe")) # Output: Hello, Universe!
# count()
print(my_string.count("l")) # Output: 3
B) Define dictionary? Explain all the functions of dictionary?
Dictionary ek prakaar ka data structure hai jo key-value pairs ko store karta hai.
Yah ek mutable data structure hai, jiska matlab hai ki ismein elements ko add,
remove aur modify kiya ja sakta hai.
Dictionary ka Syntax:
Dictionary ke Functions:
1. keys(): Dictionary ke sabhi keys ko return karta hai.
2. values(): Dictionary ke sabhi values ko return karta hai.
3. items(): Dictionary ke sabhi key-value pairs ko return karta hai.
4. get(): Dictionary se ek key ka value ko return karta hai.
5. pop(): Dictionary se ek key-value pair ko remove aur return karta hai.
6. popitem(): Dictionary se ek random key-value pair ko remove aur return karta
hai.
7. update(): Dictionary ko update karta hai.
8. clear(): Dictionary ko clear karta hai.
9. copy(): Dictionary ka ek copy banata hai.
10. fromkeys(): Ek dictionary banata hai jismein keys aur values hote hain.
Udaaharan:
# keys()
print(my_dict.keys()) # Output: dict_keys(['name', 'age', 'city'])
# values()
print(my_dict.values()) # Output: dict_values(['John', 30, 'New York'])
# items()
print(my_dict.items()) # Output: dict_items([('name', 'John'), ('age', 30), ('city', 'New
York')])
# get()
print(my_dict.get("name")) # Output: John
# pop()
print(my_dict.pop("age")) # Output: 30
# popitem()
print(my_dict.popitem()) # Output: ('city', 'New York')
# update()
my_dict.update({"country": "USA"})
print(my_dict) # Output: {'name': 'John', 'city': 'New York', 'country': 'USA'}
# clear()
my_dict.clear()
print(my_dict) # Output: {}
# copy()
my_dict = {"name": "John", "age": 30, "city": "New York"}
my_dict_copy = my_dict.copy()
print(my_dict_copy) # Output: {'name': 'John', 'age': 30, 'city': 'New York'}
# fromkeys()
my_dict = dict.fromkeys(["name", "age", "city"], "Unknown")
print(my_dict) # Output: {'name': 'Unknown', 'age': 'Unknown', 'city': 'Unknown'}
or
a) Define list operation and methods?
List operations aur methods woh hote hain jo list ke elements ko manipulate karne,
access karne aur modify karne mein madad karte hain.
List Operations:
1. Indexing: List ke elements ko index ke through access kiya ja sakta hai.
2. Slicing: List ke elements ko slicing ke through access kiya ja sakta hai.
3. Concatenation: Do ya adhik lists ko concatenate kiya ja sakta hai.
4. Repetition: List ko repeat kiya ja sakta hai.
5. Membership: List mein element ki membership ki jaanch ki ja sakti hai.
6. Length: List ki length ki jaanch ki ja sakti hai.
List Methods:
1. append(): List mein ek naya element add kiya ja sakta hai.
2. extend(): List mein ek ya adhik elements add kiye ja sakte hain.
3. insert(): List mein ek naya element insert kiya ja sakta hai.
4. remove(): List se ek element remove kiya ja sakta hai.
5. pop(): List se ek element remove aur return kiya ja sakta hai.
6. index(): List mein ek element ki index ki jaanch ki ja sakti hai.
7. count(): List mein ek element ki frequency ki jaanch ki ja sakti hai.
8. sort(): List ke elements ko sort kiya ja sakta hai.
9. reverse(): List ke elements ko reverse kiya ja sakta hai.
10. clear(): List ke sabhi elements remove kiye ja sakte hain.
Udaaharan:
my_list = [1, 2, 3, 4, 5]
# Indexing
print(my_list[0]) # Output: 1
# Append
my_list.append(6)
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
# Sort
my_list.sort()
print(my_list) # Output: [1, 2, 3, 4, 5, 6]
# Reverse
my_list.reverse()
print(my_list) # Output: [6, 5, 4, 3, 2, 1]
b) How many types of operations we can perform using tuple?
Tuple ke saath kai prakaar ke operations kiye ja sakte hain: