0% found this document useful (0 votes)
29 views21 pages

Writing Boolean in Python

The document provides an overview of Python programming basics, including the Python IDLE modes, character sets, tokens, and data types. It explains identifiers, keywords, literals, operators, and the syntax for variable assignment and input/output operations. Additionally, it covers arithmetic operations, conditional statements, and type casting in Python.

Uploaded by

anantaadyant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views21 pages

Writing Boolean in Python

The document provides an overview of Python programming basics, including the Python IDLE modes, character sets, tokens, and data types. It explains identifiers, keywords, literals, operators, and the syntax for variable assignment and input/output operations. Additionally, it covers arithmetic operations, conditional statements, and type casting in Python.

Uploaded by

anantaadyant
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

10/30/25, 2:59 PM Basic of 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.

In [1]: Fundamentales of Python Programmig Language


Featurs of Python, Advantages and Disadvantages of Python

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 - @ , . ? / ' " '" () {} [] $ % + - * / < > ! = _ -

In [ ]: Toaken - is the smallest individual unit of a program.


5 types of tokens
1. identifier
2. keyword
3. literal
4. operator
5. punctuator

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

1. it should be unique and meaningful.


2. It should not be a keyword.
3. It should not contain any space and any other special character. abc@123, abc 123
4. It must start with an albhabet or underscore but not with number. _abc, abc_123
5. It can be alphanumeric but not numericalpha.

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

1. integer 2. float 3. String 4. Boolean 5. None


2. integer - whole numeric value + or - 85, 96, -1254, 7466545
3. float - decial value + or - 85.25 9635.2455,
4. String - is used to store a single character(s), digit(s) or special character(s) either in
single or double quotes. 'a' '1', '@', ',' "Name" "855157451" "01/01/2012"
"abc@[Link]"
5. boolean - it accept the value either in True or False.
6. None - is a special literal. It means no value.

[Link] of [Link] 1/21


10/30/25, 2:59 PM Basic of Python

In [ ]: a=10
a=10.25
a=-10
a="@"
a="a"
a="1"

In [7]: name="ABC" # string value


name

Out[7]: 'ABC'

In [8]: name1

---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Cell In[8], line 1
----> 1 name1

NameError: name 'name1' is not defined

In [ ]: name1=0 or 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 [ ]: Operator - is used to perform the operations on data.


