Python First I,II,III IV Notes
Python First I,II,III IV Notes
Introduction to Python
V.L.Kartheek
B.Tech(CSE),M.Tech(CST),
(Ph.D(CSIS))
Dept of CSE
TEXT BOOK:
1. Core Python Programming, Wesley J. Chun, Second Edition, Pearson
Topics to discuss:-
Program vs Script,
What is Python,
History of Python
Scope of Python,
Advantages of Python,
Fascinating Python Applications in the Real time,
Code Editor vs IDE,
Installation of Python,
Python Code Execution.
Date : 18-04-2023
Day: Tuesday CLASS-I
Python is a great language for the beginner level programmers and supports the
development of a wide range of applications.
Python is a programming language that allows you work quickly and integrate
systems more efficiently.
History of Python:
Python was developed by Guido van Rossum, a Dutch computer programmer in
December 1989 at the National Research Institute for Mathematics and Computer
This was a successor to the ABC programming language which was capable of
2. Expressive
First, let’s learn about expressiveness. Suppose we have two languages A and
B, and all programs that can be made in A can be made in B using local
transformations. However, there are some programs that can be made in B,
but not in A, using local transformations. Then, B is said to be more
expressive than A.
3. Free and Open-Source
Firstly, Python is freely available. You can download it from the following
link
https://www.python.org/downloads/.
5. Portable
We can take one code and run it on any machine, there is no need to write
different code for different machines. This makes Python a portable language.
However, you must avoid any system-dependent features in this case.
6. Interpreted:
If you’re any familiar with languages like C++ or Java, you must first compile it,
and then run it. But in Python, there is no need to compile it. Internally, its source
code is converted into an immediate form called bytecode.
So, all you need to do is to run your Python code without worrying about linking
to libraries, and a few other things.
By interpreted, we mean the source code is executed line by line, and not all at
once. Because of this, it is easier to debug your code.
Also, interpreting makes it just slightly slower than Java, but that does not matter
compared to the benefits it has to offer.
7. Object-Oriented
A programming language that can model the real world is said to be object-
oriented. It focuses on objects, and combines data and functions.
8. Extensible
If needed, you can write some of your Python code in other languages like C++.
This makes Python an extensible language, meaning that it can be extended to other
languages.
9. Embeddable
We can put our Python code in a source code in a different language like C++.
We can be easily integrated with C, C++, COM, ActiveX, CORBA, and Java.
CPython is a python integrated with C/C++ language.
Similarly JPython is a purely integrated language.
Python can connect to database systems. It can also read and modify files.
Python can be used to handle big data and perform complex mathematics.
Python has syntax that allows developers to write programs with fewer lines
than some other programming languages.
In Python, there is no provision to write multi-line comments, or a block comment. (As
in C#/C/C++, where multiple lines inside /* .. */ are treated as a multi-line comment).
Each line should have the # symbol at the start to be marked as a comment. Many
Python IDEs have shortcuts to mark a block of statements as a comment. In IDLE, select the
block and press Alt + 3.
Date : 25-04-2023
Day: Tuesday CLASS-IV
4.Indentation:
One of the most distinctive features of python is use of indentation to mark blocks of code.
Groups of individual statements making up a single code block are called "suites“(:)
Generally, we use curly brackets { } in C,C++,java languages but where as in python curly
braces are not allowed to indicate block of code for class ,function definition or flow of control.
So, in python block of code are denoted by indentation.
Instead of curly braces { }, python uses whitespaces(spaces and tabs) to define program
blocks.
The number of whitespaces (spaces and tabs) in the indentation is not fixed, but all
statements within the block must be the indented same amount.
When a block is to be started, type the exclamation symbol (:) and press Enter.
In order to signal the end of a block, the whitespace is de-dented by pressing the backspace
key
In the following program, the block statements have no indentation.
Instructions:
Use 4 spaces per indentation and no tabs.
Do not mix tabs and spaces. Tabs create confusion and it is recommended
to use only spaces.
5. Python identifiers:
A identifier is a name used to identify a variable, function, class, module or other object.
An identifier start with a letter A t o Z (or) a to z an underscore(_) followed by zero or
more letters, underscores and digits(0-9).
Python doesn’t allows punctuation characters such as @,$,% with identifiers.
1.An identifier should not start with digit but it can contains digit.
Ex:
>>> 12ab=20 syntax error: invalid syntax
>>> ab12=20 20
2. An identifier can contain underscores (single, double, and also triple)
3. An identifier must not contain special characters except underscores.
4. Identifiers are case sensitive, so python is a case sensitive language.
>>> a=100
>>> A=200
>>> print(a)
100
>>> print(A)
200
>>> var='python'
>>> print(Var)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'Var' is not defined
>>> x=y=z=50
>>> x
50
>>> y
50
>>> z
50
>>>
4. To check the data type of specific variable we can use type( ) function.
In C, you know that assignments are treated as expressions. This is not the case in
Python, where assignments do not have inherent values.
Statements such as the following are invalid in Python:
>>> x = 1
>>> y = (x = x + 1) # assignments not expressions!
File "<stdin>", line 1
y = (x = x + 1)
^
SyntaxError: invalid syntax
b) . Augmented Assignment:
Augmented assignment refers to the use of operators, which imply both an arithmetic
operation as well as an assignment.
Ex:
>>> a=2
>>> a+=1
>>> a
3
>>> a*=2
>>> a
6
>>> a-=3
>>> a
3
>>> a/=3
>>> a
1.0
>>> a%=3
>>> a
1.0
>>>
9.OUTPUT Statement:
In some languages, such as C, displaying to the screen is accomplished with a function,
e.g., printf(),
while with Python and most interpreted and scripting languages, print is a statement.
Many shell script languages use an echo command for program output.
Example:
>>> myString = 'Hello World!'
>>> print (myString)
Hello World!
>>> myString
'Hello World!‘
Note: The underscore (_) also has special meaning in the interactive interpreter: the last
evaluated expression.
>>> _
Hello World!
In python2, print statement, paired with the string format operator ( % ), supports
string substitution, much like the printf() function in C:
Ex:
>>> print "%s is number %d!" % ("Python", 1)
Python is number 1!
>>> p,q=4,3
>>> print "addition of %d and %d is %d" %(p,q,p+q)
addition of 4 and 3 is 7
In python3, The print() function to display output prints the specified message to the
o/p:
hello
python
Programming
print("hello",end='')
print("python",end='')
print("programming")
o/p:
hello python programming
Note: The default value for end attribute is \n ,which is nothing but new line character
5.print(object) statement:
We can pass any object (list,tuple,set..) as an argument to the print() statement.
Ex:
>>> list=[1,2,3,4]
>>> tuple=(10,20,30,40)
>>> print(list)
[1, 2, 3, 4]
>>> print(tuple)
(10, 20, 30, 40)
>>>
6.print(string, varaible list):
We can use print() statement with string and any number of arguments
Ex:
>>>s="python"
>>> s1="Guido van Rossum"
>>> s2=1989
>>> print("Hello",s,"was developed by",s1,"in the year",s2)
Output:
Hello python was developed by Guido van Rossum in the year 1989
EX2:
>>> a=5
>>>b=6
>>>c=7
>>>print("a=",a,"b=",b,"c=",c);
OUTPUT:- a= 5 b= 6 c= 7
7. print(formatted string)
%d--------------------int
%f--------------------float
%s--------------------string
Ex:
>>> s="IV CSE A"
>>> list=[1,2,3,4]
>>> print("hello %s.....the list of items are %s“ %(s,list))
Output:
hello IV CSE A.....the list of items are [1, 2, 3, 4]
8. print() with replacement operator:
>>> s="python"
>>> s1="Guido van Rossum"
>>> s2=1989
>>> print("Hello {0} was developed by {1} in the year {2}".format(s,s1,s2))
Output:
Hello python was developed by Guido van Rossum in the year 1989
10.Reading data from the keyboard:
Here Python2 provides us with two inbuilt functions to read the input from the
keyboard.
1. raw_input ( prompt )
2. input ( prompt )
1. raw_input ( ):
This function takes exactly what is typed from the keyboard, convert it to string and
then return it to the variable in which we want to store.
Ex:
>>> key=raw_input("Enter your name")
Enter your name RAJEEV
>>> type(key)
<type 'str'>