Python basics

types

print(type(employees))

### Create empty list
empty_list = []
empty_list = list()

### Create empty tuples
empty_tuple = ()
empty_tuple = tuple()

### Create empty sets
empty_set = {} #This does not create empty sets. {} Creates dict.
empty_set = set()

# Create empty dict
empty_dict = {}
empty_dict = dict()

Lists []

courses = ['History', 'Math', 'Physics', 'CompSci']
courses_2 = ['Accounting', 'Education']

print(courses)
print(len(courses))
print(courses[0])
print(courses[-1])
print(courses[0:2]); print(courses[:2])
print(courses[2:])

courses.append('Art')
courses.insert(0, 'Art')

if 'Math' in courses:
    print(courses.count('Math'))

courses.extend(courses_2)
print(courses)

courses.insert(0, courses_2)
print(courses)

courses.remove(('Math'))
courses.sort()
courses.reverse()
courses.pop()

popped = courses.pop(0)
print(popped)

courses = ['History', 'Math', 'Math', 'Math', 'Physics']
print(courses.count('Math'))

numbers = [10, 44, 33, 11, 2, 5, 99]
print(min(numbers))
print(max(numbers))
print(sum(numbers))


### Loop with enumerate
for index, course in enumerate(courses):
    print(index, course)
for index, course in enumerate(courses, start=1):
    print(index, course)

### join and split with string
course_str = ', '.join(courses)
print(course_str)
print(course_str.split(', '))

### List 1 and List 2 are the same!
list_1 = ['History', 'Math', 'Physics', 'CompSci', 'Math']
list_2 = list_1

print(list_1)
print(list_2)

list_1[0] = 'Art'

print(list_1)
print(list_2)

Tuple ()

tuple_1 = ('History', 'Math', 'Physics', 'CompSci')
tuple_2 = tuple_1

print(tuple_1)
print(tuple_2)

tuple_1[0] = 'Art'
### Unable to modify values in tuple!

Sets {}

### Unordered and unique (no duplicates)


### Check if 'Math' exists in the set
myDataSet = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
print(myDataSet)
print('Math' in myDataSet)

### set intersection & difference
myDataSet_1 = {'History', 'Math', 'Physics', 'CompSci', 'Math'}
myDataSet_2 = {'History', 'Math', 'Art', 'Design'}

print(myDataSet_1.intersection(myDataSet_2))
print(myDataSet_1.difference(myDataSet_2))
print(myDataSet_2.difference(myDataSet_1))

### set union
print(myDataSet_1.union(myDataSet_2))

dict {}

### example of dict

student = {
    'name':'kim',
    'age':99,
    'courses': ['Math', 'CompSci'],
}

print(student)
print(student['name'])
print(student.get('name'))
print("{} is studying {} at the age of {}.".format(student['name'], student['courses'], student['age']))

### Access a key that does not exist
print(student.get('contact', 'Not found!'))

### Create a new key in dict
student['contact'] = '1234-5678'
print(student.get('contact', 'Not found!

### Delete a key in dict
del student['name']
nameRemoved = student.pop('name')

### Update values in dict
student.update({'name':'Kim Full', 'age':100, 'contact':999})

### Get number of keys in dict
print(len(student))
print(student.keys())

### Get values
print(student.values())

### Get key and value
print(student.items())

### Loop dict
for key, value in student.items():
    print(key, value)

++ increment is not allowed in Python

if 'Math' in courses:
    TotalNumberOfMath = 0
    print(courses.count('Math'))
    
    for item in courses:
        print(item)
        if item == 'Math':
            TotalNumberOfMath += 1

    print("There are total {} Math".format(TotalNumberOfMath))

Deploy python to executable

### Install pyinstaller
(venv) pip3 install pyinstaller

### Executable .exe file will be created in dist folder
pyinstaller --onefile myProgram.py

Get full path location for *.csv

globItems = glob.glob(os.getcwd()+'//*.csv')

Extract only filename from full path

print(os.path.basename(globItems[0]))

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top