Python_01 (8)
Python_01 (8)
It is widely used general purpose,high level programming language.Developed by Guido van Rossum in
1991.
Features:
Python has two basic modes: script and interactive. The normal mode is the mode where the scripted and
finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate
feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the
interpreter, the fed program is evaluated both in part and in whole.
(i) in Interactive mode * Search the python.exe file in the drive in which it is installed. If found double
click it to start python in interactive mode
>>> 5
5
>>> print(5*7)
35
>>> "hello" * 4
'hellohellohellohello'
>>> "hello".__class__
<type 'str'>
(ii) Click start button -> All programs -> python->IDLE(Python GUI)
Escape sequence
Escape Sequence are non printable characters.
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
Operators
Operators can be defined as symbols that are used to perform operations on operands.
Types of Operators
1. Arithmetic Operators.
Arithmetic Operators are used to perform arithmetic operations like addition, multiplication, division
etc.
Operators Description Example
+ perform addition of two number a+b
- perform subtraction of two number a-b
/ perform division of two number a/b
* perform multiplication of two number a*b
% Modulus = returns remainder a%b
// Floor Division = remove digits after the decimal point a//b
** Exponent = perform raise to power a**b
2. Relational Operators.
Relational Operators are used to compare the values.
Operators Description Example
== Equal to, return true if a equals to b a == b
!= Not equal, return true if a is not equals to b a != b
> Greater than, return true if a is greater than b a>b
>= Greater than or equal to a >= b
< Less than, return true if a is less than b a<b
<= Less than or equal to a <= b
Example:
a=30
b=20
if(a==30 and b==20):
print('hello')
Output :- hello
5. Membership Operators
The membership operators in Python are used to validate whether a value is found within a sequence
such as such as strings, lists, or tuples.
Operators Description Example
In return true if value exists in the sequence, else false. a in list
not in return true if value does not exists in the sequence, else false. a not in list
E.g.
a = 22
list = [22,99,27,31]
In_Ans = a in list
Punctuators
Used to implement the grammatical and structure of a Syntax.Following are the python punctuators.
< > , . ; “ [ ] ( ) ' ' " " { }
Creating Variables
Variable is a name given to a memory location. A variable can consider as a container which holds value.
Python is a type infer language that means you don't need to specify the datatype of variable.Python
automatically get variable datatype depending upon the value assigned to the variable.
Unlike other programming languages, Python has no command for declaring a variable.
x = 5
y = "John"
print(x)
print(y)
In above assignment variable i.e., x and y are refer to L-Value because it reside in memory and is
addressable and 5 and “John” refer to as R-Value as they are expression.
Multiple Assignment:
assign a single value to many variables
a = b = c = 1 # single value to multiple variable
a,b = 1,2 # multiple value to multiple variable
a,b = b,a # value of a and b is swaped
Input/Output in python
To combine both text and a variable, Python uses the + character or , chacater:
x = "awesome"
print("Python is" + x)
OR
x = "awesome"
print("Python is" , x)
Note : in first code there is no space between the word is and awesome while in
second case there is a space.
You can also use the + character to add a variable to another variable:
x = "Python is "
y = "awesome"
z = x + y
print(z)
x = 5
y = 10
print(x + y)
Output is: 15
If you try to combine a string and a number, Python will give you an error:
x = 5
y = "John"
print(x + y)
print(‘Computer',‘Science')
print(‘Computer',‘Science',sep=' & ')
print(‘Computer',‘Science',sep=' & ',end='.')
Output :-
Computer Science
Computer & Science
Computer & Science.
Comments
Python has commenting capability for the purpose of in-code documentation.
Comments start with a #, and Python will render the rest of the line as a comment:
#This is a comment.
print("Hello, World!"
Docstrings
Python also has extended documentation capability, called docstrings.
Docstrings can be one line, or multiline.
Python uses triple quotes at the beginning and end of the docstring:
Docstrings are also comments:
"""This is a
multiline docstring."""
print("Hello, World!")
Data Types
x = 1 # int
y = 2.8 # float
z = 1j # complex
print(type(x))
print(type(y))
print(type(z))
Output
<class 'int'>
<class 'float'>
<class 'complex'>
c. Complex numbers.: Complex numbers are combination of a real and imaginary part. Complex
numbers are in the form of X+Yj, where X is a real part and Y is imaginary part.
e.g. a = complex(5) # convert 5 to a real part val and zero imaginary part
print(a)
b=complex(101,23) #convert 101 with real part and 23 as imaginary part
print(b)
Output :-
(5+0j)
(101+23j)
NOTE: REST OF THE DATAYPES WILL BE DISCUSSED IN COMING CHAPTERS
e.g
a = "101" # string
b=int(a) # converts string data type to integer.
c=int(122.4) # converts float data type to integer.
print(b)
print(c)
Output :-
101
122
e.g.
a='301.4' #string
b=float(a) #converts string data type to floating point number.
c=float(121) #converts integer data type to floating point number.
print(b)
print(c)
Output :-
Python has well-defined rules for specifying the order in which the operators in an expression are evaluated
when the expression has several operators. For example, multiplication and division have a higher precedence
than addition and subtraction. Precedence rules can be overridden by explicit parentheses.
Precedence Order
When two operators share an operand, the operator with the higher precedence goes first. For example, since
multiplication has a higher precedence than addition, a + b * c is treated as a + (b * c), and a * b +
c is treated as (a * b) + c.
Associativity
When two operators share an operand and the operators have the same precedence, then the expression is
evaluated according to the associativity of the operators. For example, since the ** operator has right-to-left
associativity, a ** b ** c is treated as a ** (b ** c). On the other hand, since the /operator has left-to-
right associativity, a / b / c is treated as (a / b) / c.
Operators Meaning
() Parentheses
** Exponent
+, - Addition, Subtraction
^ Bitwise XOR
==, !=, >, >=, <, <=, is, is not, in, not in Comparisions, Identity, Membership operators
or Logical OR
Indentation
Python relies on indentation, using whitespace, to define scope in the code. Other programming
languages often use curly-brackets for this purpose.
All statements with the same distance to the right belong to the same block of code, i.e. the statements
within a block line up vertically. The block ends at a line less indented or the end of the file. If a block has to
be more deeply nested, it is simply indented further to the right.
Example
# Initializing a list using the multi-line statement
>>> my_list = [1, \
2, 3\
,4,5 \
Part – 3 (Programs)