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

Python 1

Uploaded by

ramesh yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Python 1

Uploaded by

ramesh yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

• Names & Assignment

• Data types
• Sequences types: Lists, Tuples, and
Strings
• Mutability
• Understanding Reference Semantics in
Some material adapted Python
from Upenn cmpe391
slides and other sources

• Indentation matters to meaning the code


x = 34 - 23 # A comment. • Block structure indicated by indentation
y = “Hello” # Another one. • The first assignment to a variable creates it
z = 3.45 • Dynamic typing: No declarations, names don’t have
types, objects do
if z == 3.45 or y == “Hello”:
• Assignment uses = and comparison uses ==
x = x + 1
y = y + “ World” # String concat.
• For numbers + - * / % are as expected.
print x • Use of + for string concatenation.
print y • Use of % for string formatting (like printf in C)
• Logical operators are words (and,or,not)
not symbols
• The basic printing command is print

1
• Integers (default for numbers) Whitespace is meaningful in Python, especially
z = 5 / 2 # Answer 2, integer division indentation and placement of newlines
• Floats •Use a newline to end a line of code
x = 3.456 Use \ when must go to next line prematurely
• Strings •No braces {} to mark blocks of code, use
• Can use ”…" or ’…’ to specify, "foo" == 'foo’ consistent indentation instead
• Unmatched can occur within the string • First line with less indentation is outside of the block
“John’s” or ‘John said “foo!”.’ • First line with more indentation starts a nested block
• Use triple double-quotes for multi-line strings or •Colons start of a new block in many constructs,
strings than contain both ‘ and “ inside of them:
e.g. function definitions, then clauses
“““a‘b“c”””

• Binding a variable in Python means setting a


• Start comments with #, rest of line is ignored name to hold a reference to some object
• Can include a “documentation string” as the • Assignment creates references, not copies
first line of a new function or class you define
• Names in Python don’t have an intrinsic type,
• Development environments, debugger, and objects have types
other tools use it: it’s good style to include one Python determines type of the reference auto-
def fact(n): matically based on what data is assigned to it
“““fact(n) assumes n is a positive • You create a name the first time it appears on the
integer and returns facorial of n.””” left side of an assignment expression:
assert(n>0) x = 3
return 1 if n==1 else n*fact(n-1) • A reference is deleted via garbage collection after
any names bound to it have passed out of scope
• Python uses reference semantics (more later)

2
• Names are case sensitive and cannot start The Python community has these
with a number. They can contain letters, recommended naming conventions
numbers, and underscores.
• joined_lower for functions, methods and,
bob Bob _bob _2_bob_ bob_2 BoB
attributes
• There are some reserved words:
and, assert, break, class, continue, • joined_lower or ALL_CAPS for constants
def, del, elif, else, except, exec, • StudlyCaps for classes
finally, for, from, global, if,
import, in, is, lambda, not, or, • camelCase only to conform to pre-existing
pass, print, raise, return, try, conventions
while
• Attributes: interface, _internal, __private

• Where do such conventions come from? • You can assign to multiple names at the
• The community of users same time
• Codified in PEPs >>> x, y = 2, 3
• Python's development is done via the Python >>> x
Enhancement Proposal (PEP) process 2
>>> y
• PEP: a standardized design document, e.g. 3
proposals, descriptions, design rationales,
and explanations for language features
• This makes it easy to swap values
>>> x, y = y, x
• Similar to IETF RFCs
• See the PEP index • Assignments can be chained
>>> a = b = x = 2
• PEP 8: Style Guide for Python Code

3
Accessing a name before it’s been properly
created (by placing it on the left side of an
assignment), raises an error
>>> y

Traceback (most recent call last):


File "<pyshell#16>", line 1, in -toplevel-
y
NameError: name ‘y' is not defined
>>> y = 3
>>> y
3

• Python data is represented by objects or by


relations between objects
• Every object has an identity, a type and a value
• Identity never changes once created Location
or address in memory
• Type (e.g., integer, list) is unchangeable and
determines the possible values it could have and
operations that can be applied
• Value of some objects is fixed (e.g., an integer)
and can change for others (e.g., list)

4
• Sequences are containers that hold objects
• Finite, ordered, indexed by integers
• Tuple
• An immutable ordered sequence of items
• Items can be of mixed types, including collection
types
• Strings
• An immutable ordered sequence of chars
• Conceptually very much like a tuple
• List
• A Mutable ordered sequence of items of mixed
types

