Python programing basic knowledge hh.pdf

raviofficialmrm06 4 views 10 slides Mar 07, 2025
Slide 1
Slide 1 of 10
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10

About This Presentation

Python


Slide Content

Lists in Python
By NaimishDobariya

Introduction to Lists
•• A list is a collection of items in a particular
order.
•• Lists are mutable (can be changed).
•• Lists can contain different data types.

Creating Lists
•• Lists are defined using square brackets []
•• Example: my_list = [1, 2, 3, 'Python', True]

Accessing Elements
•• Use indexing to access elements (zero-based
indexing)
•• Example: my_list[0] gives first element
•• Negative indexing: my_list[-1] gives last
element

Modifying Lists
•• Lists are mutable, meaning elements can be
changed.
•• Example: my_list[1] = 'New Value'

Common List Methods
•• append(): Adds an item to the end
•• insert(): Inserts an item at a specified index
•• remove(): Removes a specific item
•• pop(): Removes and returns the last or
specified item
•• sort(): Sorts the list

Looping Through Lists
•• Use a for loop to iterate through a list
•• Example:
• for item in my_list:
• print(item)

List Comprehensions
•• A concise way to create lists
•• Example: squares = [x**2 for x in range(10)]

List Operations
•• Concatenation: list1 + list2
•• Repetition: list * 3
•• Membership: 'item' in list

Examples & Exercises
•• Create a list of your favorite fruits.
•• Access the second item in the list.
•• Append a new item and print the list.
•• Remove an item from the list.
Tags