SlideShare uma empresa Scribd logo
1 de 20
Baixar para ler offline
ADVANCE YOUR PYTHON SKILLS
!"#$%&'%$$%"&()$*+,&-+.%
!#$*&/*%0%&12&/"#340
Learn how to code in the Pythonic way
Yong Cui, Ph.D. Follow
Aug 2 · 9 min read
You have 2 free stories left this month. Sign up and get an extra one for free.
:
Photo by Christopher Gower on Unsplash
Coding is fun, and coding in Python is even more fun
because there are many di7erent ways to accomplish the
same functionalities. However, most of the time, there are
preferred implementations, which some people refer to as
Pythonic. One common characteristic of these Pythonic
implementations is the neatness and conciseness of the
code.
Programming in Python or any coding language is not
rocket science, and it’s mostly about crafting skills. If you
intentionally try Pythonic coding, these techniques will
soon become part of your toolkit, and you’ll Bnd it more
and more natural to use them in your project. So let’s
explore some of these simple tricks, which I hope you’ll
Bnd helpful.
. . .
1.Negative Indexing
People like to work with sequences because we know the
order of the elements, and we can operate these elements
order-wise. In Python, strings, tuples, and lists are the
most common sequence data types. We can access
individual items using indexing. Like other mainstream
:
programming languages, Python supports 0-based
indexing, where we access the Brst element using zero
within a pair of square brackets. Besides, we can also use
slice objects to retrieve particular elements of the
sequence, as shown in the code examples below.
Positive Indexing
However, Python takes a step further by supporting
negative indexing. SpeciBcally, we can use -1 to refer to
the last element in the sequence and count the items
backward. For example, the last but one element has an
index of -2 and so on. Importantly, the negative indexing
can also work with the positive index in the slice object.
1
2
3
4
5
6
7
8
9
>>> # Positive Indexing
... numbers = [1, 2, 3, 4, 5, 6, 7, 8]
... print("First Number:", numbers[0])
... print("First Four Numbers:", numbers[:4])
... print("Odd Numbers:", numbers[::2])
...
First Number: 1
First Four Numbers: [1, 2, 3, 4]
Odd Numbers: [1, 3, 5, 7]
1
2
3
4
5
6
7
8
9
10
>>> # Negative Indexing
... data_shape = (100, 50, 4)
... names = ["John", "Aaron", "Mike", "Danny"]
... hello = "Hello World!"
...
... print(data_shape[-1])
... print(names[-3:-1])
... print(hello[1:-1:2])
...
4
:
Negative Indexing
. . .
2.Check Emptiness of Containers
Containers refer to those container data types that can
store other data. Some frequently used built-in containers
are tuples, lists, dictionaries, and sets. When we deal with
these containers, we often need to check if they contain
any elements before we perform additional operations.
Indeed, we can check the length of these containers,
which corresponds to the number of stored items. When
the length is zero, the container is empty. The below
shows you a trivial example.
Check Length
However, it’s not the most Pythonic way. Instead, we can
simply check the container itself, which will evaluate True
when it contains elements. Although the following code
shows you major container data types, such usage can also
apply to strings too (i.e., any non-empty strings are True ).
10
11
4
['Aaron', 'Mike']
1
2
3
4
if len(some_list) > 0:
# do something here when the list is not empty
else:
# do something else when the list is empty
:
Emptiness of Containers
. . .
3.Create List of Strings With Split()
We often use strings as identiBers for particular objects.
For example, we can use strings for the keys in a
dictionary. In a data science project, strings are often
column names for the data. When we select multiple
columns, we’ll inevitably need to create a list of strings.
Indeed, we can create strings using literals in a list.
However, we’ll have to write pairs of quotes to enclose
each of the strings, which is kind of tedious for us “lazyish”
people. Thus, I’d prefer to create a list of strings by taking
advantage of the string’s split() method, as shown in the
code snippet below.
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> def check_container_empty(container):
... if container:
... print(f"{container} has elements.")
... else:
... print(f"{container} doesn't have elements.")
...
... check_container_empty([1, 2, 3])
... check_container_empty(set())
... check_container_empty({"zero": 0, "one": 1})
... check_container_empty(tuple())
...
[1, 2, 3] has elements.
set() doesn't have elements.
1 >>> # List of strings
:
List of Strings
As shown above, the split() method by default uses the
spaces as the separators and creates a list of strings from
the string. Notably, when you create a list of strings with
some elements containing spaces, you can optionally use a
di7erent kind of separator (e.g., commas).
Such usage is inspired by some built-in functionalities. For
example, when you create a named tuple class, we can do
this: Student = namedtuple(“Student”, [“name”,
“gender”, “age”]) . The list of strings speciBes the tuple’s
“attributes.” However, it’s also natively supported by
deBning the class this way: Student =
namedtuple(“Student”, “name gender age”) . For another
instance, creating an Enum class supports the same
alternative solutions.
. . .
2
3
4
5
6
7
8
9
10
11
12
13
14
... # The typical way
... columns = ['name', 'age', 'gender', 'address', 'account_type']
... print("* Literals:", columns)
...
... # Do this instead
... columns = 'name age gender address account_type'.split()
... print("* Split with spaces:", columns)
...
... # If the strings contain spaces, you can use commas instead
... columns = 'name, age, gender, address, account type'.split(', '
... print("* Split with commas:", columns)
...
* Literals: ['name', 'age', 'gender', 'address', 'account_type']
:
4.Ternary Expression
In many use cases, we need to deBne variables with
particular values based on the conditions, and we can
simply use the if…else statement to check the condition.
However, it requires several lines of code. If we’re only
dealing with just the assignment of one variable, we may
want to use the ternary expression, which checks the
condition and completes the assignment with just one line
of code. Besides, it has a shorter form, which makes the
code even more concise. Consider the following example.
Ternary Expression
Sometimes, we can get some data from a deBned function,
and we can take advantage of this and write a shorthand
operation of the ternary expression, as shown below.
1
2
3
4
5
6
7
8
# The typical way
if score > 90:
reward = "1000 dollars"
else:
reward = "500 dollars"
# Do this instead
reward = "1000 dollars" if score > 90 else "500 dollars"
1
2
3
4
5
# Another possible scenario
# You got a reward amount from somewhere else, but don't know if None/0 or not
reward = reward_known or "500 dollars"
# The above line of code is equivalent to below
reward = reward_known if reward_known else "500 dollars"
:
Ternary Expression — Shorthand Operation
. . .
5.With Statement For File Object
We often need to read data from Bles and write data to
Bles. The most common way is to simply open a Ble using
the built-in open() function, which creates a Ble object
that we can operate. Have you encountered the following
problem before?
File Object Operations
In the preceding code snippet, we start with a text Ble,
which has the text of “Hello World!” We then append some
new data to the Ble. However, after a while, we want to
work on the Ble again; the text Ble still has the old data
when we read it. In other words, the appended texts are
not included in the text Ble. Why can that happen?
1
2
3
4
5
6
7
8
9
10
>>> # Create a text file that has the text: Hello World!
...
... # Open the file and append some new data
... text_file0 = open("hello_world.txt", "a")
... text_file0.write("Hello Python!")
...
... # Open the file again for something else
... text_file1 = open("hello_world.txt")
... print(text_file1.read())
...
:
It’s because we haven’t closed the Ble object in the Brst
place. Without closing the Ble, the changes can’t be saved.
Indeed, we can explicitly call the close() method on the
Ble object. However, we can do this using the “with”
statement, which will close the Ble object for us
automatically, as shown below. When we’re done with the
operation with the Ble, we can verify that the Ble is closed
by accessing the Ble object’s closed attribute.
With Statement
In a more general term, the with statement is the syntax
for using context managers in Python. The previous
example involves the Ble operation because the Bles are
shared resources, and we’re responsible for releasing these
resources. Context managers can help us get the job done.
As shown previously, after the Ble operation is over, the
Ble gets closed automatically by using the with statement.
You can learn more about context management in my
previous article.
1
2
3
4
5
6
7
8
9
>>> with open("hello_world.txt", "a") as file:
... file.write("Hello Python!")
...
... with open("hello_world.txt") as file:
... print(file.read())
...
... print("Is file close?", file.closed)
...
Hello World!Hello Python!Hello Python!
:
. . .
6.Evaluate Multiple Conditions
Oftentimes we need to evaluate multiple conditions. There
are several possible scenarios. For numeric values, we can
have multiple comparisons for the same variable. In this
case, we can chain these comparisons.
Chain the Comparison
In some other scenarios, we can have multiple equality
comparisons, and we can take advantage of the following
technique using the in keyword for membership testing.
Multiple Equality Test
1
2
3
4
5
6
7
8
# Multiple Comparisons
# The typical way
if a < 4 and a > 1:
# do something here
# Do this instead
if 1 < a < 4:
# do somerthing here
1
2
3
4
5
6
7
# The typical way
if b == "Mon" or b == "Wed" or b == "Fri" or b == "Sun":
# do something here
# Do this instead, you can also specify a tuple ("Mon", "Wed", "Fri", "Sun")
if b in "Mon Wed Fri Sun".split():
# do something here
:
Another technique is the use of the built-in all() and
any() functions for evaluating multiple conditions.
SpeciBcally, the all() function will evaluate to be True
when the elements in the iterable are all True , and thus
this function is suitable to replace a series of AND logical
comparisons. On the other hand, the any() function will
evaluate to be True when any element in the iterable is
True , and thus it’s suitable to replace a series of OR logical
operations. Pertinent examples are shown below.
all() and any() Functions
. . .
7.Use Default Values in Function Declarations
In almost all Python projects, most of the code involves
creating and calling functions. In other words, we
continuously deal with function declarations and
1
2
3
4
5
6
7
8
9
10
11
12
# The typical ways
if a < 10 and b > 5 and c == 4:
# do something
if a < 10 or b > 5 or c == 4:
# do something
# Do these instead
if all([a < 10, b > 5, c == 4]):
# do something
if any([a < 10, b > 5, c == 4]):
:
refactorings. In many scenarios, we need to call a function
multiple times. Depending on varied sets of parameters,
the function will operate slightly di7erently. However,
sometimes one set of parameters may be often used than
others, in which case, we should consider setting default
values when we declare the functions. Consider the
following trivial example.
Default Parameters in Function Declarations
One thing to note is that if you’re dealing with mutable
data types (e.g., lists, sets) when you set the default value,
make sure that you use None instead of the constructor
(e.g., arg_name=[]). Because Python creates the function
object where it’s deBned, providing the empty list will be
“stuck” with the function object. In other words, the
function object won’t be created on the ay when you’re
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# The original form:
def generate_plot(data, image_name):
"""This function creates a scatter plot for the data"""
# create the plot based on the data
...
if image_name:
# save the image
...
# In many cases, we don't need to save the image
generate_plot(data, None)
# The one with a default value
def generate_plot(data, image_name=None):
pass
:
calling it. Instead, you’ll be dealing with the same function
object, including its initially created default mutable
object, in the memory, which may lead to unexpected
behaviors (see here for more discussion).
. . .
8.Use Counter for Element Counting
When we have multiple items in a list, tuple, or string
(e.g., multiple characters), we often want to count how
many there are for each item. To do that, it’s possible to
write some tedious code for this functionality.
Counting of Elements
As shown above, we Brst had to create a set that includes
only unique words. We then iterated the word set and
used the count() method to Bnd out the occurrences of
each word. However, there is a better way to do it — using
1
2
3
4
5
6
7
8
9
10
11
>>> words = ['an', 'boy', 'girl', 'an', 'boy', 'dog', 'cat', 'Dog',
... 'GIRL', 'AN', 'dog', 'cat', 'cat', 'bag', 'BAG', 'BOY'
... unique_words = {x.lower() for x in set(words)}
... for word in unique_words:
... print(f"* Count of {word}: {words.count(word)}")
...
* Count of cat: 3
* Count of bag: 1
* Count of boy: 3
* Count of dog: 2
* Count of an: 5
:
the Counter class, which is designed to fulBll this counting
task.
Counting With Counter
The Counter class is available in the collections module.
To use the class, we simply created a generator: x.lower()
for x in words , and each of the items will be counted. As
you can see, the Counter object is a dict-like mapping
object with each key corresponding to the unique item of
the word list, while the values are the counts for these
items. Pretty concise, right?
Moreover, if you’re interested in Bnding out the most
frequently occurring items of the word list, we can take
advantage of the most_common() method of the Counter
object. The following code shows you this usage. You just
need to specify an integer (N), which will Bnd out the
most frequent N items from the list. As a side note, the
Counter object will also work with other sequence data,
such as strings and tuples.
1
2
3
4
5
6
>>> from collections import Counter
...
... word_counter = Counter(x.lower() for x in words)
... print("Word Counts:", word_counter)
...
Word Counts: Counter({'an': 5, 'boy': 4, 'cat': 4, 'dog': 3, 'girl':
1 >>> # Find out the most common item
:
Most Common Items
. . .
9.Sorting With DiSerent Order Requirements
Sorting items in a list is a prevalent task in many projects.
The most basic sorting is based on the numeric or
alphabetic order, and we can use the built-in sorted()
function. By default, the sorted() function will sort the
list (actually, it can be any iterable) in the ascending order.
If we specify the reverse argument to be True , we can get
the items in the descending order. Some simple usages are
shown below.
2
3
4
5
6
... print("Most Frequent:", word_counter.most_common(1))
Most Frequent: [('an', 5)]
>>> # Find out the most common 2 items
... print("Most Frequent:", word_counter.most_common(2))
Most Frequent: [('an', 5), ('boy', 4)]
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> # A list of numbers and strings
... numbers = [1, 3, 7, 2, 5, 4]
... words = ['yay', 'bill', 'zen', 'del']
... # Sort them
... print(sorted(numbers))
... print(sorted(words))
...
[1, 2, 3, 4, 5, 7]
['bill', 'del', 'yay', 'zen']
>>> # Sort them in descending order
... print(sorted(numbers, reverse=True))
... print(sorted(words, reverse=True))
...
:
Basic Sorting
Besides these basic usages, we can specify the key
argument such that complicated items can be sorted, such
as a list of tuples. Consider the following example for such
a case.
More Complicated Sorting
The above code shows you the two advanced sorting
examples by leveraging a lambda function, which is
passed to the key argument. The Brst one is sorting the
items using a descending order, while the second one is
using the default ascending order. What if we want to
combine these two requirements? If you think about
playing with the reverse argument, you probably bark up
the wrong tree, because if you’re trying to sort by multiple
criteria, the reverse argument will apply to all. What’s the
trick then? See the code snippet below.
1
2
3
4
5
6
7
8
9
>>> # Create a list of tuples
... grades = [('John', 95), ('Aaron', 99), ('Zack', 97), ('Don', 92),
... ('Jennifer', 100), ('Abby', 94), ('Zoe', 99), ('Dee',
>>> # Sort by the grades, descending
... sorted(grades, key=lambda x: x[1], reverse=True)
[('Jennifer', 100), ('Aaron', 99), ('Zoe', 99), ('Zack', 97), ('John'
>>> # Sort by the name's initial letter, ascending
... sorted(grades, key=lambda x: x[0][0])
[('Aaron', 99), ('Abby', 94), ('Don', 92), ('Dee', 93), ('John', 95), (
1
2
>>> # Requirement: sort by name initial ascending, and by grades, descending
... # Both won't work
:
Advanced Sorting
As you can see, by setting the reverse argument to True
or False , neither worked. Instead, the trick is to negate
the grades, and thus when you sort by the default
ascending order, the scores will be sorted reversely
because of the negation of these values. However, there is
a caveat for this method, because negation can only work
with numeric values, but not strings.
. . .
10.Don’t Forget defaultdict
Dictionaries are a potent data type that allows us to store
data in the form of key-value pairs. It’s required that all
the keys are hashable such that under the hood, storing
these data can involve the use of a hash table. Such
implementation allows an O(1) edciency for data
retrieving and insertion. However, it should be noted that
besides the built-in dict type, we have alternative
dictionaries that we can use. Among them, I’d like to
discuss the defaultdict type. Unlike the built-in dict type,
3
4
5
6
7
8
9
... sorted(grades, key=lambda x: (x[0][0], x[1]), reverse=True)
[('Zoe', 99), ('Zack', 97), ('Jennifer', 100), ('John', 95), ('Dee',
>>> sorted(grades, key=lambda x: (x[0][0], x[1]), reverse=False)
[('Abby', 94), ('Aaron', 99), ('Don', 92), ('Dee', 93), ('John', 95), (
>>> # This will do the trick
... sorted(grades, key=lambda x: (x[0][0], -x[1]))
[('Aaron', 99), ('Abby', 94), ('Dee', 93), ('Don', 92), ('Jennifer',
:
the defaultdict allows us to set a default factory function
that creates an element when the key doesn’t exist. You’re
probably not unfamiliar with the following error.
KeyError Example
Suppose that we’re dealing with words, and we want to
group the same characters as a list, and these lists are
associated with the characters being the keys. Here’s a
naive implementation using the built-in dict type. Notably,
it’s critical to check if the dict object has the letter key,
because calling the append() method can raise a KeyError
exception if the key doesn’t exist.
Group Letters Using Dict
1
2
3
4
5
6
>>> student = {'name': "John", 'age': 18}
... student['gender']
...
Traceback (most recent call last):
File "<input>", line 2, in <module>
KeyError: 'gender'
1
2
3
4
5
6
7
8
9
>>> letters = ["a", "a", "c", "d", "d", "c", "a", "b"]
... final_dict = {}
... for letter in letters:
... if letter not in final_dict:
... final_dict[letter] = []
... final_dict[letter].append(letter)
...
... print("Final Dict:", final_dict)
...
:
Let’s see how we can use the defaultdict to write more
concise code. Although the example is trivial, it’s just
giving you some ideas about the defaultdict class, which
saves us from dealing with non-existing keys among
dictionary objects.
Use Example of the defaultdict
. . .
Conclusions
You may know some of these tricks before reading this
article, but I hope you still got a good refresh on these
skills. Practicing these idiomatic usages in your projects
will make your Python code more readable and
performant.
That’s it for this piece. Thanks for reading.
1
2
3
4
5
6
7
8
9
>>> from collections import defaultdict
...
... final_defaultdict = defaultdict(list)
... for letter in letters:
... final_defaultdict[letter].append(letter)
...
... print("Final Default Dict:", final_defaultdict)
...
Final Default Dict: defaultdict(<class 'list'>, {'a': ['a', 'a', 'a'
:
Sign up for The Daily Pick
By Towards Data Science
Hands-on real-world examples, research, tutorials, and cutting-
edge techniques delivered Monday to Thursday. Make learning
your daily ritual. Take a look
Get
this
newsletterBy signing up, you will create a Medium account if you don’t already have one.
Technology Programming ArtiZcial Intelligence Data Science
Python
Discover Medium
Welcome to a place
where words matter. On
Medium, smart voices
and original ideas take
center stage - with no
ads in sight. Watch
Make Medium
yours
Follow all the topics you
care about, and we’ll
deliver the best stories
for you to your
homepage and inbox.
Explore
Become a
member
Get unlimited access to
the best stories on
Medium — and support
writers while you’re at it.
Just $5/month. Upgrade
About Help Legal
Your email
:

Mais conteúdo relacionado

Mais procurados

The amazing world behind your ORM
The amazing world behind your ORMThe amazing world behind your ORM
The amazing world behind your ORMLouise Grandjonc
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basicsLuigi De Russis
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)Pedro Rodrigues
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Pedro Rodrigues
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayUtkarsh Sengar
 
Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008eComm2008
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlowBayu Aldi Yansyah
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on WorkshopJeanne Boyarsky
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2Zaar Hai
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & listsMarc Gouw
 
Python and sysadmin I
Python and sysadmin IPython and sysadmin I
Python and sysadmin IGuixing Bai
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2Marc Gouw
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...Matt Harrison
 
Python tutorial
Python tutorialPython tutorial
Python tutorialRajiv Risi
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Matt Harrison
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In PythonMarwan Osman
 

Mais procurados (20)

Conf orm - explain
Conf orm - explainConf orm - explain
Conf orm - explain
 
The amazing world behind your ORM
The amazing world behind your ORMThe amazing world behind your ORM
The amazing world behind your ORM
 
AmI 2015 - Python basics
AmI 2015 - Python basicsAmI 2015 - Python basics
AmI 2015 - Python basics
 
Python Tutorial
Python TutorialPython Tutorial
Python Tutorial
 
Basics of Python programming (part 2)
Basics of Python programming (part 2)Basics of Python programming (part 2)
Basics of Python programming (part 2)
 
Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)Introduction to the basics of Python programming (part 3)
Introduction to the basics of Python programming (part 3)
 
Python for Dummies
Python for DummiesPython for Dummies
Python for Dummies
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008Rocky Nevin's presentation at eComm 2008
Rocky Nevin's presentation at eComm 2008
 
Introduction to Python and TensorFlow
Introduction to Python and TensorFlowIntroduction to Python and TensorFlow
Introduction to Python and TensorFlow
 
Intro to Python
Intro to PythonIntro to Python
Intro to Python
 
java 8 Hands on Workshop
java 8 Hands on Workshopjava 8 Hands on Workshop
java 8 Hands on Workshop
 
Advanced Python, Part 2
Advanced Python, Part 2Advanced Python, Part 2
Advanced Python, Part 2
 
Class 5: If, while & lists
Class 5: If, while & listsClass 5: If, while & lists
Class 5: If, while & lists
 
Python and sysadmin I
Python and sysadmin IPython and sysadmin I
Python and sysadmin I
 
Class 2: Welcome part 2
Class 2: Welcome part 2Class 2: Welcome part 2
Class 2: Welcome part 2
 
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
How to Become a Tree Hugger: Random Forests and Predictive Modeling for Devel...
 
Python tutorial
Python tutorialPython tutorial
Python tutorial
 
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
Analysis of Fatal Utah Avalanches with Python. From Scraping, Analysis, to In...
 
Programming Under Linux In Python
Programming Under Linux In PythonProgramming Under Linux In Python
Programming Under Linux In Python
 

Semelhante a Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 2020 | towards data science

These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2sadhana312471
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python amiable_indian
 
Sphinx autodoc - automated api documentation - PyCon.KR 2015
Sphinx autodoc - automated api documentation - PyCon.KR 2015Sphinx autodoc - automated api documentation - PyCon.KR 2015
Sphinx autodoc - automated api documentation - PyCon.KR 2015Takayuki Shimizukawa
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Takayuki Shimizukawa
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style GuidesMosky Liu
 
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)Takayuki Shimizukawa
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Simplilearn
 
Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015Takayuki Shimizukawa
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonTariq Rashid
 
Notes1
Notes1Notes1
Notes1hccit
 
Streams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupStreams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupBrian Cardiff
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...DRVaibhavmeshram1
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And AnswersH2Kinfosys
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsPatchSpace Ltd
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202Mahmoud Samir Fayed
 

Semelhante a Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 2020 | towards data science (20)

These questions will be a bit advanced level 2
These questions will be a bit advanced level 2These questions will be a bit advanced level 2
These questions will be a bit advanced level 2
 
Dynamic Python
Dynamic PythonDynamic Python
Dynamic Python
 
Introduction to Python
Introduction to Python Introduction to Python
Introduction to Python
 
Python cheat-sheet
Python cheat-sheetPython cheat-sheet
Python cheat-sheet
 
Sphinx autodoc - automated api documentation - PyCon.KR 2015
Sphinx autodoc - automated api documentation - PyCon.KR 2015Sphinx autodoc - automated api documentation - PyCon.KR 2015
Sphinx autodoc - automated api documentation - PyCon.KR 2015
 
Python 101 1
Python 101   1Python 101   1
Python 101 1
 
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
Sphinx autodoc - automated API documentation (PyCon APAC 2015 in Taiwan)
 
Beyond the Style Guides
Beyond the Style GuidesBeyond the Style Guides
Beyond the Style Guides
 
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)
Sphinx autodoc - automated API documentation (EuroPython 2015 in Bilbao)
 
Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...Python Interview Questions | Python Interview Questions And Answers | Python ...
Python Interview Questions | Python Interview Questions And Answers | Python ...
 
Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015Sphinx autodoc - automated api documentation - PyCon.MY 2015
Sphinx autodoc - automated api documentation - PyCon.MY 2015
 
Python basics
Python basicsPython basics
Python basics
 
A Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with PythonA Gentle Introduction to Coding ... with Python
A Gentle Introduction to Coding ... with Python
 
Notes1
Notes1Notes1
Notes1
 
Streams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetupStreams of information - Chicago crystal language monthly meetup
Streams of information - Chicago crystal language monthly meetup
 
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
Introduction to Python 01-08-2023.pon by everyone else. . Hence, they must be...
 
CSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptxCSV JSON and XML files in Python.pptx
CSV JSON and XML files in Python.pptx
 
Python Interview Questions And Answers
Python Interview Questions And AnswersPython Interview Questions And Answers
Python Interview Questions And Answers
 
Uses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & StubsUses & Abuses of Mocks & Stubs
Uses & Abuses of Mocks & Stubs
 
The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202The Ring programming language version 1.8 book - Part 7 of 202
The Ring programming language version 1.8 book - Part 7 of 202
 

Último

VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...singhpriety023
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceDelhi Call girls
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445ruhi
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024APNIC
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableSeo
 
Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGAPNIC
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024APNIC
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girladitipandeya
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...APNIC
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Delhi Call girls
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Delhi Call girls
 
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Onlineanilsa9823
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...Escorts Call Girls
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Servicegwenoracqe6
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...tanu pandey
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.soniya singh
 

Último (20)

Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No AdvanceRohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
Rohini Sector 6 Call Girls Delhi 9999965857 @Sabina Saikh No Advance
 
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting  High Prof...
VIP Model Call Girls Hadapsar ( Pune ) Call ON 9905417584 Starting High Prof...
 
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Saket Delhi 💯Call Us 🔝8264348440🔝
 
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
@9999965857 🫦 Sexy Desi Call Girls Laxmi Nagar 💓 High Profile Escorts Delhi 🫶
 
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort ServiceEnjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
Enjoy Night⚡Call Girls Dlf City Phase 3 Gurgaon >༒8448380779 Escort Service
 
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
All Time Service Available Call Girls Mg Road 👌 ⏭️ 6378878445
 
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
Call Girls In Defence Colony Delhi 💯Call Us 🔝8264348440🔝
 
On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024On Starlink, presented by Geoff Huston at NZNOG 2024
On Starlink, presented by Geoff Huston at NZNOG 2024
 
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service AvailableCall Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
Call Girls Ludhiana Just Call 98765-12871 Top Class Call Girl Service Available
 
Networking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOGNetworking in the Penumbra presented by Geoff Huston at NZNOG
Networking in the Penumbra presented by Geoff Huston at NZNOG
 
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
DDoS In Oceania and the Pacific, presented by Dave Phelan at NZNOG 2024
 
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call GirlVIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
VIP 7001035870 Find & Meet Hyderabad Call Girls LB Nagar high-profile Call Girl
 
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
'Future Evolution of the Internet' delivered by Geoff Huston at Everything Op...
 
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
Hire↠Young Call Girls in Tilak nagar (Delhi) ☎️ 9205541914 ☎️ Independent Esc...
 
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
Best VIP Call Girls Noida Sector 75 Call Me: 8448380779
 
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service OnlineCALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Online
CALL ON ➥8923113531 🔝Call Girls Lucknow Lucknow best sexual service Online
 
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...(+971568250507  ))#  Young Call Girls  in Ajman  By Pakistani Call Girls  in ...
(+971568250507 ))# Young Call Girls in Ajman By Pakistani Call Girls in ...
 
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl ServiceRussian Call girl in Ajman +971563133746 Ajman Call girl Service
Russian Call girl in Ajman +971563133746 Ajman Call girl Service
 
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...Nanded City ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready ...
Nanded City ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready ...
 
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
Call Now ☎ 8264348440 !! Call Girls in Green Park Escort Service Delhi N.C.R.
 

Write better python code with these 10 tricks | by yong cui, ph.d. | aug, 2020 | towards data science

  • 1. ADVANCE YOUR PYTHON SKILLS !"#$%&'%$$%"&()$*+,&-+.% !#$*&/*%0%&12&/"#340 Learn how to code in the Pythonic way Yong Cui, Ph.D. Follow Aug 2 · 9 min read You have 2 free stories left this month. Sign up and get an extra one for free. :
  • 2. Photo by Christopher Gower on Unsplash Coding is fun, and coding in Python is even more fun because there are many di7erent ways to accomplish the same functionalities. However, most of the time, there are preferred implementations, which some people refer to as Pythonic. One common characteristic of these Pythonic implementations is the neatness and conciseness of the code. Programming in Python or any coding language is not rocket science, and it’s mostly about crafting skills. If you intentionally try Pythonic coding, these techniques will soon become part of your toolkit, and you’ll Bnd it more and more natural to use them in your project. So let’s explore some of these simple tricks, which I hope you’ll Bnd helpful. . . . 1.Negative Indexing People like to work with sequences because we know the order of the elements, and we can operate these elements order-wise. In Python, strings, tuples, and lists are the most common sequence data types. We can access individual items using indexing. Like other mainstream :
  • 3. programming languages, Python supports 0-based indexing, where we access the Brst element using zero within a pair of square brackets. Besides, we can also use slice objects to retrieve particular elements of the sequence, as shown in the code examples below. Positive Indexing However, Python takes a step further by supporting negative indexing. SpeciBcally, we can use -1 to refer to the last element in the sequence and count the items backward. For example, the last but one element has an index of -2 and so on. Importantly, the negative indexing can also work with the positive index in the slice object. 1 2 3 4 5 6 7 8 9 >>> # Positive Indexing ... numbers = [1, 2, 3, 4, 5, 6, 7, 8] ... print("First Number:", numbers[0]) ... print("First Four Numbers:", numbers[:4]) ... print("Odd Numbers:", numbers[::2]) ... First Number: 1 First Four Numbers: [1, 2, 3, 4] Odd Numbers: [1, 3, 5, 7] 1 2 3 4 5 6 7 8 9 10 >>> # Negative Indexing ... data_shape = (100, 50, 4) ... names = ["John", "Aaron", "Mike", "Danny"] ... hello = "Hello World!" ... ... print(data_shape[-1]) ... print(names[-3:-1]) ... print(hello[1:-1:2]) ... 4 :
  • 4. Negative Indexing . . . 2.Check Emptiness of Containers Containers refer to those container data types that can store other data. Some frequently used built-in containers are tuples, lists, dictionaries, and sets. When we deal with these containers, we often need to check if they contain any elements before we perform additional operations. Indeed, we can check the length of these containers, which corresponds to the number of stored items. When the length is zero, the container is empty. The below shows you a trivial example. Check Length However, it’s not the most Pythonic way. Instead, we can simply check the container itself, which will evaluate True when it contains elements. Although the following code shows you major container data types, such usage can also apply to strings too (i.e., any non-empty strings are True ). 10 11 4 ['Aaron', 'Mike'] 1 2 3 4 if len(some_list) > 0: # do something here when the list is not empty else: # do something else when the list is empty :
  • 5. Emptiness of Containers . . . 3.Create List of Strings With Split() We often use strings as identiBers for particular objects. For example, we can use strings for the keys in a dictionary. In a data science project, strings are often column names for the data. When we select multiple columns, we’ll inevitably need to create a list of strings. Indeed, we can create strings using literals in a list. However, we’ll have to write pairs of quotes to enclose each of the strings, which is kind of tedious for us “lazyish” people. Thus, I’d prefer to create a list of strings by taking advantage of the string’s split() method, as shown in the code snippet below. 1 2 3 4 5 6 7 8 9 10 11 12 13 >>> def check_container_empty(container): ... if container: ... print(f"{container} has elements.") ... else: ... print(f"{container} doesn't have elements.") ... ... check_container_empty([1, 2, 3]) ... check_container_empty(set()) ... check_container_empty({"zero": 0, "one": 1}) ... check_container_empty(tuple()) ... [1, 2, 3] has elements. set() doesn't have elements. 1 >>> # List of strings :
  • 6. List of Strings As shown above, the split() method by default uses the spaces as the separators and creates a list of strings from the string. Notably, when you create a list of strings with some elements containing spaces, you can optionally use a di7erent kind of separator (e.g., commas). Such usage is inspired by some built-in functionalities. For example, when you create a named tuple class, we can do this: Student = namedtuple(“Student”, [“name”, “gender”, “age”]) . The list of strings speciBes the tuple’s “attributes.” However, it’s also natively supported by deBning the class this way: Student = namedtuple(“Student”, “name gender age”) . For another instance, creating an Enum class supports the same alternative solutions. . . . 2 3 4 5 6 7 8 9 10 11 12 13 14 ... # The typical way ... columns = ['name', 'age', 'gender', 'address', 'account_type'] ... print("* Literals:", columns) ... ... # Do this instead ... columns = 'name age gender address account_type'.split() ... print("* Split with spaces:", columns) ... ... # If the strings contain spaces, you can use commas instead ... columns = 'name, age, gender, address, account type'.split(', ' ... print("* Split with commas:", columns) ... * Literals: ['name', 'age', 'gender', 'address', 'account_type'] :
  • 7. 4.Ternary Expression In many use cases, we need to deBne variables with particular values based on the conditions, and we can simply use the if…else statement to check the condition. However, it requires several lines of code. If we’re only dealing with just the assignment of one variable, we may want to use the ternary expression, which checks the condition and completes the assignment with just one line of code. Besides, it has a shorter form, which makes the code even more concise. Consider the following example. Ternary Expression Sometimes, we can get some data from a deBned function, and we can take advantage of this and write a shorthand operation of the ternary expression, as shown below. 1 2 3 4 5 6 7 8 # The typical way if score > 90: reward = "1000 dollars" else: reward = "500 dollars" # Do this instead reward = "1000 dollars" if score > 90 else "500 dollars" 1 2 3 4 5 # Another possible scenario # You got a reward amount from somewhere else, but don't know if None/0 or not reward = reward_known or "500 dollars" # The above line of code is equivalent to below reward = reward_known if reward_known else "500 dollars" :
  • 8. Ternary Expression — Shorthand Operation . . . 5.With Statement For File Object We often need to read data from Bles and write data to Bles. The most common way is to simply open a Ble using the built-in open() function, which creates a Ble object that we can operate. Have you encountered the following problem before? File Object Operations In the preceding code snippet, we start with a text Ble, which has the text of “Hello World!” We then append some new data to the Ble. However, after a while, we want to work on the Ble again; the text Ble still has the old data when we read it. In other words, the appended texts are not included in the text Ble. Why can that happen? 1 2 3 4 5 6 7 8 9 10 >>> # Create a text file that has the text: Hello World! ... ... # Open the file and append some new data ... text_file0 = open("hello_world.txt", "a") ... text_file0.write("Hello Python!") ... ... # Open the file again for something else ... text_file1 = open("hello_world.txt") ... print(text_file1.read()) ... :
  • 9. It’s because we haven’t closed the Ble object in the Brst place. Without closing the Ble, the changes can’t be saved. Indeed, we can explicitly call the close() method on the Ble object. However, we can do this using the “with” statement, which will close the Ble object for us automatically, as shown below. When we’re done with the operation with the Ble, we can verify that the Ble is closed by accessing the Ble object’s closed attribute. With Statement In a more general term, the with statement is the syntax for using context managers in Python. The previous example involves the Ble operation because the Bles are shared resources, and we’re responsible for releasing these resources. Context managers can help us get the job done. As shown previously, after the Ble operation is over, the Ble gets closed automatically by using the with statement. You can learn more about context management in my previous article. 1 2 3 4 5 6 7 8 9 >>> with open("hello_world.txt", "a") as file: ... file.write("Hello Python!") ... ... with open("hello_world.txt") as file: ... print(file.read()) ... ... print("Is file close?", file.closed) ... Hello World!Hello Python!Hello Python! :
  • 10. . . . 6.Evaluate Multiple Conditions Oftentimes we need to evaluate multiple conditions. There are several possible scenarios. For numeric values, we can have multiple comparisons for the same variable. In this case, we can chain these comparisons. Chain the Comparison In some other scenarios, we can have multiple equality comparisons, and we can take advantage of the following technique using the in keyword for membership testing. Multiple Equality Test 1 2 3 4 5 6 7 8 # Multiple Comparisons # The typical way if a < 4 and a > 1: # do something here # Do this instead if 1 < a < 4: # do somerthing here 1 2 3 4 5 6 7 # The typical way if b == "Mon" or b == "Wed" or b == "Fri" or b == "Sun": # do something here # Do this instead, you can also specify a tuple ("Mon", "Wed", "Fri", "Sun") if b in "Mon Wed Fri Sun".split(): # do something here :
  • 11. Another technique is the use of the built-in all() and any() functions for evaluating multiple conditions. SpeciBcally, the all() function will evaluate to be True when the elements in the iterable are all True , and thus this function is suitable to replace a series of AND logical comparisons. On the other hand, the any() function will evaluate to be True when any element in the iterable is True , and thus it’s suitable to replace a series of OR logical operations. Pertinent examples are shown below. all() and any() Functions . . . 7.Use Default Values in Function Declarations In almost all Python projects, most of the code involves creating and calling functions. In other words, we continuously deal with function declarations and 1 2 3 4 5 6 7 8 9 10 11 12 # The typical ways if a < 10 and b > 5 and c == 4: # do something if a < 10 or b > 5 or c == 4: # do something # Do these instead if all([a < 10, b > 5, c == 4]): # do something if any([a < 10, b > 5, c == 4]): :
  • 12. refactorings. In many scenarios, we need to call a function multiple times. Depending on varied sets of parameters, the function will operate slightly di7erently. However, sometimes one set of parameters may be often used than others, in which case, we should consider setting default values when we declare the functions. Consider the following trivial example. Default Parameters in Function Declarations One thing to note is that if you’re dealing with mutable data types (e.g., lists, sets) when you set the default value, make sure that you use None instead of the constructor (e.g., arg_name=[]). Because Python creates the function object where it’s deBned, providing the empty list will be “stuck” with the function object. In other words, the function object won’t be created on the ay when you’re 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 # The original form: def generate_plot(data, image_name): """This function creates a scatter plot for the data""" # create the plot based on the data ... if image_name: # save the image ... # In many cases, we don't need to save the image generate_plot(data, None) # The one with a default value def generate_plot(data, image_name=None): pass :
  • 13. calling it. Instead, you’ll be dealing with the same function object, including its initially created default mutable object, in the memory, which may lead to unexpected behaviors (see here for more discussion). . . . 8.Use Counter for Element Counting When we have multiple items in a list, tuple, or string (e.g., multiple characters), we often want to count how many there are for each item. To do that, it’s possible to write some tedious code for this functionality. Counting of Elements As shown above, we Brst had to create a set that includes only unique words. We then iterated the word set and used the count() method to Bnd out the occurrences of each word. However, there is a better way to do it — using 1 2 3 4 5 6 7 8 9 10 11 >>> words = ['an', 'boy', 'girl', 'an', 'boy', 'dog', 'cat', 'Dog', ... 'GIRL', 'AN', 'dog', 'cat', 'cat', 'bag', 'BAG', 'BOY' ... unique_words = {x.lower() for x in set(words)} ... for word in unique_words: ... print(f"* Count of {word}: {words.count(word)}") ... * Count of cat: 3 * Count of bag: 1 * Count of boy: 3 * Count of dog: 2 * Count of an: 5 :
  • 14. the Counter class, which is designed to fulBll this counting task. Counting With Counter The Counter class is available in the collections module. To use the class, we simply created a generator: x.lower() for x in words , and each of the items will be counted. As you can see, the Counter object is a dict-like mapping object with each key corresponding to the unique item of the word list, while the values are the counts for these items. Pretty concise, right? Moreover, if you’re interested in Bnding out the most frequently occurring items of the word list, we can take advantage of the most_common() method of the Counter object. The following code shows you this usage. You just need to specify an integer (N), which will Bnd out the most frequent N items from the list. As a side note, the Counter object will also work with other sequence data, such as strings and tuples. 1 2 3 4 5 6 >>> from collections import Counter ... ... word_counter = Counter(x.lower() for x in words) ... print("Word Counts:", word_counter) ... Word Counts: Counter({'an': 5, 'boy': 4, 'cat': 4, 'dog': 3, 'girl': 1 >>> # Find out the most common item :
  • 15. Most Common Items . . . 9.Sorting With DiSerent Order Requirements Sorting items in a list is a prevalent task in many projects. The most basic sorting is based on the numeric or alphabetic order, and we can use the built-in sorted() function. By default, the sorted() function will sort the list (actually, it can be any iterable) in the ascending order. If we specify the reverse argument to be True , we can get the items in the descending order. Some simple usages are shown below. 2 3 4 5 6 ... print("Most Frequent:", word_counter.most_common(1)) Most Frequent: [('an', 5)] >>> # Find out the most common 2 items ... print("Most Frequent:", word_counter.most_common(2)) Most Frequent: [('an', 5), ('boy', 4)] 1 2 3 4 5 6 7 8 9 10 11 12 13 >>> # A list of numbers and strings ... numbers = [1, 3, 7, 2, 5, 4] ... words = ['yay', 'bill', 'zen', 'del'] ... # Sort them ... print(sorted(numbers)) ... print(sorted(words)) ... [1, 2, 3, 4, 5, 7] ['bill', 'del', 'yay', 'zen'] >>> # Sort them in descending order ... print(sorted(numbers, reverse=True)) ... print(sorted(words, reverse=True)) ... :
  • 16. Basic Sorting Besides these basic usages, we can specify the key argument such that complicated items can be sorted, such as a list of tuples. Consider the following example for such a case. More Complicated Sorting The above code shows you the two advanced sorting examples by leveraging a lambda function, which is passed to the key argument. The Brst one is sorting the items using a descending order, while the second one is using the default ascending order. What if we want to combine these two requirements? If you think about playing with the reverse argument, you probably bark up the wrong tree, because if you’re trying to sort by multiple criteria, the reverse argument will apply to all. What’s the trick then? See the code snippet below. 1 2 3 4 5 6 7 8 9 >>> # Create a list of tuples ... grades = [('John', 95), ('Aaron', 99), ('Zack', 97), ('Don', 92), ... ('Jennifer', 100), ('Abby', 94), ('Zoe', 99), ('Dee', >>> # Sort by the grades, descending ... sorted(grades, key=lambda x: x[1], reverse=True) [('Jennifer', 100), ('Aaron', 99), ('Zoe', 99), ('Zack', 97), ('John' >>> # Sort by the name's initial letter, ascending ... sorted(grades, key=lambda x: x[0][0]) [('Aaron', 99), ('Abby', 94), ('Don', 92), ('Dee', 93), ('John', 95), ( 1 2 >>> # Requirement: sort by name initial ascending, and by grades, descending ... # Both won't work :
  • 17. Advanced Sorting As you can see, by setting the reverse argument to True or False , neither worked. Instead, the trick is to negate the grades, and thus when you sort by the default ascending order, the scores will be sorted reversely because of the negation of these values. However, there is a caveat for this method, because negation can only work with numeric values, but not strings. . . . 10.Don’t Forget defaultdict Dictionaries are a potent data type that allows us to store data in the form of key-value pairs. It’s required that all the keys are hashable such that under the hood, storing these data can involve the use of a hash table. Such implementation allows an O(1) edciency for data retrieving and insertion. However, it should be noted that besides the built-in dict type, we have alternative dictionaries that we can use. Among them, I’d like to discuss the defaultdict type. Unlike the built-in dict type, 3 4 5 6 7 8 9 ... sorted(grades, key=lambda x: (x[0][0], x[1]), reverse=True) [('Zoe', 99), ('Zack', 97), ('Jennifer', 100), ('John', 95), ('Dee', >>> sorted(grades, key=lambda x: (x[0][0], x[1]), reverse=False) [('Abby', 94), ('Aaron', 99), ('Don', 92), ('Dee', 93), ('John', 95), ( >>> # This will do the trick ... sorted(grades, key=lambda x: (x[0][0], -x[1])) [('Aaron', 99), ('Abby', 94), ('Dee', 93), ('Don', 92), ('Jennifer', :
  • 18. the defaultdict allows us to set a default factory function that creates an element when the key doesn’t exist. You’re probably not unfamiliar with the following error. KeyError Example Suppose that we’re dealing with words, and we want to group the same characters as a list, and these lists are associated with the characters being the keys. Here’s a naive implementation using the built-in dict type. Notably, it’s critical to check if the dict object has the letter key, because calling the append() method can raise a KeyError exception if the key doesn’t exist. Group Letters Using Dict 1 2 3 4 5 6 >>> student = {'name': "John", 'age': 18} ... student['gender'] ... Traceback (most recent call last): File "<input>", line 2, in <module> KeyError: 'gender' 1 2 3 4 5 6 7 8 9 >>> letters = ["a", "a", "c", "d", "d", "c", "a", "b"] ... final_dict = {} ... for letter in letters: ... if letter not in final_dict: ... final_dict[letter] = [] ... final_dict[letter].append(letter) ... ... print("Final Dict:", final_dict) ... :
  • 19. Let’s see how we can use the defaultdict to write more concise code. Although the example is trivial, it’s just giving you some ideas about the defaultdict class, which saves us from dealing with non-existing keys among dictionary objects. Use Example of the defaultdict . . . Conclusions You may know some of these tricks before reading this article, but I hope you still got a good refresh on these skills. Practicing these idiomatic usages in your projects will make your Python code more readable and performant. That’s it for this piece. Thanks for reading. 1 2 3 4 5 6 7 8 9 >>> from collections import defaultdict ... ... final_defaultdict = defaultdict(list) ... for letter in letters: ... final_defaultdict[letter].append(letter) ... ... print("Final Default Dict:", final_defaultdict) ... Final Default Dict: defaultdict(<class 'list'>, {'a': ['a', 'a', 'a' :
  • 20. Sign up for The Daily Pick By Towards Data Science Hands-on real-world examples, research, tutorials, and cutting- edge techniques delivered Monday to Thursday. Make learning your daily ritual. Take a look Get this newsletterBy signing up, you will create a Medium account if you don’t already have one. Technology Programming ArtiZcial Intelligence Data Science Python Discover Medium Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch Make Medium yours Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore Become a member Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade About Help Legal Your email :