0% found this document useful (0 votes)
229 views33 pages

ch-1 Introduction To Python

Python is an interpreted, high-level and general-purpose programming language. It can be used to build many applications like web applications, scripting, game development, and database applications. Python code is written in a more human-readable syntax and contains statements that are executed line by line by the Python interpreter. Key features of Python include being easy to use, having a simple syntax, being an interpreted language, being case sensitive, and being a complete and portable programming language with ample libraries and support for GUI programming.

Uploaded by

Adhiraj Sengar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
229 views33 pages

ch-1 Introduction To Python

Python is an interpreted, high-level and general-purpose programming language. It can be used to build many applications like web applications, scripting, game development, and database applications. Python code is written in a more human-readable syntax and contains statements that are executed line by line by the Python interpreter. Key features of Python include being easy to use, having a simple syntax, being an interpreted language, being case sensitive, and being a complete and portable programming language with ample libraries and support for GUI programming.

Uploaded by

Adhiraj Sengar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 33

CHAPTER 2:-GETTING STARTED WITH PYTHON

PROGRAMMING LANGUAGE:-
A programming language is a formal language comprising a set of instructions that
produce various kinds of output.

LOW LEVEL LANGUAGE:


A low-level language is the language the machine understands( machine language
or binary code)
Example: Assembly language.

HIGH LEVEL LANGUAGE


High level languages are written in a form that is close to our human language.
These level languages provide almost everything that the programmer might need
to do as already build into the language.
Example: Java, Python

LANGUAGE PROCESSORS:
A Language processor is a special type of computer software which can translate
the source code into object code or machine code.
A source code refers to the program code written by programmer in a high level
programming language(HLL) such as C,Java,C++ etc.
An object code refers to a code usually in Low level language(machine language).
Some language processors are:-
a. Assembler(converts assembly language into machine language)
b. Compiler
c. Interpreter

PYTHON:-
 Python is an interpreted, high-level programming language.
 It is general-purpose programming language as it can be used to
build many applications.
 Created by Guido van Rossum and first released in 1991 at the
national research institute for mathematics in Netherlands.It is
presently owned by PSF(python software foundation)
 Python is an open source programming language
 It is an Object Oriented programming language.
 However,the programming language has been named ‘python’
because just as the reptile has the flexible body,the language too is
easy,simple and highly flexible.

FEATURES OF PYTHON:-
1. Easy to use
2. Expressive language due to simpler syntax
3. Interpreted language
Python is an interpreted language not compiled language.This
means that python executes the code line by line.This makes
the python easy to debug language.
4. Case sensitive language
5. Completeness
When we install python,we no need to download or install
additional libraries.. All types of required functionality is
contained in python standard library.
6. Platform independent(can run on all platforms)
7. Portable
8. Free and open source
Free means it is freely available and we need not to pay for
it.Open source means that its source code is available,which
can be modified/improved etc.
9. Syntax highlighting:- It allows to distinguish between
input,output and error messages by different colour codes.
10. Ample availability of libraries
11. GUI Programming
12. Less learning time

APPLICATIONS:-
 Scripting
 Web applications
 Game development
 Database applications

INTERPRETATION VS COMPILATION:- Topic to be


discussed(why python is an interpreted language?)

SOME MORE POINTS:-


 python can be downloaded from the website www.python.org.
 The default installation from this site is CPython installation and it comes
with python interpreter,python IDLE(python GUI) and pip(package installer)
 Yet there are many other python distributions available such as Anaconda
python distribution,Spyder IDE, Pycharm IDE etc.
 Python programs are saved using .py or .pyw extension.
 Once python is installed in our computer,we can work in python in two
ways:-
i. In interactive mode(immediate mode)
ii. In script mode
i. Interactive mode(python shell)-In this mode python commands are
directly typed in command prompt(>>>) and when we press enter
key the interpreter displays result. But the commands can not be
saved. This mode is good for testing code instantly.
ii. Script mode- Here we can write commands and save it as well for
future reference.To execute the command,Run->Run module or
directly press F5 key.

 In order to exit python, perform anyone of the following:


