Professional Documents
Culture Documents
04 Type and Variables
04 Type and Variables
1
Python Programming
Elements of Python
• A Python program is a sequence of
definitions and commands (statements)
• Commands manipulate objects
• Type: prutor.ai
• Each object is associated with a Type
– A set of values
– A set of operations on these values
• Expressions: An operation (combination of
objects and operators)
2
Programming
Types
• We have been using types
• In school mathematics
–Natural numbers
prutor.ai
• Values: 1, 2, 3, …
• Operations: +, - , *, >, <, …
–Complex numbers
• Values: 5 + 3i, 7 + 2i, …
• Operations: +, - , *, /, conjugate, …
• Operations >, < are NOT VALID
3
Programming
Types in Python
• int
– Bounded integers, e.g. 732 or -5
prutor.ai
• float
– Real numbers, e.g. 3.14 or 2.0
• long
– Long integers with unlimited precision
• str
– Strings, e.g. ‘hello’ or ‘C’
4
Programming
Types in Python
• Scalar
– Indivisible objects that do not have internal
structure
prutor.ai
– int (signed integers), float (floating point),
bool (Boolean), NoneType
• NoneType is a special type with a single value
• The value is called None
• Non-Scalar
– Objects having internal structure
– str (strings)
5
Programming
Finding Types of Expressions
You can use “type” function
to find the type of an expression
prutor.ai
6
Programming