Arithmetic Operator (+ - * / // % **)
Assignment Operator ( = )
Augmented Assignment Operator (+=, -=, *=, /=, //=, %=, **=)
Conditional Operator ( < > <= >= == !=)
Logical Operator (and or not)
Membership operator (is, is not)
in operator

In [ ]: Punctuator - are the some special symbols which convey a special meaning to the com
()
[]
{}
.
:

In [ ]: variable - is a mermory location to store the value.


synatx - varname=value
a=10
a=10.5
a="a"
a="abc"
a=[]
a=()

[Link] of [Link] 2/21


10/30/25, 2:59 PM Basic of Python

a={}
a=None
a=True
b=False

In [10]: #how to assign the multiple values to multiple variables.


a=10
b=20
c=30
a,b,c=10,20,30

In [11]: a1,b1,c1=10,20

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[11], line 1
----> 1 a1,b1,c1=10,20

ValueError: not enough values to unpack (expected 3, got 2)

In [12]: a1,b1,c1=10,20,None

In [13]: #how to assign same value to multiple variables


a2=b2=c2=10

In [ ]: input and output


Type casting

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

In [2]: a=input("enter a number : ")


b=input("enter a number :")
c=a+b
print(c)

8565

In [ ]: "85" "65" +(concatenation) - "8565"

Type casting is to convert the value of one data type into another data type. Types of type
casting

1. implicit

[Link] of [Link] 3/21


10/30/25, 2:59 PM Basic of Python

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.

Functions for explicit type casting

1. int() 2 float() 3 str()

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

float(10+10) = 20.0 str(10+10) - "20"

In [2]: num1=input("enter a number :")


num1=int(num1)

OR
num1=int(input("enter a number"))

Cell In[2], line 5


num1=int(input(
^
SyntaxError: incomplete input

In [ ]:

In [3]: a=float(input("enter a number for no use"))


b=float(input("enter a number :"))
c=a+b
print(c)

[Link] of [Link] 4/21


10/30/25, 2:59 PM Basic of Python

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 [6]: print("The value entered by the user is .")

The value entered by the user is .

In [7]: print("************@@@@@@***************")

************@@@@@@***************

In [8]: print("The value entered by the user is :",a)

The value entered by the user is : 10.0

In [9]: print(a)
print(b)
print("the message or value")

10.0
20.0
the message or value

In [6]: name=input("enter your name :")


print("Hello",name)

Hello Aman

In [7]: name=input("enter your name :")


print("Hello",name," welcome to python programming")

Hello Aman welcome to python programming

In [10]: print(a,b)

10.0 20.0

In [20]: #multiple variables with multiple values


a,b,c=10,20,30
print(a)
print(b)
print(c)

[Link] of [Link] 5/21


10/30/25, 2:59 PM Basic of Python

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)

ValueError: not enough values to unpack (expected 3, got 2)

In [22]: a,b,c=10,20,None
print(a)
print(b)
print(c)

10
20
None

In [ ]: #same value to multiple variables


a=10
b=10
c=10
a,b,c=10,10,10

a=b=c=10

In [23]: #multiple values to a single variable


a=10,20,30,40,50,60,70,80

In [24]: a

Out[24]: (10, 20, 30, 40, 50, 60, 70, 80)

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)

[Link] of [Link] 6/21


10/30/25, 2:59 PM Basic of Python

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 Statement- Conditional statement is a statement which executes based on a


condition. if the condition is true then a particular block of code will execute as a result and
if the condition is false then another set of code or statement(s) will execute as a result.

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.

[Link] of [Link] 7/21


10/30/25, 2:59 PM Basic of Python

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

AND Cond1 Cond2 Result


True True True
False True False
True False False
False False False
OR Cond1 Cond2 Result
True True True
False True True
True False True
False False False

NOT True True False


False False True

[Link] of [Link] 8/21


10/30/25, 2:59 PM Basic of Python

In [ ]: name is Ajay and city is Delhi


name=="Ajay" and city=="Delhi"

In [11]: name=input("enter a name : ")


city=input("enter the delhi:")
rollno=int(input("enter the roll number is :"))
rollno==5 and name=="Ajay" or city=="Delhi"

Out[11]: True

In [12]: 10==10

Out[12]: True

In [13]: 10=10

Cell In[13], line 1


10=10
^
SyntaxError: cannot assign to literal here. Maybe you meant '==' instead of '='?

In [14]: a=10

In [15]: a==10

Out[15]: True

In [ ]: marks between 91 and 100.


marks>=91 and marks<=100

In [ ]: Types of conditoinal statement in python


if
if-else
if-elif-else

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

In [17]: a=int(input("enter a number :"))


b=int(input("enter a number:"))
if a>0 and b>0:
c=a+b
print(c)
print("*********after if block***********")

[Link] of [Link] 9/21


10/30/25, 2:59 PM Basic of Python

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

In [ ]: if-elif-else (multiple conditions on a single variable)


if condition:
statement
elif condition:
statement
elif condition:
statement
elif 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:

1. decalre and initialize the loop variable.


2. test the condition
3. execution of loop body
4. updation of loop vraible(by defualt the updation will happen by 1 step.)

Types of loop in python

1. while loop 2 for loop.

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.

Syntax var=value while codnition: statement updation

In [1]: #wap to print your name 10 times.


name=input("enter your name : ")
a=1
while a<=10:

[Link] of [Link] 10/21


10/30/25, 2:59 PM Basic of Python

print(name)
a=a+1

Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann
Aryemann

In [2]: #wap TO PRINT the counting from 1 to 10.


a=1
while a<=10:
print(a)
a=a+1

1
2
3
4
5
6
7
8
9
10

In [ ]: #WAP to print the table of any given number.


num x 1 = value
num x 2 = value

In [4]: num=int(input("enter a number :"))


a=1
while a<=10:
tab=num*a
print(num," x ",a," = ",tab)
a+=1

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

In [ ]: #wap to do sum of first 5 natural numbers. 5 sum is 15


1+2+3+4+5=15
0+1=1
1+2=3

[Link] of [Link] 11/21


10/30/25, 2:59 PM Basic of Python

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 [ ]: sum1 a a<=5 sum1=sum1+a print(sum1) a=a+1


0 1 True 0 + 1 = 1 1 2
1 2 True 1 + 2 = 3 3 3
3 3 True 3 + 3 = 6 6 4
6 4 True 6 + 4 = 10 10 5
10 5 True 10 + 5 =15 15 6
15 6 False

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,

wap to find out the sum of odd number


and even numbers.
[Link] of [Link] 12/21
10/30/25, 2:59 PM Basic of Python

WAP to accpet the n numbers from a user


and do the sum of those numbers until a
user enter a number greater than 0.

WAP to accept a number from a user and


print the number in reverse order.
1234 4321

WAP to accept a number and check the


length of the number whether the number
is of 2 digits, 3 digits or more.
45236 The given number is of 5 digits.

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")

[Link] of [Link] 13/21


10/30/25, 2:59 PM Basic of Python

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[5], line 1
----> 1 len(1234)

TypeError: object of type 'int' has no len()

In [7]: number=int(input("enter a number :"))


number=str(number)
a=len(number)
if a==1:
print("The given number is of :",a," digit.")
else:
print("The given number is of :",a," digits.")

The given number is of : 8 digits.

In [9]: number=input("enter a number :")


#number=str(number)
a=len(number)
if a==1:
print("The given number is of :",a," digit.")
else:
print("The given number is of :",a," digits.")

The given number is of : 11 digits.

In [ ]: Actual Number Last Digit Remaining Number Rev Number


8521
8521 1 852 1
852 2 85 12 rev*10+lastdigit=last
85 5 8 125
8 8 0 1258
//
%

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

In [15]: num=int(input("enter a number :"))


rev=0
while num>0:

[Link] of [Link] 14/21


10/30/25, 2:59 PM Basic of Python

digit=num%10
rev=rev*10+digit
num=num//10
print(rev)

1
12
125
1258

In [ ]: #waP to print the factorial of a given number.


5
1*2*3*4*5=120
5*4*3*2*1=120

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)

In [2]: for i in range(1,10,1):


print(i)

[Link] of [Link] 15/21


10/30/25, 2:59 PM Basic of Python

1
2
3
4
5
6
7
8
9

In [3]: for i in range(10):


print(i)

0
1
2
3
4
5
6
7
8
9

In [4]: for i in range(5,51,5):


print(i)

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

In [8]: for i in range(1,11,1):


print(i)

[Link] of [Link] 16/21


10/30/25, 2:59 PM Basic of Python

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

In [1]: for a in range(1,11,1):


print(a)

#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

In [9]: for a1 in range(1,11,1):


a1=a1+100
print(a1)

[Link] of [Link] 17/21


10/30/25, 2:59 PM Basic of Python

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

In [10]: for i in [10,20,30,40,50,60,70,80,100]:


print(i)

10
20
30
40
50
60
70
80
100

In [11]: for i in [10,20,30,40,50,60,70,80,100]:


print(i+5)

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

[Link] of [Link] 18/21


10/30/25, 2:59 PM Basic of Python

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

In [1]: for i in range(1,11,1):


if i==5:
continue
print(i)

1
2
3
4
6
7
8
9
10

[Link] of [Link] 19/21


10/30/25, 2:59 PM Basic of Python

In [2]: for i in range(1,10,1):


pass

In [3]: for i in range(1,11,1):


if i==5:
continue
print(i)
else:
print("The loop terminated as the condition is false.")

1
2
3
4
6
7
8
9
10
The loop terminated as the condition is false.

In [ ]: if condition:
statement
else:
statement

In [2]: age=int(input("enter your age:"))


if age>=18:
print("eligible for voting")

In [3]: age=int(input("enter your age:"))


if age>=18:
print("eligible for voting")
else:
print("Under 18.")

Under 18.

In [6]: avg=float(input("enter the avg marks :"))


if avg>=91 and avg<=100:
grade="A"
elif avg>=81 and avg<=90.99:
grade="A2"
elif avg>=71 and avg<=80.99:
grade="b1"
else:
grade="B2"
print("Grade is :",grade)

Grade is : A2

In [7]: topcost=0
top1=top2=top3=""
top1=input("Onion Yes/No : ")
top2=input("Capsicum Yes/No : ")

[Link] of [Link] 20/21


10/30/25, 2:59 PM Basic of Python

top3=input("Cheese Yes/No : ")


if top1=="Yes" or top1=="yes":
topcost=topcost+40
if top2=="Yes" or top2=="yes":
topcost=topcost+30
if top3=="Yes" or top3=="yes":
topcost=topcost+50
print("Total topping cost is :")
print("pizza price is :",200+topcost)

Total topping cost is :


pizza price is : 290

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

[Link] of [Link] 21/21

You might also like