10 helpful Python Tips and Tricks for Beginners | Hacker Noon

Author profile picture

@Just-into-DataLianne and Justin

Enhance data science skills and jump on a career with Just into Data Tutorials + Applications.

In this post, we’d like to share 10 useful Python tips and tricks for beginners with you.

Python is one of the most in-demand skills for data scientists. Besides providing a free Python course for beginners, we also summarize these 10 tips and tricks, which should help you with daily data science tasks.

Following this beginners’ tutorial, you’ll learn how to:

  • format strings
  • use enumerate, sorted functions
  • return multiple values from a function
  • use lambda expression, list comprehensions
  • Lots more!

If you want to make your Python coding more efficient, don’t miss these tips/tricks!

Let’s begin.

Tip #1: Display Methods/Functions

With Python being so powerful, there are many different methods and
functions. It’s especially hard for beginners to memorize all.

How do we find out the available methods/functions for a Python object?

The first Python tip for beginners is about two quick ways of doing this.

Method #1: Code completion feature

Many IDEs (Integrated Development Environment) or code editing applications for Python can auto-complete the code while you are typing. This feature is helpful to speed up programming.

For example, within Jupyter Notebook, you can type in the first few characters of a function/files, etc., and then press the Tab key to fill in the rest of the item. The screenshot below shows the available autocomplete options that start with the letter ‘p’.

Method #2: dir function

The dir function returns a list of valid attributes for the object in its argument, which means we can use it to return an object’s methods.

For example, let’s run the below Python code to apply dir on a string object.

s = 'a string'
print(dir(s))

This will return a long list of names.

Ignoring those special methods with ‘__’ at the beginning of the list, you can
find interesting methods such as capitalize, find, and lower.

[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__dir__’, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’, ‘__getnewargs__’, ‘__gt__’, ‘__hash__’, ‘__init__’, ‘__init_subclass__’, ‘__iter__’, ‘__le__’, ‘__len__’, ‘__lt__’, ‘__mod__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_ex__’, ‘__repr__’, ‘__rmod__’, ‘__rmul__’, ‘__setattr__’, ‘__sizeof__’, ‘__str__’, ‘__subclasshook__’, ‘capitalize’, ‘casefold’, ‘center’, ‘count’, ‘encode’, ‘endswith’, ‘expandtabs’, ‘find’, ‘format’, ‘format_map’, ‘index’, ‘isalnum’, ‘isalpha’, ‘isascii’, ‘isdecimal’, ‘isdigit’, ‘isidentifier’, ‘islower’, ‘isnumeric’, ‘isprintable’, ‘isspace’, ‘istitle’, ‘isupper’, ‘join’, ‘ljust’, ‘lower’, ‘lstrip’, ‘maketrans’, ‘partition’, ‘replace’, ‘rfind’, ‘rindex’, ‘rjust’, ‘rpartition’, ‘rsplit’, ‘rstrip’, ‘split’, ‘splitlines’, ‘startswith’, ‘strip’, ‘swapcase’, ‘title’, ‘translate’, ‘upper’, ‘zfill’]

Note: dir only provides an interesting set of names more than a full list. But it’s convenient to use when you can’t recall a method that you are aware of.

Besides dir, you can also try the help function. For example, help(str) will print out the Help page on the string object, which contains more details of the methods than dir.

Tip #2: Format Strings

Printing out strings is a common task within data science projects. The format method of str can piece multiple variables and strings together and format them in specific ways.

Let’s see an example.

We’ll define three variables price, paid, and change (=paid — price). What if we want to print out a full sentence containing these variables formatted as dollars?

We can use the format method as below.

price = 9.99*1.13
paid = 20
change = paid - price
print('The item cost ${0:,.2f}. I paid ${1:,.2f}. I received ${2:,.2f} in change'.format(price, paid, change))

The item cost $11.29. I paid $20.00. I received $8.71 in change

Tip #3: Enumerate Function

When iterating over an object such as a list, dictionary, or file, enumerate is a useful function. The function returns a tuple containing both the values obtained from iterating over the object, and the loop counter (from the start position of 0). The loop counter is especially handy when you want to write code based on the index.

