Pyton Notes
Pyton Notes
Snake
AIM: Be able to run a basic programs in Python
independently.
Learning Outcome
1. Know programming mode in Python and how to debug simple
syntax errors.
l=10 If
X=20 for loop
Print(x) print('Hello’) B=20 condition:
while..
20 Hello
RECAP
Class discussion – Syntax errors
Variables are
containers for
storing data
values.
Rules For Writing Variables
•A variable name must start with a letter or the underscore
character
1. $mark
2. Mark1
3. 1name
4. _salary
5. average
6. AVERAGE
7. Average
Write your very first python program and create
some variables.
# find the area of a rectangle
l=5
b=8
area=l*b
print(area)
TYPES OF VARIABLES
There are different types of variables –
• Numeric Variable
eg. x, mark,average,total,hra
Float:
Positive and negative real numbers with a fractional part denoted by the decimal symbol or the
scientific notation using E or e, e.g. 1234.56, 3.142, -1.55, 0.23.
Complex:
A complex number is a number with real and imaginary components. For example, 5 + 6j is a
complex number where 5 is the real component and 6 multiplied by j is an imaginary component.
Examples: 1+2j, 10-5.5J, 5.55+2.33j, 3.11e-6+4j
Type the following commands in Python prompt
and find out the results.
>>>x=5
>>>y=10
>>>x+y
>>>p=x+y
>>>print(p)
What will be the
output of the following Python Statements
m1=20
m2=25 Output
M3=30
Tot=m1+m2+m3 _________
avg=tot/3
Print(tot,avg)
>>>print(4*”hello”) >>>_______
Employing variables in Python
Assigning a value
Type var=8
Type print(var)
Next, assign a new value and display that stored value.
Type var=‘Python is easy’
Type print(var)
What happened?
Can you explain what happened when you assigned a value in Python?
Error Analysis
a=10;
b=20
c=15
sum=a+b+c:
avg=sum/3
print(sum,average)
Python - String
A string object is one of the sequence data types in Python.
Numeric
A numeric value is any representation of data which has a
numeric value. Python identifies three types of numbers:
•Integer: Positive or negative whole numbers (without a
fractional part)
•Float: Any real number with a floating point representation in
which a fractional component is denoted by a decimal symbol
or scientific notation
•Complex number: A number with a real and imaginary
component represented as x+yj. x and y are floats and j is -
1(square root of -1 called an imaginary number)
Operator Description Example
+ Addition a + b will give 30
– Subtraction b will give -10
* Multiplication a * b will give 200
/ Division b / a will give 2
% Modulus b % a will give 0
** Exponent a**b will give 10 to the power 20
// Floor Division 9//2 is equal to 4 and 9.0//2.0 is equal to 4.0
Comparison Operators
The basic comparison operators such as ==, <, >=, and so forth are
used on all manner of values.
Operator Description
< less than
<= less than or equal
== equal
> greater than
>= greater than or equal
!= not equal
<> not equal
Logical Operators
The logical operators and and or also return a Boolean value when
used in a decision structure.
There are three logical operators: and, or, and not.
For example, x > 0 and x < 10 is true only if x is greater than 0 and
less than 10
Operator Description
and logical AND
or logical OR
not logical NOT
input(prompt) to accept input from a user.