Python Objects, Variables and Data Types
Everything in Python is an object. Each object has identity, a type, and a value. The identity refers to the address and does not change.
The type can be any of the following:
- None: This represents the absence of a value.
- Numbers: Python has three types of numbers: Integer, Floating Point and Complex.
- Sequences: These are ordered collections of elements. There are three types of sequences in Python: String, Tuples and Lists.
- Sets: This is an unordered collection of elements.
Numbers
Integers: They do not have any fractional part.
Floating point numbers: That do have a fractional part.
Complex numbers: The numbers having a real and an imaginary part.
Strings
In Python, a string is a predefined object which contains characters. The value can be printed by using the print function. The value at a particular location of a string can be displayed using indexing. The index of the first location is 0. So, name[0] would print the first letter of the string, which is J.
name = "John"
print(name)
print(name[0])
Length
The length of a string can be found by calling the len function. len(str) returns the length of the string.
Concatenation
The + operator concatenates the strings.
Lists
A list is a collection of objects. It is the most general sequence provided by the language. Unlike strings, lists are mutable. That is, an element at a particular position can be changed in a list. A list is useful in dealing with homogeneous and heterogeneous sequences.