0% found this document useful (0 votes)
2 views

Python Summary-Final Exam

The document provides a comprehensive overview of Python sequences, specifically focusing on lists and tuples, highlighting their properties, methods, and operations. It covers key concepts such as mutability, indexing, slicing, concatenation, and various built-in functions, as well as string manipulation techniques in Python. Additionally, it discusses two-dimensional lists, list comprehensions, and string methods for searching and manipulating text.

Uploaded by

cadekaahiye12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Python Summary-Final Exam

The document provides a comprehensive overview of Python sequences, specifically focusing on lists and tuples, highlighting their properties, methods, and operations. It covers key concepts such as mutability, indexing, slicing, concatenation, and various built-in functions, as well as string manipulation techniques in Python. Additionally, it discusses two-dimensional lists, list comprehensions, and string methods for searching and manipulating text.

Uploaded by

cadekaahiye12
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Python Summary

Final Exam Unit 7 & 8


❖ Sequence an object that contains multiple items of data
• Python provides different types of sequence including List and
Tuples.
• Difference between these is that List is mutable and Tuple is
immutable.
❖ Mutable sequence: the items in the sequence can be changed.
• list[1] = new_value can be used to assign a new value to a list
element
• when you using an indexing expression to assign a value to a list
element, you must use a valid index for an existing element or an
indexError exception will occur.
❖ List an item that contains multiple data items.
• An Element: An item in the list.
• Format: list = [item1, item2, etc.]
❖ Repetition operator makes multiple copies of a list and joins them
together.
• Asterisk symbol (*) is used as a repetition operator.
• General format: list * n
• Sequence is left operand and number is right
• You can iterate over a list using a for loop
• Format: for x in list:
❖ for loop is one the easiest way to access individual elements in the list.
❖ indexing is another way to access individual elements in the list.
• Each element in the list has index because it specifies its position in
the list.
• indexing starts at 0
• You can also use negative indexes with lists to identify element
positions relative to the end of the list.
• index error exception will be raised if you use invalid index with a list.
❖ len function: returns the length of a sequence such as a list.
• General format: size = len(my_list)

Prepared by: Eng Bakar


• it can be used to prevent an indexError exception when iterating
over a list with a loop
❖ Concatenate joins to things together.
• The + operator can be used to concatenate to lists.
• Cannot concatenate a list with another data type such as a number.
• The += augmented assignment operator can also be used to
concatenate lists.
❖ Slice is a span of items that are taken from a sequence.
• General format: list [ start : end ]
• Span is a list containing copies of elements from start up to, but
including end.
• If start not specified , 0 used for start index
• If end not specified, len (list) is used for end index
• Slicing expressions can be include a step value and negative indexes
relative to the end of list.
❖ The in operator can be used to determine whether an item contained in a
list.
• General format: item in list
• Returns True if the item is in the list, or false if it is not in the list
• Similarly you can use not in operator to determine whether an item
is not in a list.
❖ List have numerous methods that allow you to add elements, remove
elements, change the ordering elements and so forth.
• append (item) : is used to add items to a list -item is
appended to end of the existing list.
• index (item): used to determine whether an item is located in a
list.
o Returns the index of the first element in the list containing
item.
o Raises ValueError exception if item is not in the list.
• insert (index, item): is used to insert item at position index
in the list .
• sort(): is used to sort the elements of the list in ascending order,

Prepared by: Eng Bakar


• remove (item): removes the first occurrence of the item in the
list.
• reverse ( item): reverses order of the elements in the list.
• del statement: removes an element from specific index in a list.
o General format: del list[i]
• min and max functions: built in functions that returns the item that
has the lowest or highest value in a sequence.
o The sequence is based as an arguement

❖ There is two way to make copy list.


• Creating a new empty list and using for loop to add a copy of each
element from the original list to the new list.
• Creating a new empty list and concatenating the old list to the new
empty list.
❖ To calculate total of numeric values in a list use loop with accumulator
variable.
❖ To average numeric values in a list:
• Calculate the total of the values
• Divide the total of the values by len(list)
❖ list can be passed as an argument to a function.
def main():
numbers = [2, 4, 6, 8, 10,12]
print(f'The total is {get_total(numbers)}.')

def get_total(value_list):
total = 0
for num in value_list:
total += num
return total

main()

❖ list comprehension: a concise expression that creates a new list by iterating


over the elements of an existing list.

Prepared by: Eng Bakar


❖ Two-dimensional list: is a list that has another lists as an elements.

❖ Lists of lists are also known as nested lists or two-dimensional lists.


