3.PYTHONbasicNotes new
3.PYTHONbasicNotes new
Python was developed by Guido Van Rossum in Feb 1991. It is high level, object
oriented,interpreted language.
Advantages:1.It is easy to use. 2. It is cross platform i.e it can run on windows,linux, unix,
macintosh etc.
Python character set: supports Unicode encoding standards such as
letters,digits,specialsymbols,white spaces.
Tokens in Python: Tokens are smallest individual unit in a program.
Types of tokens:
a) Keywords:-Words having special meanings reserved by programming language.
Eg. True,False,if,for,while,try etc.
b) Identifiers:- Name given to different parts of a program such as variables.
c) Literals: -These are constants that have fixed value throughout the
program.Types:string literals, numeric literals, Boolean literals, special literal None,
Literal collections like tuples, list etc.
d) Operators:- Operator trigger some action when applied to variables.
Eg.Arithmetic operators. Assignment operators. Comparison operators.
Logical operators. Identity operators. Membership operators. Bitwise operators.
e) Punctuators:- Symbols used to organize programming sentence structure. Eg. “ “,
(), {}, [],: etc.
Expressions: Combinations of symbols which Python evaluates to produce a value. Eg.
sum=a+b
Comments: Used to explain python code and make it readable.These are not executed.
Comments begin with “#”
String literals: Single line strings are represented in “(double quotes)”.
Multiline strings in ‘’’(triple quotes)’’’.
Variables: Are containers used for storing data values. It is created the moment we
assign value to it.
1
Rules to declare variable/identifier name:-
1.A variable name must start with a letter or the underscore character.2.A variable name
cannot start with a number.3.A variable name can only contain alpha-numeric characters
and underscores (A-z, 0-9, and _ ).4. Variable names are case-sensitive (age, Age and AGE
are three different variables)
Data Types:- Variables can store 1.numbers(int,float,complex).2.Strings.3.
List.4.Tuples.5.Dictionary
Dynamic typing:- We donot have to declare data type to a variable before assigning a
value to it in Python. Python states kind of variable at runtime.
2
Boolean:- Represents True or False values.
3
enter name:ram
enter age:16
your name is ram
your age is 16
Type casting:- is a method used for changing the variables/ values declared in a certain
data type into a different data type in order to match for the operation required to be
performed.
Implicit type conversion:- is performed automatically by the interpreter, without
user intervention. Python automatically converts one data type to another data
type.
In Explicit Type conversion, the user or programmer converts the data type of an
object to the required data type. InPython we use predefined functions like int(),
float(), str(), bool() etc to perform explicit type conversion.
Syntax:- type(expression)
Eg. float(expression)
4
Python collections (List,Tuples,Dictionary):-
List:- A list is a collection which is ordered and changeable. In Python lists are written
with square brackets.
5
Tuples:- A tuple a collection which is ordered and unchangeable. In Python tuples are
written within round brackets
6
String:- Sequence of characters represented in single or double quotes.
7
Try and Except:-
try block lets you test a block of code for errors.
The except block lets you handle the error.
The finally block lets you execute code, regardless of the result of the
try- and except blocks.
Exception Handling
When an error occurs, or exception as we call it, Python will normally
stop and generate an error message.
These exceptions can be handled using the try statement:
8
#program to check whether a no is even/odd using if..else
enter
no=7
7 is odd
x=5
y=6
z=8
largest of 5 6 8
is 8
9
enter x-5
-5 is
negative
enter
marks55
Grade C
[3, 6, 8,
9, 2]
sum is 28
10
enter natural
number6
sum is 21
#program to find number that are divisible by 7 and multiple of 5 between 1200
and 2200
n8
8 is not prime no
n5
5 is prime no
11
[1 2 3]
12