0% found this document useful (0 votes)
4 views12 pages

Python Meterial

The document outlines the features and characteristics of Python, highlighting its interpreted, interactive, object-oriented, and beginner-friendly nature. It also explains the installation process, modes of the Python interpreter (interactive and script mode), and provides an overview of data types, variables, and expressions in Python. Additionally, it introduces the Integrated Development Learning Environment (IDLE) and its features.

Uploaded by

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

Python Meterial

The document outlines the features and characteristics of Python, highlighting its interpreted, interactive, object-oriented, and beginner-friendly nature. It also explains the installation process, modes of the Python interpreter (interactive and script mode), and provides an overview of data types, variables, and expressions in Python. Additionally, it introduces the Integrated Development Learning Environment (IDLE) and its features.

Uploaded by

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

FEATURES OF PYTHON:

 Python is interpreted: Python is processed at runtime by the interpreter. You do not


need to compile your program before executing it.
 Python is Interactive: You can actually sit at a Python prompt and interact with the
interpreter directly to write your programs.
 Python is Object-Oriented: Python supports Object-Oriented style or technique of
programming that encapsulates code within objects.
 Python is a Beginner's Language: Python is a great language for the beginner-
level programmers and supports the development of a wide range of applications.
 Easy-to-learn: Python is clearly defined and easily readable. The structure
of the program is very simple. It uses few keywords.
 Easy-to-maintain: Python's source code is fairly easy-to-maintain.
 Portable: Python can run on a wide variety of hardware platforms and has
the same interface on all platforms.
 Interpreted: Python is processed at runtime by the interpreter. So, there is no
need to compile a program before executing it. You can simply run the program.
 Extensible: Programmers can embed python within their C,C++,Java script,Active X,
etc.
 Free and Open Source: Anyone can freely distribute it, read the source code, and edit
it.
 High Level Language: When writing programs, programmers concentrate on
solutions of the current problem, no need to worry about the low level details.
Scalable: Python provides a better structure and support for large programs than shell
scripting.
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 http://python.org/downloads/

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.

Source Byte code


Runtime

PVM
m.py m.pyc

Source code extension is .py


Byte code extension is .pyc (Compiled python code)

MODES OF PYTHONINTERPRETER:
Python Interpreter
Python intepreter is a program that reads and executes Pythoncode. It uses 2 modes of Execution.
1. Interactive mode
2. Script mode
Interactive mode:
 Interactive Mode, as the name suggests, allows us to interact with OS.
 When we type Python statement, interpreter displays the result(s)
immediately.
Advantages:
 Python, in interactive mode, is good enough to learn, experiment or explore.
 Working in interactive mode is convenient for beginners and for testing
small pieces of code.
Drawback:
 We cannot save the statements and have to retype all the statements once again to
re-run them.
In interactive mode, you type Python programs and the interpreter displays the result:
>>> 1 + 1
2
The chevron, >>>, is the prompt the interpreter uses to indicate that it is ready for you
to enter code. If you type 1 + 1, the interpreter replies 2.
>>> print ('Hello, World!') Hello, World!
Script mode:
 In script mode, we type python program in a file and then use interpreter to
execute the content of thefile.
 Scripts can be saved to disk for future use. Python scripts have the extension.py,
meaning that the filename ends with .py Save the code with filename.py and run the
interpreter in script mode to execute the script.

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 MyFile.py and you're working on Unix, to run
the script you have to type:

python MyFile.py

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:

Interactive mode Script mode


A way of using the Python interpreter by A way of using the Python interpreter to
typing commands and expressions at the read and execute statements in a script.
prompt.
Cant save and edit the code Can save and edit the code
If we want to experiment with the code, If we are very clear about the code, we can
we can use interactive mode. use script mode.
we cannot save the statements for further we can save the statements for further use
use and we have to retype and we no need to retype
all the statements to re-run them. all the statements to re-run them.
We can see the results immediately. We cant see the code immediately.

Integrated Development Learning Environment (IDLE):


 Is a graphical user interface which is completely written in Python.
 It is bundled with the default implementation of the python language and also comes
with optional part of the Python packaging.

Features of IDLE:
Multi-window text editor with syntax highlighting.
 Auto completion with smart indentation.
 Python shell to display output with syntax highlighting.

Value:
Value can be any letter ,number or string.
Eg, Values are 2, 42.0, and 'Hello, World!'. (These values belong to different data
types.)
Data type:
The data stored in memory can be of many types. For example, a student roll number is
stored as a numeric value and his or her address is stored as alphanumeric characters. Python
has various standard data types that are used to define the operations possible on them and the
storage method for each of them.

Numbers:
 Number data type stores Numerical Values.
 This data type is immutable [i.e. values/items cannot be changed].
 Python supports integers, floating point numbers and complex numbers.
Int:

Int, or integer, is a whole number, positive or negative, without decimals, of


unlimited length.

