Python Lab ALL 10 Prgms
Python Lab ALL 10 Prgms
1. Write a python program to open a file and check what are the access permissions acquired
by that file using os module.
import os
def check_file_permissions(file_path):
if not os.path.exists(file_path):
return
try:
mode = os.stat(file_path).st_mode
permissions = {
print(f"Readable: {permissions['readable']}")
print(f"Writable: {permissions['writable']}")
print(f"Executable: {permissions['executable']}")
except OSError as e:
print(f"Error: {e}")
if __name__ == "__main__":
check_file_permissions(file_path)
n = np.size(x)
m_x = np.mean(x)
m_y = np.mean(y)
plt.xlabel('x')
plt.ylabel('y')
plt.show()
def main():
x = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
y = np.array([1, 3, 2, 5, 7, 8, 8, 9, 10, 12])
b = estimate_coef(x, y)
plot_regression_line(x, y, b)
if __name__ == "__main__":
main()
3. Write a program to double a given number and add two numbers using lambda().
# Define the lambda functions
double = lambda x: x * 2
add = lambda x, y: x + y
result1 = double(num)
4. Write a Python Program to Using a numpy module create an array and check the
following: 1. Type of array 2. Shape of array 3. Type of elements in array.
import numpy as np
# Create an array
# 1. Type of array
array_type = type(array)
# 2. Shape of array
array_shape = array.shape
element_type = array.dtype
my_dict = {
"name": "John",
"age": 30,
# 2. Access items
name = my_dict["name"]
age = my_dict["age"]
# 3. Use get() - The get() method returns the value of the item with the specified key.
# 4. Change values
my_dict["age"] = 31
# 5. Use len()
dict_length = len(my_dict)
file_path = 'your_file.csv'
df = pd.read_csv(file_path)
print(df.head(1))
print("\nLast Line:")
print(df.tail(1))
7. Write a python code to set background color and pic and draw a circle using turtle
module.
import turtle
screen = turtle.Screen()
screen.title("Turtle Drawing")
screen.bgcolor("lightblue")
t = turtle.Turtle()
# Draw a circle
t.penup()
t.goto(0, -100) # Move the turtle to the center of the circle
t.pendown()
t.color("red")
t.begin_fill()
t.circle(100)
t.end_fill()
t.hideturtle()
turtle.done()
8. Write a python program to create a package, sub -package, modules and create
staff and student function to module.
• Create a directory for your package. This will be the root directory of your package.
• Inside the root directory, create a Python package directory. A package directory is a
directory with an _init_.py file. This file can be empty or contain initialization code for your
package.
• Inside the package directory, you can create sub-package directories in the same
way as you created the root package directory, each with their own _init_.py file.
• Inside the sub-packages, you can create Python modules. These are .py files that
contain code and functions.
• Define functions within the modules as needed. In your case, you want to create
staff and student functions.
my_package/
sub_package/
create the Python code for the staff_module.py and student_module.py modules:
staff_module.py:
def staff_function(name):
def student_function(name):
staff_result = staff_function("John")
student_result = student_function("Alice")
print(staff_result)
print(student_result)
Make sure that you have a proper directory structure with _init_.py files where needed to
ensure that Python recognizes your directories as packages and sub-packages.
9. Write a python program to plot a bar graph for the car data set.
import matplotlib.pyplot as plt
import pandas as pd
data = pd.read_csv('car_data.csv')
# Extract data for the plot
plt.xlabel('Car Brand')
plt.tight_layout()
plt.show()
10. Write a python program to create a login web page using pycharm.
Creating a login web page involves using a web framework like Flask and HTML for the front-
end. PyCharm is just an integrated development environment (IDE) that you can use for
coding. Here's a simple example of how to create a basic login web page using Python and
Flask in PyCharm:
First, make sure you have Flask installed. You can install it using pip:
Create a new Python file (e.g., app.py) in your PyCharm project directory and add the
following code:
app = Flask(_name_)
users = {
'username': 'password'
@app.route('/')
def home():
return render_template('login.html')
@app.route('/login', methods=['POST'])
def login():
username = request.form['username']
password = request.form['password']
else:
if _name_ == '_main_':
app.run(debug=True)
Create a "templates" folder in your project directory and within it, create an HTML file
named login.html with the following content:
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h1>Login</h1>
<label for="username">Username:</label>
<label for="password">Password:</label>
<input type="password" name="password" id="password" required><br><br>
</form>
</body>
</html>
Run your Flask application by executing app.py. You should see a basic login web page at
http://127.0.0.1:5000/.
This is a minimal example. In a real-world application, you would use a proper database for
storing user information, handle sessions, and add security measures to protect against
common web vulnerabilities. This code is just a starting point for creating a simple login page
using Flask and PyCharm.