10 Minute Intro to Python

# 10 minute intro to python

# numbers
age = 45  
pi = 3.14159

# strings
s = 'Craig Lawrence Pythonic'  
tokens = s.split()  
f_name = tokens[0]  
m_name = tokens[1]  
l_name = tokens[2]  
s2 = f_name + ' ' + m_name + ' ' + l_name


# if statements
if (s == s2):  
    print('Yes!  The names match...')
else:  
    print('Nooooo...')

# list (a mutable sequence)
pink_floyd = ['Roger', 'Sid', 'Rick', 'Dave']  
pink_floyd.append('Nick')

# for loop - indentation matters
for p in pink_floyd:  
    print('Founding Member Name: ' + p)

# tuples (immutable sequence)
ages = (18, 21, 26, 21, 19, 45, 50, 76, 82, 10)

# set (no order, no duplicates)
uniqueAges = set(ages)  
uniqueAges.add(18) #already in set, no effect  
uniqueAges.remove(21)

# no guaranteed order when iterating over a set
for thisAge in uniqueAges:  
    print(thisAge)

# testing set membership
if 18 in uniqueAges:  
    print('There are minors present.  Behave!')

# sorting
pink_floyd.sort()  #sort in-place  
orderedUniqueAges = sorted(uniqueAges) #creates a new list

# dictionaries
netWorth = {}  
netWorth['Ronald McDonals'] = 120000000  
netWorth['Colonel Sanders'] = 100000000  
netWorth['Hamburger Mary'] = 500000  
netWorth['Sid Barret'] = 350000  
netWorth['Dave Gilmore'] = 4000000

# iterate over the dict
for person, worth in netWorth.items():  
    if worth < 1000000:
        print('Gosh! ' + person + ' is not a millionarie. He is only worth ' + str(worth) + '.')

# testing membership in dict
if 'Sid Barret' in netWorth:  
    print('Dark Side of the Moon...')

Craig Derington

Espousing the virtues of Secular Humanism, Libertarianism, Free and Open Source Software, Linux, Ubuntu, Terminal Multiplexing, Docker, Python, Flask, Django, Go, MySQL, PostgreSQL, MongoDB and Git.

comments powered by Disqus