8 Important Concepts and Language Features in Python
In this short video, we will discuss eight important concepts and language features in Python that
are essential to know.
References
When you assign a variable to another variable, you are creating a reference in the computer's
memory and storing it in the variable. Changing the value of a variable is actually making it refer
to a completely different value in memory. Immutable values like integers do not change, but
mutable data types like lists can be altered. To change a value inside a list, you can simply assign
a new value to a specific index.
Functions
To define a function, you need to use the def keyword. You can then use the function by
invoking it with arguments. You can also define anonymous functions or lambda expressions
using the lambda keyword. However, you cannot use them directly without giving them a
name.
Iterators and Generators
Iterators are objects used to iterate over iterable objects like lists, tuples, dictionaries, and sets.
The __next__() method is used to return the next value in the iterable. Generators are
functions that return iterators and are used to create a new collection of data.
The Yield Keyword
The yield keyword is used in Python to create generator functions. It is used in the context of a
function that takes input or data, processes it and returns a new collection of data. When
the yield keyword is encountered, it returns a value and pauses the function's execution. The
next time the function is called, it resumes from where it left off.
The yield keyword comes in handy in situations where we don't want a temporary collection
and we don't want to iterate multiple times before returning the value. To use
the yield keyword, we will delete the list and replace the res.append with yield. When
we get to the line with yield, we take that value and send it out of the function as the next
element in the sequence. If we iterate over each value, each value is handed over immediately
while suspending the function for a second. So, the process of iterating stops briefly till you ask
for the next item.
For a practical example, let's create a function called cube that takes an input of num, multiplies
each number from 1 to 5 by 3, and returns the list of numbers. We'll invoke our cube function
and return the list.
In this short video, we will discuss eight important concepts and language features in Python that
are essential to know.
References
When you assign a variable to another variable, you are creating a reference in the computer's
memory and storing it in the variable. Changing the value of a variable is actually making it refer
to a completely different value in memory. Immutable values like integers do not change, but
mutable data types like lists can be altered. To change a value inside a list, you can simply assign
a new value to a specific index.
Functions
To define a function, you need to use the def keyword. You can then use the function by
invoking it with arguments. You can also define anonymous functions or lambda expressions
using the lambda keyword. However, you cannot use them directly without giving them a
name.
Iterators and Generators
Iterators are objects used to iterate over iterable objects like lists, tuples, dictionaries, and sets.
The __next__() method is used to return the next value in the iterable. Generators are
functions that return iterators and are used to create a new collection of data.
The Yield Keyword
The yield keyword is used in Python to create generator functions. It is used in the context of a
function that takes input or data, processes it and returns a new collection of data. When
the yield keyword is encountered, it returns a value and pauses the function's execution. The
next time the function is called, it resumes from where it left off.
The yield keyword comes in handy in situations where we don't want a temporary collection
and we don't want to iterate multiple times before returning the value. To use
the yield keyword, we will delete the list and replace the res.append with yield. When
we get to the line with yield, we take that value and send it out of the function as the next
element in the sequence. If we iterate over each value, each value is handed over immediately
while suspending the function for a second. So, the process of iterating stops briefly till you ask
for the next item.
For a practical example, let's create a function called cube that takes an input of num, multiplies
each number from 1 to 5 by 3, and returns the list of numbers. We'll invoke our cube function
and return the list.