Total Pageviews

Saturday, April 18, 2020

LIST IN PYTHON

📘 List in Python 

🎯 What is a List in Python?

A list in Python is a collection of items that is:

  • Ordered (index-based)
  • Changeable (mutable)
  • Allows duplicate values
  • Can store multiple data types

📌 Definition

A list is a data structure in Python used to store multiple values in a single variable.


🧠 Syntax

my_list = [10, 20, 30, "Python", True]

📊 Example List

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

📍 Indexing in List

Element1234
Index0123

⚡ 1. Creating a List

fruits = ["apple", "banana", "mango"]
print(fruits)

Output:

['apple', 'banana', 'mango']

⚡ 2. Accessing Elements

fruits = ["apple", "banana", "mango"]
print(fruits[1])

Output:

banana

⚡ 3. Negative Indexing

fruits = ["apple", "banana", "mango"]
print(fruits[-1])

Output:

mango

⚡ 4. append() → Add Element at End

nums = [1, 2, 3]
nums.append(4)
print(nums)

Output:

[1, 2, 3, 4]

⚡ 5. insert() → Add at Specific Position

nums = [1, 2, 4]
nums.insert(2, 3)
print(nums)

Output:

[1, 2, 3, 4]

⚡ 6. extend() → Add Multiple Items

a = [1, 2]
b = [3, 4]
a.extend(b)
print(a)

Output:

[1, 2, 3, 4]

⚡ 7. remove() → Remove Value

nums = [1, 2, 3, 4]
nums.remove(3)
print(nums)

Output:

[1, 2, 4]

⚡ 8. pop() → Remove by Index

nums = [10, 20, 30]
nums.pop(1)
print(nums)

Output:

[10, 30]

⚡ 9. clear() → Remove All Items

nums = [1, 2, 3]
nums.clear()
print(nums)

Output:

[]

⚡ 10. sort() → Ascending Order

nums = [3, 1, 4, 2]
nums.sort()
print(nums)

Output:

[1, 2, 3, 4]

⚡ 11. sort(reverse=True)

nums = [3, 1, 4, 2]
nums.sort(reverse=True)
print(nums)

Output:

[4, 3, 2, 1]

⚡ 12. reverse()

nums = [1, 2, 3]
nums.reverse()
print(nums)

Output:

[3, 2, 1]

⚡ 13. len() → Length of List

nums = [1, 2, 3, 4]
print(len(nums))

Output:

4

⚡ 14. max()

nums = [10, 50, 20]
print(max(nums))

Output:

50

⚡ 15. min()

nums = [10, 50, 20]
print(min(nums))

Output:

10

⚡ 16. sum()

nums = [1, 2, 3, 4]
print(sum(nums))

Output:

10

⚡ 17. Loop through List

nums = [10, 20, 30]

for i in nums:
print(i)

Output:

10
20
30

⚡ 18. Check Element in List

nums = [1, 2, 3]

print(2 in nums)

Output:

True

⚡ 19. Copy List

a = [1, 2, 3]
b = a.copy()
print(b)

Output:

[1, 2, 3]

⚡ 20. List Slicing

nums = [10, 20, 30, 40, 50]
print(nums[1:4])

Output:

[20, 30, 40]

📌 Summary

✔ List = collection of multiple values
✔ Mutable (can change)
✔ Indexed (0-based)
✔ Supports many built-in methods


🧠 Important Exam Questions

Short Questions

  1. What is a list in Python?
  2. Is list mutable?
  3. What is indexing?
  4. Difference between append and insert?
  5. What is slicing?

Long Questions

  1. Explain list in Python with example.
  2. Write list methods with examples.
  3. Explain append, remove, pop methods.
  4. Write program using list operations.
  5. Explain list slicing with example. 

SWITCH CASE IN PYTHON

Python does NOT have a built-in switch-case statement like C/C++ or Java.

But we can simulate switch-case using:

  • if-elif-else
  • dictionary mapping (best method)

🎯 What is Switch Case?

A switch-case is a selection control structure used to execute one block of code among many alternatives based on a value.


📌 Example Idea

If choice = 1 → Add
If choice = 2 → Subtract
If choice = 3 → Multiply

FOR LOOP IN PYTHON

1. BASIC - CLICK



🎯 What is a for loop?

A for loop in Python is used to repeat a block of code for each item in a sequence (like list, string, range, etc.).


📌 Definition

A for loop is used to iterate over a sequence (list, tuple, string, range) and execute code repeatedly.


