As mentioned before, Python is a dynamically typed language. In this post we will further explore the functionality of a dynamically typed language.
In Python, there is a cool module called "types" that defines the names of the different types of variables. We can explore this module to see how Python recognizes a variable type without it being declared by the programmer.
Identifying a datatype in Python takes a very small amount of code; this hearkens back to my previous blog post that talk about the conciseness of Python's language in one-liners of code. Because Python is dynamically typed, the "type" module is able to recognize any valid datatype for what it it.
An here is the output:
Dictionaries:
Now I will briefly introduce a datatype you may have not seen before. "A Python dictionary is a mapping of unique keys to values." Dictionaries sort of act like two lists joined together or a 2D array, where there is an association between the two parts. I am introducing dictionaries, not only because they are an interesting and useful datatype to know about and one I have not encountered before in other languages, but because I will elaborate on the above example where we recognize the datatypes of each variable to show that even more complex datatypes are recognized.
Here is the example again, with an example for a dictionary as well as a list included:
Other than the possibly unfamiliar syntax of a dictionary, you might notice something else unfamiliar here. In this example, I declared name_number as a dictionary and primes as a list. Since Python is dynamically typed, I did not need to do this, but I wanted to demonstrate that it is still possible and could be useful to do, especially as your programs get more complicated.
I could have just as easily done the following:
Sources:
http://www.secnetix.de/olli/Python/dynamic_typing.hawk
https://medium.com/@ageitgey/learn-how-to-use-static-type-checking-in-python-3-6-in-10-minutes-12c86d72677b
http://www.pythonforbeginners.com/dictionary/how-to-use-dictionaries-in-python
http://developer.rhino3d.com/guides/rhinopython/python-datatypes/#tuple
Explore this last link to read about tuples, another datatype in Python. Tuples look and act very much like lists, but there are some important differences. I included this reference as supplemental reading as I feel it is important information, but does not necessarily relate to this post.
Tuesday, March 20, 2018
Sunday, March 18, 2018
Python One Liners
Many code structures that take multiple lines in a language like Java can be written using only one line of Python code. One liners of code save tons of space. They also show examples of Python's concise and structured language as I described in the first blog post.
We have already discussed how brief language is a special feature of Python, such as one line conditional statements. In this post, I will list some more examples of how Python uses one line of code to do things that take multiple lines in a language such as Java.
1. Swapping numbers
It's the tricky question that's bound to be on an exam in your coding 101 class. In Java, swapping numbers requires something like the following code, where a temp variable is created to store one of the variables before swapping:
In Python, however, we can simply use one line of code, rather than three to accomplish the swap. This is called 'unpacking for swapping variables':
2. Duplicating strings
In a language like Java, to duplicated a string, one would have to write code in a loop to concatenate a string:
But in Python, we can "multiply" a string like we would a number, all in just one line of code.
3. Store list values in separate variables
In Python, we can assign values in a list to variables using only one line of code.
Doing this in Java would require a loop to go through each position in the array. I will not show this code in Java, but you can imagine that it would take many more lines of code than we used in Python.
4. Combine items in 2 separate lists
Items in two lists can be joined in their respective positions using one line of Python code. The following is an example. Note that in this example, we use a two-liner for loop to 'zip' the lists together.
Doing this in another language would require looping through each list and concatenating the strings position by position, which would certainly take more than two lines of code.
5. Negative Indexing
Another unique feature to Python is negative indexing. Python recognizes negative indexes of a lists, and interprets them by counting through the list backwards. This is a cool feature that can be coded in one line, and it is not a feature in languages like Java.
And now for a (Monty) Python one-liner:
"My brain hurts!"
- Monty Python's Flying Circus
Until next week!
Sources:
https://www.codementor.io/sumit12dec/python-tricks-for-beginners-du107t193
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
We have already discussed how brief language is a special feature of Python, such as one line conditional statements. In this post, I will list some more examples of how Python uses one line of code to do things that take multiple lines in a language such as Java.
1. Swapping numbers
It's the tricky question that's bound to be on an exam in your coding 101 class. In Java, swapping numbers requires something like the following code, where a temp variable is created to store one of the variables before swapping:
In Python, however, we can simply use one line of code, rather than three to accomplish the swap. This is called 'unpacking for swapping variables':
2. Duplicating strings
In a language like Java, to duplicated a string, one would have to write code in a loop to concatenate a string:
But in Python, we can "multiply" a string like we would a number, all in just one line of code.
3. Store list values in separate variables
In Python, we can assign values in a list to variables using only one line of code.
Doing this in Java would require a loop to go through each position in the array. I will not show this code in Java, but you can imagine that it would take many more lines of code than we used in Python.
4. Combine items in 2 separate lists
Items in two lists can be joined in their respective positions using one line of Python code. The following is an example. Note that in this example, we use a two-liner for loop to 'zip' the lists together.
Doing this in another language would require looping through each list and concatenating the strings position by position, which would certainly take more than two lines of code.
5. Negative Indexing
Another unique feature to Python is negative indexing. Python recognizes negative indexes of a lists, and interprets them by counting through the list backwards. This is a cool feature that can be coded in one line, and it is not a feature in languages like Java.
And now for a (Monty) Python one-liner:
"My brain hurts!"
- Monty Python's Flying Circus
Until next week!
Sources:
https://www.codementor.io/sumit12dec/python-tricks-for-beginners-du107t193
http://sahandsaba.com/thirty-python-language-features-and-tricks-you-may-not-know.html
Wednesday, March 7, 2018
Binary Search
Concept
A binary search reads through a sorted list of numbers finding midpoints in order to find a particular number's position in the list. The search compares whether the desired number is equal, less than, or greater than the midpoint. If the number is equal to the midpoint, the search process is done. If the number is less than the midpoint, in the next iteration of the search, we cut off all numbers above the midpoint before calculating a new midpoint. Similarly, if the number is greater than the midpoint, we cut off all numbers less than the midpoint for the next iteration. We search until the number is equal to the midpoint and return the position of that number in the list. Otherwise, the number is not in the list.
Code
The code below is one version of binary search in Python. There are comments in the code itself, but I will draw your attention to the structure in detail. The following is a more expanded version of the Concept above:
We use a method that takes in a list and a number as inputs.
We define the original first position as position 0 and the last position as the length of the list minus 1.
We use a while loop to search through the list as long as (considering that the 'first' and 'last' positions in the list narrow down the search) first position is less than or equal to the last.
In the loop, we designate the midpoint to be between the first and last positions in the list.
If the desired number equals the midpoint, we have found the position! We return the result.
Note that .format(...) assigns variables to the bracketed words in the quotations. (This is a different way of writing a string with variables than we have seen before.)
Then, if the item in the list does not equal the midpoint, if it is less than the item in the midpoint position in the list, we change 'last' to one less than the midpoint, thereby cutting off the upper half of the list for the purpose of finding the midpoint in the next iteration of the loop.
Similarly, if the item is greater, we cut off the lower half of the list for the next iteration.
If the loop ends before returning a statement, we conclude that the item in the list is not found.
We used multiple Python features in this program that we have learned previously:
Methods
Loops
Conditionals
Variables
Arithmetic operations
:)
Sources:
I combined the following two sources in my own code for binary search
Subscribe to:
Posts (Atom)