0% found this document useful (0 votes)
41 views8 pages

Slide 2

A pdf to help python student

Uploaded by

Muizz Lukmon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views8 pages

Slide 2

A pdf to help python student

Uploaded by

Muizz Lukmon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

LECTURE 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’

• The plus operation with strings means concatenate the strings.


• Python looks at the type of operands before deciding what operation is associated with the +
Now Try

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:

1. How many people came on your fishing trip?


2. Number of fish caught on a fishing trip.
3. Length of time it took to catch the first fish, in hours.
4. Length of a fish you caught, in meters.
5. Length of your fishing rod, in meters.
TYPE AND TYPE CONVERSION

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.

print("My age is: " + str(10))

str(True)
End of Lecture 2

You might also like