Skip to content

Instantly share code, notes, and snippets.

@techsoft29
Forked from elowy01/python_cheat_sheet.txt
Created June 13, 2022 01:07
Show Gist options
  • Save techsoft29/0b5c65abb4ba5d97e953109de27749e6 to your computer and use it in GitHub Desktop.
Save techsoft29/0b5c65abb4ba5d97e953109de27749e6 to your computer and use it in GitHub Desktop.

Revisions

  1. Ernesto Lowy revised this gist Sep 21, 2021. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion python_cheat_sheet.txt
    Original file line number Diff line number Diff line change
    @@ -4112,4 +4112,12 @@ def is_tool(name):
    # from whichcraft import which
    from shutil import which

    return which(name) is not None
    return which(name) is not None

    //
    % modulo operator
    a = 9
    b = 3
    answer = a % b

    print(answer)
  2. Ernesto Lowy revised this gist Sep 14, 2021. 1 changed file with 0 additions and 39 deletions.
    39 changes: 0 additions & 39 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -1,39 +0,0 @@
    # Exercises extracted from :
    # https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises%20for%20Python%203.md

    # print the intersection of the 2 lists
    set1=set([1,3,6,78,35,55])
    set2=set([12,24,35,24,88,120,155])
    set1 &= set2
    li=list(set1)
    print(li)

    # write assert statements to verify that every number in the list [2,4,6,8] is even.
    li = [2,4,6,8]
    for i in li:
    assert i%2==0

    # Use u'strings' format to define unicode string.
    unicodeString = u"hello world!"
    print(unicodeString)

    #
    Assuming that we have some email addresses in the "[email protected]" format,
    please write program to print the user name of a given email address. Both user names
    and company names are composed of letters only.

    Example: If the following email address is given as input to the program:
    [email protected]

    Then, the output of the program should be:
    john
    In case of input data being supplied to the question, it should be assumed to be a console input.

    Hints:
    Use \w to match letters.

    import re
    emailAddress = raw_input()
    pat2 = "(\w+)@((\w+\.)+(com))"
    r2 = re.match(pat2,emailAddress)
    print(r2.group(1))
  3. Ernesto Lowy revised this gist Sep 14, 2021. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions more_tricks.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +0,0 @@
    # get creation and last modification time of a file or directory
    import os.path, time
    print("Last modified: %s" % time.ctime(os.path.getmtime("test.py")))
    print("Created: %s" % time.ctime(os.path.getctime("test.py")))

    # how to make a function to return multiple values, Read article on:
    https://note.nkmk.me/en/python-function-return-multiple-values/
  4. Ernesto Lowy revised this gist Sep 14, 2021. 1 changed file with 0 additions and 14 deletions.
    14 changes: 0 additions & 14 deletions cheat_sheet2.txt
    Original file line number Diff line number Diff line change
    @@ -1,14 +0,0 @@
    ## Pickle to serialize an object (for example a dictionary):

    # Dump first:
    import pickle

    example_dict = {1:"6",2:"2",3:"f"}

    pickle_out = open("dict.pickle","wb")
    pickle.dump(example_dict, pickle_out)
    pickle_out.close()

    #Now, you can load:
    pickle_in = open("dict.pickle","rb")
    example_dict = pickle.load(pickle_in)
  5. Ernesto Lowy revised this gist Sep 14, 2021. 1 changed file with 0 additions and 5 deletions.
    5 changes: 0 additions & 5 deletions hackerranck_practice.py
    Original file line number Diff line number Diff line change
    @@ -1,5 +0,0 @@
    # Integer division
    a = 1
    b = 2
    int_div = a // b
    print(int_div) # will print 0
  6. Ernesto Lowy revised this gist Sep 14, 2021. 1 changed file with 0 additions and 7 deletions.
    7 changes: 0 additions & 7 deletions more_tricks.py
    Original file line number Diff line number Diff line change
    @@ -5,10 +5,3 @@

    # how to make a function to return multiple values, Read article on:
    https://note.nkmk.me/en/python-function-return-multiple-values/

    # enter an int from command line
    size = int(input())

    # enter a list of numbers from command line
    numbers = list(map(int, input().split()))
    print(numbers)
  7. Ernesto Lowy revised this gist Sep 13, 2021. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,13 @@
    # Exercises extracted from :
    # https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises%20for%20Python%203.md

    # iterate a list in the reverse order:
    $ s=['a','b','c','d']
    $ s = s[::-1]
    $ print(s)
    ['d', 'c', 'b', 'a']

    # print the intersection of the 2 lists
    set1=set([1,3,6,78,35,55])
    set2=set([12,24,35,24,88,120,155])
    set1 &= set2
    li=list(set1)
    print(li)

    # list comprehension for removing a certain element of a list
    li = [12,24,35,24,88,120,155]
    li = [x for x in li if x!=24]
    print(li)

    # write assert statements to verify that every number in the list [2,4,6,8] is even.
    li = [2,4,6,8]
    for i in li:
  8. Ernesto Lowy revised this gist Sep 13, 2021. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions python_cheat_sheet.txt
    Original file line number Diff line number Diff line change
    @@ -2540,6 +2540,12 @@ assert not isinstance(lst, str)
    #In Python2
    assert not isinstance(lst, basestring)
    //
    # Check if something is either a float or int
    a='s'

    if not isinstance(a, (int, float)):
    print("a is either int or float")
    //
    #checking if something is a list:
    if not isinstance(objs,list):
    print "h"
  9. Ernesto Lowy revised this gist May 20, 2021. 1 changed file with 17 additions and 0 deletions.
    17 changes: 17 additions & 0 deletions which_equivalent.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,17 @@
    # Python program to explain shutil.which() method
    # check if python is in PATH

    # importing os module
    import os

    # importing shutil module
    import shutil

    # cmd
    cmd = 'python'

    # Using shutil.which() method
    locate = shutil.which(cmd)

    # Print result
    print(locate) # outputs: /usr/bin/python
  10. Ernesto Lowy revised this gist Apr 12, 2021. 1 changed file with 0 additions and 46 deletions.
    46 changes: 0 additions & 46 deletions python_cheat_sheet.txt
    Original file line number Diff line number Diff line change
    @@ -226,52 +226,6 @@ Find Key, Values Pairs In Common
    # Find countries where the amount of exports matches the amount of imports
    importers.items() & exporters.items()
    {('Spain', 252)}
    //
    #OrderedDict
    An OrderedDict is a dictionary subclass that remembers the order in which its contents are added.

    import collections

    print 'Regular dictionary:'
    d = {}
    d['a'] = 'A'
    d['b'] = 'B'
    d['c'] = 'C'
    d['d'] = 'D'
    d['e'] = 'E'

    for k, v in d.items():
    print k, v

    print '\nOrderedDict:'
    d = collections.OrderedDict()
    d['a'] = 'A'
    d['b'] = 'B'
    d['c'] = 'C'
    d['d'] = 'D'
    d['e'] = 'E'

    for k, v in d.items():
    print k, v

    A regular dict does not track the insertion order, and iterating over it produces the values in an arbitrary order. In an OrderedDict, by contrast,
    the order the items are inserted is remembered and used when creating an iterator.
    $ python collections_ordereddict_iter.py

    Regular dictionary:
    a A
    c C
    b B
    e E
    d D

    OrderedDict:
    a A
    b B
    c C
    d D
    e E

    //
    # Merge 2 dictionaries (dicts)
    >>> x = {'a': 1, 'b': 2}
  11. Ernesto Lowy revised this gist Mar 11, 2021. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions hackerranck_practice.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    # Integer division
    a = 1
    b = 2
    int_div = a // b
    print(int_div) # will print 0
  12. Ernesto Lowy revised this gist Mar 6, 2021. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion more_tricks.py
    Original file line number Diff line number Diff line change
    @@ -4,4 +4,11 @@
    print("Created: %s" % time.ctime(os.path.getctime("test.py")))

    # how to make a function to return multiple values, Read article on:
    https://note.nkmk.me/en/python-function-return-multiple-values/
    https://note.nkmk.me/en/python-function-return-multiple-values/

    # enter an int from command line
    size = int(input())

    # enter a list of numbers from command line
    numbers = list(map(int, input().split()))
    print(numbers)
  13. Ernesto Lowy revised this gist Feb 1, 2021. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions cheat_sheet2.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    ## Pickle to serialize an object (for example a dictionary):

    # Dump first:
    import pickle

    example_dict = {1:"6",2:"2",3:"f"}

    pickle_out = open("dict.pickle","wb")
    pickle.dump(example_dict, pickle_out)
    pickle_out.close()

    #Now, you can load:
    pickle_in = open("dict.pickle","rb")
    example_dict = pickle.load(pickle_in)
  14. Ernesto Lowy revised this gist Dec 16, 2020. 1 changed file with 4 additions and 1 deletion.
    5 changes: 4 additions & 1 deletion more_tricks.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,7 @@
    # get creation and last modification time of a file or directory
    import os.path, time
    print("Last modified: %s" % time.ctime(os.path.getmtime("test.py")))
    print("Created: %s" % time.ctime(os.path.getctime("test.py")))
    print("Created: %s" % time.ctime(os.path.getctime("test.py")))

    # how to make a function to return multiple values, Read article on:
    https://note.nkmk.me/en/python-function-return-multiple-values/
  15. Ernesto Lowy revised this gist Dec 15, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions more_tricks.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,4 @@
    # get creation and last modification time of a file or directory
    import os.path, time
    print("Last modified: %s" % time.ctime(os.path.getmtime("test.py")))
    print("Created: %s" % time.ctime(os.path.getctime("test.py")))
  16. Ernesto Lowy revised this gist Dec 14, 2020. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -27,3 +27,24 @@ for i in li:
    # Use u'strings' format to define unicode string.
    unicodeString = u"hello world!"
    print(unicodeString)

    #
    Assuming that we have some email addresses in the "[email protected]" format,
    please write program to print the user name of a given email address. Both user names
    and company names are composed of letters only.

    Example: If the following email address is given as input to the program:
    [email protected]

    Then, the output of the program should be:
    john
    In case of input data being supplied to the question, it should be assumed to be a console input.

    Hints:
    Use \w to match letters.

    import re
    emailAddress = raw_input()
    pat2 = "(\w+)@((\w+\.)+(com))"
    r2 = re.match(pat2,emailAddress)
    print(r2.group(1))
  17. Ernesto Lowy revised this gist Dec 14, 2020. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -23,3 +23,7 @@ print(li)
    li = [2,4,6,8]
    for i in li:
    assert i%2==0

    # Use u'strings' format to define unicode string.
    unicodeString = u"hello world!"
    print(unicodeString)
  18. Ernesto Lowy revised this gist Dec 14, 2020. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion MIni exercises
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,9 @@ print(li)
    # list comprehension for removing a certain element of a list
    li = [12,24,35,24,88,120,155]
    li = [x for x in li if x!=24]
    print(li)
    print(li)

    # write assert statements to verify that every number in the list [2,4,6,8] is even.
    li = [2,4,6,8]
    for i in li:
    assert i%2==0
  19. Ernesto Lowy revised this gist Dec 11, 2020. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,9 @@ set1=set([1,3,6,78,35,55])
    set2=set([12,24,35,24,88,120,155])
    set1 &= set2
    li=list(set1)
    print(li)

    # list comprehension for removing a certain element of a list
    li = [12,24,35,24,88,120,155]
    li = [x for x in li if x!=24]
    print(li)
  20. Ernesto Lowy revised this gist Dec 11, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion MIni exercises
    Original file line number Diff line number Diff line change
    @@ -5,4 +5,11 @@
    $ s=['a','b','c','d']
    $ s = s[::-1]
    $ print(s)
    ['d', 'c', 'b', 'a']
    ['d', 'c', 'b', 'a']

    # print the intersection of the 2 lists
    set1=set([1,3,6,78,35,55])
    set2=set([12,24,35,24,88,120,155])
    set1 &= set2
    li=list(set1)
    print(li)
  21. Ernesto Lowy revised this gist Dec 11, 2020. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions MIni exercises
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    # Exercises extracted from :
    # https://github.com/zhiwehu/Python-programming-exercises/blob/master/100%2B%20Python%20challenging%20programming%20exercises%20for%20Python%203.md

    # iterate a list in the reverse order:
    $ s=['a','b','c','d']
    $ s = s[::-1]
    $ print(s)
    ['d', 'c', 'b', 'a']
  22. Ernesto Lowy revised this gist Dec 11, 2020. No changes.
  23. Ernesto Lowy renamed this gist Oct 8, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python_cheat_sheet → python_cheat_sheet.txt
    Original file line number Diff line number Diff line change
    @@ -19,7 +19,7 @@ https://www.python.org/dev/peps/pep-0008/
    *Class Names:
    Class names should normally use the CapWords convention

    *Function Names;
    *Funcƒtion Names;
    Function names should be lowercase, with words separated by
    underscores as necessary to improve readability.

  24. Ernesto Lowy revised this gist Oct 8, 2020. No changes.
  25. Ernesto Lowy revised this gist May 26, 2020. No changes.
  26. Ernesto Lowy revised this gist May 26, 2020. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion python_cheat_sheet
    Original file line number Diff line number Diff line change
    @@ -4145,4 +4145,11 @@ chunks = re.split(' +', str)
    print(chunks)

    //
    # Testing if a binary exists
    # Testing if a binary exists
    def is_tool(name):
    """Check whether `name` is on PATH and marked as executable."""

    # from whichcraft import which
    from shutil import which

    return which(name) is not None
  27. Ernesto Lowy revised this gist May 26, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion python_cheat_sheet
    Original file line number Diff line number Diff line change
    @@ -4145,4 +4145,4 @@ chunks = re.split(' +', str)
    print(chunks)

    //
    # Tes
    # Testing if a binary exists
  28. Ernesto Lowy revised this gist May 26, 2020. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions python_cheat_sheet
    Original file line number Diff line number Diff line change
    @@ -4144,3 +4144,5 @@ chunks = re.split(' +', str)

    print(chunks)

    //
    # Tes
  29. Ernesto Lowy revised this gist May 26, 2020. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions python_cheat_sheet
    Original file line number Diff line number Diff line change
    @@ -4143,3 +4143,4 @@ str = '63 41 92 81 69 70'
    chunks = re.split(' +', str)

    print(chunks)

  30. Ernesto Lowy revised this gist May 26, 2020. No changes.