Create
Create boolean
Use direct assignment with True or False.
Use bool() function.
# Example, create bool
a1 = True
a2 = False
a3 = bool()
type(a1)
Create int
Create int
Use direct assignment with int literal (binary / octal / decimal / hex).
Use int() function.
# Example, create int
a1 = -1
a2 = 0
a3 = 5000
a4 = int()
a5 = 0b1111
a6 = 0O377
a7 = 0xff1100ab
type(a4)
Create int with specific base (2 to 35)
Say, you have number and its base known (2,8,10, 16 or any other, say 5), use int() function to create int.
# Example, create int with number 11 in base 2,8,10,16,5.
int_a = int("11", 2)
int_b = int("11", 8)
int_c = int("11", 10)
int_d = int("11", 16)
int_e = int("11", 5)
print(int_a, int_b, int_c, int_d, int_e) # prints 3 9 11 17 6
Create List
Create Empty List
List is created with empty square brackets.
Syntax:variable = []
Empty list can also be created using list constructor/built-in function, list(). However, List Constructor is useful to create list from other iterable sources.
Syntax:variable = list()
# Example, create empty list
countries = []
# Example, create list using built-in function, list()
countries = list()
type(countries)
Create List with initial elements
Initial elements can be provided with comma in between.
Syntax:variable = [ item1, item2 ]
# Example, create list with initial elements
fruits = ["Apple", "Grapes"]
type(fruits)
Create tuple
Create empty tuple
Tuple is created with empty round brackets.
Syntax:variable = ()
Empty tuple can also be created using tuple constructor/built-in function, tuple(). However, Tuple Constructor is useful to create tuple from other iterable sources.
Syntax:variable = tuple()
# Example, create empty tuple
a = ()
# Example, create tuple using built-in function, tuple()
a = tuple()
type(a)
Create tuple with initial elements
Initial elements can be provided with comma in between.
Syntax:variable = ( item1, item2 )
Or even without round brackets.
Syntax:variable = item1, item2
# Example, create tuple with initial elements
a = (1, 2, 3)
type(a)
a = 1, 2, 3
a = 1,2,3
type(a)
a = tuple(1,2,3) # raises TypeError: tuple() takes at most 1 argument
Create tuple with only one element
If tuple contains only one element, Specify comma after the element.
Syntax:variable = (element, ) variable = element,
This is unique case. Specifying single element without comma (element ) is not interpreted as tuple. If comma is not specified, it leads to ambiguity whether its general expression or tuple? Hence provision for comma is done.
# Example, create tuple with only one element
a = (1,)
print(a)
type(a)
a = 1,
print(a)
type(a)
# what happens if comma not specified
a = (1)
print(a)
type(a) # it is int!
Create array
Array type codes
Array type code is specified as char.
Typecode | C data type | Python data type |
---|---|---|
'b' | signed char | int |
'h' | signed short | int |
'i' | signed int | int |
'l' | signed long | int |
'q' | signed long long | int |
'f' | float | float |
'B' | unsigned char | int |
'H' | unsigned short | int |
'I' | unsigned int | int |
'L' | unsigned long | int |
'Q' | unsigned long long | int |
'd' | double | float |
Create int array
Python array supports char, short, int, long, long long, float and double - each with signed, unsigned. Python does not have all such data types, but python array facilitates user to specify it ONLY FOR array storage. Thanks to underlaying C implementation. It is specified by typecode.
To create array, use array.array() method.
Argument typecode is mandatory, initializer (sequence) is optional.
Syntax:array.array( typecode [, initializer] )Return Value:
array object
# Example, create array
import array
# second argument is tuple
some_numbers = array.array('i', (1, 2, 3, -3, -2, -1, 0) )
# array('i', [1, 2, 3, -3, -2, -1, 0])
# type 'q' - long long, second argument is list
a2 = array.array('q', [2**8, 2**16, 2**32, 2**60] )
# array('q', [256, 65536, 4294967296, 1152921504606846976])
Create empty array
# Example, create empty array
import array
a1 = array.array('l')
# array('l')
Create float array
# Example, create float array
import array
af1 = array.array('f', (2.1, 2.2, -10.5) )
# array('f', [2.0999999046325684, 2.200000047683716, -10.5])
OverflowError: Python int too large to convert to C long
Why this error:Are you trying to add element in python array? And its size exceeding C data type restriction?
Solution:While creating array, select type code as per matching need. OR
Ensure, that element to be inserted is following type code specific requirement.
# Example, try to create array with large number
import array
# type 'i' or 'l' causes OverflowError
a1 = array.array('i', (2**8, 2**16, 2**32, 2**60) ) # throws OverflowError: Python int too large to convert to C long
a1 = array.array('l', (2**8, 2**16, 2**32, 2**60) ) # throws OverflowError: Python int too large to convert to C long
# type 'h' - signed short
a1 = array.array('h')
a1.append(2**16) # throws OverflowError: signed short integer is greater than maximum
TypeError: integer argument expected, got float
Why this error:Are you trying to add element whose type is not matching with array type code?
Solution:While creating array, select type code as per matching need. OR
Ensure, that element to be inserted is following type code specific requirement.
# Example, try to append element with mismatch type
import array
a1 = array.array('i')
a1.append(1.01) # raises TypeError: integer argument expected, got float
a1 = array.array('i', (-1, 1, 0, 1.01 ) ) # raises TypeError: integer argument expected, got float