Krivalar Tutorials 
Krivalar Tutorials

Python - Syntax



< Introduction    Variable >






Python programs can be written in two ways:

  1. Interactive mode
  2. Script mode

Interactive mode

  • The simple way to type and run the python program is using the python's interactive command line.
  • This command line is sometimes called an interactive prompt./li>
  • When you can type python code at the command line, this will show some information. For instance, consider the following code.

% python
Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 18:58:18) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

  • In the above code, the % character indicates the start of the Python generic system. After that, a little information text is displayed which specifies the information about the version number, help, copyright, and so on.
  • The next line is the Python prompt '>>>' where you can type the Python statement and expression.
  • If you type your code after the prompt('>>>'), the result will be displayed immediately after you press the enter key.

The First Python Program

To run your first python code in the command prompt. You have to open the command line and type "python". This will open a python command line as shown below:



Output:



Note:

We should know exactly how we can access and type python on various platforms

  • On the window platform, you can access Python on the DOS console window.
  • On Unix, Linux, and MacOSX, you might access and type Python in the shell or terminal.

By using an interactive prompt, you can run many python commands as you like. Also, you can type any expression without saying print explicitly in the prompt. The result will be produced immediately. Once you have finished your code with the python command line, you can exit from the python command line interface by typing the following code.

exit()

Script mode

  • In the interactive prompt, we can write only single-line statement.
  • It is impossible to run multi-line statements.
  • To run multi-line statements, we can write our program in script mode.
  • In script mode, we can write the python statements on the text editor and save this file in the proper directory path.
  • The program file is saved with a .py extension.

Following are steps to explain how to write python code in script mode.

  • Step 1: Open any text editor, go to the file menu select new from the list.


  • Step 2: Write your code in the new document.
  • Step 3: Next, save your file in the proper path and give a name to the document with a .py file extension.


  • Step 4: Now, run your python file in the command prompt.


Block Indentation

  • Python uses the indentation for defining block and loop construct.
  • Whitespace is used for indentation
  • Python uses the (:) colon symbol and indentation for indicating where blocks begin and end.
  • That is, Python uses a colon for showing the start of blocks and then contains an indentation line below it.

For example

def function( ):
    X=4
    print(X)

 

Another Example

If x=1:
	print ("how are you For?")
Else:
	print (2+2)
  • For indentation, we use the spaces and tab characters.
  • The Python 3.x mix both tab and spaces.
  • The Python 2.x does not mix the space and tab.

< Introduction    Variable >





















Searching using Binary Search Tree