python programming
03. PROGRAMMING IN PYTHON
v Python:
- Python is an object oriented
programming language.
àIt
is a very high level language.
àIt
is an advanced version of JAVA programming language.
àIt
is a case sensitive language i.e. code written in uppercase and lowercase both
are treated as different.
àIt
is a platform independent language which means programs
Written on one computer can
run on another computer.
àThe
file extension name of python is “.py”
àIt
is a simplest language than another programming language like: C, C++, JAVA etc.
àIt
is a portable or robust language which means that can support another computer.
It is an interpreted language and also compatible with all operating system.
àThere
are various version of python has been released like python v0.1.0, python
v3.5.1, v2.7.0, python v3.5.1 etc.
v How
to install python?
1) First
of all open web browser and type ‘PYTHON.ORG.’
2) There
are various download links available.
3) Click
on ‘download windows x 86-84’ web based installer.
4) Now,
downloading file double click on installer file.
5) After
that click on RUN button.
6) Click
on ‘install now’ option.
7) And
finally click on CLOSE button.
v To
working with python we have to install “IDLE IDE”
àThe
interactive interpreter of python is called “Python Cell”. It creates, debug,
run, edits python program.
Note: - Python
was developed by Gaido Van Rassum in 1980.
Full form of IDE is “Integrated development environment”.
àThere
are two modes to work in python:-
1) Interactive
mode: - In this mode we type the code
in only one line at command prompt ‘>>>’.
àHere,
python interpreter interpretes the command and gives output when we press
“Enter” button.
E.g:- >>>10+20
30
>>>
To work in
interactive mode: -
1) First
of all open the IDLE.
2) Type
the command at python prompt.
3) After
pressing ‘Enter’ button display the output.
E.g:
- >>>10+20
30
>>>
Note: -Python
shell is a layout screen where we can type command.
To restart
the python shell we have to press “Ctrl +
F6”.
2) Script
mode: - In this mode we type the
command in more than one line; and after that save it with file name in python
file.
àIt
is very simple than interactive mode.
àCode
of script mode can be used in future.
àTo
display output we have to print ( ) function.
àTo
execute this code we have to press F5.
To work in script
mode: -
1) First
of all open python shell.
2) Go
to file option and select ‘new file’ option & click on it.
3) After
that type the code in open window of python shell.
4) After
that save the code in python folder.
5) Go
to ‘run’ menu & select ‘Run module F5’ option & click on it.
E.g: - A=30
B=40
C=A+B
Print (C)
v Character
Set: - It is a combination of
alphabets, alphabets, digits and some special character ($, #, @, %).
v Tokens:
- It is a smallest unit of a
program.
There are five types of
tokens: -
1) Identifiers.
2) Keywords.
3) Literals.
4) Punctuators.
5) Operators.
v Identifiers:
- It is the fundamental unit of a
program which is used to identify the elements of a program.
Rule for creating
identifiers: -
1) Identifier
name always started with either character or underscore (-) symbol.
2) Identifier
name should not start with digits.
3) It
should be not keywords.
E.g: - AB, -AB, A1 etc.
v Keywords:
- It is reserved word which has a
specific meaning or function.
àThere
are 33 keywords in python.
false |
return |
else |
lambda |
yield |
from |
def |
none |
import |
is |
with |
break |
or |
if |
true |
elif |
while |
assert |
for |
continue |
raise |
and |
in |
as |
finally |
not |
global |
|
del |
try |
except |
nonlocal |
class |
pass |
|
v Literals:
- It is a data items that never can
be changes their value during education of program.
àIt
is also known as “constant”.
There are four types of
literals in python: -
1) Integer
literals: - 1, 2, 3, 4 etc.
2) Character
literals: - A, B, C, D etc.
3) Floating
literals: - 1.0, 2.3, 7.4, 8.9 etc.
4) String
literals: - “MICA”, “VKMTG” etc.
v Punctuators:
- It is a symbol which is used in a
programming language. It is used to separate the line or terminated the line
and other word.
E.g: - [ ], ( ), #, @, “” etc.
v Operator:
- It is symbols that comes between
the operands and perform some specific task like: addition, subtraction etc.
àOperand
is a value or variable on which calculation is performed.
àThere
are following categorize of the operator such as: unary, arithmetic, bitwise, relational,
logical, assignment operator etc.
àThere
are 3 kinds of operator Unary, Binary & Ternary operator etc.
1) Unary
operator: - This type of operator performs
operation with one operand.
àThere
are two types of unary operator:
i.
Postfix: - A++, B++, C++, A--, B--, C--, etc.
ii.
Prefix: - ++A, ++B, ++C, --A, --B, --C etc.
2) Binary
operator: - This type of operator performs
operation between two operands.
E.g: - +, -, /, %,
=, *etc.
3) Ternary
operator: - This type of operator
performs operation between two or more operands.
àIt
is used in conditional statement.
E.g: - if (4%7==0)? True:
False.
v Arithmetic
operator: - An operator that perform
arithmetic operation like: addition, subtraction, multiplication, division etc.
is called “arithmetic operator”.
E.g: - +, -, *,
/, %, // (Floor division), **
(Exponent) etc.
v Relational
operator: - An operator that performs
comparing between arithmetical and logical expression is called relational
operator.
àIt
is also known as “comparison operation”.
E.g: - >, <, ==,
===, >=, <= etc.
v Logical
operator: - An operator that performs
logical operation is known as logical operator.
àThere
are three types of logical operator: -
1) Logical
AND (&&).
2) Logical
OR (||).
3) Logical
NOT (!).
v Logical
‘and’ operator: - This operator is
written a ‘AND’. It combines two expressions into one. The resulting expression
evaluates to ‘True’ only if both the conditions satisfies to ‘True’.
Logical and
operator
Expression1 |
Expression2 |
Expression3 |
False |
False |
False |
False |
True |
False |
True |
False |
False |
True |
True |
True |
v Logical
‘or’ operator: - This operator is
written as ‘or’. It combines two expressions into one. The resulting expression
evaluates ‘True’ when either of the conditions satisfies to ‘True’.
Logical ‘or’
operator
Expression1 |
Expression2 |
Expression3 |
False |
False |
False |
False |
True |
True |
True |
False |
True |
True |
True |
True |
v Logical
‘not’ operator: - This operator is
written as ‘not’. It works with single expression or operand, i.e. it is a
unary operator. This operator negates or reverses the truth value of the
expression. In other words, if the expression is ‘True’, then the use of ‘not’
expression makes it ‘False and vice versa.
Expression |
Not
expression |
False |
True |
True |
False |
Logical ‘not’
operator
v Data
type: - It shows the nature of
variables and also defines what kind of data is to be stored in variables.
E.g: - int A here it show
nature of A is “integer type”.
àThere
are five standard of data type in python such as:
i.
Numbers.
ii.
String.
iii.
List.
iv.
Tuple.
v.
Dictionary.
v Number
data type: - This data type
started with numeric value.
àIt
is used for mathematical calculation.
Syntax: - variable name =
value.
àThere
are three types of number data type.
i.
Integer.
ii.
Float.
iii.
Complex.
v Integer:
- It stores integer value.
E.g: - int A=10;
v Float:
- It stores decimal value.
E.g: - Float A=4.7;
v Complex:
- It stores complex number, like IB
where A & B are: integer ‘I’ for imaginary number.
v String:
- This data type store string value
that is word within double or single quotes.
E.g: - String A= “MICA”.
v List:
- It is a compound data type that
contains items in the lists which is separated by commas (,) and enclosed
within square brackets ([ ]).
Eg: - List A= [a, b, c, d];
v Tuple:
- It is similar to list data type
that contains items in the list which is separated by commas (,) and enclosed
within parenthesis ( ).
Syntax: - Tuple name= (elements)
E.g: - Tuple A= (a, b, c, d);
v Dictionary:
- This data type are kinds of key
value pairs where key can be any number or string and values can be any
arbitrary python object.
Key Value
v General
layout of python programs: -
#
Documentation section, Declaration
section, commands and expressions.
1) Documentation
section: - This section consists of a set
comment lines with the name of the program and other details.
àWe
can also write the name of programmer and date.
àIt
is not necessary to include in program.
àIn
this line always proceeded with #
tag.
2) Declaration
section: - In this section value can be
assigned to variables and literals.
àEqual
sign is (=) used to assign the
value.
E.g: - String A= “MICA”;
3) Commands:
- In this section commands are
written to make program and execute it.
àTo
print this statement we should use “print command”.
E.g:
- Print “Hello”;
4) Expressions:
- An expression represents something
which python evaluates and then produces result in the form of values.
E.g: - Sum= a+3;
v Print
command: - This command is used to print
the values.
v Input
command:
- This command/statement is used to getting some values from user after executing
or running.
Syntax: - Variable name=data type (input (“message to be
displayed”));
Comments
Post a Comment
Hello students
If you have any doubt then let me know.