EXPERIMENT-0 14/12/2023
BASICS OF PYTHON
(21IR009)
Aim:
1) Learn the procedure to Install Python, creating a Project in PyCharm and
add packages.
2) To code examples for Python Programming, NumPy and Matplotlib
Packages.
Software and Libraries required:
1. PyCharm IDE with Python 3.7
2. Libraries used:
a) NumPy
b) Matplotlib
Procedure:
• Launch PyCharm and create a new project.
• If PyCharm is not available, we can download it from
https://www.jetbrains.com/pycharm/.
• We can also install the required packages inside the PyCharm.
• Creating a new page leads us to a new environment where we can start typing
our code.
• Once the code is complete, run the program and we can get the result what
we expected.
• If we get error, rectify it and run again.
Programs:
i. If-else program
Program 1-Finding the grade:
grade = int(input("Enter your grade: "))
print("Your grade is below:")
if grade >= 90:
print("A")
elif grade >= 80:
print("B")
elif grade >= 70:
print("C")
elif grade >= 60:
print("D")
else:
print("F")
Output:
Enter your grade: 78
Your grade is below:
C
Program 2-Check whether a number is odd or even:
num = int(input("Enter a number: "))
if num % 2 == 0:
print(num,"is even.")
else:
print(num,"is odd.")
Output:
Enter a number: 69
69 is odd.
ii. For loop program:
Program 1-Finding squares of numbers:
for i in range(5):
print(f"Square of {i} is {i*i}")
Output:
Square of 0 is 0
Square of 1 is 1
Square of 2 is 4
Square of 3 is 9
Square of 4 is 16
Program 2-Finding the sum of numbers:
numbers = [1, 2, 3, 4, 5]
sum_numbers = 0
for num in numbers:
sum_numbers += num
print("Sum of numbers:", sum_numbers)
Output:
Sum of numbers: 15
iii. While loop program:
Program 1-Count:
count = 0
while count < 5:
print(f"Count: {count}")
count += 1
Output:
Count: 0
Count: 1
Count: 2
Count: 3
Count: 4
Program 2-Countdown:
num = 5
while num > 0:
print(f"Countdown: {num}")
num -= 1
Output:
Countdown: 5
Countdown: 4
Countdown: 3
Countdown: 2
Countdown: 1
iv. NumPy program:
Program 1-Squaring an array:
import numpy as np
arr = np.arange(1, 6)
squared_arr = np.square(arr)
print("Original array:", arr)
print("Squared array:", squared_arr)
Output:
Original array: [1 2 3 4 5]
Squared array: [ 1 4 9 16 25]
Program 2-Forming a random matrix:
import numpy as np
matrix = np.random.rand(3, 3)
print("Random 3x3 Matrix:")
print(matrix)
Output:
Random 3x3 Matrix:
[[0.65802976 0.14206676 0.71206947]
[0.18296566 0.63979205 0.90739531]
[0.17455109 0.49309066 0.05582782]]
Outputs:
v. Matplotlib program:
Program 1-Implementing a Sine function:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
plt.plot(x, y)
plt.title('Sine Function')
plt.xlabel('x')
plt.ylabel('sin(x)')
plt.show()
Program 2-implementing Sine and Cosine function:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-2*np.pi, 2*np.pi, 100)
y1 = np.sin(x)
y2 = np.cos(x)
plt.plot(x, y1, label='sin(x)')
plt.plot(x, y2, label='cos(x)')
plt.title('Sine and Cosine Functions')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.show()
Result:
Thus, the Basics of Python were covered and Coding was done in PyCharm,
and libraries like NumPy and Matplotlib were imported and used in PyCharm.