Binary operators
+ - * / work with two numbers one on either side of the symbol
these numbers are called operands
Unary operators
+ and – can be used with just one number. It will change the number from positive to negative and vice versa.
Syntax error
occurs when you type something which is not properly formed, according to the rules of the programming language.
Printing a string
print(‘text’) can also use double quotation marks
string + operation
called concatenation, adds two strings together
print(‘Hi’ + ‘Hi’ + ‘Hi’)
print(‘Hi’ * 3)
assignment operator
The = symbol is called the assignment operator, which assigns the value of the expression on the right to the variable on the left.
python reads it from right to left
input() function
get keyboard input from the user as a string.
always returns a string
statement
A statement is any valid line of Python code. Think of it like a sentence in a book.
expression
a statement which evaluates to some value.
All code involving numerical operators which we have seen so far are expressions, since they evaluate to the solution of the mathematical operation they’re performing. 9 + 11 and 3.0 / 4 are expressions. input(“Enter a number: “) is also an expression as it will evaluate to the string input by the user.
All expressions are statements, but not all statements are expressions. Only statements which evaluate to some value are expressions. (2 + 3) * (5 - 7) is both an expression and a statement.
int(…)
for positive and negative whole numbers, integers
float(..)
for real numbers such as -3.0, 0.5 or 3.14159, 1e3
list[…]
A more powerful way of storing lists of objects,
eg: [1, 3, 4] or [1.0, “hello”, “frank”]
print(type(…))
Gives the data type after the word class.
number_c = 7
print(type(number_c))
<class ‘int’>
int(*,+,-)int
int
int/int
float
float(*,+,-,/)float
float
float(*,+,-,/)int
float
int(*,+,-,/)float
floatt
type casting
converting one data type into another data type
print(int(32.7))
32
print(int(“32.7”))
this fails because decimal point is not a number/digit
modulo operator
% computes the remainder of an integer division
10 % 4
= 2
do long division
Sequence
A sequence is like a numbered list of objects, kind of like a shopping list. All sequences have a specific ordering and can contain any number of things.
The main sequence data types we will deal with in this course are:
strings, which we have been using since worksheet 1;
lists; and
tuples
indexing
To access a character at a particular position, we can specify its position number inside square brackets. This technique is called indexing or subscripting the string. The position we specify inside the square brackets is called the index. Here is an example:
s = “The number is 42.”
print(s[0])
T
counting from -> it starts at 0
counting from <- it starts at -1
len()
finding the largest valid index in a string (it would be one less than the length of the string cuz it starts counting at 0)
print(len(“Hello”))
5
or
s = “The number is 42.”
n = len(s)
print(n)
17
slicing
extracting a substring of arbitrary length
nameofvariable[start:end(but not include):step]
word[::-1]
it will inverse the entire variable