This is about forming grammatically correct sentences. {{ .Site.Title }}
This is about forming grammatically correct sentences. {{ .Site.Title }}
Harry Sejal love. ← This is not even a well-formed sentence.
This is about forming grammatically correct sentences. {{ .Site.Title }}
Harry Sejal love. ← This is not even a well-formed sentence.
This is about creating meaningful sentences.
This is about forming grammatically correct sentences. {{ .Site.Title }}
Harry Sejal love. ← This is not even a well-formed sentence.
This is about creating meaningful sentences.
Harry subtracts Sejal. ← This is a well-formed sentence that is not meaningful.
This is about forming grammatically correct sentences. {{ .Site.Title }}
Harry Sejal love. ← This is not even a well-formed sentence.
This is about creating meaningful sentences.
Harry subtracts Sejal. ← This is a well-formed sentence that is not meaningful.
This is about making sure that the meaning matches with your intent.
This is about forming grammatically correct sentences. {{ .Site.Title }}
Harry Sejal love. ← This is not even a well-formed sentence.
This is about creating meaningful sentences.
Harry subtracts Sejal. ← This is a well-formed sentence that is not meaningful.
This is about making sure that the meaning matches with your intent.
Harry killed Sejal. ← This is meaningful but wrong!
statement_1def function1(...,...): ...statement_2statement_3def function2(...,...): ...def function3(...,...): ...statement_4...
A collection of statements and functions.
statement_1def function1(...,...): ...statement_2statement_3def function2(...,...): ...def function3(...,...): ...statement_4...
A collection of statements and functions.
Are not required to appear in any particular order, except that a function must be defined before it is called.
The left hand side is a name (aka variable).
The right hand side is an expression.
i = 5j = 2*ij = 5*jcourse_name = "Computing"course_code = "ES112"
Operations in the expression depend on the types of values being combined.
The left hand side is a name (aka variable).
The right hand side is an expression.
i = 5j = 2*ij = 5*jcourse_name = "Computing"course_code = "ES112"
Operations in the expression depend on the types of values being combined.
Eg., Arithmetic operations can be performed on numeric values. Strings can be concatenated.
Integers (type: int)
Floating Point (type: float)
Integers (type: int)
Floating Point (type: float)
Floating point numbers break up into the mantissa and the exponent.
Internally, a value is stored as a finite sequence of zeroes and ones.
Normal arithmetic operations:
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
Python knows that floats generalize ints.
In particular, Python allows you to mix ints and floats.
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
Python knows that floats generalize ints.
In particular, Python allows you to mix ints and floats.
Division always produces floats, even if you are dividing two ints.
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
Python knows that floats generalize ints.
In particular, Python allows you to mix ints and floats.
Division always produces floats, even if you are dividing two ints.
Addition and multiplication works on numbers and sequences.
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
Python knows that floats generalize ints.
In particular, Python allows you to mix ints and floats.
Division always produces floats, even if you are dividing two ints.
Addition and multiplication works on numbers and sequences.
Integer division floors the result, instead of rounding towards zero.*
* Even when negative numbers are divided. Here's why.
Normal arithmetic operations:
addition (+), subtraction (-), multiplication (*), division (/)
modulus or remainder (%), integer division or quotient (//)
Exponentiation (**)
>>> y = 7//3>>> print(y)2>>> type(y)<class 'int'>
>>> x = 7/3>>> print(x)2.3333333333333335>>> type(x)<class 'float'>
Fancy arithmetic operations:
log(), sqrt(), sin(), ...
Built in, but not available by default.
import math
>>> help(math.log)Help on built-in function log in module math:log(...) log(x[, base]) Return the logarithm of x to the given base. If the base not specified, returns the natural logarithm (base e) of x.
Fancy arithmetic operations:
log(), sqrt(), sin(), ...
Built in, but not available by default.
import math
>>> help(math.log)Help on built-in function log in module math:log(...) log(x[, base]) Return the logarithm of x to the given base. If the base not specified, returns the natural logarithm (base e) of x.
>>> math.log(2**5,2)5.0>>> int(math.log(2**5,2))5
Types determine what operations make sense.
Types determine what operations make sense.
Types determine what operations make sense.
Types determine what operations make sense.
In many languages, names are declared along with their type.
Python takes the responsibility of making a best guess.
Types determine what operations make sense.
In many languages, names are declared along with their type.
Python takes the responsibility of making a best guess.
Types determine what operations make sense.
In many languages, names are declared along with their type.
Python takes the responsibility of making a best guess.
It is possible for names to be assigned values of different types as the program evolves.
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
j = i/7 #j is float, creats a float
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
j = i/7 #j is float, creats a float
...
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
j = i/7 #j is float, creats a float
...
i = 2*j #i is now a float! ☠
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
j = i/7 #j is float, creats a float
...
i = 2*j #i is now a float! ☠
This is not recommended: it is confusing and leads to debugging nightmares.
It is possible for names to be assigned values of different types as the program evolves.
i = 5 #i is an int
i = 234**2 #still an int...
j = i/7 #j is float, creats a float
...
i = 2*j #i is now a float! ☠
This is not recommended: it is confusing and leads to debugging nightmares.
Use consistent naming conventions.
True, False
>>> type(True)<class 'bool'>
>>> type(False)<class 'bool'>
True, False
>>> type(True)<class 'bool'>
>>> type(False)<class 'bool'>
>>> not TrueFalse>>> not FalseTrue
True, False
>>> type(True)<class 'bool'>
>>> type(False)<class 'bool'>
>>> True and TrueTrue>>> True and FalseFalse>>> False and TrueFalse>>> False and FalseFalse
True, False
>>> type(True)<class 'bool'>
>>> type(False)<class 'bool'>
>>> True or TrueTrue>>> True or FalseTrue>>> False or TrueTrue>>> False or FalseFalse
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
>>> x == y
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
>>> x == y
True
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
>>> x == y
True
>>> type(x) == type(y)
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
>>> x == y
True
>>> type(x) == type(y)
False
Equality (==), Not Equals (!=)
Greater than or equal to (>=), Less than or equal to (<=)
Greater Than (>), Less than (<)
>>> x = 3>>> y = 3.0
>>> x == y
True
>>> type(x) == type(y)
False
>>> True < FalseFalse
The expression:
has two possible interpretations:
Similarly,
has two possible interpretations:
help('**')
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
help('**')
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
For int operands, the result has the same type as the operands unless the second argument is negative. In that case, all arguments are converted to float and a float result is delivered.
help('**')
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
For int operands, the result has the same type as the operands unless the second argument is negative. In that case, all arguments are converted to float and a float result is delivered.
For example, "10**2" returns "100", but "10**-2" returns "0.01".
help('**')
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
For int operands, the result has the same type as the operands unless the second argument is negative. In that case, all arguments are converted to float and a float result is delivered.
For example, "10**2" returns "100", but "10**-2" returns "0.01".
Raising "0.0" to a negative power results in a "ZeroDivisionError".
help('**')
The power operator binds more tightly than unary operators on its left; it binds less tightly than unary operators on its right.
For int operands, the result has the same type as the operands unless the second argument is negative. In that case, all arguments are converted to float and a float result is delivered.
For example, "10**2" returns "100", but "10**-2" returns "0.01".
Raising "0.0" to a negative power results in a "ZeroDivisionError".
Raising a negative number to a fractional power results in a "complex" number. (In earlier versions it raised a "ValueError".)
>>> (4**12)/2
>>> (4**12)/2
8388608.0
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
8388608.0
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
8388608.0
>>> 10**-2
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
8388608.0
>>> 10**-2
0.01
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
8388608.0
>>> 10**-2
0.01
>>> -10**2
>>> (4**12)/2
8388608.0
>>> 4**(12/2)
4096.0
>>> 4**12/2
8388608.0
>>> 10**-2
0.01
>>> -10**2
-100
if a == 3 or b == 3 or c == 3:
V/S.
if a or b or c == 3:
if a == 3 or b == 3 or c == 3:
V/S.
if a or b or c == 3:
Since OR has a lower precedence than ==, the second statement will be evaluated as:
if a == 3 or b == 3 or c == 3:
V/S.
if a or b or c == 3:
Since OR has a lower precedence than ==, the second statement will be evaluated as:
which is very different from:
if a == 3 or b == 3 or c == 3:
V/S.
if a or b or c == 3:
Since OR has a lower precedence than ==, the second statement will be evaluated as:
which is very different from:
💀 Note that there are values of a,b,c for which X will evaluate to True while Y will evaluate to False.
if – else |
Conditional expression |
or |
Boolean OR |
and |
Boolean AND |
not x |
Boolean NOT |
in , not in ,
is , is not , < ,
<= , > , >= , != , == |
Comparisons, including membership tests and identity tests |
| |
Bitwise OR |
^ |
Bitwise XOR |
& |
Bitwise AND |
<< , >> |
Shifts |
+ , - |
Addition and subtraction |
* , @ , / , // , % |
Multiplication, matrix multiplication division, remainder [5] |
+x , -x , ~x |
Positive, negative, bitwise NOT |
** |
Exponentiation [6] |
Computation is a lot more than number crunching.
Text processing is increasingly important.
Computation is a lot more than number crunching.
Text processing is increasingly important.
Document preparation
Importing/exporting spreadsheet data.
Maching queries to content.
Computation is a lot more than number crunching.
Text processing is increasingly important.
Document preparation
Importing/exporting spreadsheet data.
Maching queries to content.
Computation is a lot more than number crunching.
Text processing is increasingly important.
Document preparation
Importing/exporting spreadsheet data.
Maching queries to content.
Computation is a lot more than number crunching.
Text processing is increasingly important.
Document preparation
Importing/exporting spreadsheet data.
Maching queries to content.
Type string str, a sequence of characters.
A single character is a string of length one.
No separate char type.
s = "ES112 Computing"
s = "ES112 Computing"
>>> w = 'ES112 'Computing'' File "<stdin>", line 1 w = 'ES112 'Computing'' ^SyntaxError: invalid syntax
s = "ES112 Computing"
>>> w = 'ES112 'Computing'' File "<stdin>", line 1 w = 'ES112 'Computing'' ^SyntaxError: invalid syntax
w = 'ES112 \'Computing\''x = "ES112 'Computing'"
s = "ES112 Computing"
>>> w = 'ES112 'Computing'' File "<stdin>", line 1 w = 'ES112 'Computing'' ^SyntaxError: invalid syntax
w = 'ES112 \'Computing\''x = "ES112 'Computing'"
>>> y = "Students say that "ES112 Computing" is their favorite course."
SyntaxError: invalid syntax
s = "ES112 Computing"
>>> w = 'ES112 'Computing'' File "<stdin>", line 1 w = 'ES112 'Computing'' ^SyntaxError: invalid syntax
w = 'ES112 \'Computing\''x = "ES112 'Computing'"
>>> y = "Students say that "ES112 Computing" is their favorite course."
SyntaxError: invalid syntax
z = '''Students say that "ES112 Computing" is their favorite course.'''
Strings in Python are a sequence or list of characters.
Positions 0, 1, ..., n-1 for a string of length n
Strings in Python are a sequence or list of characters.
Positions 0, 1, ..., n-1 for a string of length n
Positions -1, -2, ... count backwards from the end.
>>> s = "hello world"
>>> s = "hello world"
>>> s[0]
>>> s = "hello world"
>>> s[0]
'h'
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
11
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
11
>>> s[0:len(s)]
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
11
>>> s[0:len(s)]
'hello world'
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
11
>>> s[0:len(s)]
'hello world'
>>> s[3:7]
>>> s = "hello world"
>>> s[0]
'h'
>>> s[-1]
'd'
>>> len(s)
11
>>> s[0:len(s)]
'hello world'
>>> s[3:7]
'lo w'
For strings s and w, s + w is sw.
For strings s and w, s + w is sw.
A slice is a segment of a string.
For strings s and w, s + w is sw.
A slice is a segment of a string.
s[i:j] starts at s[i] and ends at s[j-1].
For strings s and w, s + w is sw.
A slice is a segment of a string.
s[i:j] starts at s[i] and ends at s[j-1].
>>> s = "hello">>> w = "world">>> s + w
For strings s and w, s + w is sw.
A slice is a segment of a string.
s[i:j] starts at s[i] and ends at s[j-1].
>>> s = "hello">>> w = "world">>> s + w
'helloworld'
For strings s and w, s + w is sw.
A slice is a segment of a string.
s[i:j] starts at s[i] and ends at s[j-1].
>>> s = "hello">>> w = "world">>> s + w
'helloworld'
>>> s + ' ' + w
For strings s and w, s + w is sw.
A slice is a segment of a string.
s[i:j] starts at s[i] and ends at s[j-1].
>>> s = "hello">>> w = "world">>> s + w
'helloworld'
>>> s + ' ' + w
'hello world'
>>> x = "hello world">>> x[0:5] + x[5:]
>>> x = "hello world">>> x[0:5] + x[5:]
'hello world'
>>> x = "hello world">>> x[0:5] + x[5:0]
>>> x = "hello world">>> x[0:5] + x[5:]
'hello world'
>>> x = "hello world">>> x[0:5] + x[5:0]
'hello worl'
>>> x = "hello world">>> x[0:5] + x[5:]
'hello world'
>>> x = "hello world">>> x[0:5] + x[5:0]
'hello worl'
>>> x = "hello world">>> x[5]
>>> x = "hello world">>> x[0:5] + x[5:]
'hello world'
>>> x = "hello world">>> x[0:5] + x[5:0]
'hello worl'
>>> x = "hello world">>> x[5]
' '
This is about forming grammatically correct sentences. {{ .Site.Title }}
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
t | Restart the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |