29
loading...
This website collects cookies to deliver better user experience
1 + 1 = 2
, but a computer can’t directly give you the answer without first checking how the expression is represented in the memory. This is where the concept of data types comes to play.SyntaxError
.e
or E
followed by an integer to represent a scientific notation.inf
implying Infinity.a+bj
, where a is the real part and b is the imaginary part. You can create a complex number using the complex()
function. This function takes in two arguments. The first is the real part and the second is the imaginary part.j
to the end of the imaginary part. This will set the real part to zero and the imaginary part to whatever value was specified.a
is a complex number, then you can access the real and imaginary parts of a
using a.real
and a.imag
.True
and False
. Boolean values can also behave like integers when used in numeric concepts, where True
evaluates to 1 and False
evaluates to 0.bool()
which can be used to convert any value to a Boolean. The bool()
function takes in an input and returns the boolean values True
or False
depending on the input. By default, the output is True
. It is False
if it falls under any of the following:False
, including None
and False
False
. E.g 0
, 0.0
, 0j
, Decimal(0)
, Fraction(0, 1)
False
. E.g ''
, ()
, []
, {}
, set()
, range(0)
if … else
, while
, for
in your programs. The truth value of a boolean expression is what determines whether a particular section of code is executed on not as in loops and conditionals.str
object. Unlike integers, strings are represented differently in the computer’s memory that is why they cannot be used to perform arithmetic operations. # Creating a string with a single quote
single = 'Hello, World!'
print(single)
# Creating a string with double-quote
double = "Hi, i'm Prince"
print(double)
# Creating a string with a triple quote
triple = """
The conversation went as follows:
Me: Hello
Her: Hey
"""
print(triple)
## Output
# Hello, World!
# Hi, i'm Prince
# The conversation went as follows:
# Me: Hello
# Her: Hey
SyntaxError
. But a single quote string can contain a double quote string inside, and a double quote string can contain a single quote string inside, like the double
variable in the above snippet.Indexing
. Indexing allows us to use the index of a string to access it. The first index of a string is 0, the second is 1, and so on. The last index of a string is -1, the second to last is -2, and so on.# Accessing elements of a string
message = "Hello, World!"
print(message[0]) # The first element of the string
print(message[-1]) # The last element of a string
print(message[13]) # IndexError
## output
# H
# !
# IndexError: string index out of range
IndexError
.iterables
or using the type constructor list()
that takes in an iterable
.An iterable
is any sequence or container that can be iterated upon (loop through).
# List
# create an empty list
a = []
b = list()
print(a)
print(b)
# list with values of different types
mixed = [1, 2, 3, 4, True, False, 'a', 'b', 'c', 'd']
print(mixed)
# using the list() function
list_function = list(mixed) # create a copy of the `mixed` list
print(list_function)
iterable = list(range(10))
print(iterable)
tuple_inside_function = list((1, 2, 3))
print(tuple_inside_function)
## output
# []
# []
# [1, 2, 3, 4, True, False, 'a', 'b', 'c', 'd']
# [1, 2, 3, 4, True, False, 'a', 'b', 'c', 'd']
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# [1, 2, 3]
iterable
such as a list. A list containing lists is an example of a multidimensional list.# List dimensions
# single dimensional
alphanumeric = ['a', 'b', 'c', 'z', 0, 1, 2, 9]
print(alphanumeric)
print(alphanumeric[5])
# multidimensional
alphanumeric = [['a', 'b', 'c', 'd'], [0, 1, 2, 9]]
print(alphanumeric)
print(alphanumeric[0][2])
## output
# ['a', 'b', 'c', 'z', 0, 1, 2, 9]
# 1
# [['a', 'b', 'c', 'd'], [0, 1, 2, 9]]
# c
list[a][b]
, where a
is the outer list and b
is the inner list or iterable
.iterable
. They are also created with or without the use of parentheses containing the tuple element. A tuple can also contain a single element, but the element must have a trailing comma for it to be a tuple.# Tuples
# empty tuple
empty = ()
print(empty)
# singleton tuple
single = 2,
print(single)
# tuple with strings
string_tuple = ('Hello',)
print(string_tuple)
# tuple from list
_list = [1, 2, 3]
list_tuple = tuple(_list)
print(list_tuple)
# nested tuple
tuple_a = ('a', 'b', 'c')
tuple_b = (1, 2, 3)
combined = (tuple_a, tuple_b)
print(combined)
## output
# ()
# (2,)
# ('Hello',)
# (1, 2, 3)
# (('a', 'b', 'c'), (1, 2, 3))