Python Examples - cunhapaulo/ReferenceCard GitHub Wiki
Python language reference
import os, path
main():
file_type = "*.dll"
my_path = Path("C:/WINDOWS/SYSTEM32")
my_path.glob(file_type)
list_dlls = list(my_path)
pprint(list_dlls)
##
## More directly
##
my_path = Path("C:/WINDOWS/SYSTEM32")
list_dlls = list(my_path.glob("*.dll")) # Turns into List all files conforming to "*.dll"
pprint(list_dlls)
- 1. Index
-
2. Data Structures
-
2.1. List
- 2.1.1. Getting a List from Another List with Slices:
- 2.1.2. Program
- 2.1.3. The
in
andnot in
Operators: - 2.1.4. The Multiple Assignment Trick
- 2.1.5. Using the
enumerate()
Function with Lists: - 2.1.6. Using the
random.choice()
andrandom.shuffle()
Functions with Lists: - 2.1.7. Finding a Value in a List with the
index()
Method: - 2.1.8. Adding Values to Lists with the
append()
andinsert()
Methods: - 2.1.9. Removing Values from Lists with the
remove()
Method: - 2.1.10. Sorting the Values in a List with the
sort()
Method: -
2.1.11. Reversing the Values in a List with the
reverse()
Method:
- 2.2.
Dictionary
- 2.3.
Tuple
- 2.4. Converting Types with the list() and tuple() Functions:
-
2.1. List
- 3. Special Uses
- 4. Generalities
Nome | Exemplo | Odered | Indexed | Changeable | Duplicates |
---|---|---|---|---|---|
List |
["a", "b", "c"] |
yes | yes | yes | yes |
Dictionary |
{'nome': 'John', 'idade': 30} |
yes | yes | yes | no |
Tuple |
(1, 'dois', 3.0) |
yes | yes | no | yes |
Set |
{"apple", "banana", "cherry"} |
no | no | no | no |
- Python will give you an
IndexError
error message if you use an index that exceeds the number of values in your list value.
- Getting a List’s Length with the
len()
Function - List Concatenation and List Replication with
+
- Removing Values from Lists with
del
Statements
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat', 'elephant']
>>> del spam[2]
>>> spam
['cat', 'bat']
catNames = []
while True:
print('Enter the name of cat ' + str(len(catNames) + 1) +
' (Or enter nothing to stop.):')
name = input()
if name == '':
break
catNames = catNames + [name] list concatenation
print('The cat names are:')
for name in catNames:
print(' ' + name)
myPets = ['Zophie', 'Pooka', 'Fat-tail']
print('Enter a pet name:')
name = input()
if name not in myPets:
print('I do not have a pet named ' + name)
else:
print(name + ' is my pet.')
>>> cat = ['fat', 'gray', 'loud']
>>> size, color, disposition = cat
Instead of using the range(len(someList)) technique with a for loop to obtain
the integer index of the items in the list, you can call the enumerate()
function instead.
>>> supplies = ['pens', 'staplers', 'flamethrowers', 'binders']
>>> for index, item in enumerate(supplies):
... print('Index ' + str(index) + ' in supplies is: ' + item)
Index 0 in supplies is: pens
Index 1 in supplies is: staplers
Index 2 in supplies is: flamethrowers
Index 3 in supplies is: binders
Instead of using the range(len(someList)) technique with a for loop to obtain the integer index of the items in the list, you can call the enumerate() function instead. On each iteration of the loop, enumerate() will return two values: the index of the item in the list, and the item in the list itself.
>>> import random
>>> pets = ['Dog', 'Cat', 'Moose']
>>> random.choice(pets)
'Dog'
>>> random.choice(pets)
'Cat'
>>> random.choice(pets)
'Cat'
------------------------------------------------------
>>> import random
>>> people = ['Alice', 'Bob', 'Carol', 'David']
>>> random.shuffle(people)
>>> people
['Carol', 'David', 'Alice', 'Bob']
>>> random.shuffle(people)
>>> people
['Alice', 'David', 'Bob', 'Carol']
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
>>> spam.index('hello')
0
>>> spam.index('heyas')
3
>>> spam.index('howdy howdy howdy')
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
spam.index('howdy howdy howdy')
ValueError: 'howdy howdy howdy' is not in list
Important: When there are duplicates of the value in the list, the index of its first appearance is returned.
>>> spam = ['cat', 'dog', 'bat']
>>> spam.append('moose')
>>> spam
['cat', 'dog', 'bat', 'moose']
------------------------------------------------
>>> spam = ['cat', 'dog', 'bat']
>>> spam.insert(1, 'chicken')
>>> spam
['cat', 'chicken', 'dog', 'bat']
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('bat')
>>> spam
['cat', 'rat', 'elephant']
-------------------------------------------------
>>> spam = ['cat', 'bat', 'rat', 'elephant']
>>> spam.remove('chicken')
Traceback (most recent call last):
File "<pyshell#11>", line 1, in <module>
spam.remove('chicken')
ValueError: list.remove(x): x not in list
-------------------------------------------------
>>> spam = ['cat', 'bat', 'rat', 'cat', 'hat', 'cat']
>>> spam.remove('cat')
>>> spam
['bat', 'rat', 'cat', 'hat', 'cat']
>>> spam = [2, 5, 3.14, 1, -7]
>>> spam.sort()
>>> spam
[-7, 1, 2, 3.14, 5]
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
>>> spam.sort()
>>> spam
['ants', 'badgers', 'cats', 'dogs', 'elephants']
-------------------------------------------------
>>> spam.sort(reverse=True)
>>> spam
['elephants', 'dogs', 'cats', 'badgers', 'ants']
-------------------------------------------------
>>> spam = [1, 3, 2, 4, 'Alice', 'Bob']
>>> spam.sort()
Traceback (most recent call last):
File "<pyshell#70>", line 1, in <module>
spam.sort()
TypeError: '<' not supported between instances of 'str' and 'int'
-------------------------------------------------
>>> spam = ['Alice', 'ants', 'Bob', 'badgers', 'Carol', 'cats']
>>> spam.sort()
>>> spam
['Alice', 'Bob', 'Carol', 'ants', 'badgers', 'cats']
-------------------------------------------------
>>> spam = ['a', 'z', 'A', 'Z']
>>> spam.sort(key=str.lower)
>>> spam
['a', 'A', 'z', 'Z']
>>> spam = ['cat', 'dog', 'moose']
>>> spam.reverse()
>>> spam
['moose', 'dog', 'cat']
import random
EXCEPTIONS TO INDENTATION RULES IN PYTHON
messages = ['It is certain',
'It is decidedly so',
'Yes definitely',
'Reply hazy try again',
'Ask again later',
'Concentrate and ask again',
'My reply is no',
print(messages[random.randint(0, len(messages) - 1)])
>>> name = 'Zophie'
>>> name[0]
'Z'
>>> name[-2]
'i'
>>> name[0:4]
'Zoph'
>>> 'Zo' in name
True
>>> 'z' in name
False
>>> 'p' not in name
False
>>> for i in name:
... print('* * * ' + i + ' * * *')
* * * Z * * *
* * * o * * *
* * * p * * *
* * * h * * *
* * * i * * *
* * * e * * *
import pprint
import os
allGuests = {
'Alice': {'apples': 5, 'pretzels': 12},
'Bob': {'ham sandwiches': 3, 'apples': 2},
'Carol': {'cups': 3, 'apple pies': 1}
}
def totalBrought(guests, item):
numBrought = 0
for k, v in guests.items():
pprint.pprint(k)
pprint.pprint(v)
numBrought += v.get(item, 0)
return numBrought
os.system("cls")
print('Number of things being brought:')
print(' - Apples ' + str(totalBrought(allGuests, 'apples')))
print(' - Cups ' + str(totalBrought(allGuests, 'cups')))
print(' - Cakes ' + str(totalBrought(allGuests, 'cakes')))
print(' - Ham Sandwiches ' + str(totalBrought(allGuests, 'ham sandwiches')))
print(' - Apple Pies ' + str(totalBrought(allGuests, 'apple pies')))
- Result:
Number of things being brought:
- Apples 7
- Cups 3
- Cakes 0
- Ham Sandwiches 3
- Apple Pies 1
The tuple data type is almost identical to the list data type, except in two ways. First, tuples are typed with parentheses, ( and ), instead of square brackets, [ and ]. For example, enter the following into the interactive shell:
>>> eggs = ('hello', 42, 0.5)
>>> eggs[0]
'hello'
>>> eggs[1:3]
(42, 0.5)
>>> len(eggs)
3
But the main way that tuples are different from lists is that tuples, like strings, are immutable. Tuples cannot have their values modified, appended, or removed. Enter the following into the interactive shell, and look at the TypeError error message:
>>> eggs = ('hello', 42, 0.5)
>>> eggs[1] = 99
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
eggs[1] = 99
TypeError: 'tuple' object does not support item assignment
Just like how str(42)
will return '42', the string representation of the integer
42
, the functions list()
and tuple()
will return list and tuple versions of the
values passed to them:
>>> tuple(['cat', 'dog', 5])
('cat', 'dog', 5)
>>> list(('cat', 'dog', 5))
['cat', 'dog', 5]
- Important:
>>> list("This is a Program")
['T', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm']
Converting a tuple to a list is handy if you need a mutable version of a tuple value.
import random, sys, os, math
for i in range(5):
print(random.randint(1, 10))
import sys
while True:
print('Type exit to exit.')
response = input()
if response == 'exit':
sys.exit()
print('You typed ' + response + '.')
import sys
if len(sys.argv) < 2:
print('Usage: python mclip.py [keyphrase] - copy phrase text')
sys.exit()
keyphrase = sys.argv[1] # first command line arg is the keyphrase
from datetime import date
from functools import reduce
def numOfDays(date1, date2):
return reduce(lambda x, y: (y-x).days, [date1, date2])
date1 = date(2018, 12, 13)
date2 = date(2019, 2, 25)
print(numOfDays(date1, date2), "days")
# This code is contributed by Jyothi pinjala