[go: up one dir, main page]

0% found this document useful (0 votes)
10 views9 pages

PI B8 Python 1

Python is a versatile programming language used for various applications including desktop, web, and database development, as well as data analysis and machine learning. It is known for its simplicity, open-source nature, and extensive libraries, making it accessible for developers. However, Python has limitations in performance and is not typically used for mobile applications.

Uploaded by

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

PI B8 Python 1

Python is a versatile programming language used for various applications including desktop, web, and database development, as well as data analysis and machine learning. It is known for its simplicity, open-source nature, and extensive libraries, making it accessible for developers. However, Python has limitations in performance and is not typically used for mobile applications.

Uploaded by

Hanock Jacob
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

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 Newyork 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.

Basic Data Types in Python:

1) Numeric --> a) Integer b) Float c) Complex Number


2) Boolean
3) Sets
4) Dictionary
5) Sequence --> a) Strings b) List c) Tuple

============================================
I. Fundamental Category Data Types
============================================
=>The main purpose of Fundamental Category Data Types is that "To store Single
Value".
=> In Python Programming, we have 4 types in Fundamental Category. They are
1) int
2) float
3) bool
4) complex
int data type:
We can use int data type to represent integer values.
float data type:
We can use float data type to represent floating point values (decimal values)
Eg: f=1.234
type(f) float
We can also represent floating point values by using exponential form (scientific
notation)
Eg: f=1.2e3
print(f) 1200.0
instead of 'e' we can use 'E'
The main advantage of exponential form is we can represent big values in less
memory.
***Note:
We can represent int values in decimal, binary, octal and hexa decimal forms. But
we can
represent float values only by using decimal form.
Eg:
1) >>> f=0B11.01
2) File "<stdin>", line 1
3) f=0B11.01
4) ^
5) SyntaxError: invalid syntax
6)
7) >>> f=0o123.456
8) SyntaxError: invalid syntax
9)
10) >>> f=0X123.456
11) SyntaxError: invalid syntax
Complex Data Type:
A complex number is of the form

a and b contain intergers or floating point values


Eg:
3+5j
10+5.5j
0.5+0.1j

In the real part if we use int value then we can specify that either by
decimal,octal,binary
or hexa decimal form.
But imaginary part should be specified only by using decimal form.
1) >>> a=0B11+5j
2) >>> a
3) (3+5j)
4) >>> a=3+0B11j
5) SyntaxError: invalid syntax
Even we can perform operations on complex type values.
1) >>> a=10+1.5j
2) >>> b=20+2.5j
3) >>> c=a+b
4) >>> print(c)
5) (30+4j)
6) >>> type(c)
7) <class 'complex'>

Note: Complex data type has some inbuilt attributes to retrieve the real part and
imaginary part
c=10.5+3.6j
c.real==>10.5
c.imag==>3.6
We can use complex type generally in scientific Applications and electrical
engineering
Applications.
4.bool data type:
We can use this data type to represent boolean values.
The only allowed values for this data type are:
True and False
Internally Python represents True as 1 and False as 0
b=True
type(b) =>bool
Eg:
a=10
b=20
c=a<b
print(c)==>True
True+True==>2
True-False==>1
str type:
str represents String data type.
A String is a sequence of characters enclosed within single quotes or double
quotes.
s1='PrimeIntuit'
s1='PrimeIntuit'
By using single quotes or double quotes we cannot represent multi line string
literals.
s1='PrimeIntuit'

We can also use triple quotes to use single quote or double quote in our String.
''' This is " character'''
' This i " Character '
We can embed one string in another string
'''This "Python class very helpful" for java students'''

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 X
2. Identifier should not starts with digit
123total X
total123 √

3. Identifiers are case sensitive. Of course Python language is case sensitive


language.

total=10
TOTAL=999
print(total) #10
print(TOTAL) #99

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 X
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 X
2) total123 √
3) java2share √
4) ca$h X
5) _abc_abc_ √
6) def X
7) if X
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 X
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'

We can convert one type value to another type. This conversion is called
Typecasting or
Type coersion.
The following are various inbuilt functions for type casting.
1. int()
2. float()
3. complex()
4. bool()
5. str()

1.int():
We can use this function to convert values from other types to int
Eg:
1) >>> int(123.987)
2) 123
3) >>> int(10+5j)
4) TypeError: can't convert complex to int
5) >>> int(True)
6) 1
7) >>> int(False)
8) 0
9) >>> int("10")
10) 10
11) >>> int("10.5")
12) ValueError: invalid literal for int() with base 10: '10.5'
13) >>> int("ten")
14) ValueError: invalid literal for int() with base 10: 'ten'
15) >>> int("0B1111")
16) ValueError: invalid literal for int() with base 10: '0B1111'
Note:
1. We can convert from any type to int except complex type.
2. If we want to convert str type to int type, compulsary str should contain only
integral
value and should be specified in base-10

2. float():
We can use float() function to convert other type values to float type.
1) >>> float(10)
2) 10.0
3) >>> float(10+5j)
4) TypeError: can't convert complex to float
5) >>> float(True)
6) 1.0
7) >>> float(False)
8) 0.0
9) >>> float("10")
10) 10.0
11) >>> float("10.5")
12) 10.5
13) >>> float("ten")
14) ValueError: could not convert string to float: 'ten'
15) >>> float("0B1111")
16) ValueError: could not convert string to float: '0B1111'
Note:
1. We can convert any type value to float type except complex type.
2. Whenever we are trying to convert str type to float type compulsary str should
be
either integral or floating point literal and should be specified only in base-10.

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=="mohan" :
3) print("Hello Mohan Good Morning")
4) print("How are you!!!")
5)
6) D:\Python_classes>py test.py
7) Enter Name:mohan
8) Hello Mohan 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=="mohan" :
3) print("Hello Mohan 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:mohan
10) Hello Mohan 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

You might also like