ITlec Prelims pt1 Flashcards
Who created python?
Guido Van Rossum
When was python created?
1991
What is python used for?
web development (server-side),
software development,
mathematics,
system scripting.
Whenever you are done in the python command line, you can simply type __________ to quit the python command line.
exit()
*indicate a block of code.
Indentations
Comments start with a ______, and Python will render the rest of the line as a comment
#
A _________ is created the moment you first assign a value to it.
variable
Rules of variables
- Must start with letter or underscore
-can’t start with number
-can only contain alpha numeric and underscore
There are three numeric types in Python:
*int
*float
*complex
is a named place in the memory where a programmer can store data and later retrieve the data using the variable “name”
variable
You can change the contents of a variable in a later statement
T or F
T
We assign a value to a variable using the ________?
Assignment statement (=)
Order of operation for python
Pemdas
Parenthesis
Exponent
multiplication, division, and remainder
Addition subtraction
(from left to right)
When writing code - use __________
parenthesis
You can also use str() and float() to convert between strings and integers
True or False
False
int() and float()
you will not get an error if the string does not contain numeric characters while converting to int or float
T or F
F
You will get an Error
used to output variables
print()
execution of code statements (one line after another) – like following a recipe
Sequential
used for decisions, branching – choosing between 2 or more alternative paths.
Selection
used for looping
repetition
Recall that Python has a type called __________, which can take two possible values, True and False
boolean
An expression that evaluates to a true/false value is called a
Boolean expression
Python, decisions are made with the ___________, also known as the __________________.
if statement
selection statement
If the boolean expression evaluates to TRUE, then the block of statement(s) inside the if statement is executed. If boolean expression evaluates to FALSE, then the first set of code after the end of the if statement(s) is executed
T or F
T
can be followed by an optional else statement, which executes when the boolean expression is FALSE.
An if statement
contains the block of code that executes if the conditional expression in the if statement resolves to 0 or a FALSE value.
else statement
allows you to check multiple expressions for TRUE and execute a block of code as soon as one of the conditions evaluates to TRUE.
The elif statement
allows us to execute a statement or group of statements multiple times
loop statement
Repeats a statement or group of statements while a given condition is TRUE
while loop
The loop iterates while the condition is false.
When the condition becomes true, program control passes to the line immediately following the loop.
T or F
F
Loop iterates when condition is true
A loop becomes ____________ if a condition never becomes FALSE.
infinite loop
If the _________ statement is used with a for loop, the ____ statement is executed when the condition becomes false.
else
Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.
For loop
is a block of organized, reusable code that is used to perform a single, related action.
A function
The code block within every function starts with a ___________ and is ___________
colon (:) and is indented.
The statement __________ exits a function, optionally passing back an expression to the caller
return [expression]
A return statement with no argument returns an error
False
It returns as None
______________ can be accessed only inside the function in which they are declared,
local variables
__________ can be accessed throughout the program body by all functions.
global variables
is a Python object with arbitrarily named attributes that you can bind and reference.
A module
You can use any Python source file as a module by executing an _______________ in some other Python source file.
import statement
The ________ built-in function returns a sorted list of strings containing the names defined by a module.
dir()
powerful and flexible data structure in python that allows you to store and manipulate collections of elements
Lists
Python lists can grow or shrink in size as needed making them flexinle for handling varying amounts of data
Dynamic storage and retrieval
Lists are commonly used in _____ to iterate over the elements and perform operations on them
loops
to create a python list you need to use ____
[] square brackets
List elements are accessed using their ________
indices
the indices start from ____ for the first elements
0
Lists can store elements of any data types
T or F
True
This list function return the number of elements present in a list
len()
allows you to add an element at the end of the list
append()
adds elements from another list to the end of the current list.
extend()
allows you to insert an element at a specific index in the list
insert()
function is used to remove the first occurrence of a specified element from the list
remove()
used to remove and return the element at a specific index in a list
pop()
removes all elements from the list
clear()
returns the index of the first occurrence of a specified element in the list
index()
returns the number of occurrences a specific element in the list
count()
used to sort in the list ascending order.
sort()
reverses the order of elements in the list
reverse()
creates a shallow copy of the list
copy()
provide a powerful set of tools for manipulating and managing lists
list functions
a concise and pythonic way of generating lists
List comprehension
List comprehension does not allow it to be nested
T or F
F
it is allowed to be nested to allow complex lists
is used to apply the int() function to each element to in the input list
map()
used to include only certain elements
filter()
this is generally faster and more efficient compared to traditional loops when dealing with simple operations on lists
List comprehension
an ORDERED collection of elements enclosed in parentheses ()
Tuples
Tuples are immutable
True or false
True
Elements cannot be modified once they are defined
Immutable
tuples are typically created by enclosing elements within ____ and separating them with _________
parentheses ()
Comma ,
Individual elements within a tuple can be accessed using __________
indexing
involves creating a tuple by assigning values to it
tuple packing
allows you to assign individual elements of a tuple to separate variables
tuple unpacking
Tuples can be concatenated using the “*” operator?
T or F
False you use “+”
Tuples can be repreated using the “x” operator
T or F
False you use the “*”
to check if an element exists in a tuple use the _____ keyword
in
tuples are _______, meaning their element cannot be changed after creation
immutable
you can convert lists and vice versa using the list() and tuple() function
T or F
True
Functions can only return single values as a tuple
T or F
False it can return multiple values
is an immutable unordered collection of UNIQUE elements
set
Sets are widely used for performing ___________ such as union, intersection and difference
mathematical set operations
sets can be created using _________
curly braces {}
You can use this function to add a single element in a set
add()
you can use this fucntion to add multiple elements in a set
update()
removes specified element from the set if it does not exist it will raise an error
remove()
this removes an element and if it does not exist it will not raise an error
discard()
removes a random item from a set
pop()
This set operations combines returns a combination of two sets
Union
symbol of union
|
This set operation return what’s common between two sets
Intersection
Symbol of intersection
&
returns the difference between 2 sets or more
difference
symbol of difference
-
returns what is unique in both sets
Symmetric difference
symbol of symmetric difference
to test if a set is a subset use
issubset()
to test if a set is a superset use
issuperset()
Sets only store unique elements, you can use them to store duplicates.
T or F
True
Allows you to store and retrieve data in key-value pairs
Dictionary
Another name of dictionary
Associative array or hash table
T or F
Dictionary is an ordered collection
F it is unordered
Dictionary key must be ______ and ______
unique and immutable
Dictionaries are created using
{} curly braces
To retrieve the value associated with a specific key use the _________
square bracket [] with the key inside
If you try to access a key that does not exist it will raise an index error
T or F
F it will raise a KeyError
You can use the ______ method which returns a default value if they key is not found
get()
to remove an element from the dictionary you can use the
pop() or del Keyword
to copy a dictionary use
copy() or dict() constrictor
the most common approach is to loop through its keys
T or F
T
How is dict separated
it is separated by colon (:), and the elements are separated by commas (,)