Variables in Python
In Python, variables do not need to be declared. Every variable must be assigned a value before it can be used, and only after assignment is the variable created.
In Python, a variable is just a variable, it does not have a specific type. The "type" we refer to is the type of the object that the variable points to in memory.
The equals sign (=
) is used to assign values to variables. The variable name is on the left-hand side of the equals sign, and the value stored in the variable is on the right-hand side. For example:
# Example (Python 3.0+) #!/usr/bin/python3 counter = 100 # Integer variable miles = 1000.0 # Float variable name = "runoob" # String variable print(counter) print(miles) print(name)
Running the Code
Executing the above program will output the following result:
100 1000.0 runoob
Multiple Variable Assignment
Python allows you to assign values to multiple variables simultaneously. For example:
a = b = c = 1
In the above example, an integer object with the value of 1 is created, and the values are assigned from right to left, so all three variables are assigned the same value.
You can also assign multiple variables to multiple objects simultaneously. For example:
a, b, c = 1, 2, "runoob"
In this example, the integer objects 1 and 2 are assigned to variables a
and b
, and the string object "runoob"
is assigned to variable c
.
Standard Data Types
Common data types in Python 3 include:
Number
String
Boolean
List
Tuple
Set
Dictionary
In Python 3, these data types can be categorized into:
Immutable data (3 types): Number, String, Tuple
Mutable data (3 types): List, Dictionary, Set
Additionally, there are advanced data types like byte arrays (bytes).
Number
Python 3 supports int
, float
, bool
, and complex
(complex numbers).
In Python 3, there is only one integer type int
, which behaves as long integers in Python 2. Just like most languages, the assignment and computation of numeric types are straightforward.
The built-in type()
function can be used to check the type of the object a variable refers to.
>>> a, b, c, d = 20, 5.5, True, 4+3j >>> print(type(a), type(b), type(c), type(d)) <class 'int'> <class 'float'> <class 'bool'> <class 'complex'>
You can also use isinstance
to verify the type:
>>> a = 111 >>> isinstance(a, int) True
The difference between type()
and isinstance()
is:
type()
does not consider subclass relationships.isinstance()
considers subclass relationships.
>>> class A: ... pass ... >>> class B(A): ... pass ... >>> isinstance(A(), A) True >>> type(A()) == A True >>> isinstance(B(), A) True >>> type(B()) == A False
Note: In Python 3, bool
is a subclass of int
. Therefore, True
and False
can be used in arithmetic operations, where True == 1
and False == 0
will return True
. However, is
can be used to check the exact type.
>>> issubclass(bool, int) True >>> True == 1 True >>> False == 0 True >>> True + 1 2 >>> False + 1 1 >>> 1 is True False >>> 0 is False False
In Python 2, there is no bool
type, and 0
represents False
, while 1
represents True
.
When you assign a value, a Number object is created:
var1 = 1 var2 = 10
You can also delete object references using the del
statement:
del var1[,var2[,var3[....,varN]]]
For example:
del var del var_a, var_b
Arithmetic Operations
>>> 5 + 4 # Addition 9 >>> 4.3 - 2 # Subtraction 2.3 >>> 3 * 7 # Multiplication 21 >>> 2 / 4 # Division, returns a float 0.5 >>> 2 // 4 # Floor division, returns an integer 0 >>> 17 % 3 # Modulus 2 >>> 2 ** 5 # Exponentiation 32
Notes:
Python allows assigning multiple variables at once, e.g.,
a, b = 1, 2
.A variable can be reassigned to different types of objects.
Division can return either a float or an integer using
/
or//
.In mixed-type arithmetic, Python converts integers to floats.
Numeric Type Examples:
int | float | complex |
---|---|---|
10 | 0.0 | 3.14j |
100 | 15.20 | 45.j |
-786 | -21.9 | 9.322e-36j |
080 | 32.3e+18 | .876j |
-0490 | -90. | -.6545+0J |
-0x260 | -32.54e100 | 3e+26J |
0x69 | 70.2E-12 | 4.53e-7j |
Python also supports complex numbers, which consist of a real part and an imaginary part. They can be represented by a + bj, or complex(a,b). The real part a and the imaginary part b of the complex number are both floating point types.
String
Strings in Python are enclosed in single quotes ('
) or double quotes ("
), and special characters can be escaped using the backslash ().
The syntax for string slicing is:
variable[start_index:end_index]
Indexing starts at 0, and -1 refers to the position starting from the end.
The plus sign +
is the string concatenation operator, while the asterisk *
repeats the string a specified number of times. For example:
#!/usr/bin/python3 str = 'Runoob' # Define a string variable print(str) # Print the entire string print(str[0:-1]) # Print characters from the first to the second-to-last (not including the last character) print(str[0]) # Print the first character print(str[2:5]) # Print characters from the third to the fifth print(str[2:]) # Print the string starting from the third character to the end print(str * 2) # Print the string twice print(str + "TEST") # Concatenate the string with "TEST"
The output of the above program is:
Runoob Runoo R noo noob RunoobRunoob RunoobTEST
In Python, special characters can be escaped using the backslash . If you want the backslash to be treated as a literal character, you can prefix the string with an r
, indicating a raw string:
>>> print('Ru oob') Ru oob >>> print(r'Ru oob') Ru oob
The backslash can also be used as a line continuation character, meaning the next line is a continuation of the current one. You can also use triple quotes """..."""
or '''...'''
to span multiple lines.
Note that Python does not have a specific character type; a single character is just a string of length 1.
Example:
>>> word = 'Python' >>> print(word[0], word[5]) P n >>> print(word[-1], word[-6]) n P
Unlike C strings, Python strings are immutable. Assigning a value to a specific index, such as word[0] = 'm'
, will result in an error.
Important Notes:
Backslashes can be used for escaping. Using
r
before a string prevents escaping.Strings can be concatenated using the
+
operator and repeated using the*
operator.Python strings can be indexed from left to right starting at 0 or from right to left starting at -1.
Python strings are immutable.
Bool (Boolean Type)
The Boolean type represents True
or False
.
In Python, True
and False
are keywords that represent boolean values.
Booleans are used to control program flow, such as determining whether a condition is met or executing a block of code based on a condition.
Characteristics of Boolean Type:
Boolean values are either
True
orFalse
.bool
is a subclass ofint
, meaning booleans can be used like integers, withTrue
equivalent to 1.Booleans can be compared with other data types such as numbers and strings. During comparisons,
True
is treated as 1 andFalse
as 0.Booleans can be used with logical operators like
and
,or
, andnot
. These operators can combine multiple boolean expressions into a single boolean value.Booleans can be converted to other data types like integers, floats, and strings.
True
converts to 1, whileFalse
converts to 0.You can use the
bool()
function to convert other types to booleans. The following values are converted toFalse
:None
,False
, zero (0
,0.0
,0j
), empty sequences (e.g.,''
,()
,[]
), and empty mappings (e.g.,{}
). All other values are converted toTrue
.
Example:
# Boolean values and their types a = True b = False print(type(a)) # <class 'bool'> print(type(b)) # <class 'bool'> # Boolean integer representation print(int(True)) # 1 print(int(False)) # 0 # Using the bool() function for conversion print(bool(0)) # False print(bool(42)) # True print(bool('')) # False print(bool('Python')) # True print(bool([])) # False print(bool([1, 2, 3])) # True # Boolean logical operations print(True and False) # False print(True or False) # True print(not True) # False # Boolean comparison operations print(5 > 3) # True print(2 == 2) # True print(7 < 4) # False # Booleans in control flow if True: print("This will always print") if not False: print("This will also always print") x = 10 if x: print("x is non-zero and thus True in a boolean context")
Note: In Python, all non-zero numbers and non-empty strings, lists, tuples, etc., are considered True
. Only zero, empty strings, empty lists, and empty tuples are considered False
. Keep this in mind when converting data types to booleans.
List
A List
is one of the most frequently used data types in Python.
Lists can implement most of the features of a data structure, and elements in a list can be of different types, including numbers, strings, and even other lists (also known as nested lists).
Lists are written inside square brackets []
, with elements separated by commas.
Like strings, lists can be indexed and sliced. After slicing a list, a new list containing the required elements is returned.
The syntax for list slicing is:
variable[start_index:end_index]
Indexing starts at 0, and -1 refers to the position starting from the end.
The plus sign +
is the list concatenation operator, and the asterisk *
repeats the list. For example:
#!/usr/bin/python3 list = ['abcd', 786, 2.23, 'runoob', 70.2] # Define a list tinylist = [123, 'runoob'] print(list) # Print the entire list print(list[0]) # Print the first element of the list print(list[1:3]) # Print elements from the second to the fourth (excluding the fourth element) print(list[2:]) # Print elements from the third to the end print(tinylist * 2) # Print the tinylist twice print(list + tinylist) # Concatenate both lists
The output is:
['abcd', 786, 2.23, 'runoob', 70.2] abcd [786, 2.23] [2.23, 'runoob', 70.2] [123, 'runoob', 123, 'runoob'] ['abcd', 786, 2.23, 'runoob', 70.2, 123, 'runoob']
Unlike Python strings, the elements in a list can be changed:
>>> a = [1, 2, 3, 4, 5, 6] >>> a[0] = 9 >>> a[2:5] = [13, 14, 15] >>> a [9, 2, 13, 14, 15, 6] >>> a[2:5] = [] # Remove these elements >>> a [9, 2, 6]
Lists come with many built-in methods such as append()
and pop()
, which will be discussed later.
Important Notes:
Lists are written between square brackets, with elements separated by commas.
Like strings, lists can be indexed and sliced.
Lists can be concatenated using the
+
operator.Elements in a list are mutable.
Python list slicing can accept a third parameter, which defines the step size. In the following example, the step size of 2 slices the string:
If the third parameter is a negative number, it means reading in reverse. The following example is used to reverse the string:
def reverseWords(input): # Use spaces as string delimiters to separate words into lists inputWords = input.split(" ") # Reverse a string # Assume the list list = [1,2,3,4], # list[0]=1, list[1]=2, and -1 means the last element list[-1]=4 (same as list[3]=4) # inputWords[-1::-1] has three parameters # The first parameter -1 means the last element # The second parameter is empty, indicating moving to the end of the list # The third parameter is the step size, -1 means reverse inputWords = inputWords[-1::-1] # Reassemble the string output = ' '.join(inputWords) return output if __name__ == "__main__": input = 'I like runoob' rw = reverseWords(input) print(rw)
The output is:
runoob like I
Tuple
A tuple is similar to a list, with the key difference being that the elements of a tuple cannot be modified. Tuples are written within parentheses ()
and the elements are separated by commas.
The elements in a tuple can also be of different data types:
#!/usr/bin/python3 tuple = ('abcd', 786, 2.23, 'runoob', 70.2) tinytuple = (123, 'runoob') print(tuple) # Output the complete tuple print(tuple[0]) # Output the first element of the tuple print(tuple[1:3]) # Output elements from the second to the third print(tuple[2:]) # Output elements from the third to the end print(tinytuple * 2) # Output the tuple twice print(tuple + tinytuple) # Concatenate the tuples
The output is:
#!/usr/bin/python3 tuple = ('abcd', 786, 2.23, 'runoob', 70.2) tinytuple = (123, 'runoob') print(tuple) # Output the complete tuple print(tuple[0]) # Output the first element of the tuple print(tuple[1:3]) # Output elements from the second to the third print(tuple[2:]) # Output elements from the third to the end print(tinytuple * 2) # Output the tuple twice print(tuple + tinytuple) # Concatenate the tuples
Tuples, like strings, can be indexed, with indexing starting from 0
. The index -1
represents the position from the end. They can also be sliced (as shown above, no need to repeat here).
In fact, strings can be considered as a special kind of tuple.
Example:
>>> tup = (1, 2, 3, 4, 5, 6) >>> print(tup[0]) 1 >>> print(tup[1:5]) (2, 3, 4, 5) >>> tup[0] = 11 # Modifying tuple elements is illegal Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'tuple' object does not support item assignment
Although the elements of a tuple cannot be changed, they can contain mutable objects, such as lists.
Constructing tuples with 0 or 1 element is a bit special, so there are some extra syntactical rules:
tup1 = () # Empty tuple tup2 = (20,) # One element, requires a comma after the element
If you want to create a tuple with only one element, make sure to add a comma after the element to distinguish it as a tuple rather than an ordinary value. Without the comma, Python will interpret the parentheses as part of a mathematical operation, not a tuple.
For example, without the comma, the following will be interpreted as an ordinary value, not a tuple:
not_a_tuple = (42)
In this case, not_a_tuple
will be of integer type, not tuple type.
Strings, lists, and tuples are all types of sequences.
Note:
Like strings, the elements of a tuple cannot be modified.
Tuples can also be indexed and sliced in the same way as lists.
Pay attention to the special syntactical rules for constructing tuples with 0 or 1 element.
Tuples can also be concatenated using the
+
operator.
Set
In Python, a set is an unordered and mutable data type used to store unique elements.
The elements in a set are not repeated and can be used to perform common set operations like intersection, union, and difference.
In Python, sets are represented using curly braces {}
, and elements are separated by commas ,
.
Additionally, sets can be created using the set()
function.
Note: To create an empty set, you must use set()
instead of {}
, because {}
is used to create an empty dictionary.
Creation Syntax:
parame = {value01, value02, ...} # or set(value)
Example:
#!/usr/bin/python3 sites = {'Google', 'Taobao', 'Runoob', 'Facebook', 'Zhihu', 'Baidu'} print(sites) # Output the set, duplicate elements are automatically removed # Membership test if 'Runoob' in sites: print('Runoob is in the set') else: print('Runoob is not in the set') # Set operations a = set('abracadabra') b = set('alacazam') print(a) print(a - b) # Difference between a and b print(a | b) # Union of a and b print(a & b) # Intersection of a and b print(a ^ b) # Elements that are in a or b but not both
Output:
{'Zhihu', 'Baidu', 'Taobao', 'Runoob', 'Google', 'Facebook'} Runoob is in the set {'b', 'c', 'a', 'r', 'd'} {'r', 'b', 'd'} {'b', 'c', 'a', 'z', 'm', 'r', 'l', 'd'} {'c', 'a'} {'z', 'b', 'm', 'r', 'l', 'd'}
Dictionary
A dictionary is another highly useful built-in data type in Python.
While a list is an ordered collection of objects, a dictionary is an unordered collection. The difference between them is that in a dictionary, elements are accessed via keys, not offsets.
A dictionary is a mapping type, identified by { }
. It is an unordered collection of key-value pairs.
Keys must be immutable types, and they must be unique within the same dictionary.
Example:
#!/usr/bin/python3 dict = {} dict['one'] = "1 - Runoob Tutorial" dict[2] = "2 - Runoob Tools" tinydict = {'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'} print(dict['one']) # Output the value for the key 'one' print(dict[2]) # Output the value for the key 2 print(tinydict) # Output the entire dictionary print(tinydict.keys()) # Output all keys print(tinydict.values()) # Output all values
Output:
1 - Runoob Tutorial 2 - Runoob Tools {'name': 'runoob', 'code': 1, 'site': 'www.runoob.com'} dict_keys(['name', 'code', 'site']) dict_values(['runoob', 1, 'www.runoob.com'])
The constructor dict()
can also build dictionaries directly from sequences of key-value pairs as shown below:
Example:
>>> dict([('Runoob', 1), ('Google', 2), ('Taobao', 3)]) {'Runoob': 1, 'Google': 2, 'Taobao': 3} >>> {x: x**2 for x in (2, 4, 6)} {2: 4, 4: 16, 6: 36} >>> dict(Runoob=1, Google=2, Taobao=3) {'Runoob': 1, 'Google': 2, 'Taobao': 3}
The code {x: x**2 for x in (2, 4, 6)}
uses a dictionary comprehension. For more information, refer to: Python Comprehensions.
Additionally, the dictionary type has some built-in functions like clear()
, keys()
, values()
, etc.
Important Notes:
A dictionary is a mapping type, and its elements are key-value pairs.
The keys in a dictionary must be immutable types and cannot be duplicated.
To create an empty dictionary, use
{}
.
Bytes Type
In Python 3, the bytes
type represents an immutable sequence of bytes (binary data).
Unlike string types, the elements in bytes
are integer values (ranging from 0 to 255) rather than Unicode characters.
The bytes
type is commonly used to handle binary data, such as image files, audio files, video files, and so on. It is also often used in network programming to transmit binary data.
Creating a bytes
object:
One of the most common ways to create a bytes
object is by using the b
prefix:
x = b"hello"
Additionally, you can use the bytes()
function to convert other types of objects into bytes
. The first parameter is the object to be converted, and the second parameter is the encoding method. If the second parameter is omitted, UTF-8 encoding is used by default:
x = bytes("hello", encoding="utf-8")
Similar to strings, the bytes
type supports many operations and methods such as slicing, concatenation, searching, and replacing. However, since bytes
is immutable, modifying operations require creating a new bytes
object.
Example:
x = b"hello" y = x[1:3] # Slicing, results in b"el" z = x + b"world" # Concatenation, results in b"helloworld"
Important Note:
Since the elements in a bytes
object are integer values, comparison operations must use the corresponding integer values.
Example:
x = b"hello" if x[0] == ord("h"): print("The first element is 'h'")
Here, the ord()
function is used to convert a character to its corresponding integer value.
Python Data Type Conversion
Sometimes, we need to convert between built-in data types in Python. To perform data type conversion, you simply use the data type as the function name. Detailed information will be introduced in the next chapter, Python3 Data Type Conversion.
The following built-in functions can be used to perform conversions between data types. These functions return a new object representing the converted value.
Function | Description |
---|---|
int(x [,base]) | Converts x to an integer. |
float(x) | Converts x to a floating-point number. |
complex(real [,imag]) | Creates a complex number. |
str(x) | Converts object x to a string. |
repr(x) | Converts object x to an expression string. |
eval(str) | Evaluates a valid Python expression from a string and returns an object. |
tuple(s) | Converts sequence s to a tuple. |
list(s) | Converts sequence s to a list. |
set(s) | Converts s to a mutable set. |
dict(d) | Creates a dictionary. d must be a sequence of (key, value) tuples. |
frozenset(s) | Converts s to an immutable set. |
chr(x) | Converts an integer to a character. |
ord(x) | Converts a character to its integer value. |
hex(x) | Converts an integer to a hexadecimal string. |
oct(x) | Converts an integer to an octal string. |