Writing Boolean in Python
Writing Boolean in Python
Python IDLE
1. Intractive mode - this mode allow a user to write the python code line by line
2. Script mode - this mode allow a user to write the code in multiple lines, later the code
the need to be compiled to check for the error(if any) and to see the output.
Character Set - is a set of characters which includes the characters, digits and special
symbols to write the code and instructions in that particular language. characters - a - z and
A to Z
digits - 0 - 9 special symbols - @ , . ? / ' " '" () {} [] $ % + - * / < > ! = _ -
1. identifier - are the building blocks of a program which are used to define the name of
different parts of a program. (variable, function, class, object etc.)
Naming rules
In [ ]: Keyword - are the reserved words in all the programming languages which convey a sp
int chr, str, True, False None while if else for try in not in is
Literal - is a value that can be stored or used in a program to process. Types of Literal
In [ ]: a=10
a=10.25
a=-10
a="@"
a="a"
a="1"
Out[7]: 'ABC'
In [8]: name1
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 name1
In [9]: name1=None
None - is a special literal that can be assigned to a variable as a value when the user doesn't
want to assign a value to a user and to avoid the compiler from throwing an error.
In [ ]: Punctuator - are the some special symbols which convey a special meaning to the com
()
[]
{}
.
:
a={}
a=None
a=True
b=False
In [11]: a1,b1,c1=10,20
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 a1,b1,c1=10,20
In [12]: a1,b1,c1=10,20,None
In [ ]: input() -
1. is used to accpet the value from a user at runtime.
2. it accept all the incoming value(s) in string format only (by default)
Syntax -
var=input("enter a value :")
In [1]: a=10
b=20
c=a+b
print(c)
30
8565
Type casting is to convert the value of one data type into another data type. Types of type
casting
1. implicit
2. explicit.
3. implicit - is a type casting one the value of smaller data type converted into a larger
data type. This type of casting is perfomred by the compiler without the knowledge of
the user.
In [ ]: int - 4 byte
float - 4 byte 10 10.4
10+10.4 =? 20.4 not 20
In [1]: a=10
b=20.4
c=a+b
print(c)
30.4
2. explicit - in this type of type casting a value of larger data type converted into a smaller
data type. in this type of type casting the user may loose the value. This type of casting
is done by the user. The user force/compell the compiler to perform this casting.
these functions need to write before the expression/valuewhich needs to be converted into a
specific data type.
10+10.4 = 20.4
int(10+10.4) = 20
OR
num1=int(input("enter a number"))
In [ ]:
137.73
In [4]: num1=int(input(""))
num2=float(input(""))
sum1=num1+num2
print(sum1)
110.0
In [ ]: print()
1. this function is used to print the output on the screen.
2. this function is used to print any message for a user on the screen.
3. this function is used to print the output along with the message.
In [5]: print(a)
10.0
In [7]: print("************@@@@@@***************")
************@@@@@@***************
In [9]: print(a)
print(b)
print("the message or value")
10.0
20.0
the message or value
Hello Aman
In [10]: print(a,b)
10.0 20.0
10
20
30
In [21]: a,b,c=10,20
print(a)
print(b)
print(c)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[21], line 1
----> 1 a,b,c=10,20
2 print(a)
3 print(b)
In [22]: a,b,c=10,20,None
print(a)
print(b)
print(c)
10
20
None
a=b=c=10
In [24]: a
In [ ]: Arithmetic operators
+ - to add the values
- - to subtract the values
* - to multiply the values
/ - to divide one number from another number return the quotient in decimal
// - to divide one number from another number return the quotient in integer
% - to divide one number from another number and return the remainder.
** - exponent - to raise a number a power of another number.
In [25]: print(10+20)
print(20-40)
print(10*20)
print(10/5)
print(10//5)
print(10%3)
print(10**2)
30
-20
200
2.0
2
1
100
In [ ]: Assignment operator - =
This operator is used to assign a value(left side) to a variable(right side)
a=10
a=a+b*c//d%e
In [26]: a=10
a=a+10 # a+=10
print(a)
20
In [29]: price=125
price+=price*5/100 # price=price+(price*5/100)
price
Out[29]: 131.25
WAP to accpet two numbers from a user and perform all arithmetic operations. WAP to
convert the temp F to C and C to F. WAP to display the area of circle, triangle and squre.
WAP to accpet the sale of sales man for 6 days and display the total sale and average sale of
a salesman.
In [30]: print(10+20)
print("the out put is :",10+20)
30
the out put is : 30
In [ ]: z=a+b*c//d-f**de+(p*10/10)
print("The calculation of expression is :",a+b*c//d-f**de+(p*10/10))
#this is a statement
In [ ]: **, * /, //, %, + -
a**b*4-j+k//6
Conditional expression - is an expression which contains the conditional or logical operators and variable(s), which will be
evaluated and return the result either in True of False.
In [ ]: Conditional operator
<
>
<=
>=
==
!=
In [1]: 10>5
Out[1]: True
In [2]: 10!=10
Out[2]: False
In [3]: 10==10
Out[3]: True
In [4]: 10<=10
Out[4]: True
In [5]: 10>=10
Out[5]: True
In [6]: 20>=10
Out[6]: True
In [7]: 5>=10
Out[7]: False
In [ ]: Logical Operator
Out[11]: True
In [12]: 10==10
Out[12]: True
In [13]: 10=10
In [14]: a=10
In [15]: a==10
Out[15]: True
match-case (10==10)
In [ ]: if - is a conditional statement which will execute the code or statement when a con
syntax -
code
if condition:
statement
statement
statement
code
d=a+b
print("the sum is :",d)
15
*********after if block***********
the sum is : 15
In [ ]: wap to accept the age of a user if age is greater than or rqual to 18 then print el
In [ ]: if-else
if condition:
statement
else:
statement
Looping statement A loop is a process that will execute a set of statement(s) or a code
repeatedly until or unless the condition is true. As the condition become false the lopp will
get terminate. Loop has four fundamentals steps:
While loop - is known as an entry controlled loop. First it checks the condition then execute
the loop body. If the condition is true then it will execute the loop body. But is the condition
is false event at first step the loop will not execute even for once.
print(name)
a=a+1
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
1
2
3
4
5
6
7
8
9
10
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
3+3=6
6+4=10
10+5=15
sum=0
sum=sum+a
In [6]: sum1=0
a=1
while a<=5:
sum1=sum1+a
a=a+1
print(sum1)
15
In [7]: sum1=0
a=1
while a<=5:
sum1=sum1+a
print(sum1)
a=a+1
1
3
6
10
15
In [11]: #wap to find out odd and even numbers between 1 to 50.
a=1
while a<=50:
if a % 2==0:
print(a, end=",")
a=a+1
2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40,42,44,46,48,50,
In [12]: a=1
while a<=50:
if a % 2!=0:
print(a, end=",")
a=a+1
1,3,5,7,9,11,13,15,17,19,21,23,25,27,29,31,33,35,37,39,41,43,45,47,49,
In [ ]: 85+
25+85=110
45+110=155
74+155=229
10+229=239
0
229
In [2]: sum1=0
num=int(input("enter a number : "))
while num>0:
sum1=sum1+num
print(sum1)
num=int(input("enter a number : "))
5
15
30
115
178
219
244
329
In [5]: len(1234)
#len("abc")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 len(1234)
In [10]: 8521//10
Out[10]: 852
In [11]: 8521%10
Out[11]: 1
In [12]: 852%10
Out[12]: 2
In [13]: 852//10
Out[13]: 85
digit=num%10
rev=rev*10+digit
num=num//10
print(rev)
1
12
125
1258
In [1]: fact=1
a=1
n=int(input("enter n number :"))
while a<=n:
fact=fact*a
print(fact)
a=a+1
print("Factorial of :",n," is :",fact)
1
2
6
24
120
Factorial of : 5 is : 120
In [ ]: range(start,stop,step) -
it is a function that will return the range of values between start and stop value.
Every time it will increase/decrease the value as per the value of step.
By default the start value is 0(if not given by the user.
Stop value will not be included in the range (stop=stop-1)
by defalut the value of STEP is 1.
range(1,10,1)
The above function will return the values between 1 and 9.
range(10)
the aove function will return the values between 0 and 9.
range(5,10)
range(5,51,5)
1
2
3
4
5
6
7
8
9
0
1
2
3
4
5
6
7
8
9
5
10
15
20
25
30
35
40
45
50
In [7]: a=1
while a<=10:
print(a)
a=a+1
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
for loop it is a looping statement that will execute a code based on a given condition till the condition is true. As the condition will
get false the loop will get terminate. for iterator in range(start,stop,step): statement for iterator in[v1,v2,v3,v4,v5....vn]: statement
#a=1
# a<11
#a=a+1 step value
1
2
3
4
5
6
7
8
9
10
In [3]: a=1
while a<11:
print(a)
a=a+1
1
2
3
4
5
6
7
8
9
10
101
102
103
104
105
106
107
108
109
110
In [8]: a=10
print(a)
print(a+10)
a=5
print(a)
10
20
5
10
20
30
40
50
60
70
80
100
15
25
35
45
55
65
75
85
105
In [12]: a=1
while a<=10:
if a==5:
break
print(a)
a=a+1
1
2
3
4
In [13]: a=1
while a<=10:
if a==5:
break
print(a)
a=a+2
1
3
In [14]: a=1
while a<=10:
if a==5:
break
print(a)
a=a+1.5
1
2.5
4.0
5.5
7.0
8.5
10.0
In [ ]: a=1
while a<=10:
if a==5:
continue
print(a)
a=a+1
1
2
3
4
1
2
3
4
6
7
8
9
10
1
2
3
4
6
7
8
9
10
The loop terminated as the condition is false.
In [ ]: if condition:
statement
else:
statement
Under 18.
Grade is : A2
In [7]: topcost=0
top1=top2=top3=""
top1=input("Onion Yes/No : ")
top2=input("Capsicum Yes/No : ")
In [ ]: WAP to accpet the ATM pin from a user. the pin entreed by a user should be a valid
If a user enters a wrong pin your should ask for a correct pin again.
A user can attemp only 3 chances to enter the correct pin. If user exceeeds the lim