DEV Community

Cover image for 7 useful python tips

7 useful python tips

Sonika Baniya on April 09, 2021

1. Multiple variable assignment You can assign values to multiple variables on one line. You can also assign different data type in sing...
Collapse
 
vestemir profile image
bkrzys

Operation done in 1st point isn't technically multiple variable assignment but tuple unpacking. What you have on the right side is an implicit tuple created due to the commas and then you unpack its values to the variables on the left side of the assignment.

Collapse
 
miguelmj profile image
MiguelMJ

Tuple unpacking is technically what it is, but it's not wrong to call multiple variable assignment the effect that you achieve. It's more didactic and, in fact, more accurate, because there is more to tuple unpacking than said here.

Collapse
 
sonikabaniya profile image
Sonika Baniya

Yeah but this method is named as ' multiple variable assignment ', no?

Collapse
 
vestemir profile image
bkrzys

Tuple unpacking is more specific. 'multiple variable assignment' would be also `a = b = c = None', wouldn't it?
Here, a comma on the right side of the assignment represents implicit tuple creation, which is first executed and then unpacked.

Collapse
 
paddy3118 profile image
Paddy3118

floor, ceil but no trunc?!

# FUNCTIONS THAT GENERATE INTEGERS FROM FLOATS IN DIFFERENT WAYS.
ceil
  Return the ceiling of x as an Integral.

  This is the smallest integer >= x.

floor
  Return the floor of x as an Integral.

  This is the largest integer <= x.

trunc
  Truncates the Real x to the nearest Integral toward 0.

  Uses the __trunc__ magic method.

 ceil( 1.2) ==  2; floor( 1.2) ==  1; trunc( 1.2) ==  1
 ceil( 0.2) ==  1; floor( 0.2) ==  0; trunc( 0.2) ==  0
 ceil(   0) ==  0; floor(   0) ==  0; trunc(   0) ==  0
 ceil(-0.2) ==  0; floor(-0.2) == -1; trunc(-0.2) ==  0
 ceil(-1.2) == -1; floor(-1.2) == -2; trunc(-1.2) == -1

trunc acts like floor for n >=0 and like ceil for n < 0
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sonikabaniya profile image
Sonika Baniya

seemed to have missed it out, will be mindful about this from now onwards :))

Collapse
 
suecarmol profile image
Susana Cárdenas Molinar

That dir() tip is super useful! Thank you for this :)

Collapse
 
sonikabaniya profile image
Sonika Baniya

Very happy to know it helped :))

Collapse
 
anishde12020 profile image
Anish De

Nice!!! Really helpful tips!

Collapse
 
sonikabaniya profile image
Sonika Baniya

thank you Anish

Collapse
 
0trenixjetix profile image
0trenixjetix

Your prints don't have parenthesis in the 4th tip

Collapse
 
sonikabaniya profile image
Sonika Baniya

Thank you for pointing this out, i fixed it now :))

Collapse
 
wireless90 profile image
wireless90

For/while else loop tip is cool

Collapse
 
sonikabaniya profile image
Sonika Baniya

thank you

Collapse
 
manibibek profile image
Bibek Mani Acharya

Good one Sonika

Collapse
 
sonikabaniya profile image
Sonika Baniya

thank you