Learn Python
- Python basic
- Introduction to File Handling
- Basics of List Comprehension
- Introduction to Matplotlib
- Classes and Objects
- Introduction to Functions
- Python Numbers
- Creating Basic Plots
- Opening and closing files
- Function parameters and arguments
- Advanced Techniques
- Attributes and Methods
- Python Strings
- Scope and lifetime of variables
- Advanced Plotting
- Reading from files
- Performance and Limitations
- Encapsulation
- Python List
- Specialized Plots
- Writing to files
- Return statement and output
- Inheritance
- Python Tuple
- Advanced Customization
- Working with different file formats
- Lambda Functions
- Polymorphism
- Python Sets
- File management operations
Python List
Introduction to Lists
Imagine a shopping list where you jot down items you need to buy. In Python, lists are similar, they are ordered collections of items that can hold various data types like numbers, text, or even other lists!
This makes them versatile for storing all sorts of information.
This makes them versatile for storing all sorts of information.
1. Creating Lists
This is the most common way. You place your items within square brackets, separated by commas.
Example:
Example:
fruits = ["apple", "banana", "cherry"] numbers = [1, 2, 3.14]
2. Understanding elements and data types
One of the powerful aspects of lists is their ability to hold different data types within the same list. You can mix and match integers, strings, floats, booleans, or even other lists!
For instance:
mixed_list = [10, "hello", 3.14, True]
3. Accessing elements by position
Each item in a list has a specific position, starting from index 0. You can access any element using its index within square brackets. The first item has index 0, the second item has index 1, and so on.
fruits = ["apple", "banana", "cherry"] first_fruit = fruits[0] # first_fruit will be "apple" last_fruit = fruits[2] # last_fruit will be "cherry"
You can also use negative indexing to access elements from the end. For example, fruits[-1] will give you the last element ("cherry") in the fruits list.
It's time to take a quiz!
Test your knowledge and see what you've just learned.
How do you create a list in Python?
AUsing square brackets and separating items with commas.
BUsing curly braces.
CUsing parentheses.
DUsing angle brackets.
Check Answer
What types of data can a Python list hold?
ADifferent data types including integers, strings, and other lists.
BOnly integers.
COnly strings.
DOnly floats.
Check Answer
How do you access the first element in a list?
AUsing index 0.
BUsing index 1.
CUsing index -1.
DUsing index 2.
Check Answer
What does the expression fruits[-1] return in a list called fruits?
AThe first element in the list.
BThe second element in the list.
CThe last element in the list.
DAn error will occur.
Check Answer
Which of the following is a valid way to create a list in Python?
Afruits = ["apple", "banana", "cherry"]
Bfruits = { "apple", "banana", "cherry" }
Cfruits = ( "apple", "banana", "cherry" )
Dfruits = < "apple", "banana", "cherry" >
Check Answer