Learn Python – First Python Program- Basic and advance

In this Section, we will discuss the simple syntax of Python, we will run a simple program to print Hello World on the console.

Python provides us the two ways to run a program:

Using Interactive interpreter prompt

Using a script file

Let’s discuss each one of them in detail.

Interactive interpreter prompt

Python offers us the characteristic to execute the Python declaration one by means of one at the interactive prompt. It is preferable in the case where we are involved about the output of every line of our Python program.

To open the interactive mode, open the terminal (or command prompt) and type python (python3 in case if you have Python2 and Python3 both installed on your system).

It will open the following instant the place we can execute the Python announcement and test their affect on the console.

After writing the print statement, press the Enter key.

Here, we get the message “Hello World !” printed on the console.

Using a script file (Script Mode Programming)

The interpreter prompt is nice to run the single-line statements of the code. However, we can’t write the code every-time on the terminal. It is no longer suitable to write multiple lines of code.

Using the script mode, we can write a couple of traces code into a file which can be finished later. For this purpose, we want to open an editor like notepad, create a file named and store it with .py extension, which stands for “Python”. Now, we will implement the above example the use of the script mode.

print ("hello world"); #here, we have used print() function to print the message on the console.    

To run this file named as first.py, we need to run the following command on the terminal.

Step – 1: Open the Python interactive shell, and click on “File” then pick out “New”, it will open a new blank script in which we can write our code.

Step -2: Now, write the code and press “Ctrl+S” to retailer the file.

Step – 3: After saving the code, we can run it by using clicking “Run” or “Run Module”. It will display the output to the shell.

The output will be shown as follows.

Step – 4: Apart from that, we can additionally run the file using the running system terminal. But, we be conscious of the direction of the directory the place we have saved our file.

Open the command line prompt and navigate to the directory.

We need to type the python keyword, followed by the file name and hit enter to run the Python file.

Multi-line Statements

Multi-line statements are written into the notepad like an editor and saved it with .py extension. In the following example, we have described the execution of the a couple of code lines the usage of the Python script.

Code:

name = "Andrew Venis"  
branch = "Computer Science"  
age = "25"  
print("My name is: ", name, )  
print("My age is: ", age)  

Script File:

Pros and Cons of Script Mode

The script mode has few benefits and hazards as well. Let’s understand the following advantages of jogging code in script mode.

We can run multiple lines of code.

Debugging is easy in script mode.

It is appropriate for beginners and also for experts.

Let’s see the disadvantages of the script mode.

We have to save the code every time if we make any change in the code.

It can be tedious when we run a single or a few lines of code.

Get Started with PyCharm

In our first program, we have used gedit on our CentOS as an editor. On Windows, we have an alternative like notepad or notepad++ to edit the code. However, these editors are no longer used as IDE for python in view that they are unable to show the syntax related suggestions.

JetBrains gives the most popular and a widely used cross-platform IDE PyCharm to run the python programs.

PyCharm installation

As we have already stated, PyCharm is a cross-platform IDE, and consequently it can be established on a variety of the working systems. In this section of the tutorial, we will cowl the installation technique of PyCharm on Windows, MacOS, CentOS, and Ubuntu.

Windows

Installing PyCharm on Windows is very simple. To installation PyCharm on Windows running system, visit the hyperlink https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows to download the executable installer. Double click the installer (.exe) file and set up PyCharm by means of clicking subsequent at every step.

To create a first application to Pycharm follows the following step.

Step – 1. Open Pycharm editor. Click on “Create New Project” choice to create new project.

Step – 2. Select a location to save the project.

We can retailer the newly created venture at favored memory region or can maintain file area as it is but atleast alternate the project default title untitled to “FirstProject” or something meaningful. Pycharm robotically determined the established Python interpreter. After alternate the title click on the “Create” Button.

Step – three Click on “File” menu and pick out “New”. By clicking “New” option it will show a range of file formats. Select the “Python File”.

Step – 4 Now type the identify of the Python file and click on on “OK”. We have written the “FirstProgram”.

