Practice Programs Set 1-1
Practice Programs Set 1-1
# Python 2.x:
# Python 3.x:
x = 5 # Variable
y = 10
z = x + y # Expression
Keywords
if x > 0:
print("Positive")
else:
print("Non-positive")
Literals
x = 10 # Integer literal
if x > 0:
Comments in Python
x = 10 # Assign 10 to x
print(x) # Output: 10
'''
'''
x = 20
y = 30
print(x + y) # Output: 50
print(y) # Output: 10
num = -3
if num > 0:
elif num == 0:
else:
# Original code
x = x + 1 # Add 1 to x
'''
'''
def fibonacci(n):
# Base case
if n <= 1:
return n
# Recursive case
return fibonacci(n-1) + fibonacci(n-2)
print(fibonacci(5)) # Output: 5
x = 5 # Assignment statement
y = "Hello"
z = x + 10
a = b = c = 100
print(a) # 100
print(b) # 100
print(c) # 100
x, y, z = 10, 20, 30
print(x) # 10
print(y) # 20
print(z) # 30
# Swapping two variables
x, y = 5, 10
# Swap
x, y = y, x
PI = 3.14159
RADIUS = 5
area = PI * RADIUS ** 2
# Chained assignment
x = y = z = 50
print(x, y, z) # 50 50 50
# Modifying one variable doesn't affect the others
x = 10
print(x, y, z) # 10 50 50
a = 10
print(a) # 10
# Deleting variable a
del a
Name Spaces
x = 10 # Global namespace
def my_function():
x = 20 # Local namespace
print("Inside function:", x)
my_function()
print("Outside function:", x)
Python Objects
x = 10
y = "Python"
Data types
Integer Programs:
#Addition
a = 12
b=8
result = a + b
print("Addition:", result) # 20
#Division
a = 17
b=4
div = a // b
mod = a % b
print("Division:", div) # 4
print("Modulus:", mod) # 1
#EXponentiation
a=3
b=4
result = a ** b
print("Exponentiation:", result) # 81
#Subtraction
x = 100
y = 35
result = x - y
print("Subtraction:", result) # 65
#MUltiplication
p=7
q=6
product = p * q
print("Multiplication:", product) # 42
Float Programs:
#addition
x = 10.5
y = 2.3
result = x + y
print("Sum:", result) # 12.8
#Division
a = 20.0
b = 3.0
result = a / b
print("Division:", result) # 6.666666666666667
num = 16.0
sqrt = num ** 0.5
print("Square Root:", sqrt) # 4.0
#Subtracting floats
x = 10.7
y = 5.3
result = x - y
print("Subtraction:", result) # 5.4
String Programs:
#Concatenation
first = "Hello"
second = "World"
result = first + " " + second
print(result) # Hello World
#String repetition
text = "Python"
result = text * 3
print(result) # PythonPythonPython
#Length of a string
#Slicing a string
#Converting to uppercase
text = "hello"
result = text.upper()
print("Uppercase:", result) # HELLO
List Programs:
my_list = [1, 2, 3, 4, 5]
first = my_list[0]
print("First element:", first) # 1
my_list = [1, 2, 3]
result = my_list * 2
print("Repeated List:", result) # [1, 2, 3, 1, 2, 3]
#Length of a list
Tuple Programs:
my_tuple = (1, 2, 3, 4)
first = my_tuple[0]
print("First element:", first) # 1
tuple1 = (1, 2, 3)
tuple2 = (4, 5, 6)
result = tuple1 + tuple2
print("Concatenated Tuple:", result) # (1, 2, 3, 4, 5, 6)
#Tuple slicing
Set Programs:
my_set = {1, 2, 3}
my_set.add(4)
print("Set after adding:", my_set) # {1, 2, 3, 4}
my_set = {1, 2, 3, 4}
my_set.remove(2)
print("Set after removing:", my_set) # {1, 3, 4}
#Union of two sets
set1 = {1, 2, 3}
set2 = {3, 4, 5}
union_set = set1.union(set2)
print("Union:", union_set) # {1, 2, 3, 4, 5}
set1 = {1, 2, 3}
set2 = {2, 3, 4}
intersection = set1.intersection(set2)
print("Intersection:", intersection) # {2, 3}
#Set difference
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5}
difference = set1.difference(set2)
print("Difference:", difference) # {1, 2}
Dictionary Programs:
#Updating a value