0% found this document useful (0 votes)
15 views4 pages

2.5. Data Types

Uploaded by

bluevoid19
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)
15 views4 pages

2.5. Data Types

Uploaded by

bluevoid19
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/ 4

2.5.

Data Types
If you are not sure what class (data type) a value falls into, Python has a function called type which can
tell you.

Save & Run Show in CodeLens


Original - 1 of 1
1 print(type("Hello, World!"))
2 print(type(17))
3 print("Hello, World")
4 print(type(3.2))
5

<class 'str'>
<class 'int'>
Hello, World
<class 'float'>

Activity: 2.5.1 ActiveCode (ac2_5_1)

What about values like "17" and "3.2" ? They look like numbers, but they are in quotation marks like
strings.

Save & Run Show in CodeLens


Original - 1 of 1
1 print(type("17"))
2 print(type("3.2"))
3

<class 'str'>
<class 'str'>

Activity: 2.5.2 ActiveCode (ac2_5_2)

They’re strings!

Strings in Python can be enclosed in either single quotes ( ' ) or double quotes ( " ), or three of each ( '''
or """ )

Save & Run Show in CodeLens


Original - 1 of 1
1 print(type('This is a string.'))
2 print(type("And so is this."))
3 print(type("""and this."""))
4 print(type('''and even this...'''))
5

<class 'str'>
<class 'str'>
<class 
'str'> 
<class 'str'>
Activity: 2.5.3 ActiveCode (ac2_5_3)

Double quoted strings can contain single quotes inside them, as in "Bruce's beard" , and single quoted
strings can have double quotes inside them, as in 'The knights who say "Ni!"' . Strings enclosed with
three occurrences of either quote symbol are called triple quoted strings. They can contain either single or
double quotes:

Save & Run Show in CodeLens


Original - 1 of 1
1 print('''"Oh no", she exclaimed, "Ben's bike is broken!"''')
2

"Oh no", she exclaimed, "Ben's bike is broken!"

Activity: 2.5.4 ActiveCode (ac2_5_4)

Triple quoted strings can even span multiple lines:

Save & Run Show in CodeLens


Original - 1 of 1
1 print("""This message will span
2 several lines
3 of the text.""")
4

This message will span


several lines
of the text.

Activity: 2.5.5 ActiveCode (ac2_5_5)

Python doesn’t care whether you use single or double quotes or the three-of-a-kind quotes to surround
your strings. Once it has parsed the text of your program or command, the way it stores the value is
identical in all cases, and the surrounding quotes are not part of the value.

Save & Run Show in CodeLens


Original - 1 of 1
1 print('This is a string.')
2 print("""And so is this.""")
3

This is a string.
And so is this.

Activity: 2.5.6 ActiveCode (ac2_5_6)

 
So the Python language designers usually chose to surround their strings by single quotes. What do you
think would happen if the string already contained single quotes?

When you type a large integer, you might be tempted to use commas between groups of three digits, as in
42,000 . This is not a legal integer in Python, but it does mean something else, which is legal:
Save & Run Show in CodeLens
Original - 1 of 1
1 print(42500)
2 print(42,500)
3

42500
42 500

Activity: 2.5.7 ActiveCode (ac2_5_7)

Well, that’s not what we expected at all! Because of the comma, Python chose to treat this as a pair of
values. In fact, a print statement can print any number of values as long as you separate them by
commas. Notice that the values are separated by spaces when they are displayed.

Save & Run Show in CodeLens


Original - 1 of 1
1 print(42, 17, 56, 34, 11, 4.35, 32)
2 print(3.4, "hello", 45)
3

42 17 56 34 11 4.35 32
3.4 hello 45

Activity: 2.5.8 ActiveCode (ac2_5_8)

Remember not to put commas or spaces in your integers, no matter how big they are. Also revisit what we
said in the previous chapter: formal languages are strict, the notation is concise, and even the smallest
change might mean something quite different from what you intended.

Note
The examples in this online text describe how print works in Python 3. If you install Python 2.7 on
your machine, it will work slightly differently. One difference is that print is not called as a function,
so there are no parentheses around the values to be printed.

Check your understanding

How can you determine the type of a variable?

A. Print out the value and determine the data type based on the value printed.
B. Use the type function.
C. Use it in a known equation and print the result.
D. Look at the declaration of the variable.

Check Me Compare me

✔️ The type function will tell you the class the value belongs to.
 
Activity: 2.5.9 Multiple Choice (question2_5_1)
What is the data type of ‘this is what kind of data’?

A. Character
B. Integer
C. Float
D. String

Check Me Compare me

✔️ Strings can be enclosed in single quotes.

Activity: 2.5.10 Multiple Choice (question2_5_2)

You have attempted 11 of 11 activities on this page

 Completed. Well Done!

Finished reading assignment. Page 0 of 0.

© Copyright 2017 bradleymiller. Created using Runestone (http://runestoneinteractive.org/) 7.1.9. | Back to top

 

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy