0% found this document useful (0 votes)
19 views5 pages

Generalfx

The document contains 7 questions about Python programming concepts like variables, data types, operators, control flow, functions and plotting. The questions cover assigning values, printing results, manipulating variables, conditional logic, iterating over data structures, importing libraries and defining functions.

Uploaded by

stozzie0
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)
19 views5 pages

Generalfx

The document contains 7 questions about Python programming concepts like variables, data types, operators, control flow, functions and plotting. The questions cover assigning values, printing results, manipulating variables, conditional logic, iterating over data structures, importing libraries and defining functions.

Uploaded by

stozzie0
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/ 5

Question 1:

Write a Python program that does the following:

1. Assign the values 5 and 10 to the variables a and b respectively.

2. Print the values of a and b.

3. Reassign the value of a to be a + b and the value of b to be a - b.

4. Print the new values of a and b.

5. Assign the values 20, 30, and 40 to the variables x, y, and z respectively in a
single line.

6. Print the values of x, y, and z.

7. Reassign the variables x, y, and z so that x is x + y + z, y is x - y, and z is z * 2.

8. Print the final values of x, y, and z.

Question 2:

2.1 From Question 1 above add the following:

Using arithmetic operators, calculate and print the following:

• The sum of a, x, and z

• The difference between b and y

• The product of a and y

• The integer division of z by a

• The remainder of z divided by b

• The result of x raised to the power of 2

Question 3:

3.1 Write a Python script that demonstrates the use of the following types of operators:
comparison operators, logical operators, bitwise operators, membership operators, and
identity operators. Include examples of each type of operator and show how they can be
used in various expressions. Make sure to print the results of each operation to
demonstrate their functionality.
Question 4:

Write a Python script that performs the following tasks:

1. Assign the values 7 to variable a, 12 to variable b, 5 to variable c, and 18, 25, 32 to


variables x, y, and z respectively.

2. Use comparison operators to compare these variables and print the results:

• Check if a is less than b.

• Check if b is greater than or equal to c.

• Check if c is equal to 5.

• Check if z is not equal to 32.

• Check if x is less than or equal to y.

3. Use logical operators to combine multiple comparisons and print the results:

• Check if a is less than b and b is greater than c.

• Check if a is less than b or b is less than c.

• Check the negation of a being equal to 7.

4. Use bitwise operators to manipulate a and b, then print the results:

• Perform bitwise AND on a and b.

• Perform bitwise OR on a and b.

• Perform bitwise XOR on a and b.

• Perform bitwise NOT on a.

• Perform left shift on a by 2 positions.

• Perform right shift on b by 2 positions.

5. Use membership operators to check for the presence of elements in a list, then
print the results:

• Create a list with the values [7, 12, 5, 18, 25, 32].

• Check if 7 is in the list.

• Check if 10 is in the list.

• Check if 18 is not in the list.

6. Use identity operators to compare objects and print the results:

• Assign d to be equal to a.
• Check if a is d.

• Check if a is not b.

• Check if x is y.

Question 5:

Write a Python script that performs the following tasks:

1. Create variables of different data types:

• An integer variable age with the value 25.

• A float variable height with the value 5.9.

• A string variable name with the value "Alice".

• A boolean variable is student with the value True.

2. Create a list grades containing the following integer values: [85, 92, 78, 90, 88].

3. Create a tuple dimensions containing the following float values: (1920.0,


1080.0).

4. Create a dictionary student info with the following key-value pairs:

• "name": "Alice"

• "age": 25

• "height": 5.9

• "is student": True

5. Use conditions and if statements to check and print:

• If age is greater than 20, print "Age is greater than 20".

• If height is less than 6.0, print "Height is less than 6.0".

• If name is equal to "Alice", print "Name is Alice".

• If is student is True, print "Is a student".

• If the average of the grades list is greater than 80, print "Average grade is
greater than 80".

6. Use a loop to iterate through the grades list and print each grade.
Question 6:

Write a Python script that performs the following tasks:

1. Create a list of dictionaries where each dictionary represents a student with the
following keys: "name", "age", and "grades". The "grades" key should hold a list
of grades.

2. Use a for loop to iterate over the list of students.

3. For each student, calculate the average grade.

4. Use nested if statements to check the following conditions:

• If the average grade is greater than or equal to 90, print "Excellent".

• Else if the average grade is between 80 and 89, print "Good".

• Else if the average grade is between 70 and 79, print "Average".

• Else, print "Needs Improvement".

5. Use control flow statements (break, continue) within the loop:

• If a student's age is less than 20, skip the student using continue.

• If a student's name is "Charlie", stop processing further students using


break.

Question 7:

Write a Python script that performs the following tasks:

1. Import the necessary libraries:

• Use numpy to generate numerical data.

• Use matplotlib.pyplot for plotting graphs.

2. Create a function generate_data that:

• Takes an integer n as input.

• Generates two arrays of n random numbers each, representing x and y


coordinates using numpy.

• Returns the generated x and y arrays.

3. Create a function plot_data that:

• Takes x and y arrays, and a string title as input.


• Plots the data as a scatter plot using matplotlib.

• Adds the title to the plot.

• Labels the x-axis as "X-axis" and y-axis as "Y-axis".

• Displays the plot.

4. In the main part of the script:

• Use the generate_data function to create data for 100 points.

• Call the plot_data function to plot the generated data with the title
"Scatter Plot of Random Data".

You might also like