0% found this document useful (0 votes)
18 views

Py in

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Py in

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python was created by Guido van Rossum, and released in 1991.

Python is a simple, general purpose, high level,interpreted and object-oriented programming


language.
python can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh.

Python provides many useful features

Easy to use and Learn


Expressive Language
Interpreted Language
Object-Oriented Language
Open Source Language
Extensible
Learn Standard Library
GUI Programming Support
Integrated
Embeddable
Dynamic Memory Allocation
Wide Range of Libraries and Frameworks

Python is used in various software domains some application areas are given below.

Web and Internet Development


Games
Scientific and computational applications
Language development
Image processing and graphic design applications
Enterprise and business applications development
Operating systems
GUI based desktop applications

Python provides various web frameworks to develop web applications. The popular python web
frameworks are Django, Pyramid, Flask.

Advantages of Python are:

Python is Interpreted language


Interpreted: Python is an interpreted language. It does not require prior compilation of
code and executes instructions directly.

It is Free and open source


Free and open source: It is an open-source project which is publicly available to reuse. It
can be downloaded free of cost.

It is Extensible
Extensible: It is very flexible and extensible with any module.

Object-oriented
Object-oriented: Python allows to implement the Object-Oriented concepts to build
application solution.

It has Built-in data structure


Built-in data structure: Tuple, List, and Dictionary are useful integrated data structures
provided by the language.
Readability
High-Level Language
Cross-platform
Portable: Python programs can run on cross platforms without affecting its performance.

PEP 8 stands for Python Enhancement Proposal, it can be defined as a document that helps us
to provide the guidelines on how to write the Python code. It is basically a set of rules
that specify how to format Python code for maximum readability.

The print() function displays the given object to the screen


input() function which is used to take input from the user.

Variable is a name that is used to refer to memory location. Python variable is also known
as an identifier and used to hold value.

The rules to name an identifier are given below.

The first character of the variable must be an alphabet or underscore ( _ ).


All the characters except the first character may be an alphabet of lower-case(a-z),
upper-case (A-Z), underscore, or digit (0-9).
Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &,
*).
Identifier name must not be similar to any keyword defined in the language.

There are two types of variables in Python - Local variable and Global variable.

Local variables are the variables that declared inside the function and have scope within
the function

Global variables can be used throughout the program, and its scope is in the entire program.
We can use global variables inside or outside the function.
A variable declared outside the function is the global variable by default.

The data types defined in Python are given below.

Numbers - int , complex number , float


Sequence Type - strings , list , tuple
Boolean
Set
Dictionary

Python breaks each statement into a sequence of lexical components known as tokens

Literals are the fixed values used in a source code.Literals can be defined as a data which
is given in a variable or constant.

String literals can be formed by enclosing a text in the quotes.

Numeric Literals: These are the literals written in form of numbers. Python supports the
following numerical literals:

Integer Literal: It includes both positive and negative numbers along with 0. It doesn’t
include fractional parts. It can also include binary, decimal, octal, hexadecimal literal.
Float Literal: It includes both positive and negative real numbers. It also includes
fractional parts.
Complex Literal: It includes a+bi numeral, here a represents the real part and b represents
the complex part.

Boolean literals have only two values in Python. These are True and False.

Special Literals: Python has a special literal ‘None’. It is used to denote nothing, no
values, or the absence of value.

Character Literals: Character literal is also a string literal type in which the character
is enclosed in single or double-quotes.

Literals Collections: Literals collections in python includes list, tuple, dictionary, and
sets.

List:

List contains items of different data types. Lists are mutable i.e., modifiable.
The values stored in List are separated by comma(,) and enclosed within square brackets([]).
We can store different types of data in a List.

Dictionary:

Python dictionary stores the data in the key-value pair.


A dictionary is a collection which stores values along with keys.
A for loop is used to traverse all keys and values of a dictionary.
The in and not in can be used to check if a key is present in á dictionary.
It is enclosed by curly-braces {} and each pair is separated by the commas(,).

Tuple:

A tuple is a built-in data collection type. It allows us to store values in a sequence. It


is immutable which means it cannot be modified after creation.
It is enclosed by the parentheses () and each element is separated by the comma(,).

Set:

A set is an unordered collection of unique elements without duplicates. A set is mutable.


Hence, we can easily add or remove elements from a set.
It is enclosed by the {} and each element is separated by the comma(,).

Python Operators
The operator can be defined as a symbol which is responsible for a particular operation
between two operands.

Arithmetic operators
Comparison operators
Assignment Operators
Logical Operators
Bitwise Operators
Membership Operators
Identity Operators

Operator Type Operators


+ - * / // % ** Arithmetic Operator
== != <> <= >= Relational Operator
and not or Logical Operator
& | ~ ^ << >> Bitwise Operator

Arithmetic operators are used to perform arithmetic operations between two operands. It
includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor
division), and exponent (**) operators.

Comparison operators are used to comparing the value of the two operands and returns Boolean
true or false accordingly.

Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition
becomes true.
>= If the first operand is greater than or equal to the second operand, then the
condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes
true.
< If the first operand is less than the second operand, then the condition becomes
true.

The assignment operators are used to assign the value of the right expression to the left
operand. The assignment operators are described in the following table.

Operator Description
= It assigns the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b
will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and
assigns the modified value back to left operand. For example, if a = 20, b = 10 => a- = b
will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and
assigns the modified value back to then the left operand. For example, if a = 10, b = 20 =>
a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and
assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a % = b
will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 =
16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign
4//3 = 1 to a.

