A complete course in Python typically covers the following topics:-
on
Get link
Facebook
X
Pinterest
Email
Other Apps
Get Started With Python
Python is one of the most popular programming languages, often used for data analytics and machine learning applications. This article will help you get started with Python Programming language by installing it and running your first program. Also, it will walk you through the basic concepts.
Table of contents
What is Python?
Applications of Python
Install Python
Create and Run Your First Python Program
Run Python Using IDLE
Run Python on Command Line
Execute Python File
Syntax and Indentation in Python
Using Blank Lines in code
End-of-Line to Terminate a Statement
Semicolumn to Separate Multiple Statements
Indentation
Next Steps
What is Python?
Python is a general-purpose, high-level, interpreted, object-oriented programming language used for various applications.
Python was developed by Guido Van Rossum in 1989 while working at National Research Institute in the Netherlands. But officially, Python was made available to the public in 1991.
Python code syntax uses English keywords, making it easy to understand. Therefore, Python is recommended as the first programming language for beginners.
In addition, it includes high-level data structures, dynamic typing, dynamic binding, and many more features that make it very attractive for rapid application development.
Python is simple and easy to learn. Syntax emphasizes readability and therefore reduces the cost of program maintenance. In addition, it supports modules and packages, which encourages program modularity and code reuse.
The Python interpreter and the extensive standard library are available in source or binary form for all major platforms. In addition, it has a wide range of standard and third-party libraries that helps in rapid application development.
Applications of Python
We can use Python everywhere because Python is known for its general-purpose nature, which makes it applicable in almost every domain of software development. The most common application areas are:
Desktop Applications
Web Applications
Database Applications
Network Programming
Games
Data Analysis Applications
Machine Learning
Artificial Intelligence Applications
IoT
Install Python
It may be possible that some PCs and Macs will have Python already installed. You can check which version of Python is installed before proceeding to the installation.
Open the command line or terminal and type the below command.
python --version
If you find Python is not installed, install it using the following instructions.
Installing or updating Python on your computer is the first step to programming in Python. There are multiple installation methods, such as installing Python using an installer or a source code (.zip file)
Download the latest version of Python frompython.org. Once you’ve downloaded the installer as per the operating system, Next run an installer by double-clicking on the downloaded file and following the steps.
Install Python on macOS
Install Python on windows
After installation is completed, we will get asetup successfullyinstall message.
Let’s open the command line or terminal and type the below command to check the version of Python.
python --version
Now it’s showing 3.9.6, the currently installed version of Python on our machine, when writing this tutorial.
Create and Run Your First Python Program
Now the installation is completed, let’s see how to write our first Python program.
We can run Python by using the following three ways
Run Python using IDLE
Run Python interactively using the command line in immediate mode
Executing Python File
We will see each one by one but before that, let’s see how to write your first Python program.
Let’s write a simple statement in Python to print the ‘hello world’ on a screen.
Use theprint()function and write a message in its opening and closing brackets shown below.
A message is a string that is a sequence of characters. In Python, strings are enclosed inside single quotes, double quotes, or triple quotes.
IDLE is an integrated development environment (IDE) for Python. The Python installer contains the IDLE module by default. Thus, when you install Python, IDLE gets installed automatically.
Go to launchpad (for mac) and start icon (for Windows) and type IDLE, to open it. IDLE is an interactive Python Shell where you can write python commands and get the output instantly.
IDLE Python Editor
Let’s see how to print ‘hello world’ in Python using IDLE. Typeprint('Hello, World')and hit enter.
Hello world in Python
As you can see, we got the output after we executed aprint()function with a message.
IDLE has features like coding hinting, syntax highlighting, checking, etc.
Also, we can create a new file, write Python code, and save it with the.pyextension. The.pyis the python file extension which denotes this is the Python script.
Let’s see how to create a Python script using IDLE.
Go to the File Menu and select the new file option
Type the same code (hello world message) in it
Next, Go to the File menu to save it ashello.py
Create Python script
Next, To run the script, go to theRun > Run Module or simply click F5.
Run Python script in IDLE
Run Python on Command Line
We can also run Python on the command line.
Typepythoncommand on the command line or terminal to run Python interactively. It will invoke the interpreter in immediate mode.
Next, type Python code and press enter to get the output.
Please find the below image for demonstration.
Run Python on the command line
To exit this mode, typequit()and press enter.
Execute Python File
Python is an interpreted programming language in which we create a code file ( .py with extension) and pass it to the Python interpreter to execute whenever required.
Open any text editor and type the below code in it, and save it as ahello.py
Now, open the terminal or command line and use the below command to execute the message.py. You need to change the directory where this file is present before executing it.
python message.py
Herepythonis the command andmessage.pyis the file name you want to execute.
Run Python script using command line
You should get the following output.
Hello, World
Syntax and Indentation in Python
Syntax is the structure of language or aset of rulesthat defines how a Python program will be written and interpreted.
A line containing only white space, possibly with acommentor within a code, is known as a blank line, and Python ignores it.
End-of-Line to Terminate a Statement
In Python end of the line terminate the statement. So you don’t need to write any symbol to mark the end of the line to indicate the statement termination. For example, in other programming languages like Java and C, the statement must end with a semicolon (;).
Python statement ends with the token NEWLINE character (\n). But we can extend the statement over multiple lines using line continuation character (\). This is known as an explicit continuation.
Most Python style guides don’t recommend adding multiple statements on a single line, though occasionally, it improves readability.
Indentation
Python indentation tells a Python interpreter that the group of statements belongs to a particular block of code. The indentation makes the code look neat, clean, and more readable.
A block is a combination of all multiple statements. Inside a code block, we group multiple statements for a specific purpose.
In other programming languages like C or Java, use curly braces{ }to define a block of code. Python uses indentation to denote the code block.
Whitespace is used for indentationin Python to define the indentation level. Ideally, we should use4 spacesper indentation level. In Python, indented code blocks are always preceded by a colon (:) on the previous line.
Take the example of theif-else statementin Python.
If one code block is nested in another, the child code block should separate by 4 spaces from the parent code block.
If a block has to be more deeply nested, it is simply indented further to the right. You can understand it better by looking at the following lines of code.
Example:
num1 =500if num1 >100:if num1 %2==0:print('Even number is greater than 100')
Comments
Post a Comment