Python Programming – The Range ( ) Function

Python Programming – The Range ( ) Function

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – The Range ( ) Function Python range ( ) function generates a list of numbers, which is generally used to iterate over with for loops. Python range ( ) function examples: >>> # One parameter >>> for i in range(5): . . . print(i) . . . 0 1 2 3 4 >>> # Two parameters >>> for i in range(3, 6): . . . print(i) . . . 3 4 5 >>> # Three parameters >>> for i in range(4, 10, 2): . . . print(i) . . . >>> # Going backwards >>> for i in range(0, -10, -2) . . . print(i) . . . 0 -2 -4 -6 -8 Python Programming – Print Statement Python Programming – Standard Data Types Python Programming – Accepting Input From The Console Let’s Try What is the output of the following code fragments? >>> for i in range(10): . . . print(i) >>> for i in range(0, -12, -2): . . . print(i) sum = 0 for i in range(16,2,-2): sum+ = i print (sum) for x in range(-300; 300, 100):…

Python Programming - The For Statement

Python Programming – The For Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – The For Statement Python provides for statements as another form for accomplishing control using loops. The for loop statement is used to repeat a statement or a block of statements a specified number of times. A Python for loop has this general form, for <var> in <sequence>: <body> The body of the loop can be a sequence of Python statements. The start and end of the body can be indicated by its indentation under the loop heading. The variable after the keyword is called the loop index. It takes on each successive value in the sequence, and the statements in the body are executed once for each value. Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing Python Programming – Python Character Set Python Programming – Conditional and Iterative Statements Usually, the sequence portion is a list of values. You can build a simple list by placing a sequence of expressions in square brackets. Some interactive examples help to illustrate the point: >>> for i in [ 0 , 1 , 2 , 3 ]: print ( i ) 0…

Python Programming – Notion Of Iterating Computation (Loops)

Python Programming – Notion Of Iterating Computation (Loops)

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Notion Of Iterating Computation (Loops) Many jobs that are required to be done with the help of a computer are repetitive in nature. For example, the calculation of wages of different workers in a factory is done using the formula (No. of hours worked) x (wage rate). This calculation will be performed by an accountant for each worker every month. Such types of repetitive calculations can easily be done with the help of a program that has a built-in loop. Figure 5.21 shows the flow diagram of the loop statement. Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing Python Programming – Conditional and Iterative Statements Introduction to Python Programming – Interpreter What is a Loop? A loop is defined as a block of instructions repeated a certain number of times. Figure 5.22 illustrates a flowchart showing the concept of looping. It shows the flowchart for printing values 1, 2, 3…, 20. In Step 5 of Figure 5.22, the current value of A is compared with 21. If the current value of A is less than 21, steps 3 and…

Python Programming - Nested If Statement

Python Programming – Nested If Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Nested If Statement There may be a situation when you want to check for another condition after one condition evaluates to true. In such a situation, you can use the nested if construct. In a nested, if construct, you can have an if…elif…else construct inside another if…elif..else construct. Python Programming – Conditional and Iterative Statements Python Programming – Standard Data Types Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing Example 16. Program to test whether the entered character is an alphabetic character or not. Suppose you want to test the ASCII code of an entered character to check if it is an alphabetic character. After checking this, you would like to further check if it is in lowercase or uppercase. Knowing that the ASCII codes for the uppercase letters are in the range of 65-90, and those for lowercase letters are in the range of 97-122, a program shown in Figure 5.20 establishes this logic. # Program to test – character is alphabetic character or not ch = input(“Enter an alphabetic character: “) if (ch>=’A’ and ch<=’Z’): print(“The character…

Python Programming – If-Elif-Else Statements

Python Programming – If-Elif-Else Statements

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – If-Elif-Else Statements In the if statement, the statement following the condition or the keyword else is a single executable statement, such as an assignment statement. If multiple tasks are to be performed depending on a condition, then they can be grouped together within the block to form a single compound statement. Thus, you can use more than one statement by embedding the statements in a block. In the compound statement, there is no limit to the number of statements that can appear inside the body, but there has to be at least one. Indentation level is used to tell Python which statement(s) belongs to which block. Python Programming – Conditional and Iterative Statements Python Programming – Conditional Statements Python Programming – How To Start Python If you want to check for several conditions, you can use elif, which is short for “else if”. It is a combination of an if clause and an else clause. Like the else, the elif statement is optional. However, unlike else for which there can be at the most one statement, there can be unlimited number…

Python Programming - If Statement

