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

3.PYTHONbasicNotes new

Python, developed by Guido Van Rossum in 1991, is a high-level, object-oriented, interpreted programming language known for its ease of use and cross-platform compatibility. It features a variety of data types, dynamic typing, and supports collections like lists, tuples, and dictionaries, along with exception handling through try and except blocks. The document also covers variable declaration rules, type casting, and provides example programs demonstrating basic Python functionalities.

Uploaded by

pocketwalab
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

3.PYTHONbasicNotes new

Python, developed by Guido Van Rossum in 1991, is a high-level, object-oriented, interpreted programming language known for its ease of use and cross-platform compatibility. It features a variety of data types, dynamic typing, and supports collections like lists, tuples, and dictionaries, along with exception handling through try and except blocks. The document also covers variable declaration rules, type casting, and provides example programs demonstrating basic Python functionalities.

Uploaded by

pocketwalab
Copyright
© © All Rights Reserved
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

PYTHON

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.

#program for input output operator in python

3
enter name:ram
enter age:16
your name is ram
your age is 16

Math module:- This module provides access to the mathematical functions

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.

#program for list functions

5
Tuples:- A tuple a collection which is ordered and unchangeable. In Python tuples are
written within round brackets

Dictionary:- dictionary is a collection which is unordered, changeable and indexed. In


Python dictionaries are written with curly brackets, and they have keys and values.

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

#program to check maximum of 3 numbers using if

x=5
y=6
z=8
largest of 5 6 8
is 8

#program to check whether no is positive or negative using nested if

9
enter x-5
-5 is
negative

#program to check grade obtained by a student using if..else ladder

enter
marks55
Grade C

#program to print sum of all elements in a list using for loop

[3, 6, 8,
9, 2]
sum is 28

#program to calculate average marks in 3 subjects


enter marks
marks60
marks70
marks50
sum= 180
avg= 60.0

#Write a program to print sum of n natural numbers using while loop

10
enter natural
number6
sum is 21

#program to find number that are divisible by 7 and multiple of 5 between 1200
and 2200

[1225, 1260, 1295, 1330, 1365, 1400, 1435,


1470, 1505, 1540, 1575, 1610, 1645, 1680, 1715,
1750, 1785, 1820, 1855, 1890, 1925, 1960, 1995,
2030, 2065, 2100, 2135, 2170]

#program to show check that whether a given number is prime or not

n8
8 is not prime no

n5
5 is prime no

#program to show single dimensional numpy array

11
[1 2 3]

#program to show multi dimensional numpy array


[[1 2]
[3 4]
[5 6]]

#program to show simple line graph using matplotlib

#program to show bar graph using matplotlib

12

You might also like