Advanced Python: for more attractive code

teosoft7

Taeho Jeon

Posted on May 20, 2019

Advanced Python: for more attractive code

Tips and Tricks for Advanced Python

Python Logo

Python is a high-level, interpreted, interactive and object-oriented scripting language. Python is designed to be highly readable. It uses English keywords frequently whereas other languages use punctuation, and it has fewer syntactical constructions than other languages.

The syntax of Python is clear, intuitive and easy to learn compared to other programming languages such as C/C++, JAVA, and Swift. Python has a lot of built-in standard features like comprehensions, dictionaries, sets, and generators to provide the most common functions required by modern programming languages. And Python also has tons of libraries provided by its own community such as 'pandas', 'numpy', 'matplotlib', and so on. Because of these, Python is one of the most popular programming languages, especially for data science.

Besides the simplicity and user-friendly of Python, learning how to write code is not an easy task. It is very similar to learn a new unfamiliar language. It needs to be practiced again and again until could feel comfortable within it. And to write more clear code, it needs to understand how Python interpreter works, but it could be out of scope for the data science. But here is seven tips and tricks for more attractive Python code as a data scientist.

1. Underscore(_) separator for Large Number

'_' can be used as a separator for expressing a large number

ten_billion = 10_000_000_000

print(f'{ten_billion:,}')
10,000,000,000

2. Assign a value with if statement

When 'if' statement has only an assignment code inside of it

# general
isHappy = True

if isHappy == True:
    result_string = 'Happy'
else:
    result_string = 'Not Happy'

print(result_string)

Happy

# advanced
isHappy = True

result_string = 'Happy' if isHappy else 'Not Happy'

print(result_string)

Happy

3. Swap values between two variable

Python can swap the values between two variable without temp variable

# general
low = 10
high = 9

if low > high:
    temp = low
    low = high
    high = temp

print(low, high)

9 10

# advanced
low = 10
high = 9

if low > high:
    low, high = high, low

print(low, high)

9 10

4. Enumerate function

If you need index value inside of for loop, use enumerate function

# general
grades = ['A', 'B', 'C', 'D', 'E', 'F']

i = 1
for grade in grades:
    print(f'{i} : {grade}')
    i += 1

1 : A
2 : B
3 : C
4 : D
5 : E
6 : F

# advanced
grades = ['A', 'B', 'C', 'D', 'E', 'F']

for i, grade in enumerate(grades, 1):
    print(f'{i} : {grade}')

1 : A
2 : B
3 : C
4 : D
5 : E
6 : F

5. List Comprehensions

a concise way to create lists

# beginner
numbers = [1, 2, 3, 4, 5, 6, 7]

squared = []
for number in numbers:
    squared.append(number * number)

print(squared)

# advanced
numbers = [1, 2, 3, 4, 5, 6, 7]

squared = [number * number for number in numbers]

print(squared)

6. Unpacking

unpacking values inside of tuples


# from 13 cards
cards = ('A', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K' )

# extract ace, 2, 3 only
ace, two, three, *_ = cards
print(ace, two, three)
A 2 3


# extract ace, [numbers], J, Q, K
ace, *numbers, J, Q, K = cards
print(ace, numbers, J, Q, K)
A ['2', '3', '4', '5', '6', '7', '8', '9', '10'] J Q K

7. For~Else

else statement also be used after for loop, it is executed after for loop

# For ~ Else
grades = ['A', 'B', 'C', 'D', 'E', 'F']

my_grade = 'A+'

for grade in grades:
    if grade == my_grade:
        print('grade found')
        break
else:
    print('grade not found')

grade not found
💖 💪 🙅 🚩
teosoft7
Taeho Jeon

Posted on May 20, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related