Lesson 1 Flashcards
(155 cards)
1
Q
INTEGRATIVE PROGRAMMING
A
2
Q
AND TECHNOLOGIES 2
A
3
Q
A
4
Q
PYTHON PROGRAMMING
A
5
Q
A
6
Q
Python Data Types
A
7
Q
• Variables can store data of different types
A
and different types can do
8
Q
different things.
A
9
Q
• Python has the following data types built-in by default
A
in these
10
Q
categories:
A
11
Q
A
12
Q
GETTING THE DATA TYPE
A
13
Q
• You can get the data type of any object by using the type() function:
A
14
Q
A
15
Q
SETTING THE DATA TYPE
A
16
Q
• In Python
A
the data type is set when you assign a value to a variable:
17
Q
A
18
Q
SETTING THE SPECIFIC DATA TYPE
A
19
Q
A
20
Q
PYTHON NUMBERS
A
21
Q
• There are three numeric types in Python:
A
22
Q
• int
A
float
23
Q
A
24
Q
•
A
25
26
Variable of numeric types are created when you assign a value to them:
27
28
INT
29
• Int
or integer
30
unlimited length.
31
32
FLOAT
33
• Float
or "floating point number" is a number
34
one or more.
35
36
COMPLEX
37
• Complex numbers are written with a "j" as the imaginary part:
38
39
TYPE CONVERSION
40
• You can convert from one type to another with the int()
float() and complex()
41
methods
42
43
NOTE
44
• You cannot convert complex
45
numbers into another number
46
type.
47
48
RANDOM NUMBER
49
• Python does not have a random() function to make a random number
but
50
Python has a built-in module called random that can be used to make random
51
numbers:
52
• Import the random module
and display a random number between 1 and 9:
53
54
PYTHON CASTING (SPECIFY A VARIABLE TYPE)
55
• There may be times when you want to specify a type on to a variable. This can
56
be done with casting. Python is an object-orientated language
and as such it
57
uses classes to define data types
including its primitive types.
58
• Casting in python is therefore done using constructor functions:
59
• int() - constructs an integer number from an integer literal
a float literal (by
60
removing all decimals)
or a string literal (providing the string represents a
61
whole number)
62
• float() - constructs a float number from an integer literal
a float literal or a
63
string literal (providing the string represents a float or an integer)
64
• str() - constructs a string from a wide variety of data types
including strings
65
integer literals and float literals
66
67
INTEGERS
68
69
FLOATS
70
71
STRINGS
72
73
PYTHON STRINGS
74
• Strings in python are surrounded by either single quotation marks
or double
75
quotation marks.
76
• 'hello' is the same as "hello".
77
• You can display a string literal with the print() function:
78
STRINGS ARE ARRAYS
79
• Like many other popular programming languages
strings in Python are arrays
80
of bytes representing unicode characters.
81
• However
Python does not have a character data type
82
simply a string with a length of 1.
83
• Square brackets can be used to access elements of the string.
84
• Get the character at position 1 (remember that the first character has the
85
position 0):
86
87
LOOPING THROUGH STRING
88
• Since strings are arrays
we can loop through the characters in a string
89
for loop.
90
• Loop through the letters in the word "banana":
91
92
LOOPING THROUGH STRING
93
• The len() function returns the length of a string:
94
95
CHECK STRING
96
• To check if a certain phrase or character is present in a string
we can use the
97
keyword in.
98
• Check if "free" is present in the following text:
99
100
• Use it in an if statement:
101
• Print only if "free" is present:
102
103
CHECK IF NOT
104
• To check if a certain phrase or character is NOT present in a string
we can use
105
the keyword not in.
106
• Check if "expensive" is NOT present in the following text:
107
108
• Use it in an if statement:
109
• print only if "expensive" is NOT present:
110
111
SLICING STRINGS
112
• You can return a range of characters by using the slice syntax.
113
• Specify the start index and the end index
separated by a colon
114
of the string.
115
• Get the characters from position 2 to position 5 (not included):
116
117
SLICING FROM THE START
118
• By leaving out the start index
the range will start at the first character:
119
• Get the characters from the start to position 5 (not included):
120
121
SLICING TO THE END
122
• By leaving out the end index
the range will go to the end:
123
• Get the characters from position 2
and all the way to the end:
124
125
NEGATIVE INDEXING
126
• Use negative indexes to start the slice from the end of the string:
127
• Get the characters:
128
• From: "o" in "World!" (position -5)
129
• To
but not included: "d" in "World!" (position -2):
130
131
MODIFY STRINGS
132
• The upper() method returns the string in upper case:
133
134
• The lower() method returns the string in lower case:
135
136
REMOVE WHITESPACE
137
• Whitespace is the space before and/or after the actual text
and very often
138
you want to remove this space.
139
• The strip() method removes any whitespace from the beginning or the
140
end:
141
142
REPLACE STRINGS
143
• The replace() method replaces a string with another string:
144
145
SPLIT STRING
146
• The split() method returns a list where the text between the specified
147
separator becomes the list items.
148
• The split() method splits the string into substrings if it finds instances of
149
the separator:
150
151
STRING CONCATENATION
152
• To concatenate
or combine
153
• Merge variable a with variable b into variable c:
154
155
• To add a space between them
add a " ":