0% found this document useful (0 votes)
24 views

Introduction of Python in Machine Learning

Python provides built-in functions and operators to perform common operations on sequences like strings, lists, and tuples. Some key operations covered in the document include: 1. Accessing items by index or slicing subsets. 2. Concatenating, repeating, and testing membership of sequences using +, *, and in/not in operators. 3. Iterating over items using for loops. 4. Getting the length, sum, sorted version, and counting/indexing functions of sequences. Python handles sequences in a consistent way, allowing similar syntax across string, list, and tuple types to perform common list-like operations. The document provides examples of applying these common sequence methods to different data

Uploaded by

Jamal Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Introduction of Python in Machine Learning

Python provides built-in functions and operators to perform common operations on sequences like strings, lists, and tuples. Some key operations covered in the document include: 1. Accessing items by index or slicing subsets. 2. Concatenating, repeating, and testing membership of sequences using +, *, and in/not in operators. 3. Iterating over items using for loops. 4. Getting the length, sum, sorted version, and counting/indexing functions of sequences. Python handles sequences in a consistent way, allowing similar syntax across string, list, and tuple types to perform common list-like operations. The document provides examples of applying these common sequence methods to different data

Uploaded by

Jamal Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 70

Introduction to Python

Interpreted Languages
• interpreted
– Not compiled like Java
– Code is written and then directly executed by an interpreter
– Type commands into interpreter and see immediate results

Java: Runtime
Code Compiler Computer
Environment

Python: Code Interpreter Computer

2
Installing Python for Windows
• https://www.python.org/downloads/
IDE for Python
• IDLE

• PyCharm
• https://www.jetbrains.com/pycharm/

• Anaconda
• https://www.anaconda.com/distribution/
Python Math functions
Symbol Function Example Result
+ addition 5+3 8
– subtraction 10 – 6 4
* multiplication 3 * 7 21
// integer division 15 // 6 2
/ float division 15 / 6 2.5
** power 7 ** 2 49
Expressions
• Arithmetic is very similar to Java
– Operators: + - * / % (plus ** for exponentiation)
– Precedence: () before ** before * / % before + -
– Integers vs. real numbers (doubles)
– You may use // for integer division

