First Python ProgramIn this Section, we will discuss the basic 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:
Let's discuss each one of them in detail. Interactive interpreter promptPython provides us the feature to execute the Python statement one by one at the interactive prompt. It is preferable in the case where we are concerned about the output of each 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 prompt where we can execute the Python statement and check their impact 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 best to run the single-line statements of the code. However, we cannot write the code every-time on the terminal. It is not suitable to write multiple lines of code. Using the script mode, we can write multiple lines code into a file which can be executed later. For this purpose, we need to open an editor like notepad, create a file named and save it with .py extension, which stands for "Python". Now, we will implement the above example using the script mode. 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 "File" then choose "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 save the file. ![]() Step - 3: After saving the code, we can run it by 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 also run the file using the operating system terminal. But, we should be aware of the path of the directory where we have saved our file.
![]()
![]() Multi-line StatementsMulti-line statements are written into the notepad like an editor and saved it with .py extension. In the following example, we have defined the execution of the multiple code lines using the Python script. Code: Script File: ![]() ![]() Pros and Cons of Script ModeThe script mode has few advantages and disadvantages as well. Let's understand the following advantages of running code in script mode.
Let's see the disadvantages of the script mode.
Get Started with PyCharmIn 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 not used as IDE for python since they are unable to show the syntax related suggestions. JetBrains provides the most popular and a widely used cross-platform IDE PyCharm to run the python programs. PyCharm installationAs we have already stated, PyCharm is a cross-platform IDE, and hence it can be installed on a variety of the operating systems. In this section of the tutorial, we will cover the installation process of PyCharm on Windows, MacOS, CentOS, and Ubuntu. WindowsInstalling PyCharm on Windows is very simple. To install PyCharm on Windows operating system, visit the link https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows to download the executable installer. Double click the installer (.exe) file and install PyCharm by clicking next at each step. To create a first program to Pycharm follows the following step. Step - 1. Open Pycharm editor. Click on "Create New Project" option to create new project. ![]() Step - 2. Select a location to save the project.
![]() Step - 3. Click on "File" menu and select "New". By clicking "New" option it will show various file formats. Select the "Python File". ![]() Step - 4. Now type the name of the Python file and click on "OK". We have written the "FirstProgram". ![]() Step - 5. Now type the first program - print("Hello World") then click on the "Run" menu to run program. ![]() Step - 6. The output will appear at the bottom of the screen. ![]() Basic Syntax of PythonIndentation and Comment in PythonIndentation is the most significant concept of the Python programming language. Improper use of indentation will end up "IndentationError" in our code. Indentation is nothing but adding whitespaces before the statement when it is needed. Without indentation Python doesn't know which statement to be executed to next. Indentation also defines which statements belong to which block. If there is no indentation or improper indentation, it will display "IndentationError" and interrupt our code. ![]() Python indentation defines the particular group of statements belongs to the particular block. The programming languages such as C, C++, java use the curly braces {} to define code blocks. In Python, statements that are the same level to the right belong to the same block. We can use four whitespaces to define indentation. Let's see the following lines of code. Example - 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 four whitespaces. The last print() statement is not indented; that's means it doesn't belong to for loop. Comments in PythonComments are essential for defining the code and help us and other to understand the code. By looking the comment, we can easily understand the intention of every line that we have written in code. We can also find the error very easily, fix them, and use in other applications. In Python, we can apply comments using the # hash character. The Python interpreter entirely ignores the lines followed by a hash character. A good programmer always uses the comments to make code under stable. Let's see the following example of a comment. We can add comment in each line of the Python code. It is good idea to add code in any line of the code section of code whose purpose is not obvious. This is a best practice to learn while doing the coding. Types of CommentPython provides the facility to write comments in two ways- single line comment and multi-line comment. Single-Line Comment - Single-Line comment starts with the hash # character followed by text for further explanation. We can also write a comment next to a code statement. Consider the following example. Multi-Line Comments - Python doesn't have explicit support for multi-line comments but we can use hash # character to the multiple lines. For example - We can also use another way. This is the basic introduction of the comments. Visit our Python Comment tutorial to learn it in detail. Python IdentifiersPython identifiers refer to a name used to identify a variable, function, module, class, module or other objects. There are few rules to follow while naming the Python Variable.
Let's understand the following example. Example - Output: 10 100 1000 We have defined the basic syntax of the Python programming language. We must be familiar with the core concept of any programming languages. Once we memorize the concepts as mentioned above. The journey of learning Python will become easier. CentOSTo install PyCharm on CentOS, visit the link https://www.w3cschoool.com/how-to-install-pycharm-on-centos. The link will guide you to install PyCharm on the CentOS. MacOSTo install PyCharm on MacOS, visit the link https://www.w3cschoool.com/how-to-install-pycharm-on-mac. The link will guide you to install PyCharm on the MacOS. UbuntuTo install PyCharm on Ubuntu, visit the link https://www.w3cschoool.com/how-to-install-pycharm-in-ubuntu. The link will guide you to install PyCharm on Ubuntu. Next TopicPython Variables
|
Python tutorial provides basic and advanced concepts of Python.
Vue.js is an open-source progressive JavaScript framework
HTML refers to Hypertext Markup Language. HTML is the gateway ...
Java is an object-oriented, class-based computer-programming language.
PHP is an open-source,interpreted scripting language.
Spring is a lightweight framework.Spring framework makes ...
JavaScript is an scripting language which is lightweight and cross-platform.
CSS refers to Cascading Style Sheets...
jQuery is a small and lightweight JavaScript library. jQuery ...
SQL is used to perform operations on the records stored in the database.
C programming is considered as the base for other programming languages.
JavaScript is an scripting language which is lightweight and cross-platform.
Vue.js is an open-source progressive JavaScript framework
ReactJS is a declarative, efficient, and flexible JavaScript library.
jQuery is a small and lightweight JavaScript library. jQuery ...
Node.js is a cross-platform environment and library for running JavaScript app...
TypeScript is a strongly typed superset of JavaScript which compiles to plain JavaScript.
Angular JS is an open source JavaScript framework by Google to build web app...
JSON is lightweight data-interchange format.
AJAX is an acronym for Asynchronous JavaScript and XML.
ES6 or ECMAScript 6 is a scripting language specification ...
Angular 7 is completely based on components.
jQuery UI is a set of user interface interactions built on jQuery...
Python tutorial provides basic and advanced concepts of Python.
Java is an object-oriented, class-based computer-programming language.
Node.js is a cross-platform environment and library for running JavaScript app...
PHP is an open-source,interpreted scripting language.
Go is a programming language which is developed by Google...
C programming is considered as the base for other programming languages.
C++ is an object-oriented programming language. It is an extension to C programming.
C# is a programming language of .Net Framework.
Ruby is an open-source and fully object-oriented programming language.
JSP technology is used to create web application just like Servlet technology.
The JSTL represents a set of tags to simplify the JSP development.
ASP.NET is a web framework designed and developed by Microsoft.
Perl is a cross-platform environment and library for running JavaScript...
Scala is an object-oriented and functional programming language.
VBA stands for Visual Basic for Applications.
Spring is a lightweight framework.Spring framework makes ...
Spring Boot is a Spring module that provides the RAD feature...
Django is a Web Application Framework which is used to develop web applications.
Servlet technology is robust and scalable because of java language.
The Struts 2 framework is used to develop MVC based web applications.
Hibernate is an open source, lightweight, ORM tool.
Solr is a scalable, ready-to-deploy enterprise search engine.
SQL is used to perform operations on the records stored in the database.
MySQL is a relational database management system based...
Oracle is a relational database management system.
SQL Server is software developed by Microsoft.
PostgreSQL is an ORDBMS.
DB2 is a database server developed by IBM.
Redis is a No SQL database which works on the concept of key-value pair.
SQLite is embedded relational database management system.
MongoDB is a No SQL database. It is an document-oriented database...
Memcached is a free, distributed memory object caching system.
Hibernate is an open source, lightweight, ORM tool.
PL/SQL is a block structured language that can have multiple blocks in it.
DBMS Tutorial is software that is used to manage the database.
Spark is a unified analytics engine for large-scale data processing...
IntelliJ IDEA is an IDE for Java Developers which is developed by...
Git is a modern and widely used distributed version control system in the world.
GitHub is an immense platform for code hosting.
SVN is an open-source centralized version control system.
Maven is a powerful project management tool that is based on POM.
Jsoup is a java html parser.
UML is a general-purpose, graphical modeling language.
RESTful Web Services are REST Architecture based Web Services.
Postman is one testing tools which is used for API testing.
JMeter is to analyze the performance of web application.
Jenkins builds and tests our software projects.
SEO stands for Search Engine Optimization.
MATLAB is a software package for mathematical computation, visualization...
Unity is an engine for creating games on multiple platforms.
Hadoop is an open source framework.
Pig is a high-level data flow platform for executing Map Reduce programs of Hadoop.
Spark is a unified analytics engine for large-scale data processing...
Spring Cloud is a framework for building robust cloud applications.
Spring Boot is a Spring module that provides the RAD feature...
AI is one of the fascinating and universal fields of Computer.
Cloud computing is a virtualization-based technology.
AWS stands for Amazon Web Services which uses distributed IT...
Microsoft Azure is a cloud computing platform...
IoT stands for Internet of Things...
Spring Cloud is a framework for building robust cloud applications.
Email:jjw.quan@gmail.com