Built-In Data Types: Setting The Data Type
Built-In Data Types: Setting The Data Type
Variables can store data of different types, and different types can do different things.
Python has the following data types built-in by default, in these categories:
Function Description
abs() Returns the absolute value of a number
all() Returns True if all items in an iterable object
are true
any() Returns True if any item in an iterable object is
true
ascii() Returns a readable version of an object.
Replaces none-ascii characters with escape
character
bin() Returns the binary version of a number
bool() Returns the boolean value of the specified
object
bytearray() Returns an array of bytes
bytes() Returns a bytes object
callable() Returns True if the specified object is callable,
otherwise False
chr() Returns a character from the specified Unicode
code.
classmethod() Converts a method into a class method
compile() Returns the specified source as an object, ready
to be executed
complex() Returns a complex number
delattr() Deletes the specified attribute (property or
method) from the specified object
dict() Returns a dictionary (Array)
dir() Returns a list of the specified object's
properties and methods
divmod() Returns the quotient and the remainder when
argument1 is divided by argument2
enumerate() Takes a collection (e.g. a tuple) and returns it as
an enumerate object
eval() Evaluates and executes an expression
exec() Executes the specified code (or object)
filter() Use a filter function to exclude items in an
iterable object
float() Returns a floating point number
format() Formats a specified value
frozenset() Returns a frozenset object
getattr() Returns the value of the specified attribute
(property or method)
globals() Returns the current global symbol table as a
dictionary
hasattr() Returns True if the specified object has the
specified attribute (property/method)
hash() Returns the hash value of a specified object
help() Executes the built-in help system
hex() Converts a number into a hexadecimal value
id() Returns the id of an object
input() Allowing user input
int() Returns an integer number
isinstance() Returns True if a specified object is an instance
of a specified object
issubclass() Returns True if a specified class is a subclass of
a specified object
iter() Returns an iterator object
len() Returns the length of an object
list() Returns a list
locals() Returns an updated dictionary of the current
local symbol table
map() Returns the specified iterator with the specified
function applied to each item
max() Returns the largest item in an iterable
memoryview() Returns a memory view object
min() Returns the smallest item in an iterable
next() Returns the next item in an iterable
object() Returns a new object
oct() Converts a number into an octal
open() Opens a file and returns a file object
ord() Convert an integer representing the Unicode of
the specified character
pow() Returns the value of x to the power of y
print() Prints to the standard output device
property() Gets, sets, deletes a property
range() Returns a sequence of numbers, starting from 0
and increments by 1 (by default)
repr() Returns a readable version of an object
reversed() Returns a reversed iterator
round() Rounds a numbers
set() Returns a new set object
setattr() Sets an attribute (property/method) of an
object
slice() Returns a slice object
sorted() Returns a sorted list
@staticmethod() Converts a method into a static method
str() Returns a string object
sum() Sums the items of an iterator
super() Returns an object that represents the parent
class
tuple() Returns a tuple
type() Returns the type of an object
vars() Returns the __dict__ property of an object
zip() Returns an iterator, from two or more iterators
Python has a set of built-in methods that you can use on lists/arrays.
Method Description
append() Adds an element at the end of the list
clear() Removes all the elements from the list
copy() Returns a copy of the list
count() Returns the number of elements with the
specified value
extend() Add the elements of a list (or any iterable), to
the end of the current list
index() Returns the index of the first element with the
specified value
insert() Adds an element at the specified position
pop() Removes the element at the specified position
remove() Removes the first item with the specified value
reverse() Reverses the order of the list
sort() Sorts the list
Python has two built-in methods that you can use on tuples.
Method Description
count() Returns the number of times a specified value
occurs in a tuple
index() Searches the tuple for a specified value and
returns the position of where it was found
Python has a set of keywords that are reserved words that cannot be used as variable
names, function names, or any other identifiers:
Keyword Description
and A logical operator
as To create an alias
assert For debugging
break To break out of a loop
class To define a class
continue To continue to the next iteration of a loop
def To define a function
del To delete an object
elif Used in conditional statements, same as else if
else Used in conditional statements
except Used with exceptions, what to do when an
exception occurs
False Boolean value, result of comparison operations
finally Used with exceptions, a block of code that will
be executed no matter if there is an exception
or not
for To create a for loop
from To import specific parts of a module
global To declare a global variable
if To make a conditional statement
import To import a module
in To check if a value is present in a list, tuple, etc.
is To test if two variables are equal
lambda To create an anonymous function
None Represents a null value
nonlocal To declare a non-local variable
not A logical operator
or A logical operator
pass A null statement, a statement that will do
nothing
raise To raise an exception
return To exit a function and return a value
True Boolean value, result of comparison operations
try To make a try...except statement
while To create a while loop
with Used to simplify exception handling
yield To end a function, returns a generator
Python Conditions and If statements
Python supports the usual logical conditions from mathematics:
Equals: a == b
Not Equals: a != b
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Indentation
Python relies on indentation (whitespace at the beginning of a line) to define scope in the
code.
Other programming languages often use curly-brackets for this purpose.
Elif
The elif keyword is pythons way of saying "if the previous conditions were not true, then try
this condition".
Else
The else keyword catches anything which isn't caught by the preceding conditions.
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a
set, or a string).
This is less like the for keyword in other programming languages, and works more like an
iterator method as found in other object-orientated programming languages.
With the for loop we can execute a set of statements, once for each item in a list, tuple, set
etc.
Creating a Function
In Python a function is defined using the def keyword:
Building Blocks
1) Variables
2) Functions
3) List
4) Loops
5) Conditionals