i. Type Ctrl+z and then press enter key
ii. Type quit() and then press enter key
iii. Type exit() and then press enter key
CHAPTER 3: PYTHON PROGRAMMING FUNDAMENTALS
 IDLE(INTEGRATED DEVELOPMENT LEARNING ENVIRONMENT):-It is a program that allows
the user to edit,run,browse and debug a python program from a single interface.Python also
comes with an IDLE.
 Python syntax are case sensitive.
 In python,syntax error are detected by the interpreter at runtime.
 Python programs are saved using .py and .pyw extension.

Python code contains certains statements(commands) and these statements are


executed by the python interpreter called PYTHON SHELL.

VARIABLE AND TYPES:-


Variable is the name of memory location where we store some value.
We can assign any type of value to a variable like
character,string,list,number,tuple,dictionary etc. Whatever value we assign to a variable
will occupy some memory location.
There are some naming rules for variable as discussed below:
 It must start with an alphabet or an underscore( _ )
 It cannot contain white spaces
 It may contain any number of letters,digits and underscore.No other
characters apart from this is allowed.
 Python keyword can not be used as variable names.
Creating variable:
For e.g.,
m=20

In the example given above m is referring to memory location 1001 which has the value
20.
Consider one more example given below:
n=20
p=70

Here in the example given above both n and p are referring to different memory
locations.
Consider one more example given below:
p=12
p=20
In the example given above:
Firstly p is referring to memory location 1098 which has the value 12 as shown below:

Now when another value is assigned to p then after that it will refer to memory location
1061 which has the value 20 as shown below:
Consider one more example below:
p=12
p=20
p=11

This shows that variable are not storage containers in python but most of the
programming language do. In python variables always refer to memory location where
value is stored in memory location.

A variable object has three main components:


a. Identity of the variable/object
The address of an object is checked using the id(object)
Syntax: id(variable/object)
b. Type of the variable/object
Type means the type of variable. Data type of value means the type of value it holds.

1. Number:
It stores numerical value.
Python supports three types of built in numeric data type:
a. Integer/long
It represents whole numbers without any fractional part.Thay can be positive
or negative value.
For e.g., 4,-2
b. Float
They are the numbers with fractional part.
For e.g., -100.2,100
c. Complex
They are made up of pairs of real and imaginary numbers.They take the form
x+yJ or x+yj
Where x is the real part and y is the imaginary part.
Note:-For any complex number if we want to extract its real part and
imaginary part we will have to use its two components that is real and imag.
Real and imag always gives floating point in result.
for e.g:-
a=5+2j
consider the example given below(in interactive mode)

The above program in script mode can be written as:

Description of print to be discussed:


2. str(string)
It is a sequence of characters that can be combination of letters,numbers and
special symbols.It is enclosed within ‘ ‘,””,’’’ ‘’’(single,double or triple quotes)
For e.g.,
“abc”,”abc12”,”abc12@#”,”1234”,”547”,”45.2”,”9+2j”,”@#$”
Consider the example given below:

3. bool(Boolean)
This data type represents one of the two values like True or False.
Consider the example given below:

4. None
None represents an empty or missing value.It represents absence of value.

If we wish to see type of a variable then type() is used.The type() determines


what type of value is hold by the variable.
c. Value of the variable/object
Variable=value
variable on the left hand side of assignment operator is called lvalue.expression on
right hand side is called rvalue.
i. Assigning multiple values to multiple variables.
x,y,z=1,2,3
The above code can also be written as:
x=1
y=2
z=3
ii. Assigning same value to multiple variables
x=y=z=4
TOKENS
It is the smallest individual unit of a program.
Python breaks each logical line into a sequence of elementary lexical components known as tokens.
Python has the following tokens:
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators

1. Keywords
Keywords are the reserved words which convey special meaning to compiler. We cannot use a
keyword as a variable name, function name or any other identifier.
To see keyword available in python library:

Note:- All the keywords are in small letters except False,None,True which starts with
capital letter.

2. Identifiers
They are the name given to different parts of program like
variables,objects,classes,functions etc.
Identifier naming rules has been already discussed above.

