Python Unit1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Feature: 4: Python is Cross Pla orm Programming Language:

Pla orm = Opera ng System


An applica on is running on Windows Pla orm
An applica on is running on UNIX pla orm
An applica on is running on LINUX Pla orm
An applica on is running on Android pla orm
An applica on is running on MAC-OS
Cross pla orm = Mul ple pla orms (or) mul ple opera ng systems.
Python programming language can run on all mul ple pla orms.
Feature: 5: Python is Open-sourced Programming Language and it is
Free-ware programming language:
What is open-sourced programming language?
Ans: Source-code is open to public or all. So that everyone can u lise
the benefits of programming language.
What is “Free-ware” programming language?
Ans: If anyone is able to download programming language freely then
that programming language is called as “Free-ware” programming
language. So that we need not to pay single rupee for Free-ware
programming languages. Python can be downloaded freely. So, Python
is free ware prog. Lang.
6th Feature: Python is procedure oriendted programming language as
well as object oriented programming language:
What is procedure-oriented programming language?
Ans: (i) Procedure-oriented programming language is a collec on of
procedures or func on.
For Example: Addi on () func on will perform “addi on” opera on.
Subtrac on () func on will perform “Subtrac on” opera on.
In banking applica on, “Transfer funds ()” func on will transfer funds
from your account to another account.
ii) Procedure oriented programming language are used to develop
“Single user applica ons”
iii) Single user applica on: Only one user can operate the applica on
then that applica on is called as single user applica on.
Examples of “Single user applica ons:”
a) Calculator
b) Paint
c) Notepad etc………
With python, we can develop “single user applica ons”
b) Object oriented programming Language:
i) It is used to develop “Mul ple user applica ons”
ii) Mul -user applica on: More than one user can operate the
applica on then that type of applica on is called as “Mul -user
applica on”
iii) Examples of Mul -user applica ons:
a) Facebook
b) Twi er
c) Whatsapp
d) Youtube etc….
So, we can develop mul -user applica ons with python.
7th Feature: Python is modular programming language:
Python Library = collec on of packages
Package = collec on of modules
Module = Collec on of func ons
Func on = Collec on of statements or set of instruc ons.
8th feature: Python is dynamic typed programming language:
i.e., programmer doesn’t need to declare any data type, the data type
will be taken automa cally based on ini alized value. if we ini alize
numerical value it will dynamically take as int.
9th feature: Python is interpreter-based programming language
Compiler based programming Interpreter based programming
language language
In compiler based prog lang, Example: PYTHON
program will get first compile, In case of interpreter-based
once compila on is completed, programming language,
then only it will execute en re interpreter (execu on engine)
program. It means here it will will compile and execute the
divided into two steps: code line by line like below:
1. Compila on: Doing by Python interpreter
compiler. Program
2. Execu on: Is doing by Line1-Compile and execute
execu on engine. Line2-Compile and execute
Example for compiler based prog Line3-compile and execute
lang: C, C++, JAVA, C#
In this process, when we compile Observa on:
a program it will produce
collec on of files, which In this case code is compiling an
consume lot of memory. execu on line by line, due to that
Ex: if we compile ‘c’ program it reason it is not producing any
will produce following files suppor ng files, which will not
(i) .bak (back up file) consume more memory.
(ii) .obj (it will contain
machine understandable
code)
(iii) .exe (It will contains
executable code)

10th Feature: Python is an embedded programming language:


Embedded = integrated/combined/mixed
Python coding can be embedded or mixed with other languages code.
Python+java = Jython
Python+c = Cpython
Python+.net = Iron python
Python + Hadoop = pydoop
Python +spart = pyspark

Installa on:

STEPS TO INSTALL PYTHON SOFTWARE ON OUR SYSTEMS

Step-1: open www.python.org website

Step-2: Select windows op on from Downloads menu

Step-3: Click on Latest Python 3 Release – python 3.11.4

Step-4: Click on windows installer (64-bit)

Step-5: Click on Downloaded file (file name: python-3.11.4-amd64)

Step-6: Click on Add Python 3.11 to PATH

Step-7: Click on install now and click on Yes bu on

Now you will see “Setup was successful”


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

Keywords Description

