DataTypes

Primitive Types

  • Numbers
    • int - integers which are whole numbers such as 3
    • float - floats which are numbers that have a decimal place such as -3.4
  • Logical
    • bool - booleans are either True or False
  • Text Sequence
    • str - strings are a sequence of characters such as 'bananas'

More complex Types (These are discussed later in the course)

  • Sequence Types
    • list
    • tuple
    • range
    • ...
  • Sets
  • Dictionaries
  • Functions
  • Classes/User Defined Types
  • ...
print (type(3), 3) # integer
print (type(2.5), 2.5) # float
print (type('apples'), 'apples') # string
print (type(True), True) # boolean
print (type('3'), '3') # string
print (type('2.5'), '2.5') # string

In [3]:
print (type(3), 3) # integer
print (type(2.5), 2.5) # float
print (type('apples'), 'apples') # string
print (type(True), True) # boolean
print (type('3'), '3') # string
print (type('2.5'), '2.5') # string


<class 'int'> 3
<class 'float'> 2.5
<class 'str'> apples
<class 'bool'> True
<class 'str'> 3
<class 'str'> 2.5

Pound sign (#) Hash to some, number sign to others

The # symbol allows you to write notes in your program. These are called comments.

print (3) # this is a comment

Operations

Each datatype has a number of operations that are valid.

Here are some commmon operators, more will be covered as we progress in the course.

Mathematical Operators ( for numeric types)

  • () - Parenthesis are used to define the order of operation
  • ** - Exponent : xn = x**n
  • * - Multiplacation : 5 * 3
  • / - Division : 15 / 3
  • // - Floor/Integer Division (rounds down to the nearest int) : 7 // 3 = 2
  • + - Addition : 5 + 3
  • - - Subtraction : 3 - 5
  • % - Modulus (returns the remainder of a division) : 16 % 3
print ('1 + 2 = ', 1 + 2) # Addition
print ('2 - 1 = ', 2 - 1) # Subtraction
print ('2 * 5 = ', 2 * 5) # Multiplication
print ('6 / 2 = ', 6 / 2) # Division
print ('7 // 2 = ', 7 // 2) # Floor/Integer division
print ('2 ** 3 = ', 2 ** 3) # Exponent
print ('16 % 3 = ', 16 % 3) # Division
print ('(1 + 3) * (4 + 5) = ', (1 + 3) * (4 + 5)) # parenthesis
print ('1 + 3 * 4 + 5 = ', 1 + 3 * 4 + 5) # PEMDAS

Parenthesis can help make statements less ambigous, this is a good thing


In [18]:
print ('1 + 2 = ', 1 + 2) # Addition
print ('2 - 1 = ', 2 - 1) # Subtraction
print ('2 * 5 = ', 2 * 5) # Multiplication
print ('6 / 2 = ', 6 / 2) # Division
print ('7 // 3.5 = ', 7 // 2) # Floor/Integer division
print ('2 ** 3 = ', 2 ** 3) # Exponent
print ('16 % 3 = ', 16 % 3) # Division
print ('(1 + 3) * (4 + 5) = ', (1 + 3) * (4 + 5)) # parenthesis
print ('1 + 3 * 4 + 5 = ', 1 + 3 * 4 + 5) # PEMDAS
print("there are {} apples".format(5 + 6 // 2))


1 + 2 =  3
2 - 1 =  1
2 * 5 =  10
6 / 2 =  3.0
7 // 3.5 =  3
2 ** 3 =  8
16 % 3 =  1
(1 + 3) * (4 + 5) =  36
1 + 3 * 4 + 5 =  18
there are 8 apples

String Operators

  • + - Addition : 'ban' + 'ana' = 'banana'
  • * - Multiplacation : 'ba' * 5 = 'bababababa'
  • [x:y:z] - Slicing : 'banana'[0:2] = 'ba'
print("'Ban' + 'ana' = ", 'Ban' + 'ana')
print("'Ba' * 5 = ", 'Ba' * 5)

Nesting quotation marks '"' or "'" will treat the quote inside the other quote as being a character


In [6]:
print("'Ban' + 'ana' = ", 'Ban' + 'ana')
print("'Ba' * 5 = ", 'Ba' * 5)


'Ban' + 'ana' =  Banana
'Ba' * 5 =  BaBaBaBaBa

String Operations

Everything in Python is an Object (More on this later)

  • Objects generally have attributes and methods ( more on these later).
  • Methods are just operations that can be applied to the object
  • These methods which are accessed using a '.' character

The following code demonstrates a couple of string operators

print("'Banana'.upper() = ", 'Banana'.upper())
print("'banana'.isupper() = ", 'banana'.isupper())
print("'BANANA'.isupper() = ", 'BANANA'.isupper())
print("'124'.isdigit() = ", '124'.isdigit()) # note that this is a string
print("'Banana'.swapcase() = ", 'Banana'.swapcase())

In [7]:
print("'Banana'.upper() = ", 'Banana'.upper())
print("'banana'.isupper() = ", 'banana'.isupper())
print("'BANANA'.isupper() = ", 'BANANA'.isupper())
print("'124'.isdigit() = ", '124'.isdigit()) # note that this is a string
print("'Banana'.swapcase() = ", 'Banana'.swapcase())


'Banana'.upper() =  BANANA
'banana'.isupper() =  False
'BANANA'.isupper() =  True
'124'.isdigit() =  True
'Banana'.swapcase() =  bANANA

Python Documentation on methods - explain documentation - how to read help

help ('banana'.upper)

In [8]:
help ('banana'.upper)


Help on built-in function upper:

upper(...) method of builtins.str instance
    S.upper() -> str
    
    Return a copy of S converted to uppercase.


Variables

What are they?

Variables allow programs to store data and manipulate it later

int_one = 5
int_two = 10
result = int_one + int_two
print (result)

In [9]:
int_one = 5
int_two = 10
result = int_one + int_two
print (result)


15

Be aware

Assignment happens from right to left

number = 5 + 1
fruit = 'banana'

variable must be assigned before being used

print (unknown_variable)

In [19]:
num1 = "5"
if num1.is_digit():
    num1 = int(num1)

print("5" + 5)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-19-f5894c46b2fc> in <module>()
----> 1 print("5" + 5)

TypeError: must be str, not int

Valid Variable Names

Variables can start with a character or underscore

  • variable ✓
  • Variable ✓
  • _variable ✓

Variables cannot start with a number or special character

  • 1variable ✗
  • !variable ✗
number = 1   # correct
_number = 1  # correct
!number = 1  # incorrect
number 1 = 1 # incorrect
1number = 1  # incorrect

In [13]:
number = 1   # correct
_number = 1  # correct
!number = 1  # incorrect
number 1 = 1 # incorrect
# 1number = 1  # incorrect


  File "<ipython-input-13-8a94eb248c71>", line 4
    number 1 = 1 # incorrect
           ^
SyntaxError: invalid syntax

Naming Convensions

  • b (single lowercase letter)
  • B (single uppercase letter)
  • lowercase
  • UPPERCASE
  • lowercase_with_underscores (this is the prefered method for python)
  • UPPERCASE_WITH_UNDERSCORES
  • CapitalizedWords (also known as CamelCase)
  • mixedCase (differs from CapitilizedWords as they always starts with a lowercase character)
  • Capitalized_Words_With_Underscores

Be aware

capital i (I), lowercase l (L) and the number 1 look the same in some fonts, capital O and 0 also look the same in some fonts

PEP8

This is a style guide which dictates how your code should look.

This helps programs to look similar, which makes them easier to understand for anyone who also uses the styleguide

Here is the link to the PEP8 style guide : https://www.python.org/dev/peps/pep-0008/

Reserved Words

These are words that are used by python and cannot be used as variable names

Reserved Words
and del from not while
as elif global or with
assert else if pass yield
break except import print class
exec in raise continue finally
is return def for lambda
try

This will cause an error

lambda = 5

In [14]:
lambda = 5


  File "<ipython-input-14-af3a722455c9>", line 1
    lambda = 5
           ^
SyntaxError: invalid syntax

Mutability

Some types are immutable.

some functions change the original object, some return a new object

Immutable types

  • str
  • int
  • float

mutable types (more on these later)

  • list
  • dictionary

Immutable example

animal = "dog"
print (id(animal), animal)
animal = "cat"
print (id(animal), animal) #animal is a new object, previous one is deleted

Mutable example

numbers = [1,2,3] # this is a list ( more on this later)
print (id(numbers), numbers)
numbers.append(4)
print (id(numbers), numbers) #numbers object is the same

In [21]:
numbers = [1,2,3] # this is a list ( more on this later)
print (id(numbers), numbers)
numbers.append(4)
print (id(numbers), numbers) #numbers object is the same


4513893256 [1, 2, 3]
4513893256 [1, 2, 3, 4]

String methods do not change the original value. Instead a new string is created by the method

fruit = 'banana'
fruit.upper()
print (fruit) # fruit is still lowercase

The return type of a method is shown by the help function.

help (fruit.upper)

In [26]:
x = 5
y = x + 4
print(x, y)


5 9

In order to use the result of the string operation we need to store it in a variable

fruit = 'banana'
fruit.upper()
print (fruit) # fruit is still lowercase

In [ ]:

Variables can be reused, simply assign the result of the operation to the original variable

fruit = 'banana'
fruit = fruit.upper() # assign fruit to the result of the upper() method
print ('fruit is now', fruit)

In [ ]:


Compound Statements

Control Flow Statements

  • if else statements - branching operation
  • repeated execution (covered later)

Conditional Execution

Boolean expressions

A boolean expressions is an expression that will either evaluate to True or False.

Operator Expression Comparison
== x == y is x equal to y?
!= x != y is x not equal to y?
< x < y is x less than y?
> x > y is x greater than y?
<= x <= y is x less than or equal to y?
>= x >= y is x greater than or equal to y?
is x is y is x, y?
x = 5
y = 10
print (x,'==',y,'\t:', x == y)
print (x,'!=',y,'\t:', x != y)
print (x,'<',y,' \t:', x < y)
print (x,'>',y,' \t:', x > y)
print (x,'<=',y,'\t:', x <= y)
print (x,'>=',y,'\t:', x >= y)
print (x,'is',y,'\t:', x is y)
print ('type(x == y) : \t', type(x==y))

In [31]:
x = 5
y = 10
print (x,'==',y,'\t:', x == y)
print (x,'!=',y,'\t:', x != y)
print (x,'<',y,' \t:', x < y)
print (x,'>',y,' \t:', x > y)
print (x,'<=',y,'\t:', x <= y)
print (x,'>=',y,'\t:', x >= y)
print (x,'is',y,'\t:', x is y)
print ('type(x == y) : \t', type(x==y))


5 == 10 	: False
5 != 10 	: True
5 < 10  	: True
5 > 10  	: False
5 <= 10 	: True
5 >= 10 	: False
5 is 10 	: False
type(x == y) : 	 <class 'bool'>

Logical Operators

There are three logical operators and, or and not.

  • and Operator
x y x and y
False False False
False True False
True False False
True True True
  • or Operator
x y x or y
False False False
False True True
True False True
True True True
  • not Operator
x not x
False True
True False

Example

The following code evaluates 2 boolean expressions

  • whether the number is even
    • n%2 == 0
  • whether the number is positive
    • n > 0

Using the 2 boolean expressions 2 logical expressions are evaluated

  • whether the number is even and positive
    • n%2 == 0 and n > 0
  • whether the number is even or positive
    • n%2 == 0 or n > 0
num = 5

print ('positive_even :', num)
print ('\tBoolean Expressions')
print ('\t\t', num, '% 2 == 0 \t\t\t:', num%2 == 0)
print ('\t\t', num, '> 0 \t\t\t\t:', num > 0)
print ('\tLogical Expressions')
print ('\t\t', num, '% 2 == 0 and',num,'> 0 \t\t:', num%2 == 0 and num > 0)
print ('\t\t', num, '% 2 == 0 or',num,'> 0 \t\t:', num%2 == 0 or num > 0)

In [33]:
num = -6

print ('positive_even :', num)
print ('\tBoolean Expressions')
print ('\t\t', num, '% 2 == 0 \t\t\t:', num%2 == 0)
print ('\t\t', num, '> 0 \t\t\t\t:', num > 0)
print ('\tLogical Expressions')
print ('\t\t', num, '% 2 == 0 and',num,'> 0 \t\t:', num%2 == 0 and num > 0)
print ('\t\t', num, '% 2 == 0 or',num,'> 0 \t\t:', num%2 == 0 or num > 0)


positive_even : -6
	Boolean Expressions
		 -6 % 2 == 0 			: True
		 -6 > 0 				: False
	Logical Expressions
		 -6 % 2 == 0 and -6 > 0 		: False
		 -6 % 2 == 0 or -6 > 0 		: True

Conditional Execution

In order to write usefull programs you need to be able to control which statements are executed. This is achieved using ~if~ statements.

if statements have the following format

if <expression>:
    <statement block>

Note that the statement block is indented. This is how Python seperates code that should be executed by the if statement and code that is executed regardless of the statement.

In the following code statement1 and statement2 are only executed if the expressin is true. statement3 will be executed regardless of the if statement.

if <expression>:
    statement1
    statement2
statement3

A few word on indentation. Python does not mind how your code is indented as long as it is consistant. The accepted convention is to use a single tab or 4 whitespaces. But anything will work as long as you are consistant.

if True:
    print ('Hello') # 4 spaces
    print ('Hello') # 1 tab (this is bad. Use one of the other)

The following code will result in an error as the spaces are inconsistant

if True:
    print ('Hello') # 4 spaces
     print ('Hello') # 5 spaces

In [36]:
if True:
    print ('Hello') # 4 spaces
    print ('Hello') # 5 spaces


Hello
Hello

Alternate execution/Branching

if <expression>:
    <statement_1>
else:
    <statement_2>

Multiple tests (chaining)

if x < y:
    STATEMENTS_A
elif x > y:
    STATEMENTS_B
else:
    STATEMENTS_C

Nested conditionals

if x < y:
    STATEMENTS_A
else:
    if x > y:
        STATEMENTS_B
    else:
        STATEMENTS_C

Example:

disease = True
x = 3
if disease == True:
    if x > 0 and x <= 4:
        print 'give drug A'
    elif x > 7 and x <= 10:
        print 'give drug B'
    else:
        print 'no drug'

In [41]:
disease = True
x = -1
if disease == True:
    if x > 0 and x <= 4:
        print('give drug A')
    elif x > 7 and x <= 10:
        print('give drug B')
    else:
        print('no drug')


no drug

User Input

Allows your programs to be dynamic.

Methods

  • input()
  • command line arguments (covered later)
  • reading from files (covered later)
  • ...

input()

  • This method will create a prompt in the console.
  • The user can then input their data

Usage

variable_to_hold_input = input('message to show user')
print (variable_to_hold_input)

In [45]:
variable_to_hold_input = input('message to show user : ')
print (type(variable_to_hold_input))


message to show user : 5
<class 'str'>

Example:

Lets build a calculator

pseudocode

  • input value1
  • input operator
  • input value2
  • convert value1 and value2 to integers
  • if operator == '+':
    • print string showing expression (value1 + value2 =) and the result
  • elif ... ('-', '*', '/', '**')
    • print expression and result
  • else:
    • print 'unknown operator'

Note

  • no error handling
    • does not check whether value1 and value2 are numbers before converting

In [ ]: