Lesson.-Python-Programming-Language

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

Introduction to Python Programming Language

Python is a popular programming language.

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

What is Python?

Python is a popular programming language. It was created by


Guido van Rossum, and released in 1991.

Uses of Python Programming Language

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.

Features of Python

 Python works on different platforms (Windows, Mac, Linux,


and Raspberry Pi).
 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.

Python 3 is the most recent version of Python.

Python 2 although not being updated with anything other than


security updates, is still quite popular.

Python can be written in a text editor or IDE (Integrated


Development Environment) such as Thonny, Pycharm, Netbeans or
Eclipse which are particularly useful when managing larger
collections of Python files.

An IDE is a software application that helps programmers develop


code efficiently by combining features like editing, building,
testing, and packaging.

Thonny is a free, open-source, beginner-friendly Python


Integrated Development Environment (IDE). It is designed to help
people learn to code and teach programming.

PyCharm is an Integrated Development Environment (IDE) for


Python that provides a variety of tools for developers.
NetBeans IDE is a free, open-source integrated development
environment (IDE) that allows users to develop applications for
desktop, mobile, and web.

It supports a variety of programming languages, including Java,


HTML5, PHP, and C++.

 Code editor: Helps write high-quality code with features like


autocompletion and color coding
 Debugging: Includes a graphical debugger
 Unit testing: Has an integrated unit tester
 Version control: Integrates with version control systems
 Web development: Supports web development with Django
 Data science: Integrates with Anaconda's data science platform

PyCharm is compatible with Windows, Linux, and macOS. It was


created by JetBrains, a Czech company, and was first released in
February 2010.

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.

Example:

Python Install

Many PCs and Macs will have python already installed.

To check if you have python installed on a Windows PC, search in


the start bar for Python or run the following on the Command
Line (cmd.exe):

C:\Users\Your Name>python --version

To check if you have python installed on a Linux or Mac, then on


linux open the command line or on Mac open the Terminal and
type:

python --version

If you find that you do not have Python installed on your


computer, then you can download it for free from the following
website: https://www.python.org/
Python Quickstart

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.

Example

First Python file, called helloworld.py, which can be done in


any text editor.

helloworld.py
print ("Hello, World!")

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.

Python Syntax

Execute Python Syntax

Python syntax can be executed by writing directly in the Command


Line:

Or by creating a python file on the server, using the .py file


extension, and running it in the Command Line:

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:

Python will give you an error if you skip the indentation:

Example:

Syntax Error

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

You have to use the same number of spaces in the same block of
code, otherwise Python will give you an error:

Example: Syntax error

Python Comments

Comments can be used to explain Python code.

Comments can be used to make the code more readable.

Comments can be used to prevent execution when testing code.

Creating a Comment

Comments starts with a #, and Python will ignore them:

Comments can be placed at the end of a line, and Python will


ignore the rest of the line:

A comment does not have to be text that explains the code, it


can also be used to prevent Python from executing code:
Python Variables

Variables

Variables are containers for storing data values.

Creating Variables

Python has no command for declaring a variable.

A variable is created the moment you first assign a value to it.

Variables do not need to be declared with any particular type,


and can even change type after they have been set.

Example

Variable Names
A variable can have a short name (like x and y) or a more
descriptive name (age, carname, total_volume).

Rules for Python variables:

 A variable name must start with a letter or the underscore


character
 A variable name cannot start with a number
 A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _)
 Variable names are case-sensitive (age, Age and AGE are
three different variables)
 A variable name cannot be any of the Python keywords.

Python Keywords

Python has a set of keywords that are reserved words that cannot be used as
variable names, function names, or any other identifiers:

Keyword Description

and A logical operator

as To create an alias
assert For debugging

break To break out of a loop

class To define a class

continue To continue to the next iteration of a loop

def To define a function

del To delete an object

elif Used in conditional statements, same as else if

else Used in conditional statements

except Used with exceptions, what to do when an exception occurs

False Boolean value, result of comparison operations

finally Used with exceptions, a block of code that will be executed no


matter if there is an exception or not

for To create a for loop

from To import specific parts of a module

global To declare a global variable


if To make a conditional statement

import To import a module

in To check if a value is present in a list, tuple, etc.

is To test if two variables are equal

