ch-1 Introduction To Python
ch-1 Introduction To Python
PROGRAMMING LANGUAGE:-
A programming language is a formal language comprising a set of instructions that
produce various kinds of output.
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
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.
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)
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.
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
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:
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.
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.
Here, the types are automatically determined at run time, so it doesn't require a declaration of variables
at the time of code.