3. Literals
They are also known as contants or values.They are the data items which have fixed
value.
Several kinds of literals are:
a. String literals
Fore.g., “abc”,”123ab”,’a’,”19/06/2020”
Conclusion:-If we add backslash with string either in single or double or triple quotes
then the string will be printed in the same line.But the string will be printed in the
same way as it is inside triple quotes if we donot add any backslash.
Strings can be single line or multiline :
Example 1

Example 2
 To continue typing text in same line add \ (backslash) at end
 For multiline enclose it within “”” “”” or ‘’’ ‘’’

b. Numeric literals
Integer/float/complex
c. Boolean literals
True/False
d. Special literal None
None
4. Operators
Operators operate upon operands. Operators are the symbols which have some
predefined operations assosciated with them.
A=5
B=6
A+B
Some operators are:
5. Punctuators
They are the symbols which are used to organize expressions,statements and
program structure.
Some punctuators in python are:
‘“#\()[]{}@,:.
Now consider the program given below:
COMMENTS IN PYTHON
Comments are the additional readable information, which is read by
programmers but ignored by python interpreter. They are just given to display
additional information or to give presentable look.
Comments can be :-
a. Single line comment
Single line comment is of two types:-
i. Full line comment

ii. Inline comment

b. Multi line comment


This comment can be given using triple quotes.

OR
IDENTATION IN PYTHON
A group of statements which are part of another statementor a function are
called block or code block.
 To create this block,python uses indentation.
 Statements whose blocks are to be indented have colon(:) at their
end.
Consider example below:

SIMPLE INPUT AND OUTPUT FUNCTIONS

In python, to get input from user we use an inbuilt function input().


Syntax:
Variablename=input(prompt to be displayed)
NOTE:-The input() always returns a value of string type.Whatever value we take as input from
user using input() will be treated as of string type.
Checking the type of variable taken through input()

But if we want to enter numbers specifically to perform mathematical operations,then python


raises error as shown below:
Python offers two functions int() and float() to be used with input() to covert the string type
into int and float type respectively.

Consider one more snapshot below:


Possible errors:-
i. While inputting any integer value through int() and input(),we must make sure that
the value being entered must be a int compatible type.
Consider the two snapshots below:
ii. While inputting floating point numbers using float() and input(),we must make sure
that value being entered must be a float compatible type.
Consider the snapshots below:
Consider the two snapshots below:
In both of the cases python will not report any error.
Python print() function
The print() function is used to display output .
Syntax:-
We can write simply:

A print without any value or name or expression prints empty line as shown above.
NOTE:- print() statement auto converts the items into string.
Questions for practice
ESCAPE SEQUENCE IN PYTHON

An escape sequence is a sequence of characters that does not represent itself when used inside a
character or string literal, but is translated into another character or a sequence of characters that may
be difficult or impossible to represent directly.

Escape sequences are indicated by a backslash ( \ ).

ESCAPE SEQUENCES MEANING


\\ Backslash(\)
\’ Single quote(‘)
\” Double quote(“)
\a ASCII Bell(BEL)
\b ASCII Backspace(BS)
\f ASCII Formfeed(FF)
\n ASCII Linefeed(LF)
\r ASCII Carriage return(CR)
\t ASCII Horizontal tab(TAB)
\v ASCII Vertical tab(VT)
MUTABLE AND IMMUTABLE TYPE:-

The python data objects can be broadly categorized into two-mutable and immutable types:
1. MUTABLE TYPE
Mutable type means those whose values can be changed.
Mutability means that in the same memory address, new value can be stored as and
when we want.
Some mutable types in python are:-
a. List
b. Dictionaries
c. sets
2. IMMUTABLE TYPE
Immutable means unchangeable.The immutable types are those which can never
change their value in place.Whenever on assigns new value to a variable referring to
immutable type,variable’s reference is changed and the previous value is left
unchanged.
In python immutable data types are:-
a. Integers
b. Floating point numbers
c. Booleans
d. Strings
e. Tuples
Why strings are immutable?
In Python, a string is immutable. we cannot overwrite the values of immutable
objects. However, we can assign the variable again. It's not modifying the string
object. it's creating a new string object.

Dynamic Typing feature of Python:


Dynamic typing in Python means the interpreter itself infers the type of data that a variable receives,
without the need of user.

Here, the types are automatically determined at run time, so it doesn't require a declaration of variables
at the time of code.

You might also like