10 Python Tricks That Make You Look Like a Pro — For Absolute Beginners

These Python tricks will make your code cleaner, faster, and more professional. This article is written especially for Python beginners. You won’t regret the few minutes it takes to read.

01 - Multiple Assignment

You already know how to assign values to variables. And honestly, I don’t know any language that makes it easier than Python. But what if you need to assign multiple variables at the same time?

a = 'I'
b = 'Love'
c = 'You'

print(a,b,c)

Well — not wrong. But you can do it in one line. You get the same result.

a, b, c = 'I', 'Love', 'You'

print(a,b,c)

02 - Swap Values

If I ask you to swap the values of two variables, how would you do it? Maybe like this?

a = 60
b = 90

c = a
a = b
b = c

print(f'a = {a}\nb = {b}')

Guess what? — There’s an easy one-line way

a = 60
b = 90

a, b = b, a

03 - Ternary If

How do you use if statements? — Like this?

a = 10
b = 15

if a > b:
    print('a > b')
else:
    print('b > a')

That’s the best method for most situations. And I admit this next method isn’t great for heavy tasks — but it looks sexy when used in the right place.

a = 10
b = 15

print('a > b' if a > b else 'b > a')

This is only applicable to small tasks.

04 - Reverse Strings

How would you reverse a string in Python? Think about it.

the_string = 'Unboxed Articles'

reversed_string = ''
for char in the_string:
    reversed_string = char + reversed_string


print(reversed_string)

Again, better than nothing. Would you believe me if I told you there’s a one-line way to do it?

print(the_string[::-1])

I want to explain how this works but it could kill the pace of the article. So, play around with it yourself. You will get it.

05 - Reverse Lists

This is literally the list version of above one.

the_list = ['I','Love','You']

reversed_list = []
for word in the_list:
    reversed_list = [word] + reversed_list

print(reversed_list)

Guess the pro method — Wow, you are getting better.

print(the_list[::-1])

06 - Join List Of Strings

You might have words. But the hardest part is joining them. Just never get nervous.

the_list = ['I','love','you']


final_str = ''
for word in the_list:
    final_str = final_str + word + ' '

print(final_str)

Just do it in one line. She might still say no — but hey, at least your code looks good.

pro_str = ' '.join(the_list)
print(pro_str)

07 - Enumerate with Index

Here is the a list.

the_list = ['Python','Java','C++','Ruby','Dart']

Write code that gives the following output.

1. Python
2. Java
3. C++
4. Ruby
5. Dart

If your code looks like following, this article is absolutely for you.

c = 1
for lang in the_list:
    print(f'{c}. {lang}')
    c += 1

This pro method might not look sexy in this case — but you’ll thank me later.

for index,lang in enumerate(the_list,1):
    print(f'{index}. {lang}')

If you didn't pass 1 as the second positional argument, the output will looks similar to if you used c = 0 in the first code. If you replace 1 with 3, the list will start counting from 3 instead.

08 - One Line for Loop

How will you create following list.

x = [0,1,2,3,4,5,6,7,8,9]

Just by typing it, right? What about this.

y = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]

Are you really going to type all that? I honestly can’t think of anything I hate more than typing out long, boring lists. Here's the way I do it.

x = [i for i in range(10)]
y = [i for i in range(50)]

Here’s the expanded version to help you understand how it works.

for i in range(10):
    x.append(i)
Challenge 01

Create the following lists by using one line for loop. Answers are available on my github.

[4, 5, 6, 7, 8, 9, 10, 11]
[0, 2, 4, 6, 8]
[19, 17, 15, 13, 11, 9, 7, 5]
Challenge 02

Create a list of first names of the following random girl name list by using one line for loop. Please don't get distracted.

my_ex_list = [
    "Zendaya",
    "Emma Watson",
    "Scarlett Johansson",
    "Margot Robbie",
    "Ana de Armas",
    "Alexandra Daddario",
    "Gal Gadot",
    "Selena Gomez",
    "Taylor Swift",
    "Jennifer Lawrence",
    "Sydney Sweeney",
    "Megan Fox",
    "Dua Lipa",
    "Emilia Clarke",
    "Florence Pugh",
    "Millie Bobby Brown",
    "Natalie Portman",
    "Rihanna",
    "Ariana Grande",
    "Sabrina Carpenter"
]

09 - Filter Lists

I want you to put all the values, which are greater or equal than 50, to a y variable. Let's see how you're going to do it.

x = [0,65,3,67,23,556,123,59,42,91,100,4,19,50]

It’s okay if your code looks like this:

y = []
for i in x:
    if i >= 50:y.append(i)

Don’t call me Mr. One-Liner.

y = [i for i in x if i >= 50]

Same logic but in one line.

Challenge 03

Use this concept to put all the girl's full names in to a list called crush_list only if their first name has letters less than or equal to 5.

my_ex_list = [
    "Zendaya",
    "Emma Watson",
    "Scarlett Johansson",
    "Margot Robbie",
    "Ana de Armas",
    "Alexandra Daddario",
    "Gal Gadot",
    "Selena Gomez",
    "Taylor Swift",
    "Jennifer Lawrence",
    "Sydney Sweeney",
    "Megan Fox",
    "Dua Lipa",
    "Emilia Clarke",
    "Florence Pugh",
    "Millie Bobby Brown",
    "Natalie Portman",
    "Rihanna",
    "Ariana Grande",
    "Sabrina Carpenter"
]

You know the place to get answers.

10 - else with for loop

Comment your age when you first learned that you can use else with for. Given a list of numbers, check if the number 5 is in it.

nums = [1, 2, 3, 4, 6]

Here's how I would do.

for num in nums:
    if num == 5:
        print("Found 5!")
        break
else:
    print("5 is not in the list.")

Just forget the else part for a second. This goes through the nums list to check whether any of its element is equal to 5. If nums had 5, it would have printed Found 5! and end the program because of break. Since break didn’t execute, the loop finishes normally — which triggers the else block.

Challenge 04

Create a function which will print True if the given numbers list has at least one even number, and False if it doesn't have any even number using this concept.

See answers on github.

Final Thoughts

Being a Python pro isn’t about writing complicated code — it’s about writing smart, clean, and efficient code. These simple tricks might look small, but they’ll make your code cleaner and your brain happier. Keep hammering.

Again, "You don't need to be a pro to act like one."

Subscribe to the newsletter to get new articles delivered straight to your inbox the moment I post them. Follow me on GitHub for more content — and maybe Instagram too.

You can support my effort by reacting to and commenting on this article. Share it with someone who would find it valuable.

12
Subscribe to my newsletter

Read articles from Beenuka Hettiarachchi directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Beenuka Hettiarachchi
Beenuka Hettiarachchi