DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Ezpie
Ezpie

Posted on

Mistakes every beginner makes

When I was learning programming I use to make a lot of mistakes and now I found out that many of you may have made one or more too.
So here's how to not do it

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!

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') # but not me!
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') # That's me!
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 (8)

Collapse
 
pengeszikra profile image
Peter Vivo
const a, b, c, d = 12, 41, 234, 42
Enter fullscreen mode Exit fullscreen mode

This format hard to read and easy to do mistake, previous is much more obviously.
In this line we do four different task - hard to tell a first view the value of c.

Collapse
 
ishaan010 profile image
Ezpie Author

It seems easy for me, sorry if it does not work out

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited on

The problem with your or and and is that they don't short cuircut. So if you have side effects in your condition it will always be executed. So your code is not the same as with or and and.

Collapse
 
ishaan010 profile image
Ezpie Author

Never happened with me, but thanks.

Collapse
 
jcubic profile image
Jakub T. Jankiewicz • Edited on

Try:

function something() {
   console.log('x');
}
if (false && something()) {

}
Enter fullscreen mode Exit fullscreen mode

This is short circuit, this how and or or works. It' used a lot of ReactJS.

Here you have Python code but it works exactly the same.

Thread Thread
 
ishaan010 profile image
Ezpie Author

Yes I know that but this post was for beginners who didn't know about these things. So not what you may have thought that I am a beginner.

Thread Thread
 
jcubic profile image
Jakub T. Jankiewicz

I didn't thought anything. You said that it never happen to you so I assume that you don't know what it is.

Collapse
 
darkwiiplayer profile image
π’Š©Wii πŸ’–πŸ’›πŸ’šπŸ’™πŸ’œπŸ’πŸ’Ÿ
a, b, c, d = 12, 41, 234, 42
Enter fullscreen mode Exit fullscreen mode

While this is more concise, it also has the disadvantage of being harder to read and manage.
One assignment per line means you can easily sort the lines (for example alphabetically), or delete individual variables or move them somewhere else.

Also keep in mind that in real programs, variables won't ever look like that; at least they absolutely shouldn't. Always spell out your variables. There's a few exceptions to this rule, but you can usually count them on one hand.

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

This will confuse most users. At the very least, one should point out how exactly the user should input the two numbers, but ideally, every value should be input separately (and labelled accordingly).

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

I don't know nearly enough about python to comment on the performance overhead of this extra array, but it does seem like something to consider. Same for the any analogue.

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.