Let’s see an example where we can do something special for the first and last element.

lst = 'Just Into Data'
length = len(lst)
for i, element in enumerate(lst):
    print('{}: {}'.format(i, element))
    if i == 0:
        print('The first element!')
    elif i == length - 1:
        print('The last element!')

We printed strings indicating the first and the last element conveniently with enumerate.

0: J
The first element!
1: u
2: s
3: t
4:
5: I
6: n
7: t
8: o
9:
10: D
11: a
12: t
13: a
The last element!

The enumerate function can also be used on files.

In the example below, we can print out the first 10 rows of the csv file before breaking out of the loop. We won’t copy the result here since it’s too long. But you can try it on any files you have.

with open('sberbank.csv') as f:
    for i, line in enumerate(f):
        if i == 10:
            break
        print(line)

Tip #4: Return Multiple Values in a Function

When defining functions, we often want to return more than one value. In
this Python tip/trick, we’ll cover three common methods below.

Method #1: Return a Tuple

First, let’s look at the most convenient way: returning a tuple. We usually only use this method if there are 2 or 3 values to return. When the number of values is more, it’s easy to forget about the order of the values within the tuple.

Below is an example function get_employee, which returns the employee’s first and last name as tuples, based on their ID numbers.

# returning a tuple.
def get_employee(id_num):
    if id_num == 0:
        return 'Jane', 'Doe'
    elif id_num == 1:
        return 'John', 'Smith'
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

If we call the function with value of 0, you can see that the function returns the tuple with two values: ‘Jane’ and ‘Doe’.

employee = get_employee(0)
print('first_name: {}, last_name: {}'.format(employee[0], employee[1]))

first_name: Jane, last_name: Doe

This is great, but what should we do when there are more values to return?

Let’s move on to the next method.

Method #2: Return a Dictionary

The second way is to return a dictionary. Dictionaries can be thought of as key: value pairs, so we can name the values that are returned, which is more clear than tuples.

The example below is similar to the previous one. But we ask the function to return a dictionary.

# returning a dictionary
def get_employee(id_num):
    if id_num == 0:
        return {'first_name': 'Jane', 'last_name': 'Doe', 'title': 'Data Scientist', 'department': 'A', 'date_joined': '20190808'}
    elif id_num == 1:
        return {'first_name': 'John', 'last_name': 'Smith', 'title': 'Data Engineer', 'department': 'B', 'date_joined': '20190808'}
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

We can call the function with id_num = 0. With the result as a dictionary, it’s easier to call the specific value with its key.

employee = get_employee(0)
print('first_name: {},nlast_name: {},ntitle: {},ndepartment: {},ndate_joined: {}'.format(
    employee['first_name'], employee['last_name'], employee['title'], employee['department'], employee['date_joined']))

first_name: Jane,

last_name: Doe,

title: Data Scientist,

department: A,

date_joined: 20190808

Method #3: Return a NamedTuple

The last way we’ll discuss is returning a namedtuple. The named tuples are
tuples with named fields. They are immutable like tuples, but also
provide naming like dictionaries.

Named tuples assign meaning to each position in a tuple and allow for more readable, self-documenting code. They can be used wherever regular
tuples are used, and they add the ability to access fields by name
instead of position index.

Python docs

The main advantage named tuples have over dictionaries is that the fields
are guaranteed to be in the object. With a dictionary, we’re not sure if all the key: value pairs will be there.

Let’s see an example of the named tuple.

We have to define the namedtuple object before using it. In the code below, we created a namedtuple object called Employee, and then used it to hold values.

# returning a namedtuple.
import collections
Employee = collections.namedtuple('Employee', ['first_name', 'last_name', 'title', 'department', 'date_joined'])

def get_employee(id_num):
    if id_num == 0:
        return Employee(first_name='Jane', last_name='Doe', title='Data Scientist', department='A', date_joined='20190808')
    elif id_num == 1:
        return Employee(first_name='John', last_name='Smith', title='Data Engineer', department='B', date_joined='20190808')
    else:
        raise Exception('No employee with this id: {}'.format(id_num))

