1.introduction To PYTHON
1.introduction To PYTHON
1.introduction To PYTHON
INTRODUCTION TO PYTHON
FEATURES OF PYTHON
Python is Interpreted
It means that it is processed at runtime by the interpreter and you do not need to compile your
program before executing it.
Python is Interactive
It means that you can actually sit at a Python prompt and interact with the interpreter directly to
write your programs.
Python is Object-Oriented
It means that Python supports Object-Oriented style or technique of programming that
encapsulates code within objects.
Python is Beginner's Language
Python is a great language for the beginner programmers and supports the development of a
wide range of applications from simple text processing to www browsers to games.
ADVANTAGES OF PYTHON
Easy-to-maintain
Python's success is that its source code is fairly easy-to-maintain.
A Broad Standard Library
One of Python's greatest strengths is the bulk of the library is very portable and cross-platform
compatible on UNIX, Windows and Macintosh.
Interactive Mode
Support for an interactive mode in which you can enter results from a terminal right to the
language, allowing interactive testing and debugging of snippets of code.
Portable
Python can run on a wide variety of hardware platforms and has the same interface on all
platforms.
Extendable
You can add low-level modules to the Python interpreter. These modules enable programmers
to add to or customize their tools to be more efficient.
Databases
Python provides interfaces to all major commercial databases.
1
GUI Programming
Python supports GUI applications that can be created and ported to many system
calls, libraries and windows systems, such as Windows MFC, Macintosh and the X
Window system of Unix.
Scalable
Python provides a better structure and support for large programs than shell scripting.
Apart from the above-mentioned features, Python has a big list of good features, few of them
are-
- Support for functional and structured programming methods as well as OOP.
- It can be used as a scripting language or can be compiled to byte-code for building large
applications.
- Very high-level dynamic data types and supports dynamic type checking.
- Supports automatic garbage collection.
- It can be easily integrated with C, C++, COM, ActiveX, CORBA and Java.
DISADVANTAGES OF PYTHON
PYTHON FUNDAMENTALS
We begin our study of Python by learning about its lexical structure and the
Python’s lexical structure comprises five lexical categories. Python uses to
translate code into symbols and punctuation
PYTHON TOKENS
PYTHON IDENTIFIERS
We use identifiers in Python to define the names of objects.
PYTHON KEYWORDS
Keywords are the reserved words in Python. We cannot use a keyword as variable name,
function name or any other identifier.
3
CONSTANTS
PI = 3.14
GRAVITY = 9.8
LITERALS
STRING LITERALS
EG “psg”,”python”, “123DDD”, “a”
STRING TYPES
SINGLE LINE
MULTILINE
Example (multiline)
Str1=“hello\
Welcome”
If slash is not given error will be encountered.
Example (multiline)
Str1=“””hello
Welcome”””
NUMERIC LITERALS
Numeric Literals are immutable (unchangeable). Numeric literals can belong to 3 different
numerical types Integer, Float and Complex.
4
INT (WHOLE NUMBERS (4 bytes– SIGNED INTEGERS)
BOOLEAN LITERALS
TRUE
FALSE
NONE
OPEARTORS
ARITHMETIC
RELATIONAL
ASSIGNMENT
LOGICAL
MEMBERSHIP OPERATORS
ARITHMETIC OPERATORS
5
COMPARISON OPERATORS
Comparison operators are used to compare values. It either returns True or False according
to the condition.
LOGICAL OPERATORS
ASSIGNMENT OPERATORS
a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a
on the left.
There are various compound operators in Python like a += 5 that adds to the variable and
later assigns the same. It is equivalent to a = a + 5.
6
IDENTITY OPERATORS
is and is not are the identity operators in Python. They are used to check if two values (or
variables) are located on the same part of the memory. Two variables that are equal does not
imply that they are identical.
MEMBERSHIP OPERATORS
in and not in are the membership operators in Python. They are used to test whether a value
or variable is found in a sequence (string, list, tuple, set and dictionary).
In a dictionary we can only test for presence of key, not the value.
PUNCTUATORS
‘ “ ( ) [ ] {}, : =
BAREBONES OF PYTHON
EXPRESSIONS (A+B)
COMMENTS (# -SINGLE ‘’’-MULTI)
STATEMENTS ( PRINT “HELLO”, IF A>B)
FUNCTIONS (DOIT())
BLOCKS AND INDENTATION
7
VARIABLE ASSIGNMENT in PYTHON
A,B,C=12,4,5
a,b,c=12,6,7
a,b,c=a+1,b+2,c+3
print a,b,c
x,x=20,30
y,y=x+10,x+20
print x,y
SIMPLE PROGRAMS
print('Hello, world!')
Output
Hello, world!
Output
8
# Python Program to find the area of triangle
a=5
b=6
c=7
s = (a + b + c) / 2
Output
x=5
y = 10
9
# create a temporary variable and swap the values
temp = x
x=y
y = temp
Output
celsius = 37.5
# calculate fahrenheit
Output
10
7. What are tokens?
8. What is an identifier?
9. Explain about the barebones of Python.
10. How many string types are accepted in Python? Explain with an example
11