Jump to content

Python Help

Go to solution Solved by Enderman,

both are right

in most programming languages you need to use the ()

not in python because its easy

 

so technically using () is unnecessary but does not affect the code or processing time at all

Hey Guys,

My teacher says that this is wrong:

if(example == "1"):

and this is right:

if example == "1":

Apparently I will get a lower test result because of "efficiency of code" or some shit like that. Is that true?

 

​Nothing Screams Professional Other Than Comic Sans


My Build: http://pcpartpicker.com/p/bQVfqs


Dark Theme, GG

Link to comment
https://linustechtips.com/topic/545822-python-help/
Share on other sites

Link to post
Share on other sites

both are right

in most programming languages you need to use the ()

not in python because its easy

 

so technically using () is unnecessary but does not affect the code or processing time at all

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/545822-python-help/#findComment-7217520
Share on other sites

Link to post
Share on other sites

It's the same thing.

 

It's just that the if statement in Python doesn't require parenthesis. Instead, they are used in expression delimitation. In your case , it is unnecessary, but at the same time doesn't make the code any less efficient neither at compile (interpret) time and even more so nor at run-time.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
https://linustechtips.com/topic/545822-python-help/#findComment-7217541
Share on other sites

Link to post
Share on other sites

2 hours ago, 8enjicraf2 said:

Hey Guys,

My teacher says that this is wrong:

if(example == "1"):

and this is right:

if example == "1":

Apparently I will get a lower test result because of "efficiency of code" or some shit like that. Is that true?

 

Both are accepted by the interpreter because parentheses are a valid way to group expressions and modify the order things are executed. Putting everything inside one set of parentheses is valid but redundant.

 

Whether or not someone think it aids code readability is more personal preference than anything. Personally, I don't think it adds any benefit to readability in the above example and would leave them out. This difference of opinion is why programmers in a team are often required to follow coding style guidelines. It helps keep the code more consistent.

 

Some teachers will give style guidelines but I expect most wont. If the class wasn't informed of style expectations then it seems unfair to have marks taken away for something like the above example (at least on the first offence).

 

40 minutes ago, Enderman said:

in most programming languages you need to use the ()

There are certainly many languages that require the parentheses however there are also many languages that don't. Ex: Python, Ruby, Lua, Go, Swift, Rust, VB.NET, F#, Haskell, Elm, Perl (version 6), etc.

 

I expect it'd be valid code in most of them to use parentheses as well, but it doesn't mean that's how you should write them.

 

1 hour ago, Enderman said:

not in python because its easy

Not in python because its easy it was a language design choice to exclude them.

Link to comment
https://linustechtips.com/topic/545822-python-help/#findComment-7218959
Share on other sites

Link to post
Share on other sites

3 hours ago, madknight3 said:

I expect it'd be valid code in most of them to use parentheses as well, but it doesn't mean that's how you should write them.

Hello! As a fellow programming student I'm hoping I can shed some light onto why the instructor would actually take points off for this.


Lately we have been discussing a study (I'm sorry I haven't yet been able to find the link myself, so I cannot offer it yet) that shows that for a majority of "keyboard time" programmers are actually reading code. This could be for debugging reasons, or any other.

 

I believe that using extraneous markup could warrant a point deduction if for no other reason than it could potentially slow someone up who was reading it, and came across an unexpected character. There are also extra keystrokes, but I'm not about to wholeheartedly defend labeling two extra key presses as inefficient. I think the more pressing issue is to try to conform to standards that are widely accepted, if not required by a compiler/interpreter. In the case of Python that means you don't see parenthesis or brackets to the extent you would in a C-style language like Java.

 

We've always had it explained to us that in the perfect world of academia the expectations are sometimes significantly different than the requirements of real world development. In that particular case there is no need for the parenthesis, and since we are dealing with the "perfect world" scenario simply remove them. I believe madknight3 said it more succinctly, so I quoted their post in case mine was too long winded and rambling.

Link to comment
https://linustechtips.com/topic/545822-python-help/#findComment-7220502
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×