We can call the function again with id_num = 0 to return the named tuple.

employee = get_employee(0)
print('first_name: {},nlast_name: {},ntitle: {},ndepartment: {},ndate_joined: {}'.format(
    employee.first_name, employee.last_name, employee.title, employee.department, employee.date_joined))

first_name: Jane,

last_name: Doe,

title: Data Scientist,

department: A,

date_joined: 20190808

We can also double check the variable type of the returned object, which is the Employee namedtuple defined earlier.

type(employee)

__main__.Employee

Tip #5: Lambda Expression

The Lambda expressions are used to create anonymous functions, which are usually single-line.

Below is an example showing how lambda can shorten the code for creating simple functions.

import pandas as pd
df = pd.DataFrame(data={'address': ['12 first St', '15 Second St', '20 ThIRd St', '2 First St', '8 THIRD St', '100 fIRST st']})

# without lambda.
def get_streetname(address):
    return address.split()[1].lower()

df['address'].map(get_streetname)

# using lambda function. Shorter and more clear.
df['address'].map(lambda address: address.split()[1].lower())

Both methods return the same result below.

0 first

1 second

2 third

3 first

4 third

5 first

Name: address, dtype: object

Tip #6: Sorted Function

We’ll cover the useful sorted function for this Python tip, with examples of lists and dictionaries. It’s a common task since we often want to see the top/bottom values in a dataset.

Sort Lists

Let’s look at a simple example for a list using the sorted function.

lst = [5, 5, 3, 8, 1, 9]

sorted_lst = sorted(lst)
sorted_lst

[1, 3, 5, 5, 8, 9]

Note: there’s also the list.sort() method, but we prefer sorted since it’s more general and creates a new list. See more detailed comparisons in
the Python docs.

Sort Dictionaries

For dictionaries, sorting is a little more complicated since there are keys and values.

We can apply the sorted function directly to the dictionary, which will sort the dictionary’s keys.

# Get the keys in sorted order.
d = {'T': 3, 'Q': 7, 'A': 9, 'G': 0, 'B': 8}
sorted(d)

[‘A’, ‘B’, ‘G’, ‘Q’, ‘T’]

Or sort the values of the dictionaries as below.

# Get the values in sorted order.
d = {'T': 3, 'Q': 7, 'A': 9, 'G': 0, 'B': 8}
sorted(d.values())

[0, 3, 7, 8, 9]

Or sort the whole dictionaries by either its keys or values in the Python code below.

# sort the dictionary by key.
{k:v for k,v in sorted(d.items())}

# sort the dictionary by value.
{k:v for k,v in sorted(d.items(), key=lambda it: it[1])}

{‘A’: 9, ‘B’: 8, ‘G’: 0, ‘Q’: 7, ‘T’: 3}

{‘G’: 0, ‘T’: 3, ‘Q’: 7, ‘B’: 8, ‘A’: 9}

Tip #7: Conditional Expressions

If you’ve learned the basics of Python, you should be familiar with the if-else statements. When the logic is simple, we can also use the conditional expression (or ternary operator) in one line.

Let’s see an example based on the below boolean variable is_raining.

is_raining = True

The Python code below shows the traditional way of doing it.

if is_raining:
    action = 'Stay at home'
else:
    action = 'Go for a walk'
print(action)

Stay at home

Yet, we can also use the expression below. It’s much shorter!

The expression x if C else y first evaluates the condition, C rather than x. If C is true, x is evaluated and its value is returned; otherwise, y is evaluated and its value is returned.

Python docs

action = 'Stay at home' if is_raining else 'Go for a walk'
print(action)

Tip #8: List Comprehensions

We can create lists using list comprehensions, which is much more compact than the traditional method. It’s commonly used when each element of the new list results from some operations on the element of another iterable object.

A list comprehension consists of brackets containing an expression followed by a for clause, then zero or more for or if clauses. The result will be a new list resulting from evaluating the expression in the context of the for and if clauses which follow it.

