Skip to main content
Mod Removes Wiki by Doorknob
Reducing this to two related tips (the others weren’t that great anyway or redundant) and improving one tip.
Source Link
Wrzlprmft
  • 3k
  • 22
  • 39
  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

  5. Returning True or False in Python

    return 1>0 vs return True return 1>1 vs return False

Use += instead of append and extend

A.append(B) 

Refcan be shortened to:

A+=B, 

B, here creates a one-element tuple which can be used to extend http://ajs-handling-problems-in-pythonblog.blogspot.in/A just like [B] in A+=[B].


A.extend(B) 

can be shortened to:

A+=B 
  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

  5. Returning True or False in Python

    return 1>0 vs return True return 1>1 vs return False

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

Use += instead of append and extend

A.append(B) 

can be shortened to:

A+=B, 

B, here creates a one-element tuple which can be used to extend A just like [B] in A+=[B].


A.extend(B) 

can be shortened to:

A+=B 
added 134 characters in body
Source Link
Coding man
  • 1.3k
  • 9
  • 20
  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

  5. Returning True or False in Python

    return 1>0 vs return True return 1>1 vs return False

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

  5. Returning True or False in Python

    return 1>0 vs return True return 1>1 vs return False

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

edited body
Source Link
Coding man
  • 1.3k
  • 9
  • 20
  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B]vs vsA+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B]vs vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

  1. To find the absolute value of a number:

    -8.32*-1 vs abs(-8.32) #saves 1 byte

  2. If one wants to append list B at the end of A, then:

    A+=[B] vs A.append(B)

  3. If one wants to extend A using elements of B, then:

    A+=B vs A.extend(B)

  4. Increment m by n if a certain condition is true in Python

    m+=(Condition)*n vs if Condition:m+=n

Ref: http://ajs-handling-problems-in-pythonblog.blogspot.in/

Source Link
Coding man
  • 1.3k
  • 9
  • 20
Loading