>>> print(24656354687654+2)
24656354687656
>>> print(20)
20
>>> print(0b10)
2
>>> print(0B10)
2
>>> print(0X20) 32
>>> 20
20
>>> 0b10

2
>>> a=10
>>> print(a) 10
# To verify the type of any object in Python, use the type() function:

>>> type(10)
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>
Float:

Float, or "floating point number" is a number, positive or negative, containing one or more
decimals.

Float can also be scientific numbers with an "e" to indicate the power of 10.

>>> y=2.8
>>> y

2.8
>>> y=2.8
>>> print(type(y))
<class 'float'>
>>> type(.4)
<class 'float'>
>>> 2.
2.0
Example:
x = 35e3
y = 12E4
z = -87.7e100

print(type(x))

print(type(y))
print(type(z))

Output:

<class 'float'>
<class 'float'>
<class 'float'>

Boolean:

Objects of Boolean type may have one of two values, True or False:

>>> type(True)

<class 'bool'>

>>> type(False)

<class 'bool'>

String:

Strings in Python are identified as a contiguous set of characters represented in the


quotation marks. Python allows for either pairs of single or double quotes.

• 'hello' is the same as "hello".

• Strings can be output to screen using the print function. For example:
print("hello").

>>> print("Welcome Home")

Welcome Home
>>> type("Welcome Home")
<class 'str'>
>>> print('Welcome Home')
Welcome Home

>>> " "

''
If you want to include either type of quote character within the string, the simplest way is
to delimit the string with the other type. If a string is to contain a single quote, delimit it
with double quotes and vice versa:

>>> print("abc is an autonomous (') college") abc is an

autonomous (') college


>>> print('abci s an autonomous (") college')

abc is an autonomous (") college

Suppressing Special Character:

Specifying a backslash (\) in front of the quote character in a string “escapes” it and
causes Python to suppress its usual special meaning. It is then interpreted simply as a
literal single quote character:

>>> print("abc is an autonomous (\') college") abc is an

autonomous (') college

>>> print(abc is an autonomous (\") college')

abc is an autonomous (") college

The following is a table of escape sequences which cause Python to suppress the
usual special interpretation of a character in a string:

>>> print('a\
....b')
a...............b
>>> print('a\ b\ c')
abc
>>> print('a \n b')

a
b
>>> print("Welcome \n college")
Welcome
college

Escape Usual Interpretation of


Sequence Character(s) After Backslash “Escaped” Interpretation

\' Terminates string with single quote opening delimiter Literal single quote (') character

\" Terminates string with double quote opening delimiter Literal double quote (") character

\newline Terminates input line Newline is ignored


\\ Introduces escape sequence Literal backslash (\) character

In Python (and almost all other common computer languages), a tab character can
be specified by the escape sequence \t:
>>> print("a\tb")

a b
List:

 It is a general purpose most widely used in data structures


 List is a collection which is ordered and changeable and allows duplicate
members. (Grow and shrink as needed, sequence type, sortable).
 To use a list, you must declare it first. Do this using square brackets and
separate values with commas.
 We can construct / create list in many ways. Ex:
>>> list1=[1,2,3,'A','B',7,8,[10,11]]

>>> print(list1)
[1, 2, 3, 'A', 'B', 7, 8, [10, 11]]

>>> x=list()
>>> x []

>>> tuple1=(1,2,3,4)
>>> x=list(tuple1)
>>> x
[1, 2, 3, 4]

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)

This produces the following result −

100

1000.0

John

Multiple Assignment:

Python allows you to assign a single value to several variables

simultaneously. For example :

a=b=c=1

Here, an integer object is created with the value 1, and all three variables are assigned to
the same memory location. You can also assign multiple objects to multiple variables.

For example −

a,b,c = 1,2,"mrcet“

Here, two integer objects with values 1 and 2 are assigned to variables a and b
respectively, and one string object with the value "john" is assigned to the variable c.

Output Variables:

The Python print statement is often used to output variables.


Variables do not need to be declared with any particular type and can even change type
after they have been set.
x=5 # x is of type int
x = "mrcet "

Output: mrcet

To combine both text and a variable, Python uses the “+” character:

Example

x = "awesome"

print("Python is " + x)

Output

Python is awesome

You can also use the + character to add a variable to another variable:

Example

x = "Python is "
y = "awesome"
z=x+y
print(z)

Output:

Python is awesome

Expressions:

An expression is a combination of values, variables, and operators. An expression


is evaluated using assignment operator.

Examples: Y=x + 17

>>> x=10

>>> z=x+20
>>> z

30
>>> x=10

>>> y=20

>>> c=x+y

>>> c

30
A value all by itself is a simple expression, and so is a variable.

>>> y=20
>>> y
20
Python also defines expressions only contain identifiers, literals, and operators. So,

Identifiers: Any name that is used to define a class, function, variable module, or object
is an identifier.
Literals: These are language-independent terms in Python and should exist independently
in any programming language. In Python, there are the string literals, byte literals, integer
literals, floating point literals, and imaginary literals.

You might also like