WE code -II(Python )
Unit-1
Unit 1: Introduction to Python
Intro to Python
1. Python is Free and open source
2. Cross platform independent – Python same piece of code can run on any
operating system like windows, Mac, Linux etc.
3. Large standard Library – It can help to web development, data manipulation it
have many packages for performing each and every task.
4. Object oriented – around to us the data is presented in the form of objects so it
find the solution for the same.
Site to install Python
[Link] downloads/
The latest version of python is
Python 3.8.1 -> Download Python 3.8.1-> click it
Choose any one option to download the setup it is available for different operating
system.
Python is easy to install but if you have need of IDE(Integrated development
Environment)
So you can install
Introduction to Python and installation:
Python is a widely used general-purpose, high level programming language. It was initially
designed by Guido van Rossum in 1991 and developed by Python Software Foundation. It
was mainly developed for emphasis on code readability, and its syntax allows programmers
to express concepts in fewer lines of code.
Python is a programming language that lets you work quickly and integrate systems more
efficiently.
There are two major Python versions- Python 2 and Python 3.
• On 16 October 2000, Python 2.0 was released with many new features.
• On 3rd December 2008, Python 3.0 was released with more testing and includes new
features.
Beginning with Python programming:
1) Finding an Interpreter:
Before we start Python programming, we need to have an interpreter to interpret and run our
programs. There are certain online interpreters like [Link]
[Link] or [Link] that can be used to start Python without installing
an interpreter.
Windows: There are many interpreters available freely to run Python scripts like IDLE
(Integrated Development Environment) which is installed when you install the python
software from [Link]
2) Writing first program:
# Script Begins
Statement1
Statement2
Statement3
# Script Ends
Why to use Python:
The following are the primary factors to use python in day-to-day life:
1. Python is object-oriented
Structure supports such concepts as polymorphism, operation overloading and
multiple inheritance.
2. Indentation
Indentation is one of the greatest feature in python
3. It’s free (open source)
Downloading python and installing python is free and easy
4. It’s Powerful
Dynamic typing
Built-in types and tools
Library utilities
Third party utilities (e.g. Numeric, NumPy, sciPy)
Automatic memory management
5. It’s Portable
Python runs virtually every major platform used today
As long as you have a compaitable python interpreter installed, python
programs will run in exactly the same manner, irrespective of platform.
6. It’s easy to use and learn
No intermediate compile
Python Programs are compiled automatically to an intermediate form called
byte code, which the interpreter then reads.
This gives python the development speed of an interpreter without the
performance loss inherent in purely interpreted languages.
Structure and syntax are pretty intuitive and easy to grasp.
7. Interpreted Language
Python is processed at runtime by python Interpreter
8. Interactive Programming Language
Users can interact with the python interpreter directly for writing the programs
9. Straight forward syntax
The formation of python syntax is simple and straight forward which also makes it
popular.
Installation:
There are many interpreters available freely to run Python scripts like IDLE (Integrated
Development Environment) which is installed when you install the python software
from [Link]
Steps to be followed and remembered:
Step 1: Select Version of Python to Install.
Step 2: Download Python Executable Installer.
Step 3: Run Executable Installer.
Step 4: Verify Python Was Installed On Windows.
Step 5: Verify Pip Was Installed.
Step 6: Add Python Path to Environment Variables (Optional)
Working with Python
Python Code Execution:
Python’s traditional runtime execution model: Source code you type is translated to byte
code, which is then run by the Python Virtual Machine (PVM). Your code is automatically
compiled, but then it is interpreted.
There are two modes for using the Python interpreter:
• Interactive Mode
• Script Mode
Running Python in interactive mode:
Without passing python script file to the interpreter, directly execute code to Python prompt.
Once you’re inside the python interpreter, then you can start.
>>> print("hello world")
hello world
# Relevant output is displayed on subsequent lines without the >>> symbol
>>> x=[0,1,2]
# Quantities stored in memory are not displayed by default.
>>> x
#If a quantity is stored in memory, typing its name will display it.
[0, 1, 2]
>>> 2+3
5
The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the python
interpreter uses to indicate that it is ready. If the programmer types 2+6, the interpreter
replies 8.
Running Python in script mode:
Alternatively, programmers can store Python script source code in a file with
the .py extension, and use the interpreter to execute the contents of the file. To execute the
script by the interpreter, you have to tell the interpreter the name of the file. For example, if
you have a script name [Link] and you're working on Unix, to run the script you have to
type:
python [Link]
Working with the interactive mode is better when Python programmers deal with small
pieces of code as you can type and execute them immediately, but when the code is more
than 2-4 lines, using the script for coding can help to modify and use the code in future.
Example:
Variables:
Variables are nothing but reserved memory locations to store values. This means that when
you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides what can
be stored in the reserved memory. Therefore, by assigning different data types to variables,
you can store integers, decimals or characters in these variables.
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)
Assigning Values to Variables:
Python variables do not need explicit declaration to reserve memory space. The declaration
happens automatically when you assign a value to a variable. The equal sign (=) is used to
assign values to variables.
The operand to the left of the = operator is the name of the variable and the operand to the
right of the = operator is the value stored in the variable.
For example −
a= 100 # An integer assignment
b = 1000.0 # A floating point
c = "John" # A string
print (a)
print (b)
print (c)
Value
In Python, the term "value" refers to the actual data stored in a variable or returned by an expression. It's the
content or data that Python uses and manipulates during execution.
Here are some key points about values in Python:
Python supports multiple data types for values:
Numeric values: int, float, complex
String values: str
Boolean values: True, False
Collection values: list, tuple, set, dict
None value: NoneType represents absence of value
Data types
In Python, a data type defines the kind of value a variable can hold. Python is a dynamically typed language,
meaning you don’t have to declare the data type explicitly — Python infers it at runtime.
Here are the main built-in data types in Python:
Basic Data Types
Data Type Description Example
int Integer numbers x = 10
float Floating point numbers (decimals) x = 3.14
complex Complex numbers x = 2 + 3j
bool Boolean values (True or False) x = True
str String of characters x = "Hello"
Collection Data Types
Data Type Description Example
list Ordered, mutable sequence x = [1, 2, 3]
tuple Ordered, immutable sequence x = (1, 2, 3)
range Sequence of numbers x = range(5)
set Unordered, no duplicates x = {1, 2, 3}
frozenset Immutable version of set x = frozenset([1, 2, 3])
dict Key-value pairs x = {'a': 1, 'b': 2}
Other Types
Data Type Description Example
NoneType Represents absence of value x = None
bytes Immutable sequence of bytes x = b'hello'
bytearray Mutable sequence of bytes x = bytearray(5)
memoryview Memory view object x = memoryview(b'abc')
🔍 Example: Checking Data Type
x = 10
print(type(x)) # <class 'int'>
Expression
An expression is a combination of operators and operands that is interpreted to produce some other value. In
any programming language, an expression is evaluated as per the precedence of its operators. So that if there is
more than one operator in an expression, their precedence decides which operation will be performed first. We
have many different types of expressions in Python. Let's discuss all types along with some exemplar codes :
1. Constant Expressions: These are the expressions that have constant values only.
Example:
# Constant Expressions
x = 15 + 1.3
print(x)
Output
16.3
2. Arithmetic Expressions: An arithmetic expression is a combination of numeric values, operators, and
sometimes parenthesis. The result of this type of expression is also a numeric value. The operators used in
these expressions are arithmetic operators like addition, subtraction, etc. Here are some arithmetic operators in
Python:
Operators Syntax Functioning
+ x+y Addition
- x-y Subtraction
* x*y Multiplication
/ x/y Division
// x // y Quotient
% x%y Remainder
** x ** y Exponentiation
Example:
Let's see an exemplar code of arithmetic expressions in Python :
# Arithmetic Expressions
x = 40
y = 12
add = x + y
sub = x - y
pro = x * y
div = x / y
print(add)
print(sub)
print(pro)
print(div)
Output
52
28
480
3.3333333333333335
3. Integral Expressions: These are the kind of expressions that produce only integer results after all
computations and type conversions.
Example:
# Integral Expressions
a = 13
b = 12.0
c = a + int(b)
print(c)
Output
25
4. Floating Expressions: These are the kind of expressions which produce floating point numbers as result
after all computations and type conversions.
Example:
# Floating Expressions
a = 13
b=5
c=a/b
print(c)
Output
2.6
5. Relational Expressions: In these types of expressions, arithmetic expressions are written on both sides of
relational operator (> , < , >= , <=). Those arithmetic expressions are evaluated first, and then compared as per
relational operator and produce a boolean output in the end. These expressions are also called Boolean
expressions.
Example:
# Relational Expressions
a = 21
b = 13
c = 40
d = 37
p = (a + b) >= (c - d)
print(p)
Output
True
6. Logical Expressions: These are kinds of expressions that result in either True or False. It basically specifies
one or more conditions. For example, (10 == 9) is a condition if 10 is equal to 9. As we know it is not correct,
so it will return False. Studying logical expressions, we also come across some logical operators which can be
seen in logical expressions most often. Here are some logical operators in Python:
Operator Syntax Functioning
and P and Q It returns true if both P and Q are true otherwise returns false
or P or Q It returns true if at least one of P and Q is true
not not P It returns true if condition P is false
Example:
Let's have a look at an exemplar code :
P = (10 == 9)
Q = (7 > 5)
# Logical Expressions
R = P and Q
S = P or Q
T = not P
print(R)
print(S)
print(T)
Output
False
True
True
7. Bitwise Expressions: These are the kind of expressions in which computations are performed at bit level.
Example:
# Bitwise Expressions
a = 12
x = a >> 2
y = a << 1
print(x, y)
Output
3 24
8. Combinational Expressions: We can also use different types of expressions in a single expression, and that
will be termed as combinational expressions.
Example:
# Combinational Expressions
a = 16
b = 12
c = a + (b >> 1)
print(c)
Output
22