0% found this document useful (0 votes)
18 views7 pages

Expt 2

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

Expt 2

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

Write python program to illustrate following concepts in python :

1. To print variable
We 1st assign the value ti the variable ,and then we use the print() function to display the
values of variable on the screen.
#taking input from user
a = (int(input("Enter a number:")))
#print variable program
print("THE number is :", a)
Output:- Enter a number:7
THE number is : 7

2. To delete Variable
We create a variable and then use the del statement to delete it.
a = 35
print("Element deleted:", a)
del a
Output:- Element deleted: 35

3. Multiple statement on single line


Semicolons are used to separate numerous statements on a single line.
#Multiple statement on single line
a = 31; b = 1; c = a + b;print(c)
Output:- 32

4. Multiline Statements
You can use a backslash \ at the end of a line to indicate that the statement continues on the
next line.
#4. Multiline Statements
sum = 6 + \
5 + \
2
print("Sum = ",sum)
Output:- Sum = 13

5. Comments
Single-line comments start with the # symbol. Everything after the # on that line is treated as
a comment.
#4. Multiline Statements
6) Multiline Comment
We can use triple-quotes (or) to create a multiline string that is not assigned to any variable,
effectively serving as a multiline comment.
*** this is
A multiline
Command***

7) String Operations
1) Concatenation: You can combine strings using the + operator.

Tejas Garad 211106068 Batch A


#concatination
s1 = 'Hi'
s2 = ' myself'
s3 = ' Tejas'
s4 = s1 + s2 + s3
print(s4)
Output:- Hi myself Tejas
2) String Length: You can find the length of a string using the len() function.
#String length
s2 = 'myself'
Length=len(s2)
print(Length)
Output:- 6
3) String Indexing: You can access individual characters in a string by their index.
#String Indexing
str = 'myself'
print("element at str(2) is :",str[2])
Output:- element at str(2) is : s

4) Slicing: You can extract substrings from a string using slicing.


#string slicing
str = 'Apple'
print("After slicing ",str[3:5])
Output:- After slicing le
5) String Formatting: You can format strings using f-strings or the format() method.
#string formatting
name = 'tejas'
like = 'icecream'
ING = f"{name} like to eat {like}"
print(ING)
Output:- tejas like to eat icecream
6) string comparison: You can check if two strings are matching
#string Comparison
name = 'tejas'
like = 'icecream'
print(name == like)
Output :- False
8) Type Casting
Typecasting (also known as type conversion) refers to the process of changing the data type of a
value from one type to another.
1. integer to string (int to str):
#Typecasting
#int to string
int = 40
str = str(int)
print(str)

Tejas Garad 211106068 Batch A


Output: 40
2. String to integer (str to int):
#Typecasting
#string to int
str = "106"
intu = int(str)
print(intu)
Output: 106
3. Floating Point to integer (float to int):
#float to int
f = 6.99
intu = int(f)
print(intu)
Output: 6
4. String to Float (str to float):
#Typecasting
#String to float
str = "6.99"
f = float(str)
print(f)
Output: 6.99
5. list to Tuple (list to tuple):
#list to tuple
list_l = [1,2,3]
tuple_r = tuple(list_l)
print(tuple_r)
Output:- (1, 2, 3)

6 ) Illustration use of Local and Global variables


Global Variables: Global variables are defined outside of any function and can be accessed from
anywhere within the script or module.
#Global variable
var = 7
def glob():
print(var)
glob()
Output:- 7

Local Variables: Local variables are defined within a function and have local scope which means
they can only be accessed from within that specific function.
#Local variable
def loc():
var = 9
print(var)
loc()

Tejas Garad 211106068 Batch A


Output:- 9
Datatypes:
#DataTypes
a = 7
r = 6.9
str = 'top'
d = [1,2,3]
i = ["t",4]
h = 4 + 3j
tuplr= ("apple",)
print("Datatype1: ",type(a))
print("Datatype2: ",type(r))
print("Datatype3: ",type(str))
print("Datatype4: ",type(d))
print("Datatype5: ",type(i))
print("Datatype6: ",type(h))

Output:-
Datatype1: <class 'int'>
Datatype2: <class 'float'>
Datatype3: <class 'str'>
Datatype4: <class 'list'>
Datatype5: <class 'list'>
Datatype6: <class 'complex'>

1) Use of Keywords in Python


if-else Statement using if, else, and elif:
#Keywords(if,else,elif):
x =4
if x>10:
print("x is greater than 10")
elif x<10:
print("x is greater than 10")
else:
print("x is 10")
Output:- x is greater than 10
Looping with for and range:
#using for with range
for i in range(5):
print(i)
Output:- 0
1
2

Tejas Garad 211106068 Batch A


3
4
Defining function with def:
var = 7
def glob():
print(var)
glob()
Output:- 7
Using import to import a module:
import calendar
yy = 2023
mm = 4
print(calendar.month(yy, mm))
Output:-
April 2023
Mo Tu We Th Fr Sa Su
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30

Working with while loop:


count = 2
while count < 5:
print(count)
count +=1
Output:- 2
3
4
Using return in functions:
def add(a,b):
return a+b
result = add(2,5)
print("Result:",result)
Output:- Result: 7
break statement:
for i in range(5):
print(i)
if i==3: break
Output:- 0
1
2
3
range statement:

Tejas Garad 211106068 Batch A


for i in range(5,8):
print(i, end =" ")
print()
Output:- 5
6
7
continue statement:
for i in range(4):
if i == 3:
continue
print(i)
Output:- 0
1
2
pass statement:
s=['g','r','a','p','e']
for i in s:
if (i == 'a'):
pass
else:
print(i)
Output:- g
r
p
e
append list:
s=['have','a','great']
s.append("day")
print(s)
Output:- ['have', 'a', 'great', 'day']
extend list:
s=['have','a','great']
s.extend("day")
print(s)
Output:- ['have', 'a', 'great', 'd', 'a', 'y']
insert list:
s=['have','a','great']
s.insert(2,'an')
print(s)
Output:- ['have', 'a', 'an', 'great']

Tejas Garad 211106068 Batch A


reverse list:
s=['g','r','a','p','e']
s.reverse()
print(s)
Output;- ['e', 'p', 'a', 'r', 'g']
pop:
s=['g','r','a','p','e']
s.pop(1)
print(s)
Output:- ['g', 'a', 'p', 'e']
count:
s=['g','r','a','a','p','e']
count =s.count('a')
print(count)
Output:- 2
split:
str = 'im happy'
str.split(",")
strnew = str.split("m")
print(strnew)
Output:- ['i', ' happy']

Conclusion:- Basic python commands were studied and implemented successfully.

Tejas Garad 211106068 Batch A

You might also like