5. Datatypes
● There are two main number types we will work
with:
○ Integers which are whole numbers.
○ Floating Point numbers which are numbers
with a decimal.
8. Variables
● We use the = to assign values to a variable
● For example:
○ my_dogs = 2
9. Variables
● Rules for variable names
○ Names can not start with a number.
○ There can be no spaces in the name, use _
instead.
○ Can't use any of these symbols
:'",<>/?|()!@#$%^&*~-+
10. Variables
● Rules for variable names
○ It's considered best practice that names are
lowercase.
○ Avoid using words that have special meaning
in Python like "list" and "str"
11. Variables
● Python uses Dynamic Typing
● This means you can reassign variables to
different data types.
● This makes Python very flexible in assigning data
types, this is different than other languages that
are “Statically-Typed”
15. Variables
● Pros of Dynamic Typing:
○ Very easy to work with
○ Faster development time
● Cons of Dynamic Typing:
○ May result in bugs for unexpected data types!
○ You need to be aware of type()
17. STRINGS
● Strings are sequences of characters, using the
syntax of either single quotes or double quotes:
○ 'hello'
○ "Hello"
○ " I don't do that "
18. String Indexing
● Because strings are ordered sequences it
means we can using indexing and slicing to
grab sub-sections of the string.
● Indexing notation uses [ ] notation after the string
(or variable assigned the string).
● Indexing allows you to grab a single character
from the string...
19. String Indexing
● These actions use [ ] square brackets and a
number index to indicate positions of what you
wish to grab.
Character : h e l l o
Index : 0 1 2 3 4
20. STRINGS
● These actions use [ ] square brackets and a
number index to indicate positions of what you
wish to grab.
Character : h e l l o
Index : 0 1 2 3 4
Reverse Index: 0 -4 -3 -2 -1
21. Python print() Function
The print() function prints the specified message to the screen, or other standard output device.
Ex:
print("Hello World")
print (5 + 7)
22. Practice
Which of the following are legitimate Python identifiers?
a) martinBradley b) C3P_OH c) Amy3 d) 3Right e) Print
What is the output from the following fragment of Python code?
myVariable = 65
myVariable = 65.0
myVariable = “Sixty Five”
print(myVariable)
how do you reference the last item in the list?
roster = ["Martin", "Susan", "Chaika", "Ted"]
23. String Slicing
● Slicing allows you to grab a subsection of multiple
characters, a “slice” of the string.
● This has the following syntax:
○ [start:stop:step]
● start is a numerical index for the slice start
● stop is the index you will go up to (but not include)
● step is the size of the “jump” you take.
24. String Slicing
mystring = "Hello World"
print(mystring[0])
print(mystring[3])
print(mystring[3:])
print(mystring[:3])
print(mystring[3:5])
print(mystring[::])
print(mystring[::2])
print(mystring[::4])
print(mystring[2:5:2])
print(mystring[::-1])
Output:
H
L
lo World
Hel
Lo
Hello World
HloWrd
Hor
Lo
dlroW olleH
29. String Methods
The upper() method returns a string where all characters are in upper case.
mystring = 'hello world'
print( mystring.upper())
HELLO WORLD
30. String Methods
The lower() method returns a string where all characters are lower case.
mystring = ‘HELLO WORLD'
print( mystring.lower())
hello world
31. String Methods
The split() method splits a string into a list.
mystring = ‘HELLO WORLD'
print( mystring.split())
[‘HELLO’, ‘WORLD’]
32. String Methods
Split on a specific character:
mystring = 'hello world'
print( mystring.split('o’))
['hell', ' w', 'rld']
33. String Formatting for Printing
● Often you will want to “inject” a variable into your
string for printing. For example:
○ my_name = “Jose”
○ print(“Hello ” + my_name)
● There are multiple ways to format strings for
printing variables in them.
● This is known as string interpolation.
34. String Formatting for Printing
● two methods for this:
○ .format() method ( classic)
○ f-strings (formatted string literals-new in
python 3)
35. String Formatting for Printing: .format()
The format() method formats the specified value(s) and insert
them inside the string's placeholder.)
{ }
36. String Formatting for Printing: .format()
Example:
name = 'Robert'
age = 51
print ("My name is {}, I'm {}".format(name, age))
Output: My name is Robert, I'm 51
37. String Formatting for Printing: .format()
Index on .format():
print ("Music scale is: {} {} {} ".format('Re', 'Do', 'Mi’))
Output: Music scale is: Re Do Mi
print ("Music scale is: {1} {0} {2} ".format('Re', 'Do', 'Mi’))
Output: Music scale is: Do Re Mi
38. Precision format in numbers
{value:width:precision f}
value= 20/300
print("the value is : {}".format(value))
value= 20/300
print("the value is : {x:1.3f}".format(x=value))
39. String Formatting for Printing: f-strings
name = 'Robert'
print ("My name is {}".format(name))
Output: My name is Robert
name = 'Robert'
print (f'My name is {name}’)
Output: My name is Robert
40. Precision format in numbers: f-strings
val = 12.3
print(f'{val:.2f}')
print(f'{val:.5f}’)
Output:
12.30
12.30000
41. LISTS
● Lists are ordered sequences that can hold a
variety of object types.
● They use [] brackets and commas to separate
objects in the list.
○ [1,2,3,4,5]
● Lists support indexing and slicing. Lists can be
nested and also have a variety of useful methods
that can be called off of them.
44. Lists: .append method
The append() method adds an item at the end of the list.
numbers = [21, 34, 54, 12]
numbers.append(32)
Print(numbers)
Output: [21, 34, 54, 12, 32]
45. Lists: .sort method
The sort() method sorts the list ascending by default.
cars = ['Ford', 'BMW', 'Volvo']
cars.sort()
print(cars)
Output: ['BMW', 'Ford', 'Volvo']
48. Dictionaries
● Dictionaries are unordered mappings for storing
objects. Previously we saw how lists store objects
in an ordered sequence, dictionaries use a key-
value pairing instead.
● This key-value pair allows users to quickly grab
objects without needing to know an index location.
49. Dictionaries
● Dictionaries use curly braces and colons to signify
the keys and their associated values.
{'key1':'value1','key2':'value2'}
● So when to choose a list and when to choose a
dictionary?
54. Dictionaries
● Dictionaries: Objects retrieved by key name.
Unordered and can not be sorted.
● Lists: Objects retrieved by location.
Ordered Sequence can be indexed or sliced.
56. Tuples
Tuples are very similar to lists. However they have
one key difference - immutability.
Once an element is inside a tuple, it can not be
reassigned.
Tuples use parenthesis: (1,2,3)
67. Practice
1) Create a programs that asks for user input about base and height and calculates area of a square.
2) Do the same but to calculate volume of a cube.
3) Do the same but calculate area of a triangle.