Lesson 1 - Python Strings, Lists, Tuples, Functions, Modules
Lesson 1 - Python Strings, Lists, Tuples, Functions, Modules
1
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
Lists
Definition: Used to store multiple items in a single variable.
Creation: Created using square brackets [].
Properties: Ordered, changeable, and allow duplicate values.
Indexing: Items are indexed starting from 0. Access items using square
brackets []. Supports negative indexing (-1 for the last item).
Length: Use the len() function to get the number of items.
Data Types: List items can be of any data type, including a mix of di erent
types.
Accessing Items: Refer to the index number.
Range of Indexes (Slicing): Specify a [start:end] range to return a new list
with specified items. start is inclusive, end is exclusive. Negative indexes
can be used for range specification.
Checking Presence: Use the in keyword to determine if a specified item is
present.
Changing Items:Change a specific item: Refer to the index number and
assign a new value.
Change a range of items: Define a list with new values and refer to the
range of index numbers.
Adding Items:append(): Adds an item to the end of the list.
insert(index, item): Inserts an item at a specified index.
Sorting:sort(): Sorts the list alphanumerically in ascending order by
default.
sort(reverse=True): Sorts in descending order.
Reversing: reverse(): Reverses the current sorting order of the elements.
List Slicing Syntax: list_name[start : end : step]. start is inclusive, end is
exclusive, and step is the interval (defaults to 1). Omitting start or end
defaults to the beginning or end of the list respectively.
2
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
3
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
5
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
6
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
Quiz
Answer the following questions in 2-3 sentences each.
1. What is a Python string, and how can you create one?
2. Explain the di erence between positive and negative indexing in Python
strings.
3. How do you find the length of a string or a list in Python?
4. What is string slicing, and how is the end index treated in a slice?
5. Describe the purpose of string concatenation and the operator used for it.
6. What are Python lists, and what are their key properties?
7. How do you access specific items in a Python list?
8. Explain how to change the value of an item in a Python list.
9. What are Python tuples, and how do they di er from lists?
10.What is a Python function, and why are functions useful?
Answer Key
1. A Python string is a sequence of characters. You can create a string by
enclosing characters within either single quotes (') or double quotes (").
2. Positive indexing in Python strings starts from the beginning, with the first
character at index 0, the second at index 1, and so on. Negative indexing
starts from the end, with the last character at index -1, the second to last
at index -2, and so forth.
3. To find the length of a string or a list in Python, you use the built-in len()
function. You pass the string or list as an argument to this function, and it
returns the number of characters or items, respectively.
4. String slicing in Python allows you to extract a part of a string by specifying
a range of indexes. In a slice [start:end], the character at the start index is
included, but the character at the end index is not included in the resulting
slice.
7
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
8
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
9
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
10
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
11
email : [email protected]
SRI JAGANNATH INSTITUTE OF MANAGEMENT & TECHNOLOGY
(SCHOOL OF DIGITAL SCIENCES)
PYTHON FUNDAMENTALS: STRINGS, LISTS, TUPLES, FUNCTIONS, AND MODULES
Notes
12
email : [email protected]