• All three sequence types (tuples,


strings, and lists) share much of the • Define tuples using parentheses and commas
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
same syntax and functionality.
• Define lists are using square brackets and
• Key difference: commas
• Tuples and strings are immutable >>> li = [“abc”, 34, 4.34, 23]
• Lists are mutable • Define strings using quotes (“, ‘, or “““).
>>> st = “Hello World”
• The operations shown in this section
>>> st = ‘Hello World’
can be applied to all sequence types >>> st = “““This is a multi-line
• most examples will just show the string that uses triple quotes.”””
operation performed on one

5
• Access individual members of a tuple, list, or
string using square bracket “array” notation
>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)
• Note that all are 0 based…
Positive index: count from the left, starting with 0
>>> tu = (23, ‘abc’, 4.56, (2,3), ‘def’)
>>> tu[1] # Second item in the tuple. >>> t[1]
‘abc’ ‘abc’
>>> li = [“abc”, 34, 4.34, 23] Negative index: count from right, starting with –1
>>> li[1] # Second item in the list.
34 >>> t[-3]
>>> st = “Hello World” 4.56
>>> st[1] # Second character in string.
‘e’

>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’) >>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)

• Returns copy of the container with a subset of • Omit 1st index to make a copy starting from
the original members. Start copying at the first the beginning of container
index, and stop copying before the second >>> t[:2]
index. (23, ‘abc’)
>>> t[1:4] • Omit 2nd index to make a copy starting at 1st
(‘abc’, 4.56, (2,3)) index and going to end of the container
• You can also use negative indices >>> t[2:]
>>> t[1:-1] (4.56, (2,3), ‘def’)
(‘abc’, 4.56, (2,3))

6
>>> l1 = l2 = ['a','b','c'] >>> l1 = ['a','b','c']
• [ : ] makes a copy of an entire sequence
>>> l1 >>> l2 = l1[:]
>>> t[:] ['a', 'b', 'c'] >>> l1
(23, ‘abc’, 4.56, (2,3), ‘def’) >>> l2 ['a', 'b', 'c']
• Note the difference between these two lines ['a', 'b', 'c'] >>> l2
for mutable sequences >>> l1[1] = 'x' ['a', 'b', 'c']
>>> l2 = l1 # Both refer to same ref, >>> l1 >>> l1[1] = 'x'
# changing one affects both ['a', 'x', 'c'] >>> l1
>>> l2 = l1[:] # Independent copies, two >>> l2 ['a', 'x', 'c']
refs ['a', 'x', 'c'] >>> l2
>>> ['a', 'b', 'c']
>>>

• Boolean test whether a value is inside a container: • The + operator produces a new tuple, list, or
>>> t = [1, 2, 4, 5]
>>> 3 in t string whose value is the concatenation of its
False arguments.
>>> 4 in t
True
>>> 4 not in t
False
>>> (1, 2, 3) + (4, 5, 6)
• For strings, tests for substrings (1, 2, 3, 4, 5, 6)
>>> a = 'abcde'
>>> 'c' in a
True >>> [1, 2, 3] + [4, 5, 6]
>>> 'cd' in a [1, 2, 3, 4, 5, 6]
True
>>> 'ac' in a
False
>>> “Hello” + “ ” + “World”
• Be careful: the in keyword is also used in the syntax ‘Hello World’
of for loops and list comprehensions

7
• 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’

>>> t = (23, ‘abc’, 4.56, (2,3), ‘def’)


>>> li = [‘abc’, 23, 4.34, 23] >>> t[2] = 3.14
>>> li[1] = 45 Traceback (most recent call last):
File "<pyshell#75>", line 1, in -toplevel-
>>> li tu[2] = 3.14
TypeError: object doesn't support item assignment
[‘abc’, 45, 4.34, 23]
• You can’t change a tuple.
• We can change lists in place. • You can make a fresh tuple and assign its
• Name li still points to the same memory reference to a previously used name.
>>> t = (23, ‘abc’, 3.14, (2,3), ‘def’)
reference when we’re done.
• The immutability of tuples means they’re faster
than lists

8
• + creates a fresh list with a new memory ref
>>> li = [1, 11, 3, 4, 5] • extend operates on list li in place.
>>> li.extend([9, 8, 7])
>>> li.append(‘a’) # Note the method syntax >>> li
>>> li [1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7]
[1, 11, 3, 4, 5, ‘a’]
• Potentially confusing:
• extend takes a list as an argument.
>>> li.insert(2, ‘i’) • append takes a singleton as an argument.
>>>li >>> li.append([10, 11, 12])
[1, 11, ‘i’, 3, 4, 5, ‘a’] >>> li
[1, 2, ‘i’, 3, 4, 5, ‘a’, 9, 8, 7, [10,
11, 12]]

