This article demonstrates several examples of Running Instructions in an Interactive Interpreter in Python. In order to work with a python command-line interpreter, first, you need to install python. After that launch the command prompt and enter the python command as given below.
Further instructions can be given on this command-line interpreter. The following code shows the basic syntax of python. While you enter a python command, you will get results immediately when you press enter. In case, an error occurs, it will also be shown immediately.
Examples of Running Instructions in an Interactive Interpreter in Python
As can be seen, the print() method displays the text on the command prompt. Also, you can use both variables and literals. For instance print(str+”hello”) concatenates variable and literal value. However, the use of variables other than string type results in an error. For this purpose, you need to convert it into a numeric type. For example, you can use print(str(x)+”abc”). Basically, the str() function converts an object to a string.
G:python>python Python 3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:57:54) [MSC v.1924 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print('Welcome to Python Programming!') Welcome to Python Programming! >>>
Using Identifiers, Variables, and Expressions
The following code shows the use of variables, identifiers, and python expressions. Since Python doesn’t require explicit declaration of variables, the variables are created when they are assigned values.
>>> x=90 >>> y=56.8976 >>> z=x*x+y*y >>> print(z) 11337.33688576 >>>
Using Conditional Statements in Python
Like any other programming language, Python also supports decision-making using the if statement. The following example describes the single line if statement when the else clause is not required. Basically, it returns a sequence of numbers.
>>> x=45 >>> y=789 >>> if(a<b): print(str(a) + ' is smaller!') ... 200 is smaller! >>>
Using if….else in Python
Similarly, you can use the if…else statement when there are two sets of statements to execute depending on the outcome of a condition. Also, note the significance of indentation for marking a block. In other words, the indentation signifies the beginning and end of a block.
>>> x=56.897 >>> y=456 >>> if(x>y): ... print(str(x) + " is Larger!") ... else: ... print(str(y) + " is larger!") ... 456 is larger! >>>
Using Nested if Statement in Python
Likewise, when there are more conditions to check, you can use the nested if statement. The following example shows how to find the largest of three numbers in python. Also, the indentations after each if or else statement specifies the corresponding block of statements.
>>> p=568 >>> q=36 >>> r=49 >>> if(p>q): ... if(p>r): ... print(str(p) + " is largest!") ... else: ... print(str(r) + " is largest!") ... else: ... if(q>r): ... print(str(q) + " is largest!") ... else: ... print(str(r) + " is largest!") ... 568 is largest!
Using Operators in Python
Like other programming languages, Python also has several categories of operators. Basically, Python provides arithmetic operators, relational operators, logical operators, bitwise operators, assignment operators, identity operators, and membership operators.
The following code shows an example of relational (comparison) and logical operators. As can be seen, the logical operator and is used to find a minimum of three numbers. Basically, python has three logical operators – and, or, not. While the and operator requires all the expressions to be true for returning a true value. In co contrast, the or operator requires any one of the conditional expressions to be true. Similarly, the not operator negates the condition.
>>> m=46 >>> n=97 >>> k=34 >>> if((m<n) and (m<k)): ... print(str(m) + " is smallest!") ... else: ... if((n<m) and (n<k)): ... print(str(n) + " is smallest!") ... else: ... print(str(k) + " is smallest!") ... 34 is smallest! >>>
Using Loops in Python
In order to perform iterations, python has for and while loop constructs. Basically, python provides the for loop and the while loop. However, there are many variations of the basic syntax of these loops.
For Loop Example
The following code shows an example of using for loop in python. Also, note the use of the range() function. Basically, the range() function returns a sequence of numbers. However, the sequence starts from 0 by default. Hence, you need to use its result accordingly in your code. For instance, in order to display values starting from 1, use i+1 instead. Also, the sequence ends at n-1. Further, the default increment is 1. However, you can change it. For instance, range(1, 10, 2) makes the increment by 2.
>>> x=10 >>> for i in range(x): ... print(i) ... 0 1 2 3 4 5 6 7 8 9
While Loop Example
Likewise, the following code shows how to use the while loop. Also, the indentation provided after the while statement indicates the loop statements. As can be seen, the following code displays the numbers 1 to 10. In contrast to the for loop, the while loop requires the loop control variable to be updated. In other words, you need to manually change its value. Otherwise, it becomes an infinite loop.
>>> n=10 >>> i=1 >>> while(i<=n): ... print(i) ... i=i+1 ... 1 2 3 4 5 6 7 8 9 10
User-Defined Functions in Python
Whenever you want to use a block of statements in several places in the code, you can create a user-defined function. Basically, the definition of a user-defined function starts with the def keyword. It is followed by the function name and parameter list. Like conditional and loop statements, the start and end of a function are indicated by the corresponding indentation.
>>> def myfunction(a, b): ... return a+b ... >>> myfunction(56, 8.4569999) 64.4569999 >>>
Drawbacks of Running Instructions in an Interactive Interpreter in Python
While Running Instructions in an Interactive Interpreter in Python makes it easy to learn, still it has certain drawbacks. For instance, you need to save the commands in a file in order to run them later. For this purpose, you can use python scripts. Basically, python scripts are files that contain python statements. The following example shows a python script. Also, how to run a python script is given below.
#p1.py # find sum of two numbers a=100 b=200 c=a+b print(c)
The following example shows how to run a python script.