Basic Python Exercises

See also: Official Python3 Tutorial


Housekeeping

We've completed the "Overview" section of the presentation and we're now doing the in-depth examination of the packages presented. Because of the varying levels of skill of the people on this call, I can't really ensure that I'm giving you the information you need. If what I'm doing is not helpful, feel free to explore the web for other Python resources (I'd start with the items at the bottom of each notebook in the "Other Links" section).

Even if you're not working on the specific section I am, please feel free to ask/send me questions.

There's no shame in Googling things. Stack Overflow is your friend.

Reminders:

  • Shift + Enter executes
  • Escape puts you in command mode
  • Enter puts you in edit mode
  • The H key in command mode brings up help

In [1]:
# 1. Make your interpreter print the statement "Hello, world!".

print('Hello, World!')


Hello, World!

In [2]:
# 2. Assign a string to a variable and then print the variable.

a = 'My string'

print(a)


My string

In [3]:
# 3. Create a list of numbers and print every number in that list.

list_o_numbers = [1,2,3,45]

for item in list_o_numbers:
    print(item)


1
2
3
45

In [4]:
# 4. Create a list of numbers and print the numbers if the number is greater than 3.
# Hint: remember to indent conditionals like 'if condition:'

for item in list_o_numbers:
    if item > 3:
        print(item)


45

In [5]:
# 5. Create an empty list and add 3 strings to that list.

new_list = [] # list()

new_list.append(1)
new_list.append(2)
new_list.append(3)

print(new_list)


[1, 2, 3]

In [1]:
# 6. Create a dictionary that links 5 US States with their 2 letter abbreviation.

dictionary = {'MO': 'Missouri', 'MN': 'Minnesota'}

dictionary


Out[1]:
{'MN': 'Minnesota', 'MO': 'Missouri'}

In [3]:
# 7. Import the "string" module. Print every character in string.punctuation individually.

import string

for character in string.punctuation:
    print(character, end='\t')


!	"	#	$	%	&	'	(	)	*	+	,	-	.	/	:	;	<	=	>	?	@	[	\	]	^	_	`	{	|	}	~	

In [10]:
# 8. Read all the information in the text_file.txt in the data folder.

with open('data/text_file.txt') as f:
    data = f.read()
    print(data)


Dear Sir or Madam,

I am a text file. Like most text files you will deal with in Python, I am encoded in UTF-8, but be aware some Windows machines encode text files as cp-1252 ... so if you run into weird errors when inputting information, that may be the reason.

Very truly yours,
Text File


In [11]:
# 9. Create a string with your name and write it to a file in the data folder.

with open('data/text_file_new.txt', 'w+') as f:
    f.write('Theo')

In [12]:
# 10. Create a function that prints "Hello, world!".

def hello():
    print('Hello, world!')
    
hello()


Hello, wolrd!

In [14]:
# 11. Import os and use os.walk to read and print the contents of every file in data/dir_tree.

import os

for root, folder_names, file_names in os.walk('data/dir_tree/'):
    for file_name in file_names:
        path = os.path.join(root, file_name)
        with open(path, 'r') as f:
            print(f.read())


I am doc 1!
I am doc 1a!
I am doc 2!
I am doc 3!
I am doc 3a!
I am doc 3b!

In [4]:
# 12. Use a range() object to print the numbers from 1 through 10.

for x in range(0, 11):
    print(x)


0
1
2
3
4
5
6
7
8
9
10

Next Up: Pandas Basics