0% found this document useful (0 votes)
8 views34 pages

Python Basics

Uploaded by

prathapmaster7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
0% found this document useful (0 votes)
8 views34 pages

Python Basics

Uploaded by

prathapmaster7
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 34

Python

By Sasikala,
python trainer,
Adhoc Network Tech Company
What is Python?

 Python is a popular programming language. It was created by Guido


van Rossum, and released in 1991.
 It is used for:
 web development (server-side),
 software development,
 mathematics,
 system scripting.
What can Python do?

 Python can be used on a server to create web applications.


 Python can be used alongside software to create workflows.
 Python can connect to database systems. It can also read and modify
files.
 Python can be used to handle big data and perform complex
mathematics.
 Python can be used for rapid prototyping, or for production-ready
software development.
Why Python?

 Python works on different platforms (Windows, Mac, Linux, Raspberry


Pi, etc).
 Python has a simple syntax similar to the English language.
 Python has syntax that allows developers to write programs with
fewer lines than some other programming languages.
 Python runs on an interpreter system, meaning that code can be
executed as soon as it is written. This means that prototyping can be
very quick.
 Python can be treated in a procedural way, an object-oriented way or
a functional way.
Good to know

 The most recent major version of Python is Python 3, which we shall


be using in this tutorial. However, Python 2, although not being
updated with anything other than security updates, is still quite
popular.
 In this tutorial Python will be written in a text editor. It is possible to
write Python in an Integrated Development Environment, such as
Thonny, Pycharm, Netbeans or Eclipse which are particularly useful
when managing larger collections of Python files.
Python Syntax compared to other programming languages

 Python was designed for readability, and has some similarities to the
English language with influence from mathematics.
 Python uses new lines to complete a command, as opposed to other
programming languages which often use semicolons or parentheses.
 Python relies on indentation, using whitespace, to define scope; such
as the scope of loops, functions and classes. Other programming
languages often use curly-brackets for this purpose.
 Example: to print("Hello, World!")
Python Installation on Windows
 The installation requires downloading the official Python .exe installer
and running it on your system. The sections below will explain several
options and details during the installation process.

Step 1: Select Python Version


 Deciding on a version depends on what you want to do in Python. The
two major versions are Python 2 and Python 3. Choosing one over the
other might be better depending on your project details. If there are no
constraints, choose whichever one you prefer.
 We recommend Python 3, as Python 2 reached its end of life in
2020. Download Python 2 only if you work with legacy scripts and older
projects. Also, choose a stable release over the newest since the
newest release may have bugs and issues.
Step 2: Download Python Executable
Installer

 Start by downloading the Python executable installer for Windows:


 1. Open a web browser and navigate to the
Downloads for Windows section of the official Python website.
 2. Locate the desired Python version.
 3. Click the link to download the file. Choose either the Windows
32-bit or 64-bit installer.
 The download is approximately 25MB.
Step 3: Run Executable Installer

 The steps below guide you through the installation process:


 1. Run the downloaded Python Installer.
 2. The installation window shows two checkboxes:
 Admin privileges. The parameter controls whether to install Python
for the current or all system users. This option allows you to change
the installation folder for Python.
 Add Python to PATH. The second option places the executable in the
PATH variable after installation. You can also add Python to the PATH
environment variable manually later.
 For the most straightforward installation, we recommend ticking both
checkboxes.
 3. Select the Install Now option for the recommended installation (in
that case, skip the next two steps).
 To adjust the default installation options, choose Customize
installation instead and proceed to the following step.
 The default installation installs Python to C:\Users\[user]\AppData\
Local\Programs\Python\Python[version] for the current user. It
includes IDLE (the default Python editor), the PIP package manager,
and additional documentation. The installer also creates necessary
shortcuts and file associations.
 Customizing the installation allows changing these installation
options and parameters.
 4. Choose the optional installation features. Python works without
these features, but adding them improves the program's usability.
 Click Next to proceed to the Advanced Options screen.
 5. The second part of customizing the installation includes advanced
options.
 Choose whether to install Python for all users. The option changes the
install location to C:\Program Files\Python[version]. If selecting the
location manually, a common choice is C:\Python[version] because it
avoids spaces in the path, and all users can access it. Due to
administrative rights, both paths may cause issues during package
installation.
 Other advanced options include creating shortcuts, file associations,
and adding Python to PATH.
After picking the appropriate options, click Install to start the installation
6. Select whether to disable the path length limit. Choosing this option
will allow python to bypass the 260-character MAX_PATH limit
 The option will not affect any other system settings, and disabling it
resolves potential name-length issues. We recommend selecting the option
and closing the setup.

