0% found this document useful (0 votes)
43 views

My Note 1 Cat 12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
0% found this document useful (0 votes)
43 views

My Note 1 Cat 12

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
Write an algorithm for interchanging/swapping two values. Teg) Write an algorithm to find the larger of two numbers. ‘Write an algorithm to find whether a number is even or odd. ‘Write an algorithm to print the grade obtained by a student using the following rules: ern ‘Write an algorithm to find the sum of first N natural numbers. Recursion Recursion is a technique of solving a problem by breaking it down into smaller and smaller sub- problems until you get oa small enough problem that it can be easily solved. Usually, recursion involves a function calling itself until a specified condition is met. Write a recursive algorithm to find the factorial ofa number. ‘Draw a flowchart to calculate the sum of the first 10 natural numbers, Read the values Draw a flowchart to calculate the salary of a daily wager. ‘Input the no_of_hrs, pay_per_hr, and travel allowance Calculate SALAR) ‘Draw a flowchart to determine the largest of three numbers. ‘Write a pseudocode for calculating the price ofa product after adding the sales tax to its original price, , [EE] Write « pseudocode to calculate the weekly wages of an employee. The pay depends ‘on wages per hour and the number of hours worked. Moreover, if the employee has ‘worked for more than 30 hours, then he or she gets twice the wages per hour, for every ‘extra hour that he or she has worked. Let us take a problem statement, do requirement analysis, design ind implement the solution in Python, and then test the program, Problem Statement ‘To develop an automatic system that acepts marks ofa student and generates his! her grade. Requirements Analysis. Ask the users to enlist the rules for ssigning grades. These rules are: ‘Test ‘The abovepprogram is then teed with different test dats to crisure that the program gives correst ouput {er all relevant and possible inputs. The test cases are shown in the table given below. Na er are Ten ip act Cit Cremer ty Note in the above table, we have identified test cases for the following, 1. "Net Possible” Combinations -£ Aridale va fom eich ange Boundary values for cosh range 42. 4). 42. UIC UIUSS HalaLy WwW UT paru ad tar. Draw a flowchart and write an algorithm and a psuedocode for the following problem statements a. Cook maggi Cross road Calculate bill of items purchased To find out whether a number is positive or negative Print “Hello” five times on the screen Find area of a rectangle Convert meters into centimeters Find the sum of first 10 numbers ass PooI rh @ Write an algorithm and draw a flowchart that calculates salary of an employee. Prompt the user to enter the Basic Salary, HRA, TA, and DA. Add these components to calculate the Gross Salary. Also deduct 10% salary from the Gross Salary to be paid as tax. Draw a flowchart and write an algorithm and a psuedocode for the following problem statements . Cook maggi . Cross road . Calculate bill of items purchased To find out whether a number is positive or negative Print “Hello” five times on the screen . Find area of a rectangle . Convert meters into centimeters . Find the sum of first 10 numbers aBaege Fe me Table 3.2. Reserved Words ee err 3.13.2 Comparison Operators Comparison opeeators also known as relational operators are used to compare the values on its either sides and determines the relation between them. For example, assuming a = 168 and b = 28, we can use the ‘comparison operators on them as specified in Table 3.4. Table 3.4 Comparison Operator Returns True if the value at the: id me ean ae oer ar es ane Ye ‘its heft Table 3.5 Assignment and in-place Operator Coca) Ein 8 ‘Assign value of the operand on the right side of the operator ‘assigns value of & to to the operand on the left the variable ¢ * ‘Add and assign: Adds the operands.on the leftand right sida a += bis same as.a=a +b ‘of the operator and assigns the resull to tha operand on the teh. S Subtract and assign: Subtracts operand on the tight from the a= bis same asa=a-b ‘operand on the leftof the operator and assigns the result to the operand on the left, = “Mutiply and assign: Multiplios the operands and assigns «a *= bis game asa=a*b result fo the operand on the left side of the operator te Divide and assign: Divides operand on the left side ofthe a/= bis same asa=a/b ‘operator with the operand on its right. The division operator ‘eluers the quotient. This result is assigned to the aperand to the left of the division operator, % ‘Modulus and assign: Divides eperand on the left side of the 4 %=bis same asa=a'%b ‘operator with the operand on its ight. The modulus operator ‘returns the remainder which is then assigned to the operand ‘on the left of the operator. We Floor division: Dividas the operands and relums the quotient, a//=bis same as a= ali Ikaiee remaves the digits after the decimal point. if ane of tne operands is negative, the resut is floored {rounded away ‘from zero towards negative infinity): the result is assigned to the operand on the left of the operator. Exponent and assign: Performs exponential calculation, that a"= bis same as a=a™"b ‘ig, aises operand on the right side to the operand on the left j of the operator and assigns the result in the left operand. Note that the in-place opcrators can also be used on other data types 3.12.5 Bitwise Operators [Aa the name suggests, bitwise operators porfor ope AND, bitwise OR, bituise XOR, and shift operator integers and treat them as a sequence of bits Bitwise AND (&) When e use the bitwise ANO operator, the bit in the first operand is ANNDed with the corresponding bit in the second operand. The bitwi'se-ANO operator compares each bit of ts first operand with the corresponding, bit of its second operand. If both bits are 4, the corresponding bit in the result is 1 and @ otherwise, For example, Yeiaieie # elaiesdi |= 6o8esed8| ns at the bit level. These operators include bitwise . Bitwise operators expect their operands to be of Bitwise OR () When we use the bitwise OR operator, the bit in the first operand is ORed with the corresponding bit in the second operand. The truth table is same as we had seen in logical of operation. The bitwise-ok operator ‘compares each bit of its first operand with the corresponding bit ofits second operand, [fone or both bits are 1, the corresponding bit in the result is 1 and 0 otherwise. For example, “tereiere | e1eie161 = 11113235 Bitwise XOR (4) When we use the bitwise XOR opertor, the bit in the first operand is XORed with the corresponding, bit in the second operand. That is, the bitwise-XOR operator compares each bit of its first operand with the corresponding bit of its second operand, If one of the bits is 2, the corresponding bit in the rooull is 4 and @ atherwise. For example, ieieieie * eie1eie1 = 11233111 Bitwise NOT (~) ‘The bitwise NOT, or complement, is a unary operation, which performs logical negation on each bit of the operand. By performing negation of each bit, it actually produces the ones’ complement of the given binary value. Bitwise NOT operator sets the bit to 1, if it was initially @ and sets it to ®, if it was initially 1. For example, 4.12.6 Shift Operators Python supports two bitwise shift operators. They ure shift left (<<) and shift right (>>). These operations are used to shift bits to the left-or to the right. The syntax for a shift operation can be given as follows: “where, the bits in operand are shifted left or right depending on the operator (left if the operator is ¢« and right if the operator is2>)by number of places denoted by num, For example, When we apply a left shift, every bit in x is shifted to the left by one place: Therefare, the MSB (most significant bil) of x is lost and the 18 of x is set to @. Therefore, for example, Ifyou observe carefully, you will notice thet shifting onge to the left multiplies the number by 2, Hence, maultiple shifts of | tothe feft, results in multiplying the number by2 over and over again, On the contrary, when we apply a right shift, every bit in x is shifted to the right by ane place, Therefore, the LSB (least significant bit) of x is lost and the MSB of x is set to 0, For example, IF you observe carefully, you will notice that shifting once to the right divides the number by 2. Hence, _ tnaltiple shifts of 2 to the right, results in dividing the number by 2 over and over again, MBI isso ie camer he al Logical AND (&&) Logical a0 operator is used to simultaneously evaluate two conditions or expressions with relational ‘operators. If expressions on both the sides (left and right side) ofthe logical operator are true, then the whole expression is true. For example, Logical OR (Il) ‘Logical OR operator is used to simultaneously evaluate two conditions or expressions with relational operators. fone or both the expressions of the logical operator is true, then the whole expression is true. For example, Logical NOT (I) The logical not operator takes a single expression ond negates the value of the expression. Logical NOT [produces a zero if the expression evalustes to a non-zero value and produces 4.1 if the expression produces a rero. In other words, it just reverses the vaiue of the expression. For example, Table 3.7 Operator Precedence Chart oe Expanentiation mA ‘Complement, unary plus (positiva), and minus (negative) i 86 Multiply, divide, modulo, and floor division he Addition and subtraction bo ee Right and faft bitwise shift & Bitwise ‘AND" aL Bitwise exclusive ‘OR’ and regular "OR" <5, 5,5 = ‘Comparison operators <>, se, [= ‘Equality operators s, s, d=, Its, <=, +, tm, Sm Assignment operators Is, isnot Identity operators in, nat in (Membership operators not, or, and Logical operators. ‘Operator precedence table is important as it affects how an expression is evaluated, Por example, Table 3.6 Functions for Type Conversions Da eee dnt(x) Converts x to an integer ‘long(x) Converts x to a long integer float (x) Converts x to a floating point number str(x) Converts x toa string tuple(x) Converts x to a tuple list(x) Converts x to a list set(x} Converts «toa set ond(x) Convers a single character to its integer value oct (x) Converts an integer to an octal string hex(x) Converts an Integer to a hexadecimal string chr (x) Converts an integer to a character unichr(x) Converts an integer to a Unicode character dict(x) Creates a dictionary if x forms a (key-value) pair

You might also like