Coder Social home page Coder Social logo

python-tips's Introduction

Python tips

List of python tips

  1. Use the Python 3 print function in Python 2
  2. Reverse a string or list
  3. Reverse by custom step
  4. List slice assignment
  5. Copy a list
  6. Create a list out of string
  7. Print elements of a list by unpacking (only Python 3)
  8. Check file or directory exists
  9. Call an external command
  10. Capture output from an external command
  11. Ternary conditional operator
  12. else in for loop
  13. Print to file
  14. Writing to file
  15. Reading from file
  16. Iterating over lines in a file
  17. Accessing attributes which start with underscores

Use the Python 3 print function in Python 2

# For Python 2, using the print() function from Python 3 helps future compatibility.
# It also allows better syntax when printing to files, or changing the line ending.
from __future__ import print_function

print('Python', 'Tips', sep='-', end='')
import sys
print('There was an error!', file=sys.stderr)
with open(filename, 'w') as f:
    print('Python!', file=f)

Reverse a string or list

my_list = ['a','b','c','d','e']
reverse_list = my_list[::-1]

my_string = "python"
print(my_string[::-1])
# output : nohtyp

Reverse by custom step

my_list = [1,2,3,4,5,6]
reverse_list = my_list[::-2]
print(reverse_list)
# output : [6,4,2]

List slice assignment

my_list = ['a','b','c','d','e']
my_list[3:] = ['x','y','z']
print(my_list)
# output : ['a', 'b', 'c', 'x', 'y', 'z']

Copy a List

a = [1, 2, 3, 4]
''' Considered as one of the weirdest syntax to copy elements.'''
a_copy = a[:]

''' Another way of copying a list.'''
a_copy2 = list()
a_copy2.extend(a) # output a_copy2 = [1, 2, 3, 4]

Create a list out of string

data = "abcd"
data_list2 = list(data)  # OutPut: data_list = ['a', 'b', 'c', 'd'] 

Print elements of a list by unpacking (only Python 3)

my_list = [1, 2, 3, 4]
print(*my_list, sep='\t')   # Print elements of my_list separated by a tab

Check file or directory exists

os.path.isfile used only for files

import os.path
os.path.isfile(filename) # True if file exists
os.path.isfile(dirname) # False if directory exists

os.path.exists used for both files and directories

import os.path
os.path.exists(filename) # True if file exists
os.path.exists(dirname) #True if directory exists

pathlib.Path method (included in Python 3+, installable with pip for Python 2)

from pathlib import Path
Path(filename).exists()

Call an external command

from subprocess import call
call(['ls,'-l'])

Capture output from an external command

from subprocess import check_output
output = check_output(['ls', '/usr/bin'])

You can also capture stderr at the same time.

from subprocess import check_output, STDOUT, CalledProcessError
try:
    output = check_output(['ls', '/nonexistent'], stderr=STDOUT)
except CalledProcessError:
    print('Error: {}'.format(output.decode()))
else:
    print('Success: {}'.format(output.decode()))

Ternary conditional operator

print('True' if True else 'False')

else in for loop

An else block is executed if the loop body is not terminated by a break statement :

for i in range(5):
    print('Here we go!')
    if i == 2:
        break
else:
    print('There we went.')
# output : Here we go!
#          Here we go!
#          Here we go!

This for loop will get all the way to else:

for i in range(5):
    print('Here we go!')
    if i == 10:
        break
else:
    print('There we went.')
# output : Here we go!
#          Here we go!
#          Here we go!
#          Here we go!
#          Here we go!
#          There we went.

Print to file

# Using `with` and `open`, the file will be closed when the `with` block finishes.
with open(filename, 'w') as outputfile:
    print('Python!', file=outputfile)

Writing to file

with open(filename, 'a') as outputfile:
    outputfile.write('Python!\n')

Reading from file

with open('file.txt', 'r') as inputfile:
    data = inputfile.read()

Iterating over lines in a file

When iterating over lines in a file, this method uses less memory because it reads one line at a time.

with open(filename, 'r') as inputfile:
    for line in inputfile:
        # Lines have a '\n' at the end, like inputfile.readlines().
        print(line, end='')

Accessing attributes which start with underscores

“Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. Since there is a valid use-case for class-private members (namely to avoid name clashes of names with names defined by subclasses), there is limited support for such a mechanism, called name mangling. Any identifier of the form __spam (at least two leading underscores, at most one trailing underscore) is textually replaced with _classname__spam, where classname is the current class name with leading underscore(s) stripped.

So to access attributes which start with underscores simply run obj_name._ClassName__attr_name .

class MyClass:

    def __init__(self):
        self.__private_attr = None

    def set_private_attr(self, attr_to_private):
        self.__private_attr = attr_to_private


attr = 7

my_class = MyClass()
my_class.set_private_attr(attr)

print(my_class._MyClass__private_attr)

📄 License

python-tips is MIT licensed, as found in the LICENSE file.

python-tips's People

Contributors

cjwelborn avatar mmadil avatar navid-kalaei avatar smoqadam avatar sramezani avatar stuartcw avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.