Running Python in interactive mode:
Without passing python script file to the interpreter, directly execute code to
Python prompt.
Once you’re inside the python interpreter, then you can start.
>>> print("hello world")
hello world
# Relevant output is displayed on subsequent lines without the >>> symbol
>>> x=[0,1,2]
# Quantities stored in memory are not displayed by default.
>>> x
#If a quantity is stored in memory, typing its name will display it.
[0, 1, 2]
>>> 2+3
The chevron at the beginning of the 1st line, i.e., the symbol >>> is a prompt the
python
interpreter uses to indicate that it is ready. If the programmer types 2+6, the
interpreter replies
8.
Running Python in script mode:
contact no : 8102555503
lOMoARcPSD|14860402
Alternatively, programmers can store Python script source code in a file with the
.py extension, and use the interpreter to execute the contents of the file. To
execute the script
by the interpreter, you have to tell the interpreter the name of the file. For
example, if you have
a script name [Link] and you're working on Unix, to run the script you have to
type:
python [Link]
Working with the interactive mode is better when Python programmers deal with
small pieces
of code as you can type and execute them immediately, but when the code is more
than 2-4
lines, using the script for coding can help to modify and use the code in future.
Example:
Data types:
The data stored in memory can be of many types. For example, a student roll
number is stored
as a numeric value and his or her address is stored as alphanumeric characters.
Python has
various standard data types that are used to define the operations possible on
them and the
storage method for each of them.
Int:
Int, or integer, is a whole number, positive or negative, without decimals, of
unlimited
length.
>>> print(24656354687654+2)
24656354687656
>>> print(20)
20
>>> print(0b10)
contact no : 8102555503
lOMoARcPSD|14860402
>>> print(0B10)
>>> print(0X20)
32
>>> 20
20
>>> 0b10
>>> a=10
>>> print(a)
10
# To verify the type of any object in Python, use the type() function:
>>> type(10)
<class 'int'>
>>> a=11
>>> print(type(a))
<class 'int'>
Float:
Float, or "floating point number" is a number, positive or negative, containing one
or more
decimals.
Float can also be scientific numbers with an "e" to indicate the power of 10.
>>> y=2.8
>>> y
2.8
>>> y=2.8
>>> print(type(y))
<class 'float'>
>>> type(.4)
<class 'float'>
>>> 2.
contact no : 8102555503
lOMoARcPSD|14860402
2.0
Example:
x = 35e3
y = 12E4
z = -87.7e100
print(type(x))
print(type(y))
print(type(z))
Output:
<class 'float'>
<class 'float'>
<class 'float'>
Boolean:
Objects of Boolean type may have one of two values, True or False:
>>> type(True)
<class 'bool'>
>>> type(False)
<class 'bool'>
String:
1. Strings in Python are identified as a contiguous set of characters represented in
the
quotation marks. Python allows for either pairs of single or double quotes.
• 'hello' is the same as "hello".
• Strings can be output to screen using the print function. For example:
print("hello").
>>> print("mrcet college")
mrcet college
>>> type("mrcet college")
<class 'str'>
contact no : 8102555503
lOMoARcPSD|14860402
>>> print('mrcet college')
mrcet college
>>> " "
''
If you want to include either type of quote character within the string, the simplest
way is to
delimit the string with the other type. If a string is to contain a single quote, delimit
it with
double quotes and vice versa:
>>> print("mrcet is an autonomous (') college")
mrcet is an autonomous (') college
>>> print('mrcet is an autonomous (") college')
mrcet is an autonomous (") college
Suppressing Special Character:
Specifying a backslash (\) in front of the quote character in a string “escapes” it
and causes
Python to suppress its usual special meaning. It is then interpreted simply as a
literal single
quote character:
>>> print("mrcet is an autonomous (\') college")
mrcet is an autonomous (') college
>>> print('mrcet is an autonomous (\") college')
mrcet is an autonomous (") college
The following is a table of escape sequences which cause Python to suppress the
usual
special interpretation of a character in a string:
>>> print('a\
....b')
a. .. b
>>> print('a\
b\
c')
contact no : 8102555503
lOMoARcPSD|14860402
10
abc
>>> print('a \n b')
>>> print("mrcet \n college")
mrcet
college
Escape
Sequence
Usual Interpretation of
Character(s) After Backslash “Escaped” Interpretation
\' Terminates string with single quote opening delimiter Literal single quote (')
character
\" Terminates string with double quote opening delimiter Literal double quote (")
character
\newline Terminates input line Newline is ignored
\\ Introduces escape sequence Literal backslash (\) character
In Python (and almost all other common computer languages), a tab character can
be
specified by the escape sequence \t:
>>> print("a\tb")
ab
List:
• It is a general purpose most widely used in data structures
• List is a collection which is ordered and changeable and allows duplicate
members.
(Grow and shrink as needed, sequence type, sortable).
• To use a list, you must declare it first. Do this using square brackets and separate
values with commas.
• We can construct / create list in many ways.
Ex:
>>> list1=[1,2,3,'A','B',7,8,[10,11]]
>>> print(list1)
[1, 2, 3, 'A', 'B', 7, 8, [10, 11]]
contact no : 8102555503
lOMoARcPSD|14860402
11
>>> x=list()
>>> x
[]
>>> tuple1=(1,2,3,4)
>>> x=list(tuple1)
>>> x
[1, 2, 3, 4]
Variables:
Variables are nothing but reserved memory locations to store values. This means
that when
you create a variable you reserve some space in memory.
Based on the data type of a variable, the interpreter allocates memory and decides
what can
be stored in the reserved memory. Therefore, by assigning different data types to
variables,
you can store integers, decimals or characters in these variables.
Rules for Python variables:
• A variable name must start with a letter or the underscore character
• A variable name cannot start with a number
• A variable name can only contain alpha-numeric characters and underscores (A-z,
0-9,
and _ )
• Variable names are case-sensitive (age, Age and AGE are three different
variables)
Assigning Values to Variables:
Python variables do not need explicit declaration to reserve memory space. The
declaration
happens automatically when you assign a value to a variable. The equal sign (=) is
used to
assign values to variables.
The operand to the left of the = operator is the name of the variable and the
operand to the
right of the = operator is the value stored in the variable.
contact no : 8102555503
lOMoARcPSD|14860402
12
For example −
a= 100 # An integer assignment
b = 1000.0 # A floating point
c = "John" # A string
print (a)
print (b)
print (c)
This produces the following result −
100
1000.0
John
Multiple Assignment:
Python allows you to assign a single value to several variables simultaneously.
For example :
a=b=c=1
Here, an integer object is created with the value 1, and all three variables are
assigned to the
same memory location. You can also assign multiple objects to multiple variables.
For example −
a,b,c = 1,2,"mrcet“
Here, two integer objects with values 1 and 2 are assigned to variables a and b
respectively,
and one string object with the value "john" is assigned to the variable c.
Output Variables:
The Python print statement is often used to output variables.
Variables do not need to be declared with any particular type and can even change
type after
they have been set.
contact no : 8102555503
lOMoARcPSD|14860402
13
x = 5 # x is of type int
x = "mrcet " # x is now of type str
print(x)
Output: mrcet
To combine both text and a variable, Python uses the “+” character:
Example
x = "awesome"
print("Python is " + x)
Output
Python is awesome
You can also use the + character to add a variable to another variable:
Example
x = "Python is "
y = "awesome"
z=x+y
print(z)
Output:
Python is awesome
Expressions:
An expression is a combination of values, variables, and operators. An expression is
evaluated using assignment operator.
Examples: Y=x + 17
>>> x=10
>>> z=x+20
>>> z
30
contact no : 8102555503
lOMoARcPSD|14860402
14
>>> x=10
>>> y=20
>>> c=x+y
>>> c
30
A value all by itself is a simple expression, and so is a variable.
>>> y=20
>>> y
20
Python also defines expressions only contain identifiers, literals, and operators. So,
Identifiers: Any name that is used to define a class, function, variable module, or
object is an
identifier.
Literals: These are language-independent terms in Python and should exist
independently in
any programming language. In Python, there are the string literals, byte literals,
integer literals,
floating point literals, and imaginary literals.
Operators: In Python you can implement the following operations using the
corresponding
tokens.
contact no : 8102555503
lOMoARcPSD|14860402
15
Operator Token
add +
subtract -
multiply *
Integer Division /
remainder %
Binary left shift <<
Binary right shift >>
and &
or \
Less than <
Greater than >
Less than or equal to <=
Greater than or equal to >=
Check equality ==
Check not equal !=
contact no : 8102555503
lOMoARcPSD|14860402
16
Some of the python expressions are:
Generator expression:
Syntax: ( compute(var) for var in iterable )
>>> x = (i for i in 'abc') #tuple comprehension
>>> x
<generator object <genexpr> at 0x033EEC30>
>>> print(x)
<generator object <genexpr> at 0x033EEC30>
You might expect this to print as ('a', 'b', 'c') but it prints as <generator object
<genexpr>
at 0x02AAD710> The result of a tuple comprehension is not a tuple: it is actually a
generator. The only thing that you need to know now about a generator now is that
you
can iterate over it, but ONLY ONCE.
Conditional expression:
Syntax: true_value if Condition else false_value
>>> x = "1" if True else "2"
>>> x
'1'
Statements:
A statement is an instruction that the Python interpreter can execute. We have
normally two
basic statements, the assignment statement and the print statement. Some other
kinds of
statements that are if statements, while statements, and for statements generally
called as
control flows.
Examples:
An assignment statement creates new variables and gives them values:
>>> x=10
contact no : 8102555503
lOMoARcPSD|14860402
17
>>> college="mrcet"
An print statement is something which is an input from the user, to be printed /
displayed on
to the screen (or ) monitor.
>>> print("mrcet colege")
mrcet college
Precedence of Operators:
Operator precedence affects how an expression is evaluated.
For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has
higher
precedence than +, so it first multiplies 3*2 and then adds into 7.
Example 1:
>>> 3+4*2
11
Multiplication gets evaluated before the addition operation
>>> (10+10)*2
40
Parentheses () overriding the precedence of the arithmetic operators
Example 2:
a = 20
b = 10
c = 15
d=5
e=0
e = (a + b) * c / d #( 30 * 15 ) / 5
print("Value of (a + b) * c / d is ", e)
e = ((a + b) * c) / d # (30 * 15 ) / 5
print("Value of ((a + b) * c) / d is ", e)
e = (a + b) * (c / d); # (30) * (15/5)
contact no : 8102555503
lOMoARcPSD|14860402
18
print("Value of (a + b) * (c / d) is ", e)
e = a + (b * c) / d; # 20 + (150/5)
print("Value of a + (b * c) / d is ", e)
Output:
C:/Users/MRCET/AppData/Local/Programs/Python/Python38-32/pyyy/[Link]
Value of (a + b) * c / d is 90.0
Value of ((a + b) * c) / d is 90.0
Value of (a + b) * (c / d) is 90.0
Value of a + (b * c) / d is 50.0