lambda To create an anonymous function

None Represents a null value

nonlocal To declare a non-local variable

not A logical operator

or A logical operator

pass A null statement, a statement that will do nothing

raise To raise an exception

return To exit a function and return a value

True Boolean value, result of comparison operations

try To make a try...except statement

while To create a while loop


with Used to simplify exception handling

yield To return a list of values from a generator

Example:

Legal variable names

Illegal variable names

Remember that variable names are case-sensitive.

Python Variables - Assign Multiple Values

Many Values to Multiple Variables

Python allows you to assign values to multiple variables in one


line:

Python - Output Variables

Output Variables
The Python print() function is often used to output variables.

Example

In the print() function, you output multiple variables, separated by a


comma:

Example

You can also use the + operator to output multiple variables:

Notice the space character after "Python " and "is ", without
them the result would be "Pythonisawesome".

For numbers, the + character works as a mathematical operator:

In the print() function, combining a string and a number with


the + operator, Python will give you an error:

The best way to output multiple variables in the print()


function is to separate them with commas, which even support
different data types:

2
3

Python - Global Variables


Global Variables
Variables that are created outside of a function (as in all of
the examples in the previous pages) are known as global
variables.
Global variables can be used by everyone, both inside of
functions and outside.
Example
Create a variable outside of a function, and use it inside the
function.

If you create a variable with the same name inside a function,


this variable will be local, and can only be used inside the
function.
The global variable with the same name will remain as it was,
global and with the original value.
Example
Create a variable inside a function, with the same name as the
global variable.
The Global Keyword

To create a global variable inside a function, you can use


the global keyword.

Example

If you use the global keyword, the variable belongs to the


global scope:

Also, use the global keyword if you want to change a global


variable inside a function.

Example

To change the value of a global variable inside a function,


refer to the variable by using the global keyword:

Python Data Types


Built-in Data Types
In programming, data type is an important concept.

Variables can store data of different types, and different types can
do different things.

Python has the following data types built-in by default, in these


categories:

Text Type: str

Numeric Types: int, float, complex

Sequence list, tuple, range


Types:

Mapping Type: dict

Set Types: set, frozenset

Boolean Type: bool

Binary Types: bytes, bytearray, memoryview

None Type: NoneType

Getting the Data Type

You can get the data type of any object by using


the type() function:

Example

Print the data type of the variable x:

Setting the Data Type

The data type is set when you assign a value to a variable:

Data type includes:

1. String
2. Integer
3. Float
4. Complex
5. List
6. Tuple
7. Range
8. Dict
9. Set
10.Frozenset
11.Boolean
12.Byte
13.Bytearray
14.memoryview
15. NoneType

String
A string is a data type that represents a sequence of
characters, words, or symbols. It is a sequence of characters
and can contain letters, numbers, symbols and even spaces. It
must be enclosed in quotation marks (“ “).
Example:

Integer
Integer is a whole number, positive or negative, without a
decimal or fractional part. Integers are represented in a
computer as a group of binary digits, or bits, and are stored in
memory locations.

Float
A float is a data type that stores fractional numbers, or
floating-point numbers, which are numbers with a decimal point.
Floats are used when more precision is needed than integers,
which are numbers without a decimal point.

Complex
Complex refers to the interactions between the various elements
of software, and how complicated it is for a developer to
understand.
Complexity is a measure of how complicated a piece of code is,
and how difficult it is to understand.

List
A list is a fundamental data structure that organizes and
manipulates data by storing related items in a specific order.

Tuple
A tuple is a finite, ordered sequence of elements that can be of
different data types.
Tuples are used to store multiple values in a single object, and
are often used to return multiple values from a method.

Range
Range refer to one of three things: The possible values that may
be stored in a variable.
The upper and lower bounds of an array.
Array is a data structure that stores a fixed number of
variables of the same type.
An alternative to iterator.
Iterator is an object that allows access to each item in a
collection in order.

dict
dict is a built-in data type that stores data in key-value
pairs.

Set
Set is an abstract data type that can store unique values,
without any particular order. It is a computer implementation of
the mathematical concept of a finite set.

Frozenset

Bool (Boolean)

Try it

Bytes

bytearray

Memoryview
NoneType
References:
https://www.w3schools.com/python/

You might also like