An Informal Introduction to Python
Published:
This lesson covers An Informal Introduction to Python 3.10.5, https://docs.python.org/3/tutorial/introduction.html
conda update anaconda
conda create -n “py310” python=3.10.5 ipython –channel conda-forge
# this is the first comment
spam = 1 # and this is the second comment
# ... and now a third!
text = "# This is not a comment because it's inside quotes."
Introduction
Using Python as a Calculator
Numbers
x = 2 + 2
print(x) #4
x = 50 - 5*6
print(x) #20
x = (50 - 5*6) / 4
print(x) #5.0
x = 8 / 5 # division always returns a floating point number
print(x) #1.6
x = 17 // 3 # floor division discards the fractional part
print(x) #5
x = 17 % 3 # remainder of the division
print(x) #2
x = 5 ** 2 # 5 squared
print(x) #25
x = 2 ** 7 # 2 to the power of 7
print(x) #128
tax = 12.5 / 100
price = 100.50
price += price * tax
price # 113.0625
round(_, 2) # 113.06 # new cell #last printed expression assigned to variable _ as read-only
Strings
x = 'no quote' # single quotes
print(x)
x = "no quote" # double quotes
print(x)
x = 'doesn\'t' # use \' to escape the single quote...
print(x) # "doesn't"
x = "doesn't" # ...or use double quotes instead
print(x) # "doesn't"
x = '"Yes," they said.'
print(x) # '"Yes," they said.'
x = "\"Yes,\" they said."
print(x) # '"Yes," they said.'
x = '"Isn\'t," they said.'
print(x) # '"Isn\'t," they said.'
x = 'First line.\nSecond line.' # \n means newline
print(x) # with print(), \n produces a new line else \n gets added
x = 'c:\some\name' # \n means newline even in path
print(x)
x = r'c:\some\name' # r (raw string)
print(r'C:\some\name') #C:\some\name # note the r (raw string) before the quote
x = """\
Usage: thingy [OPTIONS]
-h Display this usage message
-H hostname Hostname to connect to
""" # / to not include newline which is default included
print(x)
x = 2 * "Hello " + "Python" # concatenation
print(x) # Hello Hello Python
x = 2 * "Hello " "Python " # auto concatenation
print(x) # Hello Python Hello Python
text = ('Put several strings within parentheses '
'to have them joined together.'
)
print(text) # 'Put several strings within parentheses to have them joined together.'
prefix = 'Py'
# text = prefix 'thon' # can't concatenate a variable and a string literal without +
# print(text)
prefix = 'Py'
text = prefix + 'thon' # concatenate a variable and a string literal with +
# print(text)
word = 'Python' # Sting can be indexing/subscripted
print(word[0])
print(word[-1])
print(word[0:2])
# print(word[42]) # error since the word only has 6 characters
print(word[4:42]) # on # slices handled gracefully
print(word[42:]) # ""
# word[0] = "J" # Immutable
word = "J" + word[1:]
print(word) # Jython
print(len(word)) # 6 #length
Lists
print(squares[0]) # 1 # indexing
print(squares[-1]) # 25 # indexing
# slicing returns a new list
print(squares[-3:]) # [9, 16, 25]
# references objects that contain other objects
print(squares[:]) # shallow copy
# Assignment statements in Python do not copy objects
# they create bindings between a target and an object
# shallow or deep relevant for compound objects
s1 = [[1], [4], [9]]
s2 = s1[:]
s1[0][0] = 11 # Mutable
print(s2) # [[11], [4], [9]]
s1 = [[1], [4], [9]]
s2 = s1[:]
s2[0][0] = 11 # Mutable
print(s1) # [[11], [4], [9]]
s1 = [1, 4, 9]
s2 = s1[:]
s1[0] = [11] # Mutable
print(s2) # [1, 4, 9] # not a compound object
import copy
s1 = [[1], [4], [9]]
s2 = copy.copy(s1) # shallow copy
s1[0][0] = 11
print(s2) # [[11], [4], [9]]
import copy
s1 = [[1], [4], [9]]
s2 = copy.deepcopy(s1) # deep copy
s1[0][0] = 11
print(s2) # [[1], [4], [9]]
cubes = [1, 8, 27, 65, 125]
cubes[3] = 4 ** 3 # Mutable
print(cubes)
cubes.append(6 ** 3)
print(cubes)
# Assignment to slices
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters[3:] = []
print(letters) # ['a', 'b', 'c']
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
letters[3:] = ['D', 'E', 'F', 'G']
print(letters) # ['a', 'b', 'c', 'D', 'E', 'F', 'G']
letters[:] = [] # Clear
print(letters) # []
letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
print(len(letters)) # 7
# Nested Lists
letters = [['a', 'b', 'c'], ['d', 'e', 'f', 'g']]
print(len(letters)) # 2
First Steps Towards Programming
# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1 # Multiple Assignment
while a < 10: # Loop
print(a) # Indention
a, b = b, a+b
# Fibonacci series:
# the sum of two elements defines the next
a, b = 0, 1 # Multiple Assignment
while a < 10: # Loop
print(a, end=",") # Indention
a, b = b, a+b
Mutable and Immutable
- Mutable: the internal state of the object can be changed/mutated
- Lists; Sets; Dictionaries; User-Defined Classes (depends on definition)
- Immutable: the internal state of the object cannot be mutated
- Numbers (Integer, Rational, Float, Decimal, Complex & Booleans); Strings; Tuples; Frozen Sets; User-Defined Classes (depends on definition)
- Objects: everything in Python is an object with three attributes
- Identity: address that the object refers to in memory
- Type: kind e.g. integer, list, string etc.
- Value: value stored by the object e.g. list_nums = [1,2,3]
# id - keeps changing at run - assigned when created
# hexa - memory address in hexadecimal
alphabets = ["A", "B", "C"] # List Mutable
print(alphabets, id(alphabets), hex(id(alphabets)))
alphabets.append("D")
# same object updated - Mutable
print(alphabets, id(alphabets), hex(id(alphabets)))
print()
alphabets = ("A", "B", "C") # Tuple Immutable
print(alphabets, id(alphabets), hex(id(alphabets)))
alphabets += tuple("D")
# new object created - Immutable
print(alphabets, id(alphabets), hex(id(alphabets)))