-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathPart5_Tuples_Sets_Booleans.py
167 lines (124 loc) · 4.38 KB
/
Part5_Tuples_Sets_Booleans.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
##################################
## Tuples, Sets, and Booleans ####
##################################
#
# In Python tuples are very similar to lists, however, unlike lists they are
# immutable meaning they can not be changed. You would use tuples to present
# things that shouldn't be changed, such as days of the week, or dates on a calendar.
#
# In this section, we will get a brief overview of the following:
#
# 1.) Constructing Tuples
# 2.) Basic Tuple Methods
# 3.) Immutability
# 4.) When to Use Tuples.
#
# You'll have an intuition of how to use tuples based on what you've learned
# about lists. We can treat them very similarly with the major distinction being
# that tuples are immutable.
#
############################
#### Constructing Tuples ###
############################
#
# The construction of a tuples use () with elements separated by commas. For example:
# Can create a tuple with mixed types
t = (1,2,3)
# Check len just like a list
len(t)
# Can also mix object types
t = ('one',2)
# Show
t
# Use indexing just like we did in lists
t[0]
# Slicing just like a list
t[-1]
############################
### Basic Tuple Methods ####
############################
# Tuples have built-in methods, but not as many as lists do.
# Lets look at two of them:
# Use .index to enter a value and return the index
t.index('one')
# Use .count to count the number of times a value appears
t.count('one')
####################
### Immutability ###
####################
# It can't be stressed enough that tuples are immutable.
# To drive that point home:
t[0]= 'change'
# Because of this immutability, tuples can't grow.
# Once a tuple is made we can not add to it.
t.append('nope')
############################
### When to use Tuples #####
############################
# You may be wondering, "Why bother using tuples when they have fewer available
# methods?" To be honest, tuples are not used as often as lists in programming,
# but are used when immutability is necessary. If in your program you are passing
# around an object and need to make sure it does not get changed, then tuple
# become your solution. It provides a convenient source of data integrity.
#
# You should now be able to create and use tuples in your programming as well as
# have an understanding of their immutability.
#
########################################################
########################################################
############## SETS AND BOOLEANS #######################
########################################################
########################################################
#
# There are two other object types in Python that we should quickly cover. Sets and Booleans.
#
############
### Sets ###
############
# Sets are an unordered collection of *unique* elements. We can construct them
# by using the set() function. Let's go ahead and make a set to see how it works
x = set()
# We add to sets with the add() method
x.add(1)
#Show
x
# Note the curly brackets. This does not indicate a dictionary! Although you can
# draw analogies as a set being a dictionary with only keys.
#
# We know that a set has only unique entries. So what happens when we try to add
# something that is already in a set?
# Add a different element
x.add(2)
#Show
x
# Try to add the same element
x.add(1)
#Show
x
# Notice how it won't place another 1 there. That's because a set is only
# concerned with unique elements! We can cast a list with multiple repeat
# elements to a set to get the unique elements. For example:
# Create a list with repeats
l = [1,1,2,2,3,4,5,6,1,1]
# Cast as set to get unique values
set(l)
##########################
######## Booleans ########
##########################
# Python comes with Booleans (with predefined True and False displays that are
# basically just the integers 1 and 0). It also has a placeholder object called
# None. Let's walk through a few quick examples of Booleans (we will dive
# deeper into them later in this course).
# Set object to be a boolean
a = True
#Show
a
# We can also use comparison operators to create booleans. We will go over all
# the comparison operators later on in the course.
# Output is boolean
1 > 2
# We can use None as a placeholder for an object that we don't want to reassign yet:
# None placeholder
b = None
# Thats it! You should now have a basic understanding of Python objects and
# data structure types. Next, go ahead and do the Review Exercises!