Python Programming - Python Tuples

Python Programming – Python Tuples

Python Programming – Python Tuples In the Python programming language, a tuple is another important data structure. A tuple is similar to a Python list as studied in the previous section. The difference between tuple and list is that the elements in a list can be altered, whereas tuples are immutable, i.e., the elements of a tuple can not be changed. Creating a Tuple Alike a list, it is very simple to create a tuple in Python. As we see earlier that the elements of a list are enclosed in square brackets [ ], whereas the elements of a tuple are enclosed in parenthesis ( ) separated with commas. Moreover, a tuple can have any number of elements of heterogeneous type (integer, float, string_list, etc). The programming illustration to create different kinds of tuples is given in Code 5.19. In this program, we create three tuples. The first tuple new tuple is of homogeneous data elements. The second tuple new_tuplel contains heterogeneous type data and the third tuple new_tuple2 represents the nested tuple, which also contains a list as its data element. The output for the same is given as well. Python Programming – Tuple Python Programming – Tuple Functions…

Python Programming - Python Control Statements

Python Programming – Python Control Statements

Python Programming – Python Control Statements The control statements are used to alter the normal sequence of a loop. Python language provides three types of control statements given as in Table 4.3. Statement Description Break It terminates the current execution of the loop and transfers the control to the statement instantly following the block of the loop. Continue It immediately transfers the control to the beginning of the loop by skipping the rest of the statements following it. pass It is used when a statement is required syntactically rather than executing any command or code. Python Programming – Conditional and Iterative Statements Python Programming – Python Loops Python Programming – Continue Statement Python break Statement Alike the traditional break statement in C, the role and use of the break statement are similar in Python language. The break statement is used to break the loop immediately when a certain condition evaluates to true. It can be used in both for and while loop. In case, nested loops are used in a program, and if break encounters in the inner loop then, it terminates the inner loop, and execution control goes to the outer loop. On the other hand, if the break…

Python Programming - Python Loops

Python Programming – Python Loops

Python Programming – Python Loops In the previous sections, we have discussed decision-making statements that control the flow either sequentially or skip some statements depending upon the test condition. In some situations such as computing the table of a number, it is required to repeat some set of statements to attain the required results. This repetition can be achieved by using a loop control structure. In this section, we will discuss various loops provided by the Python language. We will demonstrate how loops are supportive and effective in the Python language. Types of loops The repetition of a loop can also be termed as iteration, which means repetitive execution of the same set of instructions for a given number of times or until a particular result is obtained. The repetition of a loop is controlled by a test expression (condition) specified with the loop. The loop begins and continues its execution as long as the test expression evaluates to true. The loop terminates when the conditional expression evaluates to false. After that, the control transfers to the next statement that follows the loop. The Python language supports 2 types of iterative or looping statements while and for as displayed in…

Python Programming - Operators and Expressions

Python Programming – Operators and Expressions

Python Programming – Operators and Expressions Special Operators Python language offers some special type of operators like the identity operator Identity Operator Identity operators compare the memory locations of two objects. They are used to check if two objects, values, or variables are located on the same part of the memory. Two variables that are equal do not imply that they are identical. The identity operators are listed in Table 3.7. Let us assume the value of x=10 and y=20. Operator symbol Name Example Output Description Is is X is y False Evaluates to true if the variables on either side of the operator point to the same object and false otherwise. Is not Is not X is not y True Evaluates to false if the variables on either side of the operator point to the same object and true otherwise. Python Programming – Identity Operators Python Programming – Logical/Boolean Operators Quick Tip: Using Python’s Comparison Operators | Chaining Comparison Operators in Python The Code 3.6. illustrates the use of identity operators. Code: 3.6. Illustration of identity operators # program to illustrate the use of identity operators x1=10 y1=10 x2= ‘Program’ y2= ‘Program’ x3= [1, 2, 3] y3= [1, 2,…

Python Programming - Expressions

Python Programming – Expressions

Python Programming – Expressions Expressions form a basic sentence in any programming language. To construct an expression, we need two basic entities, which are operand and operators. As described above, Python language provides a rich set of operators. Operators in Python language are symbols, which are used to perform not only arithmetic computations such as addition, subtraction, multiplication, or division but also logical computations. The operators specify the type of operation that needs to be applied. The values that are operated on by the operators are called operands. The combination of variables and constants along with Python operators create expressions. In other words, operators take more than one expression or operand to perform arithmetic and logical computations on it. The expressions are evaluated by using the operator precedence rules, which determine the order in which the operators in an expression are evaluated. Python Programming – Expressions Python Programming – Operators Python Programming – Logical/Boolean Operators The precedence and associativity of operators are described in the following sections. Python Operator Precedence The precedence of an operator tells the compiler the order in which the operators should be evaluated. In case two operators with the same precedence are part of an expression,…

Python Programming - Logical and Physical Line

Python Programming – Logical and Physical Line