The bitwise operators perform bit by bit operation on the values of the two operands

Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied
to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the
resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the
resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is
0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the
right operand.
>> (right shift) The left operand is moved right by the number of bits present in the
right operand.

The logical operators are used primarily in the expression evaluation to make a decision.
Operator Description
and If both the expression are true, then the condition will be true. If a and b are the
two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are
the two expressions, a → true, b → false => a or b → true.
not If an expression a is true, then not (a) will be false and vice versa.

Python membership operators are used to check the membership of value inside a Python data
structure. If the value is present in the data structure, then the resulting value is true
otherwise it returns false.

Operator Description
in It is evaluated to be true if the first operand is found in the second operand
(list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand
(list, tuple, or dictionary).

The identity operators are used to decide whether an element certain class or type.

Operator Description
is It is evaluated to be true if the reference present at both sides point to the same
object.
is not It is evaluated to be true if the reference present at both sides do not point to
the same object.

Keywords are reserved words with fi xed meanings assigned to them. Keywords cannot be used
as identifiers or variables.

Python contains thirty-five keywords in the most recent version, i.e., Python 3.8. Here we
have shown a complete list of Python keywords for the reader's reference.

False await else import pass


None break except in raise
True class finally is return
and continue for lambda try
as def from nonlocal while
assert del global not with
async elif if or yield

FUNCTIONS

A function is a self-contained block of one or more statements that perform a special task
when called.
A function’s defi nition in Python begins with the def keyword followed by the function’s
name,
parameter and body.
The function header may contain zero or more number of parameters.
Parameters are the names that appear in a function’s defi nition.
Arguments are the values actually passed to a function while calling a function.
Arguments to a function can be passed as positional or keyword arguments.
The arguments must match the parameters in order, number and type as defi ned in the
function.
A variable must be created before it is used.
Variables defi ned within the scope of a function are said to be local variables.
Variables that are assigned outside of functions are said to be global variables.
The return statement is used to return a value from a function.
Functions in Python can return multiple values.
Python also supports a recursive feature, i.e. a function can be called repetitively by
itself

There are three types of functions:

Built-In Functions: copy(), len(), count() are the some built-in functions.
User-defined Functions: Functions which are defined by a user known as user-defined
functions.
Anonymous functions: These functions are also known as lambda functions because they are not
declared with the standard def keyword.

There are two parameters passing mechanism in Python:

Pass by references
Pass by value

By default, all the parameters (arguments) are passed "by reference" to the functions. Thus,
if you change the value of the parameter within a function, the change is reflected in the
calling function as well.

The pass by value is that whenever we pass the arguments to the function only values pass to
the function, no reference passes to the function. It makes it immutable that means not
changeable

What are the different file processing modes supported by Python?


Python provides four modes to open files. The read-only (r), write-only (w), read-write (rw)
and append mode (a). 'r' is used to open a file in read-only mode, 'w' is used to open a
file in write-only mode, 'rw' is used to open in reading and write mode, 'a' is used to open
a file in append mode. If the mode is not specified, by default file opens in read-only
mode.

Read-only mode (r): Open a file for reading. It is the default mode.
Write-only mode (w): Open a file for writing. If the file contains data, data would be lost.
Other a new file is created.
Read-Write mode (rw): Open a file for reading, write mode. It means updating mode.
Append mode (a): Open for writing, append to the end of the file, if the file exists.

An operator is a particular symbol which is used on some values and produces an output as a
result. An operator works on operands. Operands are numeric literals or variables which hold
some values. Operators can be unary, binary or ternary. An operator which requires a single
operand known as a unary operator, which require two operands known as a binary operator and
which require three operands is called ternary operator.

a decorator is a function that modifies other functions.

iterators are used to iterate a group of elements, containers like a list. Iterators are
the collection of items, and it can be a list, tuple, or a dictionary. Iterators are
objects which can be traversed though or iterated upon

the generator is a way that specifies how to implement iterators.

Slicing is a mechanism used to select a range of items from sequence type like list, tuple,
and string. It is beneficial and easy to get elements from a range by using slice way. It
requires a : (colon) which separates the start and end index of the field.

The Python index() method helps you find the index position of an element or an item in a
string of characters or a list of items. The index() method returns the index of the
specified element in the list.

The __init__ is a method or constructor in Python. This method is automatically called to


allocate memory when a new object/ instance of a class is created. All classes have the
__init__ method.

An array is defined as a collection of items that are stored at contiguous memory locations.
It is a container which can hold a fixed number of items, and these items should be of the
same type
A combination of arrays saves a lot of time. The array can reduce the overall size of the
code.

print( ) function-
type( ) function -The type() function returns the type of the specified object.
input( ) function -The input() function allows taking the input from the user.
abs( ) function - The abs() function returns the absolute value of the specified number.
pow( ) function - The pow() function returns the calculated value of x to the power of y
dir( ) function-The dir() function returns all the properties and methods of the specified
object, without the values.
sorted( ) function-The sorted() function returns a sorted list of the specified iterable
object.
max( ) function-The max() function returns the item with the maximum value or the item with
the maximum value in an iterable.
round( ) function-The round() function returns a floating-point number that is a rounded
version of the specified number, with the specified number of decimals
len( ) function=The len() function returns the count of items present in a specified object.
sum( ) function-The sum() function returns a number, the sum of all items in an iterable.
help( ) function-The help() function is used to display the documentation of modules,
functions, classes, keywords, etc.

Documentation string or docstring is a multiline string used to document a specific code


segment.
The docstring should describe what the function or method does.

You might also like