0% found this document useful (0 votes)
124 views18 pages

Iot Lab Manual

Here is a program to print all characters in a string until 'e' or 's' is encountered using a single for loop: text = input("Enter a string: ") for char in text: if char == 'e' or char == 's': break print(char, end='') print() This program takes the input string from the user and iterates over each character in the string using a for loop. Inside the loop, it checks if the current character is 'e' or 's'. If it is one of these characters, it breaks out of the loop. Otherwise, it prints the character. Finally, it prints a new line after coming out of the loop.
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)
124 views18 pages

Iot Lab Manual

Here is a program to print all characters in a string until 'e' or 's' is encountered using a single for loop: text = input("Enter a string: ") for char in text: if char == 'e' or char == 's': break print(char, end='') print() This program takes the input string from the user and iterates over each character in the string using a for loop. Inside the loop, it checks if the current character is 'e' or 's'. If it is one of these characters, it breaks out of the loop. Otherwise, it prints the character. Finally, it prints a new line after coming out of the loop.
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
You are on page 1/ 18

EXPERIMENT-1

OBJECTIVE: - Install given version of Python on the computer system.

Here are the general steps to install Python:

1. Go to the official Python website: https://www.python.org/downloads/

2. Select the latest stable release of Python for your operating system. (e.g., Windows, Mac OS X, or Linux)

3. Click on the download link for your operating system.

4. Open the downloaded file and follow the instructions to install Python.

5. During the installation process, you will be asked to choose an installation directory and whether you want to
add Python to your system path. It is generally a good idea to add Python to your system path, which allows you
to use Python from any directory in the command line.

6. Once the installation is complete, open your command prompt or terminal and type "python" to confirm that
Python is installed correctly.

7. If the Python installation was successful, you will see the Python version number and the Python prompt (>>>)
appear on the screen.

You can also verify the installation by running a simple Python command such as "print('Hello, World!')" in the Python
prompt
EXPERIMENT-2
OBJECTIVE:- Prepare a python program using print() function and run it.

python code
Example 1:

import array
my_array = array.array('i', [1, 2, 3, 4, 5])
my_array[0] = 10
print(my_array) output: array('i', [10, 2, 3, 4, 5])