This is a logical operator which returns true if both the operands are
and
true else returns false.

This is also a logical operator which returns true if anyone operand


or
is true else returns false.

This is again a logical operator it returns True if the operand is false


not
else returns false.

if This is used to make a conditional statement.

Elif is a condition statement used with an if statement. The elif


elif
statement is executed if the previous conditions were not true.

Else is used with if and elif conditional statements. The else block is
else
executed if the given condition is not true.
Keywords Description

for This is used to create a loop.

while This keyword is used to create a while loop.

break This is used to terminate the loop.

as This is used to create an alternative.

def It helps us to define functions.

lambda It is used to define the anonymous function.

import This is used to import a module.

Identifiers:
Python Identifier is the name we give to identify a variable, function,
class, module or other object. That means whenever we want to give
an entity a name, that's called identifier.
Identifier is a user-defined name given to a variable, function, class,
module, etc.

Variables
A python variable is a symbolic name that is reference or pointer to an
object. Once an object is assigned to a variable, you can refer to the
object by that name. But the data itself is s ll contained within the
object.
The most commonly used methods of construc ng a mul word
variable name are the following three examples.
1. Camel case
Ex:- mightyMechanicalEngineering
2. Pascal case - Iden cal to camel case except the first word is also
capital
Ex:- MightyMechanicalEngineering
3. Snake case – mighty_mechanical_engineering
Comments:
Single line comments
Mul line comments
Indenta on:
 Indenta on means spaces at the beginning of code line.
 Python uses indenta on to indicate a block of code.
 In other languages the indenta on in code is only for readability, the
indenta on in python is very imp.
 Python will give error if you skip the indenta on. No of spaces is upto
programmer. But it should at least one.

Note:
We should use same no of spaces for all lines in same block of code.
No of spaces is user choice. Even one space, it will work
What is Statement in Python
A Python statement is an instruction that the Python interpreter can execute.
There are different types of statements in Python language as Assignment
statements, Conditional statements, Looping statements, etc. The token character
NEWLINE is used to end a statement in Python. It signifies that each line of
a Python script contains a statement. These all help the user to get the required
output.

Types of statements in Python?

The different types of Python statements are listed below:


 Multi-Line Statements
 Python Conditional and Loop Statements
 Python If-else
 Python for loop
 Python while loop
 Python try-except
 Python with statement
 Python Expression statements
 Python pass statement
 Python del statement
 Python return statement
 Python import statement
 Python continue and
 Python break statement
 Statement in Python can be extended to one or more lines using
parentheses (), braces {}, square brackets [], semi-colon (;), and
continuation character slash (\). When the programmer needs to do
long calculations and cannot fit his statements into one line, one can
make use of these characters.
Declared using Continuation Character (\):
s = 1 + 2 + 3 + \
4 + 5 + 6 + \
7 + 8 + 9
Declared using parentheses () :
n = (1 * 2 * 3 + 7 + 8 + 9)
Declared using square brackets [] :
footballer = ['MESSI',
'NEYMAR',
'SUAREZ']
Declared using braces {} :
x = {1 + 2 + 3 + 4 + 5 + 6 +
7 + 8 + 9}
Declared using semicolons(;) :
flag = 2; ropes = 3; pole = 4

Constants:
In Python, constants are usually declared and assigned in a module. Here, the
module is a new file containing variables, func ons etc. which is imported to the
main file.
Inside the module constants are wri en in all CAPITAL_LETTERS and underscores
separa ng the words.
Example:
1. Create a file name as constantmechc.py
2. PI=3.14, GRAVITY=9.81
3. Create a main.py (Program File)
Import constantmechc
print(constantmechc.PI)
print(constantmechc.GRAVITY)
Literals:
Literals means a constant
Generally, literal are a nota on for represen ng a fixed value in source
code. They can also be defined as raw value or data given in variables or
constants.
#Numeric Literals
Here, 20,24.5, 2+3j are considered as literals.
Python has different types of literals
1. String literals
2. Numeric Literals
3. Boolean Literals
4. Literal Collec ons
5. Special literals
6. Complex Literals

DATA Types:

Built-In Types or Data Types implicitly available in python


