oop Flashcards
the traditional type looping of iteration in python is the —- which enables a piece of code to execute repeatedly So long as some given condition is true. It executes a block of statements continuously until a specified condition ends up true once
the condition becomes false the program will execute the statement immediately after the loop
while loop
is a little different from while sense that it work more like an iterative procedure and for keyword
for Loop
is an iterative sequence that works on List, string dictionaries, tuples, set array, and Etc.
for Loop
this code for Loop iterates over a string and prints each character on a new line. The loop assigns each character to the variable I and continues until all characters in string has been displayed
for loop with strings
the inner loop will be executed each iteration if the value of outer loop is true
nested loop
sometimes ———- used in multi-dimensional array or depends on the problem outcome you want to have
nested looping
Stops the code immedietly when its met or encountered
break statement
It skips the current iteration and proceed to the next iteration
continue statement
a —— is a data structure that holds a sequence of elements such as numbers strings or even other lists it’s like a collection of related items similar to a list of things you’d write down like a to-do list or shopping list
list
are mutable which means you can change them after you create them you can add new elements remove elements or modify existing ones it’s like having a flexible bag you can toss things in take things out or rearrange them as needed
lists
the items inside a list are —–. This means that the position of each element is fixed so you can access items using their index position in the list starting from zero for the first item think think of it as a numbered shelf where each item has a specific Place
ordered
are immutable sequences in Python that is once created elements cannot be changed added to or removed from it
tuples
are useful to group together multiple values like lists but have the added property that their content cannot be modified they are defined by putting elements inside parentheses separated by commas
tuples
A —- in Python is an unordered collection of unique elements.
set
are defined using curly brace or by calling the set function
Sets
unlike lists or tuples, it does not allow duplicate values. this makes them useful when you need to store a collection of distinct item or when you want to perform common set operations like Union intersection or difference
set
characteristics of a sets
- unordered
- mutable
- no duplicates
- heterogenous
is a fundamental iterative structure in Python programming that allows you to repeat a code block as long as a specified condition is true.
The while loop
is a fundamental construct in Python programming that enables you to repeat a code block as long as a specified condition remains true.
The while loop
is a powerful iterative structure in Python that allows you to repeat a code block a specific number of times or iterate over elements in a sequence (e.g., a list, tuple, string, etc.)
The for loop
is a temporary variable that takes each element from the sequence one by one, and the code block is executed for each element in the sequence.
item
can be any iterable object, such as a list, tuple, string, or range, that contains multiple elements
The sequence
is a versatile iterative structure in Python that allows you to repeat a code block for a specific number of times or iterate over elements in a sequence.
The for loop
In Python, just like in C, it is possible to use loops within loops, which is known as
nested loops
provide a powerful way to perform repetitive tasks that require multiple iterations.
Nested loops
consist of an outer loop and one or more inner loops.
Nested loops
in Python provide a way to execute loops within loops, allowing for the iteration and manipulation of data on multiple levels.
Nested loops
is used to prematurely terminate the execution of a loop.
the break statement
is typically placed inside an if statement or a conditional block to check a specific condition.
The break statement
is crucial in programming as it provides control over loop execution.
The break statement
in Python provides a way to terminate the execution of a loop prematurely.
The break statement
plays a crucial role in optimizing program execution, improving efficiency, and implementing complex control flow within loops.
The break statement
is used to skip the current iteration of a loop and move to the next iteration.
the continue statement
in Python allows you to skip the current iteration of a loop and move to the next iteration.
The continue statement
is a versatile and fundamental data structure that allows you to store a collection of elements of different types.
a list
provide a convenient way to organize and manipulate data as a sequence of items.
Lists
purpose of lists
- grouping related data
- dynamic storage and retrieval
- Iteration and processing
are a powerful and flexible data structure in Python that allow you to store and manipulate collections of elements efficiently.
Lists
importance of list
- dynamic and flexible
- data organization
- iteration and processing
- versatility
- widely used
is an ordered collection of elements enclosed in parentheses ( )
a tuple
are immutable, meaning their elements cannot be modified once they are defined
tuples
practical applications of tuples
data integrity
returning multiple values
named tuples
is an unordered collection of unique elements
a set
are widely used for performing mathematical set operations such as union, intersection, and difference.
Sets
common use cases for sets
eliminating duplicates
membership testing
finding common elements
are a powerful data structure in Python that provide unique element storage and support efficient mathematical set operations.
Sets
Python, known for its straightforward syntax and versatility, heavily relies on
functions.
are defined blocks of code designed to perform a specific task.
Functions in Python
vital roles of function
modularity
reusability
maintainability
Variables specified in the function’s definition.
Formal Parameters:
Values provided during the function call.
Actual Parameters:
Functions can return data as a result.
Returning Values:
If return is not used, the function returns None.
No Return Value:
is used to exit a function and optionally pass an expression back to the caller.
The return statement in Python functions
are essential for writing clean, organized, and efficient code. Understanding how to effectively define and use functions is key for any Python programmer.
Functions in Python
Lists allow you to group related data elements of various types into a single entity. For example, you can use a list to store a sequence of integers, a list of strings, or a combination of different data types.
Grouping related data:
Lists provide dynamic storage and retrieval of elements. Unlike arrays in some other programming languages, Python lists can grow or shrink in size as needed, making them flexible for handling varying amounts of data.
Dynamic storage and retrieval:
Lists are commonly used in loops to iterate over the elements and perform operations on them. By accessing list elements sequentially, you can process large amounts of data efficiently.
Iteration and processing:
Lists in Python can grow or shrink dynamically, allowing you to handle varying amounts of data without the need for explicit resizing.
Dynamic and flexible:
Lists enable you to organize related data elements into a single sequence, making it easier to work with collections of data.
Data organization:
Lists are commonly used in loops to iterate over elements and perform operations on them. They facilitate efficient data processing.
Iteration and processing:
Lists can store elements of any data type, making them versatile for handling diverse types of data.
Versatility:
Lists are a foundational concept used in many Python programs, scripts, and applications. Understanding lists is essential for becoming proficient in Python programming.
Widely used:
involves creating a tuple by assigning values to it.
Tuple packing
allows you to assign individual elements of a tuple to separate variables.
Tuple unpacking
basic tuple operations
- concatenation
- repetition
- membership test
Since tuples are immutable, they can be used as keys in dictionaries, ensuring data integrity and preventing accidental modification.
Data Integrity:
Functions can return multiple values as a tuple, allowing convenient data packaging and unpacking.
Returning Multiple Values:
The collections.namedtuple function creates named tuples, providing more readability and self-documenting code.
Named Tuples:
Since sets only store unique elements, you can use them to remove duplicates from a list.
Eliminating Duplicates:
Sets provide fast membership tests, making it efficient to check if an element exists in a collection.
Membership Testing:
Set intersection can be used to find common elements between multiple sets.
Finding Common Elements: