0% found this document useful (0 votes)
46 views

Python Unit 1

This document provides an introduction to the Python programming language. It discusses that Python is a high-level, general-purpose programming language that was created in 1991 and is widely used for applications like web development, data analysis, and artificial intelligence. The document then provides examples of simple Python code to print "Hello World" and calculate the sum of two numbers. It continues with descriptions of key Python features like being easy to learn, open source, object-oriented, portable, and having an extensive library. The document concludes with discussing Python flavors, identifiers, reserved words, and conditional statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views

Python Unit 1

This document provides an introduction to the Python programming language. It discusses that Python is a high-level, general-purpose programming language that was created in 1991 and is widely used for applications like web development, data analysis, and artificial intelligence. The document then provides examples of simple Python code to print "Hello World" and calculate the sum of two numbers. It continues with descriptions of key Python features like being easy to learn, open source, object-oriented, portable, and having an extensive library. The document concludes with discussing Python flavors, identifiers, reserved words, and conditional statements.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Language Fundamentals

Introduction
Python is a general purpose high level programming language.
Python was developed by Guido Van Rossam in 1989 while working at National Research
Institute at Netherlands.
But officially Python was made available to public in 1991. The official Date of Birth
forPython is : Feb 20th 1991.
Python is recommended as first programming language for beginners.

Eg1: To print Helloworld:

Java:
1) public class HelloWorld
2) {
3) p s v main(String[] args)
4) {
5) SOP("Hello world");
6) }
7) }

C:
1) #include<stdio.h>
2) void main()
3) {
4) print("Hello world");
5) }
Python:
print("Hello World")

Eg2: To print the sum of 2 numbers


Java:
1) public class Add
2) {
3) public static void main(String[] args)
4) {
5) int a,b;
6) a =10;
7) b=20;
8) System.out.println("The Sum:"+(a+b));
9) }
10) }
C:
1) #include <stdio.h>
2)
3) void main()
4) {
5) int a,b;
6) a =10;
7) b=20;
8) printf("The Sum:%d",(a+b));
9) }
Python:
1) a=10
2) b=20
3) print("The Sum:",(a+b))

The name Python was selected from the TV Show "The Complete Monty Python's Circus",
which was broadcasted in BBC from 1969 to 1974.
Guido developed Python language by taking almost all programming features from different
languages
1. Functional Programming Features from C
2. Object Oriented Programming Features from C++
3. Scripting Language Features from Perl and Shell Script
4. Modular Programming Features from Modula-3

Most of syntax in Python Derived from C and ABC languages.

Where we can use Python:

We can use everywhere. The most common important application areas are
1. For developing Desktop Applications
2. For developing web Applications
3. For developing database Applications
4. For Network Programming
5. For developing games
6. For Data Analysis Applications
7. For Machine Learning
8. For developing Artificial Intelligence Applications
9. For IOT
...
Note:
Internally Google and Youtube use Python coding
NASA and Nework Stock Exchange Applications developed by Python.
Top Software companies like Google, Microsoft, IBM, Yahoo using Python.
Features of Python:
1. Simple and easy to learn:
Python is a simple programming language. When we read Python program,we can feel like
reading english statements.
The syntaxes are very simple and only 30+ kerywords are available.
When compared with other languages, we can write programs with very less number of
lines. Hence more readability and simplicity.
We can reduce development and cost of the project.
2. Freeware and Open Source:
We can use Python software without any licence and it is freeware.
Its source code is open,so that we can we can customize based on our requirement.
Eg: Jython is customized version of Python to work with Java Applications.

3. High Level Programming language:


Python is high level programming language and hence it is programmer friendly language.
Being a programmer we are not required to concentrate low level activities like memory
management and security etc..
4. Platform Independent:
Once we write a Python program,it can run on any platform without rewriting once again.
Internally PVM is responsible to convert into machine understandable form.
5. Portability:
Python programs are portable. ie we can migrate from one platform to another platform
very easily. Python programs will provide same results on any paltform.
6. Dynamically Typed:
In Python we are not required to declare type for variables. Whenever we are assigning
the value, based on value, type will be allocated automatically.Hence Python is considered
as dynamically typed language.
But Java, C etc are Statically Typed Languages b'z we have to provide type at the beginning
only.
This dynamic typing nature will provide more flexibility to the programmer.
7. Both Procedure Oriented and Object Oriented:
Python language supports both Procedure oriented (like C, pascal etc) and object oriented (like
C++,Java) features. Hence we can get benefits of both like security and reusability etc
8. Interpreted:
We are not required to compile Python programs explcitly. Internally Python interpreter
will take care that compilation.
If compilation fails interpreter raised syntax errors. Once compilation success then PVM
(Python Virtual Machine) is responsible to execute.
9. Extensible:
We can use other language programs in Python.
The main advantages of this approach are:
1. We can use already existing legacy non-Python code
2. We can improve performance of the application
10. Embedded:
We can use Python programs in any other language programs.
i.e we can embedd Python programs anywhere.
11. Extensive Library:
Python has a rich inbuilt library.
Being a programmer we can use this library directly and we are not responsible to
implement the functionality.
etc...
Limitations of Python:
1. Performance wise not up to the mark b'z it is interpreted language.
2. Not using for mobile Applications
Flavors of Python:
1.CPython:
It is the standard flavor of Python. It can be used to work with C lanugage Applications

2. Jython or JPython:
It is for Java Applications. It can run on JVM

3. IronPython:
It is for C#.Net platform

4.PyPy:
The main advantage of PyPy is performance will be improved because JIT compiler is
available inside PVM.

5.RubyPython
For Ruby Platforms

6. AnacondaPython
It is specially designed for handling large volume of data processing.
Identifiers
A name in Python program is called identifier.
It can be class name or function name or module name or variable name.
a = 10
Rules to define identifiers in Python:
1. The only allowed characters in Python are
alphabet symbols(either lower case or upper case)
digits(0 to 9)
underscore symbol(_)

By mistake if we are using any other symbol like $ then we will get syntax error.
cash = 10
ca$h =20

2. Identifier should not starts with digit


123total
total123

3. Identifiers are case sensitive. Of course Python language is case sensitive language.
total=10
TOTAL=999
print(total) #10
print(TOTAL) #999

Identifier:
1. Alphabet Symbols (Either Upper case OR Lower case)
2. If Identifier is start with Underscore (_) then it indicates it is private.
3. Identifier should not start with Digits.
4. Identifiers are case sensitive.
5. We cannot use reserved words as identifiers
Eg: def=10
6. There is no length limit for Python identifiers. But not recommended to use too lengthy
identifiers.
7. Dollor ($) Symbol is not allowed in Python.

Q. Which of the following are valid Python identifiers?


1) 123total
2) total123
3) java2share
4) ca$h
5) _abc_abc_
6) def
7) if
Note:
1. If identifier starts with _ symbol then it indicates that it is private
2. If identifier starts with __(two under score symbols) indicating that strongly private
identifier.
3.If the identifier starts and ends with two underscore symbols then the identifier is
language defined special name,which is also known as magic methods.
Eg: __add__

Reserved Words
In Python some words are reserved to represent some meaning or functionality. Such type
of words are called Reserved words.
There are 33 reserved words available in Python.
True,False,None
and, or ,not,is
if,elif,else
while,for,break,continue,return,in,yield
try,except,finally,raise,assert
import,from,as,class,def,pass,global,nonlocal,lambda,del,with

Note:
1. All Reserved words in Python contain only alphabet symbols.
2. Except the following 3 reserved words, all contain only lower case alphabet symbols.
True
False
None

Eg: a= true
a=True
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else','except',
'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or','pass', 'raise',
'return', 'try', 'while', 'with', 'yield']

Flow Control

I. Conditional Statements
1) if
if condition : statement
or
if condition :
statement-1
statement-2
statement-3
If condition is true then statements will be executed.

Eg:
1) name=input("Enter Name:")
2) if name=="durga" :
3) print("Hello Durga Good Morning")
4) print("How are you!!!")
5)
6) D:\Python_classes>py test.py
7) Enter Name:durga
8) Hello Durga Good Morning
9) How are you!!!
10)
11) D:\Python_classes>py test.py
12) Enter Name:Ravi
13) How are you!!!

2) if-else:
if condition :
Action-1
else :
Action-2
if condition is true then Action-1 will be executed otherwise Action-2 will be executed.

Eg:
1) name=input("Enter Name:")
2) if name=="durga" :
3) print("Hello Durga Good Morning")
4) else:
5) print("Hello Guest Good Moring")
6) print("How are you!!!")
7)
8) D:\Python_classes>py test.py
9) Enter Name:durga
10) Hello Durga Good Morning
11) How are you!!!
12)
13) D:\Python_classes>py test.py
14) Enter Name:Ravi
15) Hello Guest Good Moring
16) How are you!!!

3) if-elif-else:
Syntax:
if condition1:
Action-1
elif condition2:
Action-2
elif condition3:
Action-3
elif condition4:
Action-4
...
else:
Default Action
Based condition the corresponding action will be executed.
Eg:
1) brand=input("Enter Your Favourite Brand:")
2) if brand=="RC" :
3) print("It is childrens brand")
4) elif brand=="KF":
5) print("It is not that much kick")
6) elif brand=="FO":
7) print("Buy one get Free One")
8) else :
9) print("Other Brands are not recommended")
10)
11)
12) D:\Python_classes>py test.py
13) Enter Your Favourite Brand:RC
14) It is childrens brand
15)
16) D:\Python_classes>py test.py
17) Enter Your Favourite Brand:KF
18) It is not that much kick
19)
20) D:\Python_classes>py test.py
21) Enter Your Favourite Brand:KALYANI
22) Other Brands are not recommended
Note:
1. else part is always optional
Hence the following are various possible syntaxes.
1. if
2. if - else
3. if-elif-else
4.if-elif
2. There is no switch statement in Python

Q. Write a program to find biggest of given 2 numbers from the commad prompt?
1) n1=int(input("Enter First Number:"))
2) n2=int(input("Enter Second Number:"))
3) if n1>n2:
4) print("Biggest Number is:",n1)
5) else :
6) print("Biggest Number is:",n2)
7)
8) D:\Python_classes>py test.py
9) Enter First Number:10
10) Enter Second Number:20
11) Biggest Number is: 20

Q. Write a program to find biggest of given 3 numbers from the commad prompt?
1) n1=int(input("Enter First Number:"))
2) n2=int(input("Enter Second Number:"))
3) n3=int(input("Enter Third Number:"))
4) if n1>n2 and n1>n3:
5) print("Biggest Number is:",n1)
6) elif n2>n3:
7) print("Biggest Number is:",n2)
8) else :
9) print("Biggest Number is:",n3)
10)
11) D:\Python_classes>py test.py
12) Enter First Number:10
13) Enter Second Number:20
14) Enter Third Number:30
15) Biggest Number is: 30
16)
17) D:\Python_classes>py test.py
18) Enter First Number:10
19) Enter Second Number:30
20) Enter Third Number:20
21) Biggest Number is: 30

Q. Write a program to find smallest of given 2 numbers?


Q. Write a program to find smallest of given 3 numbers?
Q. Write a program to check whether the given number is even or odd?
Q. Write a program to check whether the given number is in between 1 and 100?
1) n=int(input("Enter Number:"))
2) if n>=1 and n<=10 :
3) print("The number",n,"is in between 1 to 10")
4) else:
5) print("The number",n,"is not in between 1 to 10")

Q. Write a program to take a single digit number from the key board and print is value in
English word?
1) 0==>ZERO
2) 1 ==>ONE
3)
4) n=int(input("Enter a digit from o to 9:"))
5) if n==0 :
6) print("ZERO")
7) elif n==1:
8) print("ONE")
9) elif n==2:
10) print("TWO")
11) elif n==3:
12) print("THREE")
13) elif n==4:
14) print("FOUR")
15) elif n==5:
16) print("FIVE")
17) elif n==6:
18) print("SIX")
19) elif n==7:
20) print("SEVEN")
21) elif n==8:
22) print("EIGHT")
23) elif n==9:
24) print("NINE")
25) else:
26) print("PLEASE ENTER A DIGIT FROM 0 TO 9")

II. Iterative Statements


If we want to execute a group of statements multiple times then we should go for Iterative
statements.
Python supports 2 types of iterative statements.
1. for loop
2. while loop

1) for loop:
If we want to execute some action for every element present in some sequence(it may be
string or collection)then we should go for for loop.

Syntax:
for x in sequence :
body
where sequence can be string or any collection.

Body will be executed for every element present in the sequence.


Eg 1: To print characters present in the given string
1) s="Sunny Day"
2) for x in s :
3) print(x)
4)
5) Output
6) S
7) u
8) n
9) n
10) y
11)
12) D
13) a
14) y
Eg 2: To print characters present in string index wise:
1) s=input("Enter some String: ")
2) i=0
3) for x in s :
4) print("The character present at ",i,"index is :",x)
5) i=i+1
6)
7)
8) D:\Python_classes>py test.py
9) Enter some String: Sunny Day
10) The character present at 0 index is : S
11) The character present at 1 index is : u
12) The character present at 2 index is : n
13) The character present at 3 index is : n
14) The character present at 4 index is : y
15) The character present at 5 index is :
16) The character present at 6 index is :D
17) The character present at 7 index is : a
18) The character present at 8 index is : y

Eg 3: To print Hello 10 times


1) for x in range(10) :
2) print("Hello")

Eg 4: To display numbers from 0 to 10


1) for x in range(11) :
2) print(x)

Eg 5: To display odd numbers from 0 to 20


1) for x in range(21) :
2) if (x%2!=0):
3) print(x)

Eg 6: To display numbers from 10 to 1 in descending order


1) for x in range(10,0,-1) :
2) print(x)

Eg 7: To print sum of numbers presenst inside list


1) list=eval(input("Enter List:"))
2) sum=0;
3) for x in list:
4) sum=sum+x;
5) print("The Sum=",sum)
6)
7) D:\Python_classes>py test.py
8) Enter List:[10,20,30,40]
9) The Sum= 100
10)
11) D:\Python_classes>py test.py
12) Enter List:[45,67]
13) The Sum= 112

2) while loop:
If we want to execute a group of statements iteratively until some condition false,then we
should go for while loop.
Syntax:
while condition :
body
Eg: To print numbers from 1 to 10 by using while loop
1) x=1
2) while x <=10:
3) print(x)
4) x=x+1
Eg: To display the sum of first n numbers
1) n=int(input("Enter number:"))
2) sum=0
3) i=1
4) while i<=n:
5) sum=sum+i
6) i=i+1
7) print("The sum of first",n,"numbers is :",sum)
Eg: write a program to prompt user to enter some name until entering Durga
1) name=""
2) while name!="durga":
3) name=input("Enter Name:")
4) print("Thanks for confirmation")

Infinite Loops:
1) i=0;
2) while True :
3) i=i+1;
4) print("Hello",i)

Nested Loops:
Sometimes we can take a loop inside another loop,which are also known as nested loops.
Eg:
1) for i in range(4):
2) for j in range(4):
3) print("i=",i," j=",j)
4)
5) Output
6) D:\Python_classes>py test.py
7) i= 0 j= 0
8) i= 0 j= 1
9) i= 0 j= 2
10) i= 0 j= 3
11) i= 1 j= 0
12) i= 1 j= 1
13) i= 1 j= 2
14) i= 1 j= 3
15) i= 2 j= 0
16) i= 2 j= 1
17) i= 2 j= 2
18) i= 2 j= 3
19) i= 3 j= 0
20) i= 3 j= 1
21) i= 3 j= 2
22) i= 3 j= 3
Q. Write a program to dispaly *'s in Right angled triangled form
1) *
2) * *
3) * * *
4) * * * *
5) * * * * *
6) * * * * * *
7) * * * * * * *
8)
9) n = int(input("Enter number of rows:"))
10) for i in range(1,n+1):
11) for j in range(1,i+1):
12) print("*",end=" ")
13) print()
Alternative way:
1) n = int(input("Enter number of rows:"))
2) for i in range(1,n+1):
3) print("* " * i)

Q. Write a program to display *'s in pyramid style(also known as equivalent triangle)


1) *
2) * *
3) * * *
4) * * * *
5) * * * * *
6) * * * * * *
7) * * * * * * *
8)
9) n = int(input("Enter number of rows:"))
10) for i in range(1,n+1):
11) print(" " * (n-i),end="")
12) print("* "*i)

III. Transfer Statements


1) break:
We can use break statement inside loops to break loop execution based on some condition.
Eg:
1) for i in range(10):
2) if i==7:
3) print("processing is enough..plz break")
4) break
5) print(i)
6)
7) D:\Python_classes>py test.py
8) 0
9) 1
10) 2
11) 3
12) 4
13) 5
14) 6
15) processing is enough..plz break
Eg:
1) cart=[10,20,600,60,70]
2) for item in cart:
3) if item>500:
4) print("To place this order insurence must be required")
5) break
6) print(item)
7)
8) D:\Python_classes>py test.py
9) 10
10) 20
11) To place this order insurence must be required

2) continue:
We can use continue statement to skip current iteration and continue next iteration.
Eg 1: To print odd numbers in the range 0 to 9
1) for i in range(10):
2) if i%2==0:
3) continue
4) print(i)
5)
6) D:\Python_classes>py test.py
7) 1
8) 3
9) 5
10) 7
11) 9
Eg 2:
1) cart=[10,20,500,700,50,60]
2) for item in cart:
3) if item>=500:
4) print("We cannot process this item :",item)
5) continue
6) print(item)
7)
8) Output
9) D:\Python_classes>py test.py
10) 10
11) 20
12) We cannot process this item : 500
13) We cannot process this item : 700
14) 50
15) 60

Eg 3:
1) numbers=[10,20,0,5,0,30]
2) for n in numbers:
3) if n==0:
4) print("Hey how we can divide with zero..just skipping")
5) continue
6) print("100/{} = {}".format(n,100/n))
7)
8) Output
9)
10) 100/10 = 10.0
11) 100/20 = 5.0
12) Hey how we can divide with zero..just skipping
13) 100/5 = 20.0
14) Hey how we can divide with zero..just skipping
15) 100/30 = 3.3333333333333335
loops with else block:
Inside loop execution,if break statement not executed ,then only else part will be executed.
else means loop without break
Eg:
1) cart=[10,20,30,40,50]
2) for item in cart:
3) if item>=500:
4) print("We cannot process this order")
5) break
6) print(item)
7) else:
8) print("Congrats ...all items processed successfully")
9)
10) Output
11) 10
12) 20
13) 30
14) 40
15) 50
16) Congrats ...all items processed successfully
Eg:
1) cart=[10,20,600,30,40,50]
2) for item in cart:
3) if item>=500:
4) print("We cannot process this order")
5) break
6) print(item)
7) else:
8) print("Congrats ...all items processed successfully")
9)
10) Output
11) D:\Python_classes>py test.py
12) 10
13) 20
14) We cannot process this order

Q. What is the difference between for loop and while loop in Python?
We can use loops to repeat code execution
Repeat code for every item in sequence ==>for loop
Repeat code as long as condition is true ==>while loop

Q. How to exit from the loop?


by using break statement

Q. How to skip some iterations inside loop?


by using continue statement.
Q. When else part will be executed wrt loops?
If loop executed without break
3) pass statement:
pass is a keyword in Python.
In our programming syntactically if block is required which won't do anything then we can
define that empty block with pass keyword.
pass
|- It is an empty statement
|- It is null statement
|- It won't do anything
Eg:
if True:
SyntaxError: unexpected EOF while parsing
if True: pass
==>valid
def m1():
SyntaxError: unexpected EOF while parsing
def m1(): pass
use case of pass:
Sometimes in the parent class we have to declare a function with empty body and child
class responsible to provide proper implementation. Such type of empty body we can
define by using pass keyword. (It is something like abstract method in java)
Eg:
def m1(): pass
Eg:
1) for i in range(100):
2) if i%9==0:
3) print(i)
4) else:pass
5)
6) D:\Python_classes>py test.py
7) 0
8) 9
9) 18
10) 27
11) 36
12) 45
13) 54
14) 63
15) 72
16) 81
17) 90
18) 99
del statement:
del is a keyword in Python.
After using a variable, it is highly recommended to delete that variable if it is no longer
required,so that the corresponding object is eligible for Garbage Collection.
We can delete variable by using del keyword.
Eg:
1) x=10
2) print(x)
3) del x
After deleting a variable we cannot access that variable otherwise we will get NameError.
Eg:
1) x=10
2) del x
3) print(x)
NameError: name 'x' is not defined.
Note:
We can delete variables which are pointing to immutable objects.But we cannot delete the
elements present inside immutable object.
Eg:
1) s="durga"
2) print(s)
3) del s==>valid
4) del s[0] ==>TypeError: 'str' object doesn't support item deletion
Difference between del and None:
In the case del, the variable will be removed and we cannot access that variable(unbind
operation)
1) s="durga"
2) del s
3) print(s) ==>NameError: name 's' is not defined.
But in the case of None assignment the variable won't be removed but the corresponding
object is eligible for Garbage Collection(re bind operation). Hence after assigning with
None value,we can access that variable.
1) s="durga"
2) s=None
3) print(s) # None

You might also like