Python docs

The example shows how to use it counting the length of words in a string and assign the result as a list.

cat_in_the_hat = """the sun did not shine.
it was too wet to play.
so we sat in the house
all that cold, cold, wet day.
i sat there with sally.
we sat there, we two.
and i said, 'how i wish
we had something to do!'
too wet to go out
and too cold to play ball.
so we sat in the house.
we did nothing at all.
so all we could do was to
sit!
sit!
sit!
sit!
and we did not like it.
not one little bit."""

# assume we want to count the length of each word in the string.

# long way.
len_list1 = []
for s in cat_in_the_hat.split():
    len_list1.append(len(s))
    
# using list comprehensions we can do this in one line.
len_list2 = [len(s) for s in cat_in_the_hat.split()]

print(len_list1)
print()
print(len_list2)

Both methods return the same results below, while the list comprehension code being much shorter.

[3, 3, 3, 3, 6, 2, 3, 3, 3, 2, 5, 2, 2, 3, 2, 3, 5, 3, 4, 5, 5, 3, 4, 1, 3, 5, 4, 6, 2, 3, 6, 2, 4, 3, 1, 5, 4, 1, 4, 2, 3, 9, 2, 4, 3, 3, 2, 2, 3, 3, 3, 4, 2, 4, 5, 2, 2, 3, 2, 3, 6, 2, 3, 7, 2, 4, 2, 3, 2, 5, 2, 3, 2, 4, 4, 4, 4, 3, 2, 3, 3, 4, 3, 3, 3, 6, 4]

Tip #9: All/Any Functions

We’d also like to cover the all and the any functions in Python. They are convenient when making multiple comparisons.

The any function returns True if any element of an iterable object is true.
The example below shows how it makes coding more straightforward.

my_string = 'Toronto, Ontario'

# the long way.
if 'Montreal' in my_string or 'Toronto' in my_string or 'Vancouver' in my_string or 'Boston' in my_string or 'New York' in my_string:
    print(my_string)
    
# use the any function

# this way is shorter than the previous way.
if any([c in my_string for c in ['Montreal', 'Toronto', 'Vancouver', 'Boston', 'New York']]):
    print(my_string)

Toronto, Ontario

Similarly, the all function returns True if all elements of an iterable object are true (or if the iterable is empty). Below is an example to compare the all function and the usual method.

my_string2 = 'Just Into Data'

# the usual way
if 'Just' in my_string2 and 'In' in my_string2 and 'Data' in my_string2:
    print(my_string2)

# use all function
if all([c in my_string2 for c in ['Just', 'Into', 'Data']]):
    print(my_string2)

Just Into Data

Tip #10: Use Virtual Environments

If you are working on multiple data science projects at once, it’s critical to learn and use virtual environments.

What are the virtual environments for Python?

A virtual environment is a Python environment such that the Python interpreter, libraries and scripts installed into it are isolated from those installed in other virtual environments, and (by default) any libraries installed in a “system” Python, i.e., one which is installed as part of your operating system.

Python docs

You can create and manage virtual environments using tools like Anaconda (conda environments), virtualenv, pipenv.

Virtual environments allow different Python environments for each project, so that we can use different versions of Python and/or sets of libraries.

For example, let’s say we used plotly version 3 to create charts in a project. After a few months, plotly version 4 came out with new features. Since the project’s code had been working well in production, we are good to continue using Plotly version 3. But for any new projects, we’d like to use the new features in Plotly version 4.

In this situation, we can create two virtual environments with plotly v3 and v4 separately, and use them for the old and new projects.

Problems solved!

That’s it! Hope you found these Python tips and tricks for beginners useful.

Which tip from this post do you want to try first?

Leave a comment for any questions you may have or anything else.

Related resources:

For beginners of Python, check out our pandas and numpy tutorials to continue learning:

Or check out our other recent data science for beginners articles:

For more useful data science articles, sign up our newsletter!

Tags

The Noonification banner

Subscribe to get your daily round-up of top tech stories!

read original article here