CodeNewbie Community 🌱

Ezpie
Ezpie

Posted on

Three Python Tips That Will Make Your Life Better.

When I was learning programming I use to do some small not really good in the look work, so here are those things I did and how I replaced them

no.1 repeat

You may have done somthing like this once:

a = 12
b = 41
c = 234
d = 42
Enter fullscreen mode Exit fullscreen mode

and more. But to avoid this you should have done

a, b, c, d = 12, 41, 234, 42
Enter fullscreen mode Exit fullscreen mode

Now that's more liking!(should use a list instead!)

no.2 multi inputs

Yes multiple inputs are a bit mess. But ever heard of the .split() method?
No? OK now you know it

# don't do this
a = input('Enter a number: ')
b = input('Enter a number: ')

# do this!
a, b = input('Enter a number: ').split()
Enter fullscreen mode Exit fullscreen mode

But do mind that when the input is asked then type both the numbers together.

no.3 multi statements(and)

Yes again multiple statements in an if statement are mind crackers!
But no more with variables!

learning = 2
contributed = 5
# write a condition
condition = [
        learning > 5,
        contributed > 10
]

if all(condition): # all is like **and**
    print('A good programmer')
Enter fullscreen mode Exit fullscreen mode

no.4 multi statement(or)

Again the same but this time if anyone is right then enter if statement

learning = 2
since = 2020

condition = [
        learning > 2,
        since < 2022
]

if any(condition):
      print('Good for starters')
Enter fullscreen mode Exit fullscreen mode

That's all for this post if you want some more then do tell or if I messed any then do mention

Top comments (0)