Enumerate is a built-in function in python which adds a counter to an iterable (iterable are objects that can be used with loop e.g., lists, dictionaries, etc.). So, often, we set a separate counter variable in the loop; enumerate function can save us from that task. Let's look at the following example.
In the above example, we created a list of strings and passed it to the enumerate method. The enumerate method returns an enumerator object. For printing convenience, we converted it to a list and printed the tuples.
Then we did the tuple unpacking using a for loop to see the indexes.
The enumerate() function also accepts the initial index setting, which is by default set to zero. So, the default start index is always zero.
Bonus learning tips for Jupyter notebook:
Cells can be used with the shortcut in two ways -
1. Command Mode ( When the cell borders are blue)
2.Edit mode (When the cell border is green)
Clicking outside or inside the cells can toggle between the edit modes
# If we want to insert a new cell below, we can use the shortcut button "B" in command mode
# Similarly double-pressing the "D" key twice will delete the current cell
# Shift+Enter(Return) will execute the cell
Showing posts with label Python. Show all posts
Showing posts with label Python. Show all posts
Friday, February 14, 2020
Tuesday, January 28, 2020
Today's python learning: Cascaded Assignments, Simultaneous assignment, Multiline statement, Multiline strings, split() method and eval method
Today I learned:
Cascaded Assignments:
We can assign the same value to multiple variables at the same time. For example, if we write
x=y=z=10
there will be 3 separate variables each of them holding 10 in their content. We can see from there content. This variable type and content explorer can be viewed from the Spyder IDE, which is cool and intuitive.
Simultaneous assignment:
We can also do simultaneous assignments in Python which is not commonly available to other languages. To do that we have to use a comma between expressions and an equal (assignment) operator at the right side. The following variable swapping problem is a perfect example of a simultaneous assignment.
We can see from the output that it works perfectly.
Multiline Statement:
This can be constructed using either escaping a newline character or using the parentheses.
Multiline String:
The multiline string can be done using enclosing strings into either a set of triple-double quotation marks or single quotation marks.
Follow the following examples for clarification.
Split method:
This is a very important method. It returns a list of strings after breaking the given string by the specified separator It accepts two parameters. Separator and the Max split. The following is an example of the split method, where the default separator is space.
eval() function:
This is a powerful tool as it evaluates the strings into python expressions. So we need to be careful about this tool. It will be an amazingly handy tool for building a calculator app or some graphing tools. The example can be found below.
Cascaded Assignments:
We can assign the same value to multiple variables at the same time. For example, if we write
x=y=z=10
there will be 3 separate variables each of them holding 10 in their content. We can see from there content. This variable type and content explorer can be viewed from the Spyder IDE, which is cool and intuitive.
Simultaneous assignment:
We can also do simultaneous assignments in Python which is not commonly available to other languages. To do that we have to use a comma between expressions and an equal (assignment) operator at the right side. The following variable swapping problem is a perfect example of a simultaneous assignment.
We can see from the output that it works perfectly.
Multiline Statement:
This can be constructed using either escaping a newline character or using the parentheses.
Multiline String:
The multiline string can be done using enclosing strings into either a set of triple-double quotation marks or single quotation marks.
Follow the following examples for clarification.
Split method:
This is a very important method. It returns a list of strings after breaking the given string by the specified separator It accepts two parameters. Separator and the Max split. The following is an example of the split method, where the default separator is space.
eval() function:
This is a powerful tool as it evaluates the strings into python expressions. So we need to be careful about this tool. It will be an amazingly handy tool for building a calculator app or some graphing tools. The example can be found below.
Tuesday, January 14, 2020
Pyhton exercises from Michele Pratusevich's website
While looking for python problems (because I do believe the best way to learn is by solving problems) to nurture my python skill I found this website https://www.practicepython.org by Michele Pratusevich. At first look, I liked the website and also sad by the fact that it is not being updated anymore. I will try to practice some problems from there. Will post those problems and my solution here.
Subscribe to:
Comments (Atom)
