unit 1 and 2 Flashcards
3 steps to solve a problem
1- Understanding the prob (input)
2- Analysing the prob (process)
3- Developing the soln (output)
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 problem is broken into smaller ones and then the process is repeated with each problem.
Bottom up approach used in ______
research
techniques and design used to solve a problem
- Pseudocode
- Algorithm
- Flowchart
Pseudocode
it is an informal high level description of the operating principle of a computer program
it is used to document the program or module design (algorithm)
pseudo means false. pseudocode means false code
Algorithm
can be defined as a complete, unambiguous, finite number of logical steps for solving a specific problem
eg: finding whether a number is prime or not
flowchart definition
diagrammatic representation
-of an algorithm work flow or process
-illustrates a solution model to a given problem
Problem
a well defined tasks or challenge that requires a computational soln.
in depth steps for problem solving
- Understanding/Analysing the problem
- Developing the algorithm/pseudocode
- Coding
- Testing and debugging
Top down design is based on ___ approach
decomposition approach
top down approach is followed by ________
structural programming languages like C, FORTRAN
Advtgs of top down approach
- better understanding of entire program and less info about subprogram
- well established communication is not required
Disadvgs of top down approach
the codes can be repeated
symbols of a flowchart
- Terminal - start/end points - OVAL
- Flowlines - default flow is left to right and top to bottom- ARROWS
- Input/Output - input/output - PARALLELOGRAM
- Process - mathematical compution / variable assignment - RECTANGLE
- Decision - true/false statement being tested - DIAMOND/KITE
- Module Call - program control is HORIZONTAL LINES IN RECT and specific task module is VERTICAL LINES IN RECT
- Connector - allows to connect two flowcharts on same or dif page - SMALL CIRCLE WITH A LETTER IN IT (on page)
SMALL PENTAGON WITH LETTER IN IT (off page)
write a flowchart for print sum of two numbers
start -> read input a -> read input b ->calc sum of a & b -> print the sum -> end
write a algorithm for print sum of two numbers
- define the function is - vowel with character as a paramether passed.
- check whether the charater is vowel or not & store it in is-char-vowel
- return the value of is-char-vowel
- call the function “in-vowel” & test the function with different characters.
Semantic error
when there is some logical errors
OR
there is no end to the loop in the syntax
Functions definition and use
allows us to create a block of code that can be reused to perform a specific task
they make the code reusable and more organised
function can be defined using ______ keyword
def
structure of function
name, parameters and body
functions can be called ________ times to reuse its functionality
multiple
parameter
named entity in a function definition that specifies an argument that function can accept
Eg: def product_of_two_numbers (a, b)
Here, a and b are
parameters example
Argument
the actual value that is passed to a function when it is called. It is used to assign a value to the corresponding parameter in the function defined.
function declaration
the act of defining a functions name, parameters and body. It doesnt run the function but sets it up, to be called later.
function call (invoking the function)
the act of running a function by using its name followed by arguments in parentheses.
return value
the value a function sends back to the place where the function was called from. it is specified by the return keyword in a function.
Flowchart shows the steps as ______
boxes of various kinds and their order by connecting the boxes with arrows
Flowchart is used in
analysing, designing, documenting or managing a process or program in various fields
Variable names must start with?
letters or underscores
Which error comes up if a variable name starts with a number?
Syntax error : invalid decimal literal
in python variables are ____?
case-sensitive
Example of ‘cannot assign value to a literal’?
5 = i
code for checking type of variable?
type(variable)
% , /, //
% - modulo - gives remainder
/ - simple division - returns float
// - returns quotient integer
converting a string into all uppercase and printing it
print(string_variable.upper())
converting a string into all lowercase and printing it
print(string_variable.lower())
capitalising the first letter of a string, making the rest of the letters lowercase and printing it
print(string_variable.capitalize())
z important
Checking if a string has all lowercase letters (True/False)
print(str_variable.islower())
Checking if a string has all uppercase letters (True/False)
print(str_variable.isupper())
Checking the starting characters in a string (True/False)
st = “Hello World”
print(st.startswith(‘Hell’))
Checking if a string has first letter capital and rest all lowercase (titlecase) (True/False)
print(str_variable.istitle())
Checking if the string is a digit (True/False)
only works on strings and checks all characters
print(“123”.isdigit())
Checking the ending characters in a string (True/False)
st = “Hello World”
print(st.endswith(‘World’))
Checking if string is alpha (True/False)
print(‘23’.isalpha())
print(‘2A’.isalpha())
print(‘ABC’.isalpha())
Checking if string is numeric alpha (True/False)
print(‘ABC123’.isalnum())
print(‘ABC 123’.isalnum())
Finding a substring within a string
#Returns index value
print(‘Hello World’.find(‘Hello’)) #Output 0
print(‘Hello World’.find(‘ello’)) #Output 1
print(‘Hello World’.find(‘llo’)) #Output 2
print(‘Hello World’.find(‘lo’)) #Output 3
print(‘Hello World’.find(‘Ello’)) #Output -1
print(‘Hello World’.find(‘plo’)) #Output -1
extract substring from one index value to another
str=’Hello World’
print(‘Hello World’[0:5])
How to use the in keyword to check whether a character or a sequence of characters exists within a specific string?
print(‘Hello’ in ‘Hello World’) #true
print(‘ello’ in ‘Hello World’) #true
print(‘Ello’ in ‘Hello World’) #False
print(‘bello’ in ‘Hello World’) #False
which operator is used to check if the 2 strings are the same?
==
output is true or false
In Python there is no distinct data type for single characters. Both strings and single characters
message = “69Hello Message”
print(message[0]) #note there is square bracket
print(type(message[0])) #<class ‘str’>
3 ways for string concatenation in python
s1 = “Hello”
s2 = “World”
res = s1 + “ “ + s2
s1 = “Python”
s2 = “programming”
#use an f string to create a formatted string that includes both variables
result = f”{s1} is a popular {s2}.”
a = 5
b = 10
print(“The sum of {} and {} is {}.”.format(a, b, a + b))
Program to print no. of hours in a day
print (24)
Calling function without passing an argument Python will throw a _______ Error
Type Error
def product_of_two_numbers(a,b):
print(a * b)
product=product_of_two_numbers(2,6)
print(product) #prints None - WHY?
12 is printed inside the function
#product stores None because the function doesn’t return anything
#In Python, if no return is used, the function returns None by default
create a Python function named is_vowel that determines whether a given character is a vowel or not.
The input consists of a single parameter: char. Char is the character that needs to be tested.
The output should be a boolean value, returning True if the character is a vowel and False if it’s not.
def is_vowel(char):
vowels=’aeiou’
is_char_vowel = char in vowels
return is_char_vowel
is_vowel(‘a’)
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
Logical OR | operator
print(True|True) #Output: True
print(True|False) #Output: True
print(False|True) #Output: True
print(False|False) #Output: False
What happens when
if(False):
print(False)
no result because condition is false
Logical AND & operator
print(True&True) #Output: True
print(True&False) #Output: False
print(False&True) #Output: False
print(False&False) #Output: False
Logical XOR ^ operator
print(True^True) #Output: False
print(True^False) #Output: True
print(False^True) #Output: True
print(False^False) #Output: False
Logical ~ (NOT) Operator
print(~True) #Output: False
print(~False) #Output: True
print(10 / 0) # Division by zero gives _____ error
ZeroDivisionError
taking input and storing it in a variable example
age = int(input(“Enter age: “))
text = “Hello”
print(text[10]) # Index out of range gives _____ error
IndexError: string index out of range
age = int(input(“Enter age: “)) #if input is string “ten” then we get a _______ error
Value Error
when calling a function, if the argument is missing, we get a ____Error
TypeError
why use lists?
Lists = Flexible, dynamic containers for organizing and working with collections of data.
marks = [25, 56, 67]
what are the basic operations you can apply to this to get results in integers?
print(max(marks))
print(min(marks))
print(len(marks))
print(sum(marks))
what are the methods one can apply to lists?
animals = [‘Cat’, ‘Dog’, ‘Elephant’]
animals.extend([‘Giraffe’, ‘Horse’])
#adds these items at the end
animals += [‘Lion’, ‘Monkey’]
#again adds these items at the end of the list
print(animals.index(“Cat”)) #index is starting from zero
animals.append([‘Tiger’, ‘Cheetah’])
#adds the list at the end as a new item
animals.insert(2, ‘Hippo’)
#use of index
marks.remove(‘Cat’)
print(‘Hippo’ in animals) #check if a value exists in the list
print(animals.index(‘Horse’)) #returns index - throws a value error if item not in list