Python Programming – If Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – If Statement The statement if tests a particular condition. Whenever that condition evaluates to true, an action or a set of actions is executed. Otherwise, the actions are ignored. The syntactic form of the if statement is as follows: if (expression): statement The expression must be enclosed within parentheses. If it evaluates to a non-zero value, the condition is considered as true, and the statement following it is executed. For example, if(marks>=40) : print(“Pass”) if (num < 0): print(“Number is negative.”) if ch==”: print(“You entered space.”) Python Programming – Conditional and Iterative Statements Python Programming – Conditional Statements Python Programming – Logical/Boolean Operators Simple Program Demo of if Statement. In the program, shown in Figure 5.4, a credit card limit is tested for a certain purchase. The value of the variable amount is accepted from the keyboard; then it is verified using the if statement. When the condition is true (i.e., the “amount” is less than or equal to 1000), the message “Your charge is accepted” is displayed. If the condition is false, the program displays a message “The amount exceeds…

Python Programming - Membership Operators

Python Programming – Membership Operators

You can learn about Operators in Python Programs with Outputs helped you to understand the language better. Python Programming – Membership Operators Python has membership operators, which test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators explained below: in Operator Evaluates to true if it finds a variable in the specified sequence and false otherwise. For example, x in y; here, in results in a 1 if x is a member of sequence y. not in Operator Evaluates to true if it does not find a variable in the specified sequence and false otherwise. For example, x not in y. Here not in results in a 1 if x is not a member of sequence y. Python Programming – Identity Operators Python Programming – Logical/Boolean Operators Python Programming – Standard Data Types Example 17. Boolean test to find whether a value is inside a con-tainer or not: >>> t = [1, 2, 4, 5] >>> 3 in t False >>> 4 in t True >>> 4 not in t False For strings, tests for substrings: >>> a = ‘abode’ >>> ‘c’ in a True >>> ‘cd’ in a True Example 18. Demo of working…

Python Programming - Bitwise Operators

Python Programming – Bitwise Operators

You can learn about Operators in Python Programs with Outputs helped you to understand the language better. Python Programming – Bitwise Operators A bit is the smallest possible unit of data storage, and it can have only one of the two values : 0 and 1. Bitwise operator works on bits and performs the bit-by-bit operation, instead of whole. Table 4.4 shows the bitwise operators and their meaning. Table 4.4 Bitwise Operators and their Meaning Bitwise operator Meaning >> Right hand << Left hand & AND I OR ∧ XOR ˜ One’s complement Python Programming – Operators And Their Precedence and Associativity Python Programming – Operators Python Programming – Expressions In some other languages, A is used for exponentiation, but in Python, it is a bitwise operator called xor. In order to understand the bitwise operators, let us first consider the SHIFT operators. There are two types of shift operators that shift the bits in an integer variable by a specified number of positions. The ‘»’ operator shifts bits to the right, and the ‘«’ operator shifts bits to the left. Right Shift Operator ‘>>’ The right shift operator (>>) shifts the bits of the number to the right by the number…

Python Programming - Assignment (Augmented) Operators

Python Programming – Assignment (Augmented) Operators

You can learn about Operators in Python Programs with Outputs helped you to understand the language better. Python Programming – Assignment (Augmented) Operators Assignment operators are used to assigning values to the variables. They combine the effect of arithmetic and assignment operator. Augmented assignments can make your code more compact, concise, and Python language offers special shorthand operators that combine assignments with each of the arithmetic operations to simplify the coding. The operators are given in Table 4.3. The equivalences are shown in Figure 4.12. Operator Symbol Form Operation Assign = a = b put the value of b into a add-assign += a += b put the value of a + b into a subtract-assign -= a -= b put the value of a-b into a multiply-assign *_ a * = b put the value of a*b into a divide-assign / = a / = b put the value of a/b into a remainder-assign %= a %= b put the value of a % b into a Python Programming – Operators Python Programming – Logical/Boolean Operators Python Programming – Relational/Conditional Operators Example 14. Demo of the assignment operator. # Demo of assignment operator a = 2 b = 10 c =…

Python Programming - Operators And Their Precedence and Associativity

Python Programming – Operators And Their Precedence and Associativity

You can learn about Operators in Python Programs with Outputs helped you to understand the language better. Python Programming – Operators And Their Precedence and Associativity All operators have two important properties called precedence and associativity. Both properties affect how operands are attached to operators. Precedence means the order in which two different kinds of operators should be applied in an expression. Associativity means the order in which the two operators with the same precedence should be applied in an expression. Note: When more than one operator appears in an expression, the order of evaluation depends on the rules of precedence. To understand how precedence works, consider the expression: 2 + 3 *4 Should it be interpreted as (2+3) *4 (that is, 20), or rather is 2+(3 *4) (that is, 14) the correct interpretation? num3=int(input(“Enter number 3:”)) sum = numl + num2 + num3 print(“Three numbers are :” , numl, num2, num3) print(“Sum is , sum) As in normal arithmetic, multiplication and division in Python have equal importance and are performed before addition and subtraction. You can say multiplication and division have precedence over addition and subtraction. In the following expression: 2+3 *4 the multiplication is performed before addition since multiplication has…