python Flashcards
(146 cards)
Functions definition and use
Python Functions is a block of statements that return the specific task.
The idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs, we can do the function calls to reuse code contained in it over and over again.
It increases code reusability & readability.
If you forget to increment the loop variable, you’ll end up with an infinite loop. Give example
To avoid the infinite loop problem, it’s essential to increment the value of i inside the loop:
i = 0
while i < 5:
print(i)
# Output:
# 0
# 0
# 0
# … (infinite loop)
i = 0
while i<5:
print(i)
i = i + 1
create a function to find whether a number is prime or not.
input - num
output - true if prime and false if not prime
def is_prime(num):
for i in range(2,num):
if num%i ==0:
return False
return True
print(is_prime(24))
transpose an array using numpy
use np.transpose()
array = np.array([[1, 2, 3], [4, 5, 6]])
Transpose the array
transposed_array = np.transpose(array)
Lists
A list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe stored at different locations.
* List can contain duplicate items.
* List in Python are Mutable. Hence, we can modify, replace or delete the items.
* List are ordered. It maintain the order of elements based on how they are added.
flowchart definition
diagrammatic representation
-of an algorithm work flow or process
-illustrates a solution model to a given problem
- shows us the steps using boxes of various kinds & their order by connecting the boxes with arrows
-used in analysing, designing, documenting or managing a process or program in various fields.
Checking if string contains numeric values and alphabets (True/False)
print(‘ABC123’.isalnum())
print(‘ABC 123’.isalnum())
Indexing of arrays
array[3]
allows us to access element at index 3
array[-3]
allows us to access elements from the end of the array, third last value in this case
In a 2D array, you can access elements using two indices (row and column)
Semantic error
when there is some logical errors
OR
there is no end to the loop in the syntax
Logical OR | operator
print(True|True) #Output: True
print(True|False) #Output: True
print(False|True) #Output: True
print(False|False) #Output: False
transpose a dataframe using pandas
dataframe.transpose()
Example of ‘syntax error: cannot assign value to a literal’?
5 = i
Logical ~ (NOT) Operator
print(~True) #Output: False
print(~False) #Output: True
top down approach is followed by ________
structural programming languages like C, FORTRAN
age = int(input(“Enter age: “)) #if input is string “ten” then we get a _______ error
Value Error
range(1,23)
including excluding?
including 1, excluding 23
plotting box plot
plt.boxplot(data)
plt.title(‘Box Plot’)
plt.ylabel(‘Value’)
plt.show()
Top down approach
a system design approach where the design starts from the complete system which is then divided into smaller sub applications with more details.
the focus is on breaking the problem into smaller ones and then the process is repeated with each problem.
This approach is followed by structural programming language
It is focused on decomposition approach
draw diagram
3 dimensional array
A 3D array in Python is essentially an array of arrays of arrays,
create a Python function named average_of_five_numbers that calculates the average of five given integers and returns the result.
The input consists of five integers: a, b, c, d, e.
The output should be a single integer, which is the average of a, b, c, d, e.
def average_of_five_numbers(a,b,c,d,e):
average =(a+b+c+d+e) /5
return average
print(average_of_five_numbers(15,14,12,13,17)) #output
The CONTINUE keyword
The continue statement is used to skip the current iteration of a loop and proceed to the next iteration.
#catches hold of semantic errors- logical error that python cannot catch - like while loop running infitite times
for i in range(1,11):
if i % 2 == 0:
continue
print(i, end=’ ‘)
print(‘done’)
output: 1 3 5 7 9 done
text = “Hello”
print(text[10]) # Index out of range gives _____ error
IndexError: string index out of range
when calling a function, if the argument is missing, we get a ____Error
TypeError
Checking if a string has all uppercase letters (True/False)
print(str_variable.isupper())