• Two-dimensional list have rows and columns

❖ Tuple is an Immutable sequence


• very similar to a list
• once it created it can not be changed
• format tuple_name = (item1,item 2)
• Tuple supports operations as lists
o Subscript indexing for retrieving elements
o Methods such as index
o Built in functions such as len, min, max
o Slicing expressions
o The in, +, and * operators
• Tuple do not support the methods
o Append
o Remove
o Insert
o Reverse
o Sort
❖ Advantages for using tuples over lists:
• Processing tuple is faster than processing lists
• Tuples are safe
• Some operations in python requires use of tuples.
❖ list () function: converts tuple to list
❖ tuple () function: coverts list to tuple

End Unit 7

Prepared by: Eng Bakar


Unit 8
❖ String are sequences, so many tools that work sequences work with strings.
❖ To access an individual character in a string:
• Use for loop
Useful when need to iterate over the whole string, such as
count the occurrences of the specific character.
Format: for character in string:
• Use indexing
Format: character = my_string[i]
❖ IndexError exception will occur if you try to use an index that is out of
range for the string.
❖ Len (string) function can be used to obtain the length of a string.
Useful to prevent loops for iterating beyond the end of a
string.
❖ Concatenation appending one string to the end of another string .
• Use the + operator to produce a string that is a combination of its
operands.
• The augmented sign operator += can also be used to concatenate
strings.
The operand on the left side += operator must be an existing
variable; otherwise an exception is raised.
❖ Strings are immutable
Concatenation doesn’t actually change the existing string, but
rather creates a new string and assigns the new string to the
previously used variable.
Note: string[index] = new_character -----> this
statement will raise an exception
❖ Slice: span of items taken from a sequence, known as substring.
Slicing format: string[start:end]
The expression will return a string containing a copy of the
characters from start up to, but not including, end
If start not specified, 0 is used as start index.
If end not specified, len (string) is used for end index.

Prepared by: Eng Bakar


Forexample:
full_name = 'Patty Lynn Smith’
middle_name = full_name[6:10]
❖ You can use in operator to determine whether one string is contained in
another string.
General format: string1 in string2
• String1 and string2 can be string literals or variables
referencing strings.
❖ You can use not operator to determine whether one string is not
contained in another string.
❖ Some string testing methods

Method Description

isalnum() Returns true if the string contains only


alphabetic letters or digits and is at least one
character in length. Returns false otherwise.

isalpha() Returns true if the string contains only


alphabetic letters and is at least one character
in length. Returns false otherwise.

isdigit() Returns true if the string contains only numeric


digits and is at least one character in length.
Returns false otherwise.

islower() Returns true if all of the alphabetic letters in


the string are lowercase, and the string
contains at least one alphabetic letter. Returns
false otherwise.

isspace() Returns true if the string contains only


whitespace characters and is at least one
character in length. Returns false otherwise.
(Whitespace characters are spaces, newlines
(\n), and tabs (\t).

Prepared by: Eng Bakar


isupper() Returns true if all of the alphabetic letters in
the string are uppercase, and the string
contains at least one alphabetic letter. Returns
false otherwise.

❖ Programs commonly need to search for substrings, or strings that appear


within other strings
Several methods to accomplish this:
• endswith(substring): checks if the string ends with
substring; and returns True or False
• startswith(substring): checks if the string starts with
substring; and returns True or False
• find(substring): searches for substring within the string
o Returns lowest index of the substring, or if the substring
is not contained in the string, returns -1
• replace(substring, new_string): returns a copy of
the string where every occurrence of substring is replaced
with new_string

Method Description

endswith(substring) The substring argument is a string. The


method returns true if the string ends
with substring.

find(substring) The substring argument is a string. The


method returns the lowest index in the
string where substring is found. If
substring is not found, the method
returns −1.

replace(old, new) The old and new arguments are both


strings. The method returns a copy of
the string with all instances of old
replaced by new.

Prepared by: Eng Bakar


startswith(substring) The substring argument is a string. The
method returns true if the string
starts with substring.

❖ Repetition operator: makes multiple copies of a string and joins them


together
The * symbol is a repetition operator when applied to a string and an
integer
o String is left operand; number is right
General format: string_to_copy * n

❖ split method: returns a list containing the words in the string


By default, uses space as separator
Can specify a different separator by passing it as an argument to the
split method

❖ Example:
'17;92;81;12;46;5'
This string contains the tokens 17, 92, 81, 12, 46, and 5
The delimiter is the ; character

End Unit 8

Prepared by: Eng Bakar


Prepared by: Eng Bakar

You might also like