Programming Using Python : SYBCS : Electronics 1: Embedded System Design : PYQ - BCS Guruji

Ad

Monday, March 13, 2023

Programming Using Python : SYBCS : Electronics 1: Embedded System Design : PYQ

 1 mark

Q. What is the difference between Lists and Tuples?

Answer:

Point List Tuples
Mutability Lists are mutable. Tuples are immutable
Performance Slower than list Faster
Usage Used for dynamic collections of items. Used for static collection of items.
Syntax Lists are defined using square brackets [] Tuples are defined using parenthesis ()
Example my_list = [1, 2, 3, 4] my_tuple = (1, 2, 3, 4)


Q. What is the use of 'time' function?

Answer: The time module in Python provides various time-related functions. It is used to measure time elapsed, delay program execution, measure the performance of code, etc.

2.5 mark

Q. Write a short note on GPIO functions.

Answer: 

GPIO (General Purpose Input/Output) is a powerful feature of Raspberry Pi that allows users to interact with the physical world through programmable interfaces. Raspberry Pi supports a range of GPIO functions that can be used in Python programs to read and write digital signals.
Some of the common GPIO functions in Raspberry Pi are:
GPIO.setmode(mode): This function sets the mode for accessing GPIO pins.
GPIO.setup(channel, GPIO.IN/OUT): This function is used to set up a GPIO pin as input or output.
GPIO.input(channel): This function reads the value of a GPIO pin that is set up as input.
GPIO.output(channel, value): This function sets the value of a GPIO pin that is set up as output.
GPIO.add_event_detect(channel, GPIO.RISING/FALLING/BOTH, callback): This function sets up an event detection on a GPIO pin. The channel parameter specifies the pin number, while the GPIO.RISING, GPIO.FALLING, or GPIO.BOTH parameter specifies the type of event to detect. The callback parameter is a function that will be called when the event is detected.

Q. Write a short note on standard data types used in python.

Answer: Python is a dynamically typed language, which means the data types of variables are automatically determined based on the value assigned to them. Python supports a variety of data types, including:


Numbers: Integers, floating-point numbers, and complex numbers are supported in Python. Mathematical operations can be performed on these data types.

Strings: A sequence of characters enclosed in single or double quotes is called a string. Python allows a wide range of string operations, including concatenation, slicing, and formatting.

Lists: A collection of items that can be of different data types is called a list. Lists are mutable, which means they can be modified after creation.

Tuples: A collection of items similar to a list, but tuples are immutable, which means they cannot be modified once created.

Sets: A collection of unique items is called a set. Set data type is mutable and can be modified after creation.

Dictionaries: A collection of key-value pairs is called a dictionary. Each key-value pair in a dictionary is separated by a colon and enclosed in curly braces. Dictionaries are mutable and can be modified after creation.

Q. Write a short note on 'elif' statement.

Answer: In Python, elif statement is used to test multiple conditions in a program. It is short for "else if".


When an if statement evaluates to False, the program can then check the next elif statement, and if that is also False, it can move on to the next elif statement, and so on. If all the if and elif statements evaluate to False, then the program executes the code in the else block, if it exists.

Here's the basic syntax of elif statement:
        
if condition1:
   # code to execute if condition1 is True
elif condition2:
   # code to execute if condition2 is True
elif condition3:
   # code to execute if condition3 is True
else:
   # code to execute if all conditions are False
        
      



Note that we can have any number of elif statements in between the if and else blocks, depending on how many conditions we need to test.

elif statements are very useful when we want to check multiple conditions in our program and execute different code blocks based on the result of the test.

Q. Write a short note on python dictionary.

Answer: Python dictionary is an unordered collection of key-value pairs that are enclosed in curly braces {}. Each key-value pair is separated by a comma and a colon. Dictionaries are also known as associative arrays or hash tables in other programming languages.


In a dictionary, the keys must be unique, immutable objects such as numbers, strings or tuples. Values can be of any data type, such as numbers, strings, lists, tuples, or even another dictionary. A key and its associated value make up a single item in a dictionary.

Dictionaries are mutable, which means that their values can be modified after they are created. They are also dynamic, which means that new items can be added to or removed from a dictionary at any time.

Q. Explain the following functions of python.

-eval(str)

-GPIO.input (channel)

-GPIO-setup (channel, GPIO.OUT)

Answer: 

1) eval(str) is a built-in function in Python that takes a string as an argument and returns the result of evaluating that string as a Python expression. This means that if you pass a string containing a valid Python expression, such as "2 + 2", to the eval() function, it will return the result of that expression as an integer (in this case, 4).

2) GPIO.input(channel) is a function provided by the RPi.GPIO library in Python that allows you to read the current state of a GPIO pin on a Raspberry Pi. The channel parameter specifies which pin to read, and the function returns either GPIO.HIGH or GPIO.LOW depending on whether the pin is currently high or low.

3) GPIO.setup(channel, GPIO.OUT) is another function provided by the RPi.GPIO library that sets up a GPIO pin on a Raspberry Pi for either input or output. The channel parameter specifies which pin to configure, and the second parameter (GPIO.OUT in this case) specifies whether the pin should be used for output (if it's GPIO.OUT) or input (if it's GPIO.IN). This function must be called before you can use a GPIO pin for input or output.

5 mark

Q. List different types of operators used in Python. Explain any three operators in detail.

Answer: Operators in Python can be classified into the following categories:


Arithmetic Operators, Comparison (Relational) Operators ,Assignment Operators ,Logical Operators, Bitwise Operators, Membership Operators, Identity Operators.

1) Arithmetic Operators:
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, division, modulo, and exponentiation. The arithmetic operators in Python are +, -, *, /, %, and **.

2) Comparison (Relational) Operators:
Comparison operators are used to compare the values on either side of the operator. The comparison operators in Python are ==, !=, >, <, >=, and <=. They return a boolean value either True or False.

3) Assignment Operators:
Assignment operators are used to assign values to variables. The assignment operators in Python are =, +=, -=, *=, /=, %=, and **=. The left operand of the assignment operator is the variable to which the value is assigned, and the right operand is the value to be assigned.

No comments:

Post a Comment