Python Programming for Internal Exam : TYBCS : Semester 5 - BCS Guruji

Ad

Wednesday, October 4, 2023

Python Programming for Internal Exam : TYBCS : Semester 5

Q. What is python language.

Answer: It is high level, interpreted, interactive and object oriented programming language.

Q. What are advantage of python.

Answer: Advanategs of python are:

1) Python syntax is clear and easy to read.

2) It has large standard library

3) It has cross platform compatibility

4) It has large community and support

5) It can be used for wide range of applications

6) It is open source and free


Q. Python is a scripting language. comment.

- Python is commonly used for scripting tasks such as automating repetative task, processing text files  ,etc. python code can be both compiled and interpreted which allows developers to write scripts and also build large and more complex applications.


Q. Python is case sensitive, comment.

- Python distinguishes between uppercase and lowercase letters in identifiers such as variable names, function names, and class names. for example, 'myFunction' and 'myfunction' would be treated as seperate functions when used as function names.


Q. Differentiate between lists and tuples.

Answer: 

CharacteristicListsTuples
MutabilityMutable: Can be modified after creationImmutable: Cannot be modified after creation
SyntaxDefined using square brackets [...]Defined using parentheses (...) or without (...)
PerformanceSlightly slower due to mutabilityFaster and more memory-efficient due to immutability
Use CasesDynamic collections that may changeFixed collections that should not change
Examplemy_list = [1, 2, 3]my_tuple = (1, 2, 3) or my_tuple = 1, 2, 3

Q. What is disconry, give example. 

Answer: A python dictionary is an unordered collection of key value pairs. It allows us to store and retirieve data using keys. They are stored in curly braces {}.

# Creating a dictionary student = { "first_name""John""last_name""Doe""age"25"grade""A" } # Accessing values using keys print("First Name:", student["first_name"]) print("Last Name:", student["last_name"]) print("Age:", student["age"]) print("Grade:", student["grade"])


Q. Short Note on Data Types in Python:

Python supports several built-in data types, which include:


Numeric Types: Integers (int), Floating-point numbers (float), and Complex numbers (complex).

Sequence Types: Lists (list), Tuples (tuple), and Strings (str).

Mapping Type: Dictionary (dict).

Set Types: Sets (set) and Frozen Sets (frozenset).

Boolean Type: Boolean (bool).

Binary Types: Bytes (bytes) and Byte Arrays (bytearray).

None Type: None (NoneType).


Q. Python Program for X^y:


calculate X raised to the power of Y using the ** operator. Here's a simple Python program:


python


x = float(input("Enter the base (X): "))

y = float(input("Enter the exponent (Y): "))

result = x ** y

print(f"{x} raised to the power {y} is {result}")

Python Program for Addition of Digits into a Single Digit:


This program calculates the digital root, which is the iterative process of summing the digits of a number until a single-digit result is obtained:


python


def digital_root(n):

    while n >= 10:

        n = sum(map(int, str(n)))

    return n


number = int(input("Enter a number: "))

result = digital_root(number)

print("The digital root is:", result)


Python Program to Check if a Number is Perfect or Not:


A perfect number is a positive integer that is equal to the sum of its proper divisors (excluding itself). Here's a program to check if a number is perfect:


python


def is_perfect_number(n):

    divisors = []

    for i in range(1, n):

        if n % i == 0:

            divisors.append(i)

    return sum(divisors) == n


number = int(input("Enter a number: "))

if is_perfect_number(number):

    print(f"{number} is a perfect number.")

else:

    print(f"{number} is not a perfect number.")

No comments:

Post a Comment