Python Fundamentals Tutorial: Types

2. Types

2.1. Strings

String literals can be defined with any of single quotes ('), double quotes (") or triple quotes (''' or """). All give the same result with two important differences.

  1. If you quote with single quotes, you do not have to escape double quotes and vice-versa.
  2. If you quote with triple quotes, your string can span multiple lines.
>>> 'hello' + " " + '''world'''
'hello world'

2.2. Integers

Integer literals are created by any number without a decimal or complex component.

>>> 1 + 2
3

2.2.1. Integer Division

Some programming tasks make extensive use of integer division and Python behaves in the expected manner.

>>> 10 / 3
3
>>> 10 % 3
1
>>> divmod(10, 3)
(3, 1)

2.3. Floats

Float literals can be created by adding a decimal component to a number.

>>> 1.0 / .99
1.0101010101010102

2.4. Complex

Complex literals can be created by using the notation x + yj where x is the real component and y is the imaginary component.

>>> 1j * 1j
(-1+0j)

Table of Contents

1. Getting Started
1.1. The Interactive Interpreter
1.2. Lab
1.3. Lab
2. Types
2.1. Strings
2.2. Integers
2.3. Floats
2.4. Complex
3. Variables
3.1. Definining
3.2. Dynamic Typing
3.3. Strong Typing
3.4. Internals
4. Simple Expressions
4.1. Boolean Evaluation
4.2. Truthiness
4.3. Branching (if / elif / else)
4.4. Block Structure and Whitespace
4.5. Lab
4.6. Multiple Cases
4.7. Lab
5. Advanced Types: Containers
5.1. Lists
5.2. Lab
5.3. Strings Revisited
5.4. Tuples
5.5. Lab
5.6. Dictionaries
5.7. Lab
5.8. Sets
5.9. Collection Transitions
6. A Bit More Iteration
6.1. Loop-Else
7. Functions
7.1. Defining
7.2. Arguments
7.3. Mutable Arguments and Binding of Default Values
7.4. Accepting Variable Arguments
7.5. Unpacking Argument Lists
7.6. Scope
7.7. Lab
8. Exceptions
8.1. Basic Error Handling
9. Code Organization
9.1. Namespaces
9.2. Importing modules
9.3. Creating Modules
10. Working with Files
10.1. File I/O
11. Interacting with the Outside World
11.1. Options
12. Regular Expressions (re)
12.1. Lab
13. Functional Programming
13.1. Functions as Objects
13.2. Higher-Order Functions
13.3. Sorting: An Example of Higher-Order Functions
13.4. Anonymous Functions
13.5. Nested Functions
13.6. Closures
13.7. Lexical Scoping
13.8. Useful Function Objects: operator
13.9. Lab
13.10. Decorators
13.11. Lab
14. Advanced Iteration
14.1. List Comprehensions
14.2. Generator Expressions
14.3. Generator Functions
14.4. Iteration Helpers: itertools
14.5. Lab
15. Debugging Tools
15.1. logging
15.2. pprint
15.3. Lab
16. Object-Oriented Programming
16.1. Classes
16.2. Emulation
16.3. classmethod and staticmethod
16.4. Lab
16.5. Inheritance
16.6. Lab
16.7. Encapsulation
16.8. Lab
17. Easter Eggs