0% found this document useful (0 votes)
6 views4 pages

Python and C Questions Answers

The document provides various programming examples in Python and C, including creating and accessing arrays with NumPy, building a simple calculator, defining user functions, and demonstrating call by value versus call by reference. It also covers string functions in C, argument and return value types in functions, and performance comparisons between NumPy arrays and Python lists. Additionally, it includes error handling techniques using try, except, and finally in Python.

Uploaded by

mukundhan0801
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)
6 views4 pages

Python and C Questions Answers

The document provides various programming examples in Python and C, including creating and accessing arrays with NumPy, building a simple calculator, defining user functions, and demonstrating call by value versus call by reference. It also covers string functions in C, argument and return value types in functions, and performance comparisons between NumPy arrays and Python lists. Additionally, it includes error handling techniques using try, except, and finally in Python.

Uploaded by

mukundhan0801
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/ 4

1.

Creating Arrays using NumPy and Accessing Elements

import numpy as np

arr = np.array([10, 20, 30, 40, 50])

# Indexing
print(arr[0]) # Output: 10

# Slicing
print(arr[1:4]) # Output: [20 30 40]

# Array Attributes
print(arr.shape) # Output: (5,)
print(arr.ndim) # Output: 1
print(arr.size) # Output: 5

2. Simple Calculator in Python

num1 = float(input("Enter first number: "))


num2 = float(input("Enter second number: "))
op = input("Enter operation (+, -, *, /): ")

if op == '+':
print("Result:", num1 + num2)
elif op == '-':
print("Result:", num1 - num2)
elif op == '*':
print("Result:", num1 * num2)
elif op == '/':
print("Result:", num1 / num2)
else:
print("Invalid operation")

3. User-Defined Function (Addition Example)

def add(a, b):


return a + b

result = add(5, 3)
print("Sum is:", result)
4. Call by Value vs Call by Reference

# Python: Call by object reference


def modify_list(lst):
lst[0] = 100

my_list = [1, 2, 3]
modify_list(my_list)
print(my_list) # Output: [100, 2, 3]

5. Passing Arguments in C Function

#include <stdio.h>

void display(int num) {


printf("Number is: %d", num);
}

int main() {
display(5);
return 0;
}

6. String Functions in C

#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "hello";
char str2[20];

printf("Length: %lu\n", strlen(str1));


// strrev is compiler-dependent
strcpy(str2, str1);
printf("Copy: %s\n", str2);
printf("Compare: %d\n", strcmp(str1, str2));
return 0;
}

7. Function Argument and Return Value Types


With argument, no return:
def greet(name):
print("Hi", name)

No argument, with return:


def get_number():
return 5

8. Slicing in NumPy

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

print(arr[1:4]) # [2 3 4]
print(arr[:3]) # [1 2 3]
print(arr[::2]) # [1 3 5]

9. Speed of Operation: NumPy vs List

import numpy as np
import time

a = np.arange(1000000)
start = time.time()
a = a * 2
print("NumPy time:", time.time() - start)

lst = list(range(1000000))
start = time.time()
lst = [x * 2 for x in lst]
print("List time:", time.time() - start)

10. try and except in Python

try:
x = int(input("Enter a number: "))
print("Result:", 10 / x)
except ZeroDivisionError:
print("Can't divide by zero!")
except ValueError:
print("Invalid input!")
11. finally Keyword in Python

try:
x = 10 / 0
except ZeroDivisionError:
print("Error occurred!")
finally:
print("This block always runs.")

You might also like