Example 2:
import array
x=array.array('u',['p','b','c','d'])
x[2]='m'
print(x) output: array('u', 'pbmd'
EXPERIMENT-3
OBJECTIVE:- Access given value from the tuple

Tuple Variables: Tuple variables are similar to list variables, but they are immutable, meaning that their values
cannot be changed once they are created.

You can declare a tuple variable by enclosing the values in parentheses, like this:

my_tuple = (1, 2, "three", 4.0)

python code
EXAMPLE 1:

tup1 = ('car' , 'bus' ,444);


print ( tup1[0]);

OUTPUT:-
Car

EXAMPLE 2:
tup1 = ('car' , 'bus' ,444);
print ( "tup1[0]: ",tup1[0]);
OUTPUT:-

tup1[0]: car
EXPERIMENT-4

OBJECTIVE:-Print the given value of key from the dict.

Dictionary Variables: Dictionary variables are used to store key-value pairs. They are often used to store and access
data in a structured way.

We can declare a dictionary variable by enclosing the key-value pairs in curly braces, like this:

my_dict = {"name": "John", "age": 30, "city": "New York"}

python code

X = {'key1' : 'Gaurav' , 'key2' : 'Madhubani' , 'key3' : '847109'}


print(X['key2'])

output:
Madhubani

Dictionary={'key1' : 'GAUTAM BUDDHA' , 'key2' : 'BODH GAYA' , 'key3' : '563BCE'}


print(Dictionary['key1'])

OUTPUT:-
GAUTAM BUDDHA
EXPERIMENT-5
OBJECTIVE:-Write a Python program to create an array of 5 integers and display the array items. Access individual element
through indexes

Creating an array

To create an array in Python, you first need to import the array module. Then, you can use the array() function to
create an array. Next, you can create an array by specifying the type of data it will contain and initializing it with
some values. Here's an example of how to create an array of integers:

Python code
import array
a = array.array('f', [5, 6, 7, 9, 0, 9, 00])
print(a)

output: array('f', [5.0, 6.0, 7.0, 9.0, 0.0, 9.0, 0.0])

In this example, f is the type code for float. You can use different type codes to create arrays of other data types.

You can access individual elements of the array using indexing:

python code

import array
my_array = array.array('i', [1, 2, 3, 4, 5])
print(my_array[0])

OUTPUT:- 1
EXPERIMENT-6
OBJECTIVE:-Write a Python program which takes two digits m (row) and n (column) as input and generates a two- dimensional
array.

2D ARRAY: A 2D array in python is a two-dimensional data structure stored linearly in the memory. It means that it has two
dimensions, the rows, and the columns, and thus it also represents a matrix.

Syntax of Python 2D Array

We can create a 2D array in python as below:

array_name=[n_rows][n_columns]

Where array_name is the array's name, n_rows is the number of rows in the array, and n_columns is the number of columns in the array.

python code

m=int(input("ENTER THE NO OF ROWS : "))


n=int(input("ENTER THE NO OF COLUMNS : "))
for i in range (1,m+1) :
for j in range (1,n+1) :
print(i*j,end=" ")
print("\n")

OUTPUT:
ENTER THE NO OF ROWS : 3
ENTER THE NO OF COLUMNS : 4
1 2 3 4

2 4 6 8

3 6 9 12
EXPERIMENT-7
OBJECTIVE:- Write a python program to check whether person is eligible for voting or not. (accept age from
the user)

python code
AGE=int(input("Enter your age : "))
if AGE>= 18:
print("You are eligible for voting")
else :
print("you are not eligible for voting")

OUTPUT:-
Enter your age : 17
you are not eligible for voting

Enter your age : 19


You are eligible for voting
EXPERIMENT-8
OBJECTIVE:- Write a python program to check whether the entered number is even or odd.

python code

x = int(input("Enter a number: "))


if x % 2 == 0:
print("The entered number is EVEN")
else:
print("The entered number is ODD")

OUTPUT:-
Enter a number: 20
The entered number is EVEN

Enter a number: 21
The entered number is ODD
EXPERIMENT-9
OBJECTIVE:Write a python program to check whether entered number is divisible by another entered number.

python code

x = int(input("Enter first number: "))


y = int(input("Enter second number: "))
if x % y == 0:
print("First number is divisible by second number")
else:
print("First number is not divisible by second number")

OUTPUT:
Enter first number: 200
Enter second number: 5
First number is divisible by second number

Enter first number: 65


Enter second number: 3
First number is not divisible by second number
EXPERIMENT-10

Write a python program to display “Yes” is entered number is divisible by 5 otherwise display “No”

P = int(input("Enter a number: "))


if P % 5 == 0:
print("YES")
else:
print("NO")

Output:
Enter a number: 50
YES

Enter a number: 77
NO
x = int(input("Enter a number: "))
i = 1

while i <= x:
if i % 2 == 0:
print(i, end=" ")
i = i + 1

Other solution:for ODD

print("====The First 10 Odd Natural Numbers====")

for i in range(1, 11):

print(2 * i - 1)

Q3:- Write a python program which can print first 10 integers and its square using while/for loop.

i=1

while i <= 10:

print(i, i * i)

i += 1

USING FOR LOOP

for i in range (1, 11):

print(i, i * i)

OTHER
# Using for loop

for i in range(1, 11):

square = i * i

print(f"{i} => {square}")

# Using while loop

i=1

while i <= 10:

square = i * i

print(f"{i} => {square}")

i += 1

Q4:

i=1

sum = 0

while i <= 10:

sum += i

i += 1

print(sum)

for i in range(1, 11):

sum += i

print(sum)

EXPLANATION:

This program first initializes a variable i to 1. Then, it uses a while loop to iterate from 1 to 10, and
adds each number to a variable sum. The loop terminates when i is greater than or equal to 10.

The program then uses a for loop to iterate from 1 to 10, and adds each number to a variable sum.
The loop terminates when i is equal to 11.
To run the program, you can save it as a Python file and then run it from the command line. For
example, if you save the program as sum_of_first_10_natural_numbers.py, you can run it by typing
the following command into the command line:

python sum_of_first_10_natural_numbers.py

This will print the sum of the first 10 natural numbers to the console.

Here is the output of the program:

55
55

Q5: Write a python program which can identify the prime number between the range given using while/for loop.

start_range = int(input("Enter the start of the range: "))

end_range = int(input("Enter the end of the range: "))

print("Prime numbers in the given range:")

for num in range(start_range, end_range + 1):

if num < 2:

continue

is_prime = True

for i in range(2, int(num**0.5) + 1):

if num % i == 0:

is_prime = False

break

if is_prime:

print(num)

OUTPUT:
Enter the start of the range: 7
Enter the end of the range: 101
Prime numbers in the given range:
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
101

start_range = int(input("Enter the start of the range: "))


end_range = int(input("Enter the end of the range: "))

print("Prime numbers in the given range:")


num = start_range
while num <= end_range:
if num < 2:
num += 1
continue
is_prime = 1
i = 2
while i * i <= num:
if num % i == 0:
is_prime = 0
break
i += 1
if is_prime:
print(num)
num += 1

Enter the start of the range: 5


Enter the end of the range: 99
Prime numbers in the given range:
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97
Q6: Consider a situation where you want to iterate over a string and want to print all the characters until a letter ‘e’
or ‘s’ is encountered. It is specified that you have to do this using loop
and only one loop is allowed to use.

text = input("Enter a string: ")

for char in text:


if char == 'e' or char == 's':
break
print(char, end='')

print()

Enter a string: rfftutjhhguykgkgjggyuugigigesfuffjhffffffffffffffvj


Rfftutjhhguykgkgjggyuugigig

Q7: Consider the situation when you need to write a program which prints the number from 1 to 10 and but not 6. It is specified
that you have to do this using loop and only one
loop is allowed to use.

for i in range(1, 11):

if i == 6:

continue

else:

print(i)

OUTPUT:
1
2
3
4
5
7
8
9
10

Q8:Create a Class with instance attributes


class Person:

pass

# Creating instances of the Person class with instance attributes

person1 = Person()

person1.name = "Alice"

person1.age = 30

person1.city = "New York"

person2 = Person()

person2.name = "Bob"

person2.age = 25

person2.city = "San Francisco"

# Displaying information for each person

print("Name:", person1.name)

print("Age:", person1.age)

print("City:", person1.city)

print("------------------------")

print("Name:", person2.name)

print("Age:", person2.age)

print("City:", person2.city)

Q9: Create a Vehicle class without any variables and methods

class Vehicle:

pass
In this class, we don't define any instance variables (attributes) or methods. It is just an empty class
definition.

This class can be used as a blueprint for creating instances of vehicles. You can later add attributes
and methods to this class as per your specific requirements. For example, you can add instance
variables like color, make, model, year, etc., and methods like start_engine(), stop_engine(), accelerate(),
brake(), etc., to make it more meaningful and functional as a representation of a vehicle.

Q10: Write a Python function to find the Max of three numbers.

# Taking input from the user for three numbers

num1 = int(input("Enter the first number: "))

num2 = int(input("Enter the second number: "))

num3 = int(input("Enter the third number: "))

# Find the maximum of the three numbers using nested conditional statements

max_number = num1

if num2 > max_number:

max_number = num2

if num3 > max_number:

max_number = num3

# Print the maximum number

print("The maximum of the three numbers is:", max_number)

OUTPUT:
Enter the first number: 45
Enter the second number: 89
Enter the third number: 9999
The maximum of the three numbers is: 9999

input_str = input("Enter a string: ")

reversed_str = ""

for char in input_str:

reversed_str = char + reversed_str

print("Reversed string:", reversed_str)


In this program, we directly take a string as input from the user and then use a for loop to iterate
through each character in the input_str. For each character, we concatenate it in front of the
reversed_str. This way, the characters are added in reverse order, effectively reversing the string.

The program then prints the reversed string as the output. For example, if you enter "hello", the
output will be:

You might also like