Step 4: Add Python to Path


(Optional)
 If the Python installer does not include the Add Python to PATH checkbox
or you have not selected that option, continue in this step. Otherwise, skip
to the next step.
 Adding the Python path to the PATH variable alleviates the need to use the
full path to access the Python program in the command line. It instructs
Windows to review all the folders added to the PATH environment variable
and to look for the python.exe program in those folders.
 To add Python to PATH, do the following:
 1. In the Start menu, search for Environment Variables and press Enter.
2. Click Environment Variables to open the overview screen.
3. Double-click Path on the list to edit it.
Alternatively, select the variable and click the Edit button.
4. Double-click the first empty field and paste the Python installation
folder path.
 Alternatively, click the New button instead and paste the path.
 5. Click OK to save the changes. If the command prompt is open, restart it for
the following step.

 Step 5: Verify Python Was Installed


on Windows
 The first way to verify that Python was installed successfully is through the
command line. Open the command prompt and run the following command:
 python –version

 The output shows the installed Python version.


 The second way is to use the GUI to verify the Python installation. Follow the
steps below to run the Python interpreter or IDLE:
 1. Navigate to the directory where Python was installed on the system.
 2. Double-click python.exe (the Python interpreter) or IDLE.
 3. The interpreter opens the command prompt and shows the following
window:
 Running IDLE opens Python's built-in IDE:
In both cases, the installed Python version shows on the screen, and the editor is ready for use.

Step 6: Verify PIP Was Installed


To verify whether PIP was installed, enter the following command in the command prompt:

pip --version

 If it was installed successfully, you should see the PIP version


number, the executable path, and the Python version:


PIP has not been installed yet if you get the following output:
'pip' is not recognized as an internal or external command, Operable
program or batch file.
If an older version of Python is installed or the PIP installation option is
disabled during installation, PIP will not be available. To install PIP, see
our article How to Install PIP on Windows.
Step 7: Install virtualenv (Optional)
python software packages install system-wide by default. Consequently, whenever a single project-specific package is changes, it changes for all your Python projects.
The virtualenv package enables making isolated local virtual environments for Python projects.Virtual environments help avoid package conflicts and enable choosing specific packages versions per project
To install virtualenv, run the following command in the command prompt
pip install virtualenv

Wait for the installation to complete. Once done, it is installed on the system and available for use.
 Python is an interpreted programming language, this means that as a
developer you write Python (.py) files in a text editor and then put those
files into the python interpreter to be executed.
 The way to run a python file is like this on the command line:
 C:\Users\Your Name>python helloworld.py
 Where "helloworld.py" is the name of your python file.
 Let's write our first Python file, called helloworld.py, which can be done in
any text editor.
helloworld.py
print("Hello, World!")

 Simple as that. Save your file. Open your command line, navigate to the
directory where you saved your file, and run:
 C:\Users\Your Name>python helloworld.py
 The output should read:
 Hello, World!
 Congratulations, you have written and executed your first Python program.
The Python Command Line
 To test a short amount of code in python sometimes it is quickest and
easiest not to write the code in a file. This is made possible because
Python can be run as a command line itself.
 Type the following on the Windows, Mac or Linux command line:
 C:\Users\Your Name>python
 Or, if the "python" command did not work, you can try "py":C:\Users\
Your Name>py
 From there you can write any python, including our hello world
example from earlier in the tutorial:
 C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900
32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
 Which will write "Hello, World!" in the command line:
C:\Users\Your Name>python
Python 3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("Hello, World!")
Hello, World!
Whenever you are done in the python command line, you can simply
type the following to quit the python command line interface:
exit()
Python Syntax

 As we learned in the previous page, Python syntax can be executed


by writing directly in the Command Line:
 >>> print("Hello, World!")
Hello, World!
 Or by creating a python file on the server, using the .py file extension,
and running it in the Command Line:
 C:\Users\Your Name>python myfile.py
Python Indentation
 Indentation refers to the spaces at the beginning of a code line.
 Where in other programming languages the indentation in code is for
readability only, the indentation in Python is very important.
 Python uses indentation to indicate a block of code.
 Example
 if 5 > 2:
print("Five is greater than two!“)
 Python will give you an error if you skip the indentation:
 Example
 Syntax Error:
 if 5 > 2:
print("Five is greater than two!")
 Note:The number of spaces is up to you as a programmer, the most common
use is four, but it has to be at least one.
 Example
 if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!“)
 Example
 if 5 > 2:
print("Five is greater than two!")
if 5 > 2:
print("Five is greater than two!")
 We Will discuss variables, Comments, datatypes in the further
sections
THANK
YOU

You might also like