🧠 Basic Syntax

for variable in sequence:
# code block

🔢 1. Using range() (Most Important)

Example 1: Print 1 to 5

for i in range(1, 6):
print(i)

Output:

1
2
3
4
5

Example 2: Print Even Numbers

for i in range(2, 11, 2):
print(i)

Output:

2
4
6
8
10

📌 range() Explanation

range(start, stop, step)
ParameterMeaning
startStarting number
stopEnding number (not included)
stepIncrement value

📚 2. Looping through a List

fruits = ["apple", "banana", "mango"]

for fruit in fruits:
print(fruit)

Output:

apple
banana
mango

🔤 3. Looping through a String

for letter in "Python":
print(letter)

Output:

P
y
t
h
o
n

🔁 4. Using for with if (Very Important)

Example: Print numbers greater than 5

for i in range(1, 11):
if i > 5:
print(i)

Output:

6
7
8
9
10

🧠 5. Nested for Loop

(Loop inside loop)

Example: Multiplication Table

for i in range(1, 4):
for j in range(1, 4):
print(i * j, end=" ")
print()

Output:

1 2 3
2 4 6
3 6 9

⚡ Advantages of for Loop

  • Easy to use
  • Reduces code length
  • Works with sequences
  • Useful for arrays, lists, strings

❌ Disadvantages

  • Not suitable for unknown repetition count (use while loop instead)

🆚 for loop vs while loop

Featurefor loopwhile loop
UsageKnown rangeUnknown condition
StructureSimpleMore flexible
ControlAutomatic iterationManual control

📌 Real-Life Example

  • Counting students in class 👨‍🎓
  • Printing attendance list 📋
  • Iterating shopping items 🛒

📘 Summary

  • for loop is used for repeating tasks
  • Works with range, lists, strings, etc.
  • Very important for programming and algorithms
  • Commonly used in sorting and searching programs

❓ Important Questions

Short Questions

  1. What is a for loop?
  2. What is range() function?
  3. Write syntax of for loop.
  4. What is nested for loop?
  5. Give one example of for loop.

Long Questions

  1. Explain for loop with example.
  2. Write program using for loop to print numbers 1 to 10.
  3. Explain nested for loop with example.
  4. Compare for loop and while loop.
  5. Write a program using for loop for multiplication table.


1. Print 1 to 10

for i in range(1, 11):
print(i)

2. Print 10 to 1 (Reverse)

for i in range(10, 0, -1):
print(i)

3. Print Even Numbers (1 to 20)

for i in range(2, 21, 2):
print(i)

4. Print Odd Numbers (1 to 20)

for i in range(1, 21, 2):
print(i)

5. Sum of 1 to 10

sum = 0
for i in range(1, 11):
sum += i
print(sum)

6. Multiplication Table of 5

for i in range(1, 11):
print(5, "x", i, "=", 5*i)

7. Factorial of a Number

n = 5
fact = 1

for i in range(1, n+1):
fact *= i

print(fact)

8. Fibonacci Series

a, b = 0, 1

for i in range(10):
print(a)
a, b = b, a + b

9. Square of Numbers (1 to 10)

for i in range(1, 11):
print(i*i)

10. Cube of Numbers (1 to 10)

for i in range(1, 11):
print(i*i*i)

11. Count Even Numbers (1 to 50)

count = 0

for i in range(1, 51):
if i % 2 == 0:
count += 1

print(count)

12. Sum of Even Numbers (1 to 20)

sum = 0

for i in range(2, 21, 2):
sum += i

print(sum)

13. Print Characters of String

for ch in "Python":
print(ch)

14. Reverse Loop Printing List

arr = [10, 20, 30, 40]

for i in range(len(arr)-1, -1, -1):
print(arr[i])

15. Find Maximum in List

arr = [10, 50, 20, 80, 30]
max = arr[0]

for i in arr:
if i > max:
max = i

print(max)

16. Find Minimum in List

arr = [10, 50, 20, 80, 5]
min = arr[0]

for i in arr:
if i < min:
min = i

print(min)

17. Print All Elements of List

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

for i in arr:
print(i)

18. Sum of List Elements

arr = [10, 20, 30, 40]
sum = 0

for i in arr:
sum += i

print(sum)

19. Print Multiples of 3 (1 to 30)

for i in range(3, 31, 3):
print(i)

20. Print Pattern (Stars)

for i in range(1, 6):
print("*" * i)