Step – 5. Now type the first application – print(“Hello World”) then click on the “Run” menu to run program.

Step – 6. The output will show up at the bottom of the screen.

Basic Syntax of Python

Indentation and Comment in Python

Indentation is the most substantial thinking of the Python programming language. Improper use of indentation will stop up “IndentationError” in our code.

Indentation is nothing but including whitespaces before the declaration when it is needed. Without indentation Python would not comprehend which declaration to be finished to next. Indentation additionally defines which statements belong to which block. If there is no indentation or wrong indentation, it will show “IndentationError” and interrupt our code.

Python indentation defines the unique crew of statements belongs to the specific block. The programming languages such as C, C++, java use the curly braces {} to define code blocks.

In Python, statements that are the identical degree to the proper belong to the equal block. We can use four whitespaces to define indentation. Let’s see the following traces of code.

Example –

list1 = [1, 2, 3, 4, 5]  
for i in list1:  
    print(i)  
    if i==4:  
       break  
print("End of for loop")  

Output:

1
2
3
4
End of for loop

Explanation:

In the above code, for loop has a code blocks and if the statement has its code block inside for loop. Both indented with 4 whitespaces. The ultimate print() announcement is now not indented; it truly is capability it doesn’t belong to for loop.

Comments in Python

Comments are necessary for defining the code and assist us and other to recognize the code. By looking the comment, we can easily understand the intention of every line that we have written in code. We can additionally locate the error very easily, fix them, and use in other applications.

In Python, we can apply comments using the # hash character. The Python interpreter absolutely ignores the traces accompanied by a hash character. A desirable programmer continually uses the feedback to make code underneath stable. Let’s see the following example of a comment.

name  = "Thomas"   # Assigning string value to the name variable   

We can add remark in every line of the Python code.

Fees = 10000      # defining course fees is 10000  
Fees = 20000      # defining course fees is 20000  

It is good idea to add code in any line of the code section of code whose cause is not obvious. This is a nice exercise to study while doing the coding.

Types of Comment

Python affords the facility to write remarks in two ways- single line remark and multi-line comment.

Single-Line Comment – Single-Line comment starts with the hash # character observed by using textual content for similarly explanation.

# defining the marks of a student   
Marks = 90  

We can additionally write a remark next to a code statement. Consider the following example.

Name = "James"   # the name of a student is James  
Marks = 90            # defining student's marks  
Branch = "Computer Science"   # defining student branch  

Multi-Line Comments – Python does not have explicit aid for multi-line comments but we can use hash # character to the multiple lines. For instance –

# we are defining for loop  
# To iterate the given list.  
# run this code.  

We can also use another way.

" " "   
This is an example  
Of multi-line comment  
Using triple-quotes   
" " "  

This is the simple introduction of the comments. Visit our Python Comment tutorial to study it in detail.

Python Identifiers

Python identifiers refer to a name used to pick out a variable, function, module, class, module or other objects. There are few regulations to observe whilst naming the Python Variable.

A variable name must start with either an English letter or underscore (_).

A variable name cannot start with the number.

Special characters are not allowed in the variable name.

The variable’s name is case sensitive.

Let’s understand the following example.

Example –

number = 10  
print(num)  
  
_a = 100  
print(_a)  
  
x_y = 1000  
print(x_y)  

Output:

10
100
1000

We have defined the basic syntax of the Python programming language. We have to be familiar with the core notion of any programming languages. Once we memorize the principles as referred to above. The experience of gaining knowledge of Python will end up easier.

CentOS

To installation PyCharm on CentOS, visit the hyperlink https://www.javatpoint.com/how-to-install-pycharm-on-centos. The link will information you to installation PyCharm on the CentOS.

MacOS

To installation PyCharm on MacOS, go to the hyperlink https://www.javatpoint.com/how-to-install-pycharm-on-mac. The link will information you to deploy PyCharm on the MacOS.

Ubuntu

To installation PyCharm on Ubuntu, go to the hyperlink https://www.javatpoint.com/how-to-install-pycharm-in-ubuntu. The hyperlink will information you to set up PyCharm on Ubuntu.