You can learn about Strings in Python Programs with Outputs helped you to understand the language better. Python Programming – Logical and Physical Line The string is an ordered sequence of letters/characters. It is enclosed in single (“) or double (” “) or triple quotation(‘”) marks. The quotes are not a part of the string. They only tell the computer where the string constant begins and ends. They can have any character or sign, including space in between them. ‘Hello, World!’ is a string; it is so-called because it contains a ‘string’ of letters. You can identify strings because they are enclosed in quotation marks. If you are not sure which type of value it is, the interpreter can tell you. >>> type(‘Hello, World!’) <class ‘str’> It is possible to change one type of value/variable to another type. It is known as type conversion or typecasting. The conversion can be done explicitly (the programmer specifies the conversion) or implicitly (the interpreter automatically converts the data type). An empty string contains no characters and has a length 0, represented by two quotation marks. The string with length 1 represents a character in Python. For example ‘a’, ‘b’, ‘c’ are strings of length one.…

Python Programming – String

Python Programming – String

You can learn about Strings in Python Programs with Outputs helped you to understand the language better. Python Programming – String A string is a sequence of one or more characters (letters, numbers, symbols) that can be either a constant or a variable. Strings are the form of data used in programming for storing and manipulating text, such as words, names, and sentences. They are immutable data types, i.e., they are unchanging. You can write them in Python using single quotes, double quotes, or triple quotes. The quotes are not part of the string. They only tell the computer where the string constant begins and ends. String literals are written by enclosing a sequence of characters in single quotes (‘hello’), double quotes (“hello”) or triple quotes (‘”hello”‘ or “””hello”””). Strings are objects of Python’s built-in class ‘str’. A string value is a collection of one or more characters put in single, double, or triple quotes. Creating String To create a string, put the sequence of characters inside single quotes, double quotes, or triple quotes and then assign it to a variable. The single quotes and double quotes work in the same manner for the string creation. Triple quotes can be used in…

Python Programming – Assert Statement

Python Programming – Assert Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Assert Statement The assert statement checks if a given logical expression is true; or false. The program executes only if the expression is true and otherwise it raises the AssertionError. The syntax of assert is: assert Expression[, Arguments] Example Demo of an assert statement. # Demo of the assert statement num=int(input(‘Enter a number: ‘)) assert num>=0 print(‘You entered: nun) RUN >>> Enter a number: 8 You entered: 8 >>> >>> Enter a number: -4 Traceback (most recent call last): File “C:\Users\shash\AppData\Local\Programs\Python\Python37-32\assert.py”, line 2, in <module> assert num>=0 AssertionError >>> Python Programming – If Statement Python Programming – If-Else Statement Python Programming – The While Statement The print statement will display if the entered number is greater than or equal to 0. Negative numbers result in aborting the program after showing the AssertionError as shown in Figure 5.56. So, Assertions are simply boolean expressions that check if the condition returns true or not. If it is true, the program does nothing and moves to the next line of code. However, if it’s false, the program stops and throws an error. Python assert…

Python Programming - Continue Statement

Python Programming – Continue Statement

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Continue Statement The continue statement forces the next iteration of the loop to take place, skipping any statement(s) that follows the continue statement in the current iteration, i.e., the continue statement jumps back to the top of the loop for the next iteration. Example 46. Demo of continue statement. # Demo of continue statement count=0 while True: count= count +1 # End loop if count is greater than 10 if (count > 10): break # Skip 5 if (count == 5): continue print(count)RUN >>> 1 2 3 4 5 6 7 8 9 10 >>> Figure 5.53 shows the use of the continue statement. At the top of the loop, the while condition is tested, and the loop is entered, if it’s true. Here, when the count is equal to 5, the program does not get to the print count) statement. So, the number 5 is skipped with the continue statement, and the loop ends with the break statement.’ Note in Figure 5.54, U is skipped due to the use of the continue statement. Python Programming – Conditional and Iterative Statements…

Python Programming - Nested Loops

Python Programming – Nested Loops

You can learn about Control Statements in Python Programs with Outputs helped you to understand the language better. Python Programming – Nested Loops A loop may also contain another loop within its body. This form of a loop inside another loop is called a nested loop. In a nested loop construction, the inner loop must terminate before the ending of the outer loop. Example 36. Program to print pyramid of stars using for loop. * * * * * * * * * * * * * * * # Nested for loop n = int(input(“Enter a number to form pyramid :”)) for i in range(l,n): for j in range(l,i): print(“*”,end =” “) print( ) Python Programming – Conditional and Iterative Statements Python Programming – Flowcharts for Sequential, Decision-Based and Iterative Processing Introduction to Python Programming – Testing and Debugging Example37. Program to print inverted triangle of stars using for loop. * * * * * * * * * * * * * * * # Print the pattern for a in range(5, 0, -1): for b in range(1,a+1): print(“*”, end=’ ‘) print(” “) Example 38. Program to print patterns of stars. * * * * * * * * *…