• Lists have many methods, including index, >>> li = [5, 2, 6, 8]

count, remove, reverse, sort >>> li.reverse() # reverse the list *in place*
>>> li = [‘a’, ‘b’, ‘c’, ‘b’] >>> li
>>> li.index(‘b’) # index of 1st occurrence [8, 6, 2, 5]

1 >>> li.sort() # sort the list *in place*


>>> li.count(‘b’) # number of occurrences >>> li
2 [2, 5, 6, 8]

>>> li.remove(‘b’) # remove 1st occurrence


>>> li.sort(some_function)
>>> li # sort in place using user-defined comparison
[‘a’, ‘c’, ‘b’]

9
• The comma is the tuple creation operator, not parens • Lists slower but more powerful than tuples
>>> 1,
(1,) • Lists can be modified, and they have lots of
• Python shows parens for clarity (best practice) handy operations and mehtods
>>> (1,)
(1,)
• Tuples are immutable and have fewer
• Don't forget the comma! features
>>> (1) • To convert between tuples and lists use the
1
list() and tuple() functions:
• Trailing comma only required for singletons others
• Empty tuples have a special syntactic form li = list(tu)
>>> () tu = tuple(li)
()
>>> tuple()
()

• Assignment manipulates references


—x = y does not make a copy of the object y
references
—x = y makes x reference the object y references
• Very useful; but beware!, e.g.
>>> a = [1, 2, 3] # a now references the list [1, 2, 3]
>>> b = a # b now references what a references
>>> a.append(4) # this changes the list a references
>>> print b # if we print what b references,
[1, 2, 3, 4] # SURPRISE! It has changed…
• Why?

10
Understanding Reference Semantics
• There’s a lot going on with x = 3 • The data 3 we created is of type integer –
• An integer 3 is created and stored in memory objects are typed, variables are not
• A name x is created • In Python, the datatypes integer, float, and
• An reference to the memory location storing string (and tuple) are “immutable”
the 3 is then assigned to the name x • This doesn’t mean we can’t change the value
• So: When we say that the value of x is 3, we of x, i.e. change what x refers to …
mean that x now refers to the integer 3 • For example, we could increment x:
>>> x = 3
Name: x Type: Integer
Ref: <address1> Data: 3 >>> x = x + 1
>>> print x
name list memory 4

Understanding Reference Semantics

Type: Integer Type: Integer


Name: x Data: 3 Name: x Data: 3
Ref: <address1> Ref: <address1>
Type: Integer
Data: 4

>>> x = x + 1 >>> x = x + 1

11
So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3
3

Type: Integer
Name: x Data: 3
Ref: <address1>
Type: Integer
Data: 4

>>> x = x + 1

So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3

Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y
Ref: <address2>

12
So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3

Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y Name: y
Type: Integer Type: Integer
Ref: <address2> Ref: <address2>
Data: 4 Data: 4

So, for simple built-in datatypes (integers, floats, So, for simple built-in datatypes (integers, floats,
strings) assignment behaves as expected strings) assignment behaves as expected
>>> x = 3 # Creates 3, name x refers to 3 >>> x = 3 # Creates 3, name x refers to 3
>>> y = x # Creates name y, refers to 3 >>> y = x # Creates name y, refers to 3
>>> y = 4 # Creates ref for 4. Changes y >>> y = 4 # Creates ref for 4. Changes y
>>> print x # No effect on x, still ref 3 >>> print x # No effect on x, still ref 3
3 3

Name: x Name: x
Ref: <address1> Type: Integer Ref: <address1> Type: Integer
Data: 3 Data: 3
Name: y Name: y
Type: Integer Type: Integer
Ref: <address2> Ref: <address2>
Data: 4 Data: 4

13
For other data types (lists, dictionaries, user-defined
types), assignment work the same, but some a = [1, 2, 3] a 1 2 3
methods change the objects
• These datatypes are “mutable”
• Change occur in place a
• We don’t copy them to a new memory address each time
• If we type y=x, then modify y, both x and y are changed b=a 1 2 3
b
immutable
>>> x = 3 mutable
x = some mutable object
>>> y = x y = x a
>>> y = 4 make a change to y
>>> print x look at x a.append(4) 1 2 3 4
3 x will be changed as well b

Surprising example surprising no more

• Python uses a simple reference


So now, here’s our code: semantics much like Scheme or Java

>>> a = [1, 2, 3] # a now references the list [1, 2, 3]


>>> b = a # b now references what a references
>>> a.append(4) # this changes the list a references
>>> print b # if we print what b references,
[1, 2, 3, 4] # SURPRISE! It has changed…

14

You might also like