EML4930/EML6934: Lecture 01 - About Python: Basics: Data Types, Math, Logic, If Statement
EML4930/EML6934: Lecture 01 - About Python: Basics: Data Types, Math, Logic, If Statement
Python
Basics: data types, math, logic, if statement
Charles Jekel
August 31, 2017
Results from the first HW
1
The operating systems used by the class
2
Textbook for this lecture
3
Comments
’’’
This is a bulk comment in python
4
Assigning variables
This officially defines a pointer named x that points to the integer 10.
We can change what x points to at any time.
x = 10 # x is the integer 10
x = 10. # x is the floating point EEE-754 double precision
x = 10.0 # same as x = 10.
x = ’ten’ # x is now a string
x = (0,1,2) # x is now a tuple
x = [0,1,2] # x is now a list
x = 1; y = 2; z = 3 # assigns x = 1, y = 2, and z = 3
5
Demo - Consequences of pointers
Be careful.
x = [1,2,3]
y = x
x[0] = 4
print(y)
Numpy warning!
import numpy as np
x = np.array([10])
y = x
x += 5
print(y)
# this doesn’t happen if I used x = x + 5
# this doesn’t happen if x = 10 (instead of np.array([10]))
6
Math operators
7
Division in Python2
a = 3
b = 2
c = a/b
8
Python3 broke division!
9
Built in data types
True
False
Operation Description
a == b a equal to b
a != b a not equal to b
a<b a less than b
a>b a greater than b
a <= b a less than or equal to b
a >= b a greater than or equal to b
11
Floating point precision
0.1+0.2 == 0.3
12
Data Structures
Immutable
Can not be changed or modified
13
Lists are amazing
Lists
14
Pimp my list
15
Listception
16
An example list
Note: x.sort() doesn’t work in this case with Python 3 since you would
need to compare different data types!!!
17
List forward indexing - lists start at 0
in : x[0:2]
out: [7, 77]
in : x[1:4]
out: [77, 777, 7777]
20
Slicing with step size
x = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15,
16, 17, 18, 19] # creates a list of integers
in : x[0:20:2]
out: [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
in : x[0:20:5]
out: [0, 5, 10, 15]
in : x[1:20:3]
out: [1, 4, 7, 10, 13, 16, 19]
21
Reverse the order of a list
in : x[::-1]
out: [7777, 777, 77, 7]
22
Creating lists with range (Python2 only)
Note: Python 3.5 changed how range works, range no longer creates
lists, it is an iterable. We won’t use range for numerical work, we’ll use a
numpy function instead.
Let’s use the range function to create lists of integers. How do we view
the docstring of range?
print(range.__doc__)
Tuples are just like lists, except you define a tuple with parentheses
instead of square brackets. List indexing a slicing of Tuples works exactly
like lists, and you’ll use square brackets to call items of a tuple.
Tuples are immutable which means that once they are created they
can’t be modified in any way. I hardly ever use Tuples.
Let’s create a tuple of 2, 4, 6.
x = (2, 4, 6)
in : x[0]
out: 2
in : x[-1]
out: 6
24
Dictionaries
We don’t can add keys to the dictionary at any time! Let’s take a look at
param.
in : print(param)
out: {’silent’: 1, ’eval_metric’: ’auc’, ’nthread’: 4,
’eta’: 1, ’objective’: ’binary:logistic’, ’max_depth’: 2} 25
Dictionaries - param continued
in : param[’objective’]
out: ’binary:logistic’
in : param[’objective’] = ’reg:linear’
in : print(param[’objective’])
out: ’reg:linear’
26
Sets
Sets are like lists and tuples, but are defined with curly brackets. Sets
obey mathmatical set definitions. VanderPlas provides a good example
on page 36-37. Which I’ll show here.
27
Membership operator
Operator Description
a is b True if a and b are identical objects
a is not b True if a and b are not identical objects
a in b True if a is a member of b
a not in b True if a is not a member of b
28
If-then statements
x = 9
if x < 0:
print(x,’ is a negative number’)
elif x > 0:
print(x,’ is a positive number’)
elif x == 0:
print(’Single’)
else:
print(x,’ makes me confused!’)
29
If-then statements - notes
x = 9
if x < 0:
print(x,’ is a negative number’)
elif x > 0:
print(x,’ is a positive number’)
elif x == 0:
print(’Single’)
else:
print(x,’ makes me confused!’)
x = 9
if x > 0:
print(x,’ is a greater than zero’)
if x > 5:
print(x,’ is a greater than five’)
if x > 10:
print(x,’ is greater than 10’)
# this is the continued x > 0 code block
print(’the date type of x is ’, type(x))
Note that this will print the type of x only when x > 0!
31
tabs vs spaces
In a survey it was found that those who use spaces make more money
https://stackoverflow.blog/2017/06/15/
developers-use-spaces-make-money-use-tabs/
32
tabs vs spaces
33
HW 01 - turn in one week from today in Canvas
Turn in the 5 questions as a single .py file onto canvas. Use comments to
clearly indicate which question you are working on. Your filename should
end as py2.py if you use Python2 and py3.py if you use Python3.
1. Name one difference between Python2 and Python3. Print your
answer as a string in Python.
2. You are given a list x = [0,1,2,3,4,5,6]. Print the third item in list x.
3. Assign y as the reversed order of list x. Print y.
4. Use list slicing to assign z [1,3,5] from x. Print z.
5. Your friend is new to Python. He is confused why his if statement
isn’t working, he has a number x and wants to print ’x is positive’ by
checking with an if statement. His code is following.
x = 99
if (x > 0) is True
print(’x is positive’)
This returns an ’invalid syntax error’. Copy this code into your .py
file, and correct the code.
34