Complex Data Type:
It is used to represent the complex numbers.
Ex: a+bj
Cn1=1+2j
Cn2=2+3j
Cn=Cn1+Cn2
print(Cn)
o/p= 3+5j
Complex data types will be used in data science applica on.
Type Conversion/Cas ng

1. What is type cas ng?


Ans. Type cas ng a process of conver ng one data type to another
data type.
In Python we have following type cas ng func ons.
int()
float()
str()
bool()
int() : This method will convert given value into integer type.
float() : This method will convert given value into float type.
str() : This method will convert given value into str type.
bool() : This method will convert given value into bool type.
Python Interning: (-5 to 256)
Import Module in Python:
Import in python is similar to #include header_file in C/C++. Python modules can
get access to code from another module by impor ng the file/func on using
import. The import statement is the most common way of invoking the import
machinery, but it is not the only way.
import module_name
When the import is used, it searches for the module ini ally in the local scope
by calling __import__() func on. The value returned by the func on is then
reflected in the output of the ini al code.

For user defined modules refer constants topic, which is already discussed
above.
>>>help(math) to get more details about the math module in python
>>>import sys
>>>sys.path
>>>import from math as mt
>>>mt.pi
Operators: Operator is used to perform opera ons on data
Types of operators:
Arithme c operators,
Comparison operators,
Logical operators,
Bitwise operators,
Assignment operators,
Iden ty operators,
Membership operators

Assignment Operator:

Arithme c operators:
It is used to perform the mathema cal opera ons.

+ is for addi on
- is for subtrac on
* is for mul plica on
/ is for division
// floor division
% modula on
Arithme c – Assignment Operators:

+= addi on and assignment #ex: a+=b means a=a+b


-= subtrac on and assignment #ex: a-=b means a=a-b
*= mul plica on and assignment
/= division and assignment
//= floor division and assignment
%= modula on and assignment

Rela onal Operators: Rela onal operator checks a condi on and return
Boolean value. (True/False) < (Less than), > (Greater Than)
10<5
False
5<10
True
Logical Operators: A logical operator checks mul ple condi ons at a me and
return bool value. (True or False)
 ‘and’ logical operator: It will return True, when all the given condi on are
True otherwise it returns False.

 ‘or’ logical operator: It will return True, when at least one condi on is True
within given condi ons. Otherwise, it will return False.

Membership Operator: It is used to check whether given member is exists or


not. If exists it returned true, otherwise it will return false.
In Python we have two membership operators.
 in
 not in
Iden ty Operators: It returns True if mul ple object have same id
(iden ty) otherwise it will return false
 is
 is not

Bitwise operators:
In Python, bitwise operators are used to perform bitwise calculations on
integers. The integers are first converted into binary and then operations are
performed on each bit or corresponding pair of bits, hence the name bitwise
operators. The result is then returned in decimal format.

OPERATOR NAME DESCRIPTION SYNTAX

Bitwise Result bit 1, if both operand bits are 1; otherwise,


& AND results bit 0.
x&y
OPERATOR NAME DESCRIPTION SYNTAX

Bitwise Result bit 1, if any of the operand bit is 1;


| OR otherwise, results bit 0. x|y

Bitwise
~ NOT
inverts individual bits ~x

Bitwise Results bit 1, if any of the operand bit is 1 but not


^ XOR both, otherwise results bit 0. x^y

Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)

a & b = 1010
&
0100
= 0000
= 0 (Decimal)
Bitwise or operator Returns 1 if either of the bit is 1 else 0.
Example:
a = 10 = 1010 (Binary)
b = 4 = 0100 (Binary)

a | b = 1010
|
0100
= 1110
= 14 (Decimal)
Name Space and Scope:

What is current role number?


 Used to uniquely iden fy the person
 Two persons cannot have same roll number.
Name Space:
 Unique name for each and every object
 Python also doesn’t have same name for objects (variables, method)
with in a single scope.

What is Scope?
1. Local Scope:
Contains names defined inside the current func on.
2. Global Scope:
Contains names defined at the top level of the script or module
3. Enclosing Scope:
Contains names defined inside any and all enclosed func on.
4. Built-In Scope:
Contains names built in to the python language.
Modifying the global variable in local scope:
We can’t modify global variable inside the local scope:

You might also like