Python Complete Guide with Explanations,
Examples & Answers
Variables
Variables are used to store data in Python.
Example:
x = 10 y = 'Hello'
Answer: x stores integer 10, y stores string 'Hello'
Data Types
Python has int, float, string, boolean types.
Example:
a = 5 b = 3.2 c = 'Hi' d = True
Answer: Different data types store different kinds of values
If-Else
Used for decision making.
Example:
x = 5 if x > 3: print('Yes') else: print('No')
Answer: Since 5 > 3, output is Yes
Loops
Loops repeat code multiple times.
Example:
for i in range(3): print(i)
Answer: Output: 0 1 2
Functions
Functions reuse code.
Example:
def add(a,b): return a+b print(add(2,3))
Answer: Output: 5
Lists
Lists store multiple values.
Example:
l = [1,2,3] [Link](4) print(l)
Answer: Output: [1,2,3,4]
Dictionaries
Store key-value pairs.
Example:
d = {'name':'Ram'} print(d['name'])
Answer: Output: Ram
File Handling
Used to read/write files.
Example:
f = open('[Link]','w') [Link]('Hello') [Link]()
Answer: Writes Hello into file
Exception Handling
Handles errors safely.
Example:
try: x=1/0 except: print('Error')
Answer: Prints Error instead of crash