0% found this document useful (0 votes)
10 views5 pages

Lecture 1

This document provides an overview of key concepts in Python including: - Variables are assigned values using the equality symbol and names inherit the type of the assigned value. - Numeric types include integers and floats which have different representations and manipulations. - Collections like lists allow storing multiple values and individual items can be accessed by index. - Arithmetic, comparison, and boolean operators can be used to perform calculations and logical evaluations on values. - Standard math functions are available after importing the math library. - Data types determine valid operations and names should not be reassigned different types of values.

Uploaded by

Noor Aldeen
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)
10 views5 pages

Lecture 1

This document provides an overview of key concepts in Python including: - Variables are assigned values using the equality symbol and names inherit the type of the assigned value. - Numeric types include integers and floats which have different representations and manipulations. - Collections like lists allow storing multiple values and individual items can be accessed by index. - Arithmetic, comparison, and boolean operators can be used to perform calculations and logical evaluations on values. - Standard math functions are available after importing the math library. - Data types determine valid operations and names should not be reassigned different types of values.

Uploaded by

Noor Aldeen
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/ 5

Lecture1

September 11, 2022

0.0.1 Values and names


• Equality symbol assigns a value: name = value
• Names are usually referred to as variables
[1]: x= 5

[2]: x

[2]: 5

• RHS can be an expression, variable=expression


• expression can use previously assigned names
[3]: y=x+5
z=y-x+1

[4]: x,y,z

[4]: (5, 10, 6)

• Using uninitialized names on RHS generates error


[5]: y=x+b

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 y=x+b

NameError: name 'b' is not defined

• Mistyping a name can raise problems that are difficult to debug


[ ]: width = 5
witdh = 10

[ ]: width

1
0.0.2 Names, values and types
Python keeps track of names dynamically - Values are assigned as they come - Values have types,
names inherit their type from the value
Type specifies - Domain of values that can be used - Set of valid operators that are available in
expressions

0.0.3 Numeric types


Numbers can be int or float - int stands for integers - float stands for “floating point” = reals
- The decimal point “floats” - An integer is essentially a base 2 representation of the value - A
float is two integers: the value with a fixed decimal point and the exponent telling you how much
to shift the decimal point (recall scientific notatation, 6.02 × 1023 ) - Representation, and hence
manipulation, of these types of values is different
[ ]: x = 5.0
y = 5
type(x), type(y)

0.0.4 Boolean type


• if m < n …
• True and False are values of type bool
• Expressions are made up of and, or, not

0.0.5 Collections of values


List
• Sequence of values
• [1,2,3,4] is a list of integers
• Python allows mixed lists [1,3.5,True]
[ ]: mylist = [1,3.5,True]

[ ]: mylist

Extract an individual item from the list, by position - Positions are numbered from 0 - mylist =
[1,3.5,True] - mylist[1] is the value at position 1, second from start, which is 3.5
[ ]: mylist[0],mylist[1],mylist[2]

• Illegal to access a position outside valid range


• Use len(l) to get the length of a list l
• Valid positions are 0,1,2,..., len(l)-1
[ ]: len(mylist)

[ ]: x = [5.0]
len(x)

2
• Also index from right as -1,-2,...,-len(l)
• Note asymmetry between 0,1,...,len(l)-1 and -1,-2,...,-len(l)
[ ]: mylist = [0,1,2,3,4,5,6]
mylist[6],mylist[-1]

0.0.6 Arithmetic operators


• Arithmetic: + , - , *
• Division?: written / – what is 7/3?
• Normal division / always produces a float
• “integer division” // which (for int arguments) produces an int = quotient
• remainder: %
• Given two integers m,n, m = (m//n)*n + (m%n)
• Exponentiation: m ** n (in some languages m^n)
• In general, if the answer requires a float, the type will be automatically “upgraded”
[ ]: 7 // 3, 7/3, 7%3, 7.1//2.01, 8/4, 8//4, 2**3, 2**0.5

[ ]: (7/3)*3

[ ]: type(2**3), type(2.0**3), type(4**0.5)

0.1 Shortcuts for booleans


• A numeric value of 0 is False
• An empty list is False
• An empty string is False
• Anything that is not False is True

0.1.1 Mixing types and expressions


• Results can be unpredictable
[ ]: y

[ ]: 7 and y, not(7 and y)

[ ]: True + False

0.1.2 Standard math functions


• Need to import the math library
[ ]: sqrt(2), sin(pi/2)

• Import all functions from math, qualify function names as math.xyz()

3
[ ]: import math
math.sqrt(2), math.sin(math.pi/2)

• Import all functions from math without requiring qualification


[ ]: from math import *

[ ]: log(2.71818)

• Import function names with qualification, but introduce an alternative (shorter) name for the
library, quite commonly used practice as we shall see
[ ]: import math as mt

[ ]: mt.pi

0.1.3 No serious limit on size of integers

[ ]: x = 2**84 # Won't fit in 64 bits


y = 3**91
x*y

0.1.4 Comparisons
• Comparison: m < n, a > b
• With equality: m <= n, b >= a
• Not equal? m != b
• Equality? Cannot be m = n, so m == n
• All return a value of type ‘bool
[ ]: a = 7
b = 8
c = 9

[ ]: a < 7, a < b, c >= b, a != b, b == 8

0.1.5 Boolean operations


• Combine values using and, or, not
[ ]: a < b and b < c, a > b or b < c, a > b or b > c, not(b > c)

0.2 Data type


• Determines what operations are allowed
• len(x) does not make sense if value of x is not a list
• Names inherit their type from the values they currently hold
– Not a good practice to use the same name for different types of values

4
[ ]:

You might also like