The document outlines various Python programming tasks, including functions for decision structures, eligibility checking, string comparison, temperature categorization, numeric range checking, and operations using NumPy arrays. Each task includes a question, sample input, and expected output. The tasks cover fundamental programming concepts such as conditionals, logical operators, and array manipulations.
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 ratings0% found this document useful (0 votes)
5 views
FAI_3(Functions, loops, numpy)-1-1
The document outlines various Python programming tasks, including functions for decision structures, eligibility checking, string comparison, temperature categorization, numeric range checking, and operations using NumPy arrays. Each task includes a question, sample input, and expected output. The tasks cover fundamental programming concepts such as conditionals, logical operators, and array manipulations.
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
1.
Decision Structures: Check if a Number is Positive, Negative, or Zero
Question: Write a Python function that takes a number as input and determines whether it is positive, negative, or zero using an if-elif-else statement.
2. Boolean Logic in Eligibility Checking (Voting Eligibility)
Question: Create a function that accepts a person's age as input and returns whether they are eligible to vote (age 18 or above) using an if-else statement.
Sample Input and Output:
Input: 20 Output: "Eligible to vote" Input: 16 Output: "Not eligible to vote" Input: 18 Output: "Eligible to vote"
3. String Comparison Using Relational Operators
Question: Write a function that takes two strings as input and compares them using relational operators (>, <, ==). The function should return which string is greater, smaller, or if they are equal.
Sample Input and Output:
Input: "apple", "banana" Output: "apple is smaller than banana" Input: "zebra", "ant" Output: "zebra is greater than ant" Input: "hello", "hello" Output: "Both strings are equal"
4. Temperature Categorization Using Nested Conditions
Question: Implement a function that categorizes a given temperature into four categories: • Freezing (below 0°C) • Cold (0°C to 19°C) • Warm (20°C to 29°C) • Hot (30°C and above) Sample Input and Output: Input: -5 Output: "Freezing" Input: 10 Output: "Cold" Input: 25 Output: "Warm" Input: 35 Output: "Hot"
5. Checking Numeric Ranges Using Logical Operators
Question: Write a function that checks whether a given number falls within a specified range [lower, upper]. If the number is inside the range, return "within range"; otherwise, return "outside range".
Sample Input and Output:
Input: 15, range (10, 20) Output: "15 is within the range [10, 20]" Input: 25, range (10, 20) Output: "25 is outside the range [10, 20]" Input: 10, range (10, 20) Output: "10 is within the range [10, 20]" 1. Decision Structures: Check if a Number is Positive, Negative, or Zero Question: Write a Python function that takes a number as input and determines whether it is positive, negative, or zero using an if-elif-else statement.
2. Boolean Logic in Eligibility Checking (Voting Eligibility)
Question: Create a function that accepts a person's age as input and returns whether they are eligible to vote (age 18 or above) using an if-else statement.
Sample Input and Output:
Input: 20 Output: "Eligible to vote" Input: 16 Output: "Not eligible to vote" Input: 18 Output: "Eligible to vote"
3. String Comparison Using Relational Operators
Question: Write a function that takes two strings as input and compares them using relational operators (>, <, ==). The function should return which string is greater, smaller, or if they are equal. Sample Input and Output: Input: "apple", "banana" Output: "apple is smaller than banana" Input: "zebra", "ant" Output: "zebra is greater than ant" Input: "hello", "hello" Output: "Both strings are equal"
4. Temperature Categorization Using Nested Conditions
Question: Implement a function that categorizes a given temperature into four categories: • Freezing (below 0°C) • Cold (0°C to 19°C) • Warm (20°C to 29°C) • Hot (30°C and above) Sample Input and Output: Input: -5 Output: "Freezing" Input: 10 Output: "Cold" Input: 25 Output: "Warm" Input: 35 Output: "Hot" 5. Checking Numeric Ranges Using Logical Operators Question: Write a function that checks whether a given number falls within a specified range [lower, upper]. If the number is inside the range, return "within range"; otherwise, return "outside range".
Sample Input and Output:
Input: 15, range (10, 20) Output: "15 is within the range [10, 20]" Input: 25, range (10, 20) Output: "25 is outside the range [10, 20]" Input: 10, range (10, 20) Output: "10 is within the range [10, 20]"
6. Create a NumPy Array and Find its Shape
Question: Write a Python function that takes a list of numbers and converts it into a NumPy array, then returns its shape. Sample Input and Output: Input: [1, 2, 3, 4, 5] Output: (5,) Input: [[1, 2], [3, 4], [5, 6]] Output: (3, 2)
7. Perform Element-wise Operations on Two NumPy Arrays
Question: Write a function that takes two NumPy arrays of the same shape and returns their sum, difference, product, and quotient element-wise. Sample Input and Output: Input: [1, 2, 3], [4, 5, 6] Output: Sum: [5, 7, 9], Difference: [-3, -3, -3], Product: [4, 10, 18], Quotient: [0.25, 0.4, 0.5]
8. Generate an Array of Random Numbers Using NumPy
Question: Write a function that generates a NumPy array of random floating-point numbers between 0 and 1, given the number of elements as input. Sample Input and Output: Input: 5 Output: [0.23, 0.67, 0.89, 0.45, 0.12] 9. Find the Mean and Standard Deviation of a NumPy Array Question: Write a function that computes the mean and standard deviation of a given NumPy array. Sample Input and Output: Input: [10, 20, 30, 40, 50] Output: Mean: 30.0, Standard Deviation: 15.81
10. Find the Maximum and Minimum Elements in a NumPy Array
Question: Write a function that finds the maximum and minimum values in a NumPy array. Sample Input and Output: Input: [3, 7, 2, 9, 5] Output: Max: 9, Min: 2