>>> 1 + 1
2
>>> 1 + 3 * 4 - 2
11
>>> 7 // 2
3
>>> 7 / 2
3.5
>>> 7.0 / 2
3.5
6
The print Statement
print("text”)
print()(a blank line)
– Escape sequences such as \" are the same as in Java
– Strings can also start/end with '

swallows.py
1 print("Hello, world!”)
2 print()
3 print("Suppose two swallows \"carry\" it together.”)
4 Print('African or "European" swallows?’)

7
Variables
• Declaring
– no type is written; same syntax as assignment
• Operators
– no ++ or -- operators (must manually adjust by 1)

Java Python
int x = 2; x = 2
x++; x = x + 1
System.out.println(x); print(x)
x = x * 8; x = x * 8
System.out.println(x); print(x)
double d = 3.2; d = 3.2
d = d / 2; d = d / 2
System.out.println(d); print(d)

8
4 2 1 5 3
Example: x = 1 + 5 ** (3 // 2) – 6 % 4
Order of x = 1 + 5 ** (3 //
1 2) – 6 % 4
Operations
x = 1 + 5 ** (3
5 // 2) – 6 % 4
1. ( )
2. ** x = 1 + 5 ** (3
5 // 2) – 6 %2 4
3. * / // %
x = 1 + 5 **6 (3 // 2) – 6 %2 4
4. + –
5. left to right x = 1 + 5 ** (3 //
4 2) – 6 % 4
The ‘in’ Operator
· Boolean test whether a value is inside a container:
>>> t = [1, 2, 4, 5]
>>> 3 in t
False
>>> 4 in t
True
>>> 4 not in t
False
· For strings, tests for substrings
>>> a = 'abcde'
>>> 'c' in a
True
>>> 'cd' in a
True
>>> 'ac' in a
False
· Be careful: the in keyword is also used in the syntax of for loops and list
comprehensions
range
range gives a sequence of integers “to” means up to but not
including.
range(2, 10, 3) # returns [2, 5, 8]

for i in range(5): [0, 1, 2, 3, 4]


from to step for i in range(7, 9): [7, 8]
(Inclusive. (Not
(Default 1) for i in range(1, 10, 2): [1, 3, 5, 7, 9]
Default 0) inclusive)
for i in range(10, 6, -1): [10, 9, 8, 7]
list indexing

• lists can be indexed with positive or negative numbers (we’ve seen


this before!)

index 0 1 2 3 4 5 6 7

value 9 14 12 19 16 18 24 15

index -8 -7 -6 -5 -4 -3 -2 -1
12
indexing
• Access any item in the sequence using its index
String

x = 'frog'
print (x[3]) # prints 'g'
List

x = ['dog', 'cow', 'horse']


print (x[1]) # prints 'cow'
slicing
• Slice out substrings, sublists, subtuples using indexes
[start : end : step] x = 'computer'
Code Result Explanation
x[1:4] 'omp' Items 1 to 3
x[1:6:2] 'opt' Items 1, 3, 5
x[3:] 'puter' Items 3 to end
x[:5] 'compu' Items 0 to 4
x[-1] 'r' Last item
x[-3:] 'ter' Last 3 items
x[:-2] 'comput' All except last 2 items
The + Operator
The + operator produces a new tuple, list, or string whose value
is the concatenation of its arguments.

>>> (1, 2, 3) + (4, 5, 6)


(1, 2, 3, 4, 5, 6)

>>> [1, 2, 3] + [4, 5, 6]


[1, 2, 3, 4, 5, 6]

>>> “Hello” + “ ” + “World”


‘Hello World’
adding / concatenating
• Combine 2 sequences of the same type using +
String
x = 'horse' + 'shoe'
print (x) # prints 'horseshoe'

List

x = [‘dog', 'cow'] + ['horse']


print (x) # prints [‘dog', 'cow', 'horse']
The * Operator
· The * operator produces a new tuple, list, or string that
“repeats” the original content.
>>> (1, 2, 3) * 3
(1, 2, 3, 1, 2, 3, 1, 2, 3)

>>> [1, 2, 3] * 3
[1, 2, 3, 1, 2, 3, 1, 2, 3]

>>> “Hello” * 3
‘HelloHelloHello’
String Concatenation
• Integers and strings cannot be concatenated in Python.
– Workarounds:
– str(value) - converts a value into a string
– print(value, value) - prints values, separated by a space

>>> x = 4
>>> print("Thou shalt not count to " + x + ".")
TypeError: cannot concatenate 'str' and 'int' objects

>>> print("Thou shalt not count to " + str(x) + ".")


Thou shalt not count to 4.

>>> print(x + 1, "is out of the question.")


5 is out of the question.

18
SEQUENCE
S
String List Tuple
•multiplying
• Multiply a sequence using *

String
x = ‘bug' * 3
print (x) # prints ‘bugbugbug'

List

x = [8, 5] * 3
print (x) # prints [8, 5, 8, 5, 8, 5]
SEQUENCE
S
String List Tuple
•checking membership
• Test whether an item is in or not in a sequence

String

x = 'bug'
print ('u' in x) # prints True

List

x = [‘dog', 'cow', 'horse']


print ('cow' not in x) # prints False
SEQUENCE
S
String List Tuple
•iterating
• Iterate through the items in a sequence
Item

x = [7, 8, 3]
for item in x:
print (item * 2) # prints 14, 16, 6

Index & Item

x = [7, 8, 3]
for index, item in enumerate(x):
print (index, item) # prints 0 7, 1 8, 2 3
SEQUENCE
S
String List Tuple
•number of items
• Count the number of items in a sequence

String
x = 'bug'
print (len(x)) # prints 3

List

x = [‘dog', 'cow', 'horse']


print (len(x)) # prints 3
SEQUENCE
S
String List Tuple
•sum
• Find the sum of items in a sequence
• entire sequence must be numeric type
String -> Error

x = [5, 7, 'bug‘]
print (sum(x)) # error!

List
x = [2, 5, 8, 12]
print (sum(x)) # prints 27
print (sum(x[-2:])) # prints 20
SEQUENCE
S
•sorting String List Tuple

• Returns a new list of items in sorted order


• Does not change the original list
String

x = 'bug'
print (sorted(x)) # prints ['b', 'g', 'u']

List

x = ['pig', 'cow', 'horse']


print (sorted(x)) # prints ['cow', ‘dog', ‘horse']
SEQUENCE
S
String List Tuple
•count (item)
• Returns count of an item
String

x = 'hippo'
print (x.count('p')) # prints 2

List

x = ['dog', 'cow', 'horse', 'cow']


print (x.count('cow')) # prints 2
SEQUENCE
S
String List Tuple
•index (item)
• Returns the index of the first occurrence of an item
String
x = 'hippo'
print (x.index('p')) # prints 2

List

x = ['dog', 'cow', 'horse', 'cow']


print (x.index('cow')) # prints 1
LISTS
All operations from Sequences, plus:
• constructors
• del list1[2] delete item from list1
• list1.append(item) appends an item to list1
• list1.extend(sequence1) appends a sequence to list1
• list1.insert(index, item) inserts item at index
• list1.pop() pops last item
• list1.remove(item) removes first instance of item
• list1.reverse() reverses list order
• list1.sort() sorts list in place
• list1.clear() empties list
LISTS
• delete
• Delete a list or an item from a list

x = [5, 3, 8, 6]
del(x[1]) # [5, 8, 6]
del(x) # deletes list x

• append
• Append an item to a list
x = [5, 3, 8, 6]
x.append(7) # [5, 3, 8, 6, 7]
•extend: Append an sequence to a list
x = [5, 3, 8, 6]
y = [12, 13]
x.extend(y) # [5, 3, 8, 6, 7, 12, 13]

•insert: Insert an item at given indexx.insert(index,


item)
x = [5, 3, 8, 6]
x.insert(1, 7) # [5, 7, 3, 8, 6]
x.insert(1,['a','m']) # [5, ['a', 'm'], 7, 3, 8, 6]
The extend method vs +
· + creates a fresh list with a new memory ref
· extend operates on list li in place.
>>> li.extend([9, 8, 7])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
· Potentially confusing:
• extend takes a list as an argument.
• append takes a singleton as an argument.
>>> li.append([10, 11, 12])
>>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10, 11, 12]]
LISTS
•pop: Pops last item off the list, and returns item
x = [5, 3, 8, 6]
x.pop() # [5, 3, 8] and returns the 6
print(x.pop()) # prints 8, x is now [5, 3]

•remove: Remove first instance of an item


x = [5, 3, 8, 6, 3]
x.remove(3) # [5, 8, 6, 3]
LISTS
•reverse: Reverse the order of the list
x = [5, 3, 8, 6]
x.reverse() # [6, 8, 3, 5]

•sort: Sort the list in place


x = [5, 3, 8, 6]
x.sort() # [3, 5, 6, 8]
LISTS

•clear: delete all items from the list

x = [5, 3, 8, 6]
x.clear() # []
Dictionaries
• Dictionaries are lookup tables.
• They map from a “key” to a “value”.
symbol_to_name = {
"H": "hydrogen",
"He": "helium",
"Li": "lithium",
"C": "carbon",
"O": "oxygen",
"N": "nitrogen"
}
• Duplicate keys are not allowed
• Duplicate values are just fine
DICTIONARI
ES
•constructors – creating a new dict
x = {'lamb':25.3, 'beef':33.8, 'chicken':22.7}
x = dict([('lamb', 25.3),('beef', 33.8),('chicken', 22.7)])
x = dict(lamb=25.3, beef=33.8, chicken=22.7)
DICTIONARI
ES
•basic dict operations
Description Code
Add or change item in dict x x['beef'] = 25.2
Remove item from dict x del x['beef']
Get length of dict x len(x)
Check membership in x item in x
(only looks in keys, not values) item not in x
Delete all items from dict x x.clear()
Delete dict x del x
DICTIONARI
ES
•accessing keys and values in a dict
x.keys() # returns list of keys in x
x.values() # returns list of values in x
x.items() # returns list of key-value tuple pairs in x

item in x.values() # tests membership in x: returns boolean


DICTIONARI
ES
•iterating a dict
for key in x: # iterate keys
print(key, x[key]) # print all key/value pairs

for k, v in x.items(): # iterate key/value pairs


print(k, v) # print all key/value pairs

Note:
Entries in a dict are in random order.
Functions
• Function:
• Syntax:
hello2.py
def name():
statement 1 # Prints a helpful message.
2 def hello():
statement 3 print("Hello, world!")
... 4
5 # main (calls hello twice)
statement 6 hello()
7 hello()

– Must be declared above the 'main' code


– Statements inside the function must be indented

39
Whitespace Significance
• Python uses indentation to indicate blocks, instead of {}
– Makes the code simpler and more readable
– In Java, indenting is optional. In Python, you must indent.
– You may use either tabs or spaces, but you must be
consistent
hello3.py

1 # Prints a welcoming message.


2 def hello():
3 print("Hello, world!")
4 print("How are you?")
5
6 # main (calls hello twice)
7 hello()
8 hello()

40
Default Parameter Values
def name(parameter=value, ..., parameter=value):
statements
– Can make parameter(s) optional by specifying a default value
>>> def print_many(word, n=1):
... for i in range(n):
... print(word)
>>> print_many("shrubbery")
shrubbery
>>> print_many("shrubbery", 4)
shrubbery
shrubbery
shrubbery
shrubbery

– Exercise: Modify stars.py to add an optional parameter for


the character to use for the outline of the box (default "*").

41
Parameter Keywords
name(parameter=value, ..., parameter=value)
– Can specify the names of parameters as you call a function
– This allows you to pass the parameters in any order

>>> def print_many(word, n):


... for i in range(n):
... print(word)

>>> print_many(word="shrubbery", n=4)


shrubbery
shrubbery
shrubbery
shrubbery
>>> print_many(n=3, word="Ni!")
Ni!
Ni!
Ni!

42
Returning Values
def name(parameters):
statements
...
return value

>>> def ftoc(temp):


... tempc = 5.0 / 9.0 * (temp - 32)
... return tempc

>>> ftoc(98.6)
37.0

43
Simple functions: ex.py
"""factorial done recursively and iteratively"""

def fact1(n):
ans = 1
for i in range(2,n):
ans = ans * n
return ans

def fact2(n):
if n < 1:
return 1
else:
return n * fact2(n - 1)
Simple functions: ex.py
671> python
Python 2.5.2 …
>>> import ex
>>> ex.fact1(6)
1296
>>> ex.fact2(200)
78865786736479050355236321393218507…000000L
>>> ex.fact1
<function fact1 at 0x902470>
>>> fact1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fact1' is not defined
>>>
The for Loop
– for name in range(max):
– statements

– Repeats for values 0 (inclusive) to max (exclusive)

>>> for i in range(5):


... print(i)
0
1
2
3
4

46
for Loop Variations
– for name in range(min, max):
– statements
– for name in range(min, max, step):
– statements
– Can specify a minimum other than 0, and a step other than 1

>>> for i in range(2, 6):


... print(i)
2
3
4
5
>>> for i in range(15, 0, -5):
... print(i)
15
10
5
47
Nested Loops
• Nested loops are often replaced by string * and +
Java
....1
...2 1 for (int line = 1; line <= 5; line++) {
2 for (int j = 1; j <= (5 - line); j++) {
..3 3
4 System.out.print(".");
.4 5 }
6 System.out.println(line);
}
5

Python

1 for line in range(1, 6):


2 print((5 - line) * ".", line, sep="")

48
input
input : Reads a string from the user's keyboard.
– reads and returns an entire line of input

>>> name = input("Howdy. What's yer name? ")


Howdy. What's yer name? Paris Hilton

>>> name
'Paris Hilton'

49
input for numbers
• to read a number, cast the result of input to an int
– Only numbers can be cast as ints!
– Example:
age = int(input("How old are you? "))
print("Your age is", age)
print("You have", 65 - age, "years until
retirement“)

Output:
How old are you? 53
Your age is 53
You have 12 years until retirement

50
if/else
if condition:
statements
elif condition:
statements
else:
statements
– Example:
gpa = int(input("What is your GPA? "))
if gpa > 3.5:
print("You have qualified for the honor roll.")
elif gpa > 2.0:
print("Welcome to Moon University!")
else:
print("Apply again in Spring.")

51
if ... in
if value in sequence:
statements
– The sequence can be a range, string, tuple, or list

– Examples:

x = 3
if x in range(0, 10):
print("x is between 0 and 9“)

name = input("What is your name? ")


name = name.lower()
if name[0] in "aeiou":
print("Your name starts with a vowel!“)

52
while Loops
while test:
statements

>>> n = 91
>>> factor = 2 # find first factor of n

>>> while n % factor != 0:


... factor += 1
...

>>> factor
7

53
Logical Operators
Operator Meaning Example Result
== equals 1 + 1 == 2 True
!= does not equal 3.2 != 2.5 True
< less than 10 < 5 False
> greater than 10 > 5 True
<= less than or equal to 126 <= 100 False
>= greater than or equal to 5.0 >= 5.0 True

Operator Example Result


and (2 == 3) and (-1 < 5) False
or (2 == 3) or (-1 < 5) True
not not (2 == 3) True

54
Defining a Class
• Declaring a class:
class Name:
...

– class name is capitalized (e.g. Point)


– saved into a file named name.py (filename is lowercase)

56
Fields
• Declaring a field:
name = value point.py

1 class Point:
– Example: 2 x = 0
3 y = 0
class Point:
x = 0
y = 0

57
Using a Class
from name import *
– client programs must import the classes they use
– the file name (lowercase), not class name, is used

point_main.py

1 from point import *


2
3 # main
4 p1 = Point()
5 p1.x = 7
6 p1.y = -3
7
8 ...

58
"Implicit" Parameter (self)
• Java object methods refer to the object's fields implicitly:
public void translate(int dx, int dy) {
x += dx;
y += dy; // change this object's x/y
}

• Python's implicit parameter is named self


– self must be the first parameter of any object method
– access the object's fields as self.field
def translate(self, dx, dy):
self.x += dx
self.y += dy

59
Methods
def name(self [, parameter, ..., parameter]):
statements

– Example:
class Point:
def translate(self, dx, dy):
self.x += dx
self.y += dy
...

– Exercise: Write the following methods in class Point:


• set_location
• draw
• distance

60
Exercise Answer
point.py

1 from math import *


2
3 class Point:
4 x = 0
5 y = 0
6
7 def set_location(self, x, y):
8 self.x = x
9 self.y = y
10
11 def draw(self, panel):
12 panel.canvas.create_oval(self.x, self.y, \
13 self.x + 3, self.y + 3)
14 panel.canvas.create_text(self.x, self.y, \
15 text=str(self), anchor="sw")
16
17 def distance(self, other):
18 dx = self.x - other.x
19 dy = self.y - other.y
20 return sqrt(dx * dx + dy * dy)

61
Initializing Objects
• Right now, clients must initialize Points like this:
p = Point()
p.x = 3
p.y = -5

• We'd prefer to be able to say:


p = Point(3, -5)

62
Constructors
def __init__(self [, parameter, ..., parameter]):
statements

– a constructor is a special method with the name __init__


that initializes the state of an object

– Example:
class Point:
def __init__(self, x, y):
self.x = x
self.y = y

63
More About Fields
point.py

1 class Point:
2 def __init__(self, x,
3 y):
4 self.x = x
5 self.y = y
...

>>> p = Point(5, -2)


>>> p.x
5
>>> p.y
-2

– fields can be declared directly inside class,


or just in the constructor as shown here (more common)

64
Printing Objects
• By default, Python doesn't know how to print an object:

>>> p = Point(5, -2)


>>> print p
<Point instance at 0x00A8A850>

• We'd like to be able to print a Point object and have its


state shown as the output.

65
Printable Objects: __str__
def __str__(self):
return string
– converts an object into a string (like Java toString method)
– invoked automatically when str or print is called

def __str__(self):
return "(" + str(self.x) + ", " + str(self.y) + ")"

>>> p = Point(5, -2)


>>> print p
(5, -2)
>>> print "The point is " + str(p) + "!"
The point is (5, -2)!

66
Complete Point Class
point.py

1 from math import *


2
3 class Point:
4 def __init__(self, x, y):
5 self.x = x
6 self.y = y
7
8 def distance_from_origin(self):
9 return sqrt(self.x * self.x + self.y * self.y)
10
11 def distance(self, other):
12 dx = self.x - other.x
13 dy = self.y - other.y
14 return sqrt(dx * dx + dy * dy)
15
16 def translate(self, dx, dy):
17 self.x += dx
18 self.y += dy
19
20 def __str__(self):
21 return "(" + str(self.x) + ", " + str(self.y) + ")"

67
Python Object Details
• Drawbacks
– Does not have encapsulation like Java (ability to protect fields'
data from access by client code)
– Not easy to have a class with multiple constructors
– Must explicitly declare self parameter in all methods
– Strange names like __str__, __init__

• Benefits
– operator overloading: Define < by writing __lt__ , etc.
http://docs.python.org/ref/customization.html

68
Reading Files
name = open("filename")
– opens the given file for reading, and returns a file object

name.read() - file's entire contents as a string

>>> f = open("hours.txt")
>>> f.read()
'123 Susan 12.5 8.1 7.6 3.2\n
456 Brad 4.0 11.6 6.5 2.7 12\n
789 Jenn 8.0 8.0 8.0 8.0 7.5\n'

69
Line-based File Processing
name.readline() - next line from file as a string
– Returns an empty string if there are no more lines in the file

name.readlines() - file's contents as a list of lines


– (we will discuss lists in detail next week)

>>> f = open("hours.txt")
>>> f.readline()
'123 Susan 12.5 8.1 7.6 3.2\n'

>>> f = open("hours.txt")
>>> f.readlines()
['123 Susan 12.5 8.1 7.6 3.2\n',
'456 Brad 4.0 11.6 6.5 2.7 12\n',
'789 Jenn 8.0 8.0 8.0 8.0 7.5\n']

70
Writing Files
name = open("filename", "w") # write
name = open("filename", "a") # append
– opens file for write (deletes any previous contents) , or
– opens file for append (new data is placed after previous data)

name.write(str) - writes the given string to the file


name.close() - closes file once writing is done

>>> out = open("output.txt", "w")


>>> out.write("Hello, world!\n")
>>> out.write("How are you?")
>>> out.close()
>>> open("output.txt").read()
'Hello, world!\nHow are you?'
71

You might also like