5 Handy Python Tips For Beginners
After 30+ days of my 100 day Python journey, I stop to reflect on the five tips that have been unusual by helpful coming into the language.
On this page
This is Day 33 of the #100DaysOfPython challenge.
Today's post will cover five tips that have come in handy while learning Python.
Zip function
A zip object yielding tuples until an input is exhausted. The definition for the zip function is zip(*iterables).
# Zip function
a = [1,2,3,4]
b = [5,6,7,8]
for index, _ in enumerate(a):
print(a[index] + b[index]) # Prints out 6, 8, 10, 12 respectively
# Zip equivalent
for i, j in zip(a, b):
print(i + j) # Prints out 6, 8, 10, 12 respectively
Zip also works with strings:
a = ['Hello']
b = ['World']
for i, j in zip(a, b):
print(f"{i}, {j}!") # Hello, World!
Chaining of comparison operators
Something cool that I came across from Python was the ability to chain comparison operators without the need to write separate conditionals.
For example, instead of writing a conditional x >= 8 and x <= 10, I could omit the and and simply check 8 <= x <= 10 to be true.
# Chaining of comparison operators
print(8 < 9 < 10) # True
# Follow lambda is equivalent to... lambda x: x >= 8 and x <= 10
is_between_eight_and_ten = lambda x: 8 <= x <= 10
print(is_between_eight_and_ten(4)) # False
print(is_between_eight_and_ten(9)) # True
Slice notation
Slice notation works like so [start:stop:index] where each value start, stop and index is an integer.
This can be incredibly useful when working with data where you need to reshape lists and tuples.
# Slice notation
# The notation looks like so [start:stop:step]
a = [1,2,3,4]
print(a[::]) # equivalent to a[0:len(a):1] -> [1,2,3,4]
print(a[1::]) # equivalent to a[1:len(a):1] -> [2, 3, 4]
print(a[:1:]) # equivalent to a[0:1:1] -> [1]
print(a[::2]) # equivalent to a[0:len(a):2] -> [1,3]
# Also works with strings
hello = 'hello'
print(hello[::]) # hello
print(hello[1::]) # ello
print(hello[:1:]) # h
print(hello[::2]) # hlo
The help function
This function is very useful when working with Python. It will print out the docstring for the function.
# `help` function
import os
help(os.path) # Prints out help information for function
The above would print out the following:
Help on module posixpath:
NAME
posixpath - Common operations on Posix pathnames.
MODULE REFERENCE
https://docs.python.org/3.9/library/posixpath
The following documentation is automatically generated from the Python
source files. It may be incomplete, incorrect or include features that
are considered implementation detail and may vary between Python
implementations. When in doubt, consult the module reference at the
location listed above.
DESCRIPTION
Instead of importing this module directly, import os and refer to
this module as os.path. The "os.path" name is an alias for this
module on Posix systems; on other systems (e.g. Windows),
os.path provides the same operations in a manner specific to that
platform, and is an alias to another module (e.g. ntpath).
Some of this can actually be useful on non-Posix systems too, e.g.
for manipulation of the pathname component of URLs.
# ... rest ommited for brevity
List comprehensions
Finally, the last tip I want to mention is the use of list comprehensions. This has been useful for cutting down on the amount of code that I have to write.
Be careful not to make your code too unreadable, but it is a nice way to quickly create a conditional list of values from another list.
# List comprehensions
a = [1,2,3,4,5]
print([x for x in a if x % 2 == 0]) # Prints out [2, 4]
print([x ** 2 for x in a]) # Prints out [1, 4, 9, 16, 25]
# Creating lists
b = [x for x in range(10) if x < 5]
print(b) # Prints out [0, 1, 2, 3, 4]
It also works well with string lists:
# Working with string lists
c = ['hey', 'world']
print([x.upper() for x in c if len(x) > 3]) # Prints out ['WORLD'])
# Working with enumerations
print([x for index, x in enumerate(c) if index < 1]) # Prints out ['hey']
Summary
Today was a recap on some of the cooler things I have come across with Python of the past 30 days. I hope you found this useful!
I will continue to iterate on this as I find Python syntax that are readily used in everyday projects.
Resources and further reading
- Python
zipfunction helpfunction in Python- Python List Comprehensions
- Chaining of comparison operators
- Slice notation in Python
Photo credit: susangold