0% found this document useful (0 votes)
8 views4 pages

Python Module2

The document outlines various programming tasks and concepts, including printing numbers, calculating sums, and creating a menu-driven calculator. It also covers string operations such as slicing, searching, and using built-in string methods. Additionally, it introduces common random functions and demonstrates string immutability and comparison.

Uploaded by

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

Python Module2

The document outlines various programming tasks and concepts, including printing numbers, calculating sums, and creating a menu-driven calculator. It also covers string operations such as slicing, searching, and using built-in string methods. Additionally, it introduces common random functions and demonstrates string immutability and comparison.

Uploaded by

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

Write a program to print numbers from 1 to 10.

Write a program to print even numbers from 1 to 20.


Sum of First N Numbers
Write a program to print multiplication table of a number.
Write a program to find factorial of a number using for loop.

Write a program to create a simple menu-driven calculator using while loop.


Develop a number guessing game using while loop.

Common Random Functions


1. randint(a, b)
👉 Returns a random integer between a and b (inclusive)
[Link](1, 5)

2. random()
👉 Returns a float between 0 and 1

import random
[Link]()
👉 Example output: 0.5678

3. choice()
👉 Picks a random element from a list
[Link]([10, 20, 30])

What is a String?
👉 A string is a sequence of characters enclosed in quotes (' ' or " ").
✅ Example
name = "Vidyashree"
print(name)
🔹 2. String as a Sequence
👉 A string is a sequence, so each character has an index.
s = "Python"
print(s[0]) # P
print(s[3]) # h

🔹 3. len() Function
👉 Used to find the length of a string
✅ Program
s = "Python"
print("Length:", len(s))

🔹 4. Traversal with for loop


👉 Access each character one by one
✅ Program
s = "Hello"
for ch in s:
print(ch)
🔸 Variation (using index)
s = "Hello"
for i in range(len(s)):
print(s[i])

🔹 5. String Slicing
👉 Extract part of a string
✅ Program
s = "Python"
print(s[0:3]) # Pyt
print(s[2:]) # thon
print(s[:4]) # Pyth
print(s[-3:]) # hon

🔹 6. Strings are Immutable


👉 Strings cannot be changed after creation
❌ Wrong
s = "Hello"
s[0] = 'Y' # Error
✅ Correct Way
s = "Hello"
s = "Y" + s[1:]
print(s) # Yello

🔹 7. Searching in String
👉 Use in or find()
✅ Program
s = "Python Programming"

print("Python" in s) # True
print([Link]("Pro")) # index
print([Link]("Java")) # -1

🔹 8. Looping and Counting


👉 Count occurrences of characters
✅ Program
s = "banana"
count = 0

for ch in s:
if ch == 'a':
count += 1

print("Count of 'a':", count)


🔸 Alternative
s = "banana"
print([Link]('a'))

🔹 9. String Methods
👉 Built-in functions for string operations
✅ Program
s = "python programming"

print([Link]()) # PYTHON PROGRAMMING


print([Link]()) # python programming
print([Link]()) # Python Programming
print([Link]()) # removes spaces
print([Link]("python", "java"))
print([Link]()) # splits into list

🔹 10. String Comparison


👉 Compare strings using operators
✅ Program
a = "apple"
b = "banana"

if a == b:
print("Equal")
elif a > b:
print("a is greater")
else:
print("b is greater")
🔸 Case Sensitivity
print("Apple" == "apple")

You might also like