Slide 2
Slide 2
MICHEAL OGUNDERO
EMAIL – [email protected]
STRINGS
String Delimiters
• A string in Python is a sequence of characters. For Python to recognize a sequence of characters, like
hello, as a string, it must be enclosed in quotes to delimit the string.
Now Try
“Hello World”
’Hello World’
Note that the interpreter gives back the string with single quotes.
Now try to print out ‘I’m the best’ with both methods described above.
• A string can have any number of characters in it, including 0.
• The empty string is ’ ’
STRINGS
String Concatenation
• Just like integer operations, strings also have operation symbols.
‘happy ’+ ‘sad’
3 * ‘happy ’+ ‘sad’
‘40’ + ‘4’
‘40’ + 4
STRINGS
Exercise
• Figure out a compact way to get Python to make the string, ’YesYesYesYesYes’, and try it.
How about ’MaybeMaybeMaybeYesYesYesYesYes’
INTEGERS AND FLOAT
There are two Python data types that could be used for numeric values:
• int - for integer values e.g number of people.
• float - for decimal or floating point values e.g number of pie.
int(4.7)
float(4)
The first statement rounds down the given parameter to the nearest integer therefore returning 4.
The second statement approximates the given parameter to a decimal therefore returning 4.0
EXERCISE
In the fishy situation below, specify the quantities that should be of type int and those that should be of
type float:
Recall that the type() function is used to check the data type of any variable you are working with.
Variable types can be changed to perform different operations. For example, numbers to be used as part
of a sentence are better converted to strings, True-False values are better to encode when they are
Boolean and not string.
This is important because there are specially designed functions for working with each data type.
str(True)
End of Lecture 2