0% found this document useful (0 votes)
6 views16 pages

Python Programming Basics and Examples

This document about that notes of python language and useful for quick revision

Uploaded by

v82108742
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views16 pages

Python Programming Basics and Examples

This document about that notes of python language and useful for quick revision

Uploaded by

v82108742
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1. WAP to show the operation of arithmetic operators.

a = 12

b=6

print("The value of a is:", a)

print("The value of b is:", b)

print()

print("Addition (a + b):", a + b)

print("Subtraction (a - b):", a - b)

print("Multiplication (a * b):", a * b)

print("Division (a / b):", a / b)

print("Floor Division (a // b):", a // b)

print("Modulus (a % b):", a % b)

print("Exponent (a ** b):", a ** b)

1
2. WAP to show the operation of identity operators.

a = 10

b = 10

c = [1, 2, 3]

d = [1, 2, 3]

print("a is b:", a is b)

print("c is d:", c is d)

print("a is not b:", a is not b)

print("c is not d:", c is not d)

print("\nMemory address of a:", id(a))

print("Memory address of b:", id(b))

print("Memory address of c:", id(c))

print("Memory address of d:", id(d))

2
3. WAP to show the operation of membership operator.

a = [1, 2, 3, 4, 5]

print(3 in a)

print(6 in a)

print(6 not in a)

print(2 not in a)

3
4. WAP to find the factorial of a number.

num = int(input("Enter a number: "))

factorial = 1

for i in range(1, num + 1):

factorial =factorial* i

print("The factorial of", num, "is:", factorial)

4
5. WAP to check the prime number or not.

num = int(input("Enter a number: "))

if num > 1:

for i in range(2, int(num**0.5) + 1):

if num % i == 0:

print(num, "is not a prime number")

break

else:

print(num, "is a prime number")

else:

print(num, "is not a prime number")

5
6. WAP to show break ,continue and pass.

for i in range(1, 6):

if i == 2:

pass

elif i == 3:

continue

elif i == 5:

break

print(i)

6
7. WAP to add the list elements.

numbers = [10, 20, 30, 40, 50]

total = 0

for num in numbers:

total = total + num

print("The sum of list elements is:", total)

7
8. WAP to show the operations of tuple.
# Creating tuples

tuple1 = (1, 2, 3, 4)

tuple2 = (5, 6, 7)

# Accessing elements

print("First element of tuple1:", tuple1[0])

print("Last element of tuple1:", tuple1[-1])

# Slicing

print("Elements from index 1 to 3 in tuple1:", tuple1[1:4])

# Concatenation

tuple3 = tuple1 + tuple2

print("Concatenated tuple:", tuple3)

# Repetition

tuple4 = tuple2 * 2

print("Repetition of tuple2:", tuple4)

# Membership

print(" 3 in tuple1 :", 3 in tuple1)

print(" 8 not in tuple2 :", 8 not in tuple2)

8
9. WAP to show the operations of a set.

# Creating sets

set1 = {1, 2, 3, 4}

set2 = {3, 4, 5, 6}

# Union

print("Union of set1 and set2:", set1 | set2)

# Intersection

print("Intersection of set1 and set2:", set1 & set2)

# Difference

print("Difference (set1 - set2):", set1 - set2)

print("Difference (set2 - set1):", set2 - set1)

# Membership

print(" 3 in set1 :", 3 in set1)

print(" 7 not in set2 :", 7 not in set2)

9
[Link] to show the operations of Dictionary.

# Creating a dictionary

student = {

"name": "krishn",

"age": 20,

"course": "BCA"

# Accessing elements

print("Name:", student["name"])

print("Age:", [Link]("age"))

# Adding/Updating elements

student["grade"] = "A"

student["age"] = 21

print("Updated dictionary:", student)

# Deleting elements

del student["course"]

print("After deletion:", student)

# Membership

print("Is 'name' in dictionary ?", "name" in student)

print("Is 'course' not in dictionary ?", "course" not in student)

10
[Link] of call by value using function(swapping).

def swap(x, y):

x, y = y, x

print("Inside function: x =", x, "y =", y)

a = 10

b = 20

print("Before function call: a =", a, "b =", b)

swap(a, b)

print("After function call: a =", a, "b =", b)

11
[Link] of call by reference using function(swapping).

def swap(list):

list[0], list[1] = list[1], list[0]

print("Inside function: list =", list)

numbers = [10, 20]

print("Before function call: numbers =", numbers)

swap(numbers)

print("After function call: numbers =", numbers)

12
[Link] to show required argument, keyword argument , default argument.

# Function with required arguments

def add(a, b):

return a + b

# Function with default arguments

def greet(name, msg="Hello"):

return f"{msg}, {name}!"

# Function call using keyword arguments

def info(name, age):

return f"Name: {name}, Age: {age}"

# Using required arguments

print("Addition (required arguments):", add(5, 10))

# Using default argument

print(greet("krishn"))

print(greet("Radha", "Good Morning"))

# Using keyword arguments

print(info(age=25, name="Balram"))

13
[Link] to write content in a file.

# Open a file in write mode

file = open("[Link]", "w")

# Write content to the file

[Link]("Hello! This is a sample content.\n")

[Link]("Python makes file handling easy.\n")

# Close the file

[Link]()

print("Content written to file successfully.")

14
[Link] to read a file and print it on the console.

# Open the file in read mode

file = open("[Link]", "r")

# Read the content

content = [Link]()

# Print the content

print("File content:\n", content)

# Close the file

[Link]()

15
[Link] to read the content of a file and write it in another file.

# Open the project file in read mode

source_file = open("[Link]", "r")

# Read the content

content = source_file.read()

# Close the source file

source_file.close()

# Open the destination file in write mode

destination_file = open("[Link]", "w")

# Write the content into the destination file

destination_file.write(content)

# Close the destination file

destination_file.close()

print("Content copied from [Link] to [Link] successfully.")

16

You might also like