0% found this document useful (0 votes)
4 views7 pages

Python

The document provides an overview of various Python programming concepts, including constructors, string indexing, recursion for calculating factorials, Tkinter widgets (Entry and RadioButton), the math module, and string methods. Each concept is explained with examples to illustrate their usage and functionality in Python. This serves as a concise reference for beginners and intermediate programmers to understand key Python features.

Uploaded by

shaikhmoin4173
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Python

The document provides an overview of various Python programming concepts, including constructors, string indexing, recursion for calculating factorials, Tkinter widgets (Entry and RadioButton), the math module, and string methods. Each concept is explained with examples to illustrate their usage and functionality in Python. This serves as a concise reference for beginners and intermediate programmers to understand key Python features.

Uploaded by

shaikhmoin4173
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Q.

Write a short note for constructor in python


Ans. Certainly! Here is a short note on constructors in Python:

In Python, a constructor is a special method that is automatically called when an


instance of a class is created. It is used to initialize the object's state. The
constructor method is typically named __init__. It allows the class to initialize the
attributes of the class.

For example:

class MyClass:

def __init__(self, attribute1, attribute2):

self.attribute1 = attribute1

self.attribute2 = attribute2

# Creating an instance of the class

my_object = MyClass("value1", "value2")

In this example, the __init__ method initializes the attributes attribute1 and attribute2
with the values passed during the object's instantiation. The self keyword is a
reference to the instance of the class and is used to access variables that belong to
the class.

Constructors are vital for setting up the initial state of objects and are used to
perform any necessary setup or initialization. They are an essential concept in
object-oriented programming and help ensure that instances of a class have all the
necessary attributes set to sensible defaults.
Q. Explain the concept of forward and backward indexing using string with
example in python
Ans. In Python, forward and backward indexing refer to the methods of accessing
individual elements of a sequence, such as a string, list, or tuple. Forward indexing
starts from the beginning of the sequence, with the first element having an index of
0, while backward indexing starts from the end of the sequence, with the last
element having an index of -1.

Let's explain this concept using strings in Python:

# Example string

my_string = "Hello, World!"

# Forward indexing

print(my_string[0]) # Output: H

print(my_string[7]) # Output: W

# Backward indexing

print(my_string[-1]) # Output: !

print(my_string[-6]) # Output: W

In the example above, the string "Hello, World!" is used. When accessing the
elements of the string using forward indexing, we start with index 0. So my_string[0]
would return the first character 'H', and my_string[7] would return the character 'W'.

With backward indexing, we start from the end of the string using negative indices.
So my_string[-1] would return the last character '!', and my_string[-6] would return
the sixth character 'W'.

Understanding both forward and backward indexing is crucial when dealing with
sequences in Python, as it provides a convenient way to access elements from both
ends of the sequence.
Q. write a recursive program to find factorial of number in python

def factorial(n):

if n == 0 or n == 1:

return 1

else:

return n * factorial(n - 1)

# Example usage

num = 5

result = factorial(num)

print(f"The factorial of {num} is {result}.")

In this code, the factorial function takes an integer n as an argument and computes
its factorial recursively. The base case is when n is 0 or 1, in which case the factorial
is 1. For any other positive integer n, the function calls itself with n - 1 and multiplies
the result by n. This process continues until the base case is reached.

You can try running this code with different values of num to find the factorial for the
desired number.
Q. write short note on entry and radio button widget
Ans. Certainly! Here's a short note on the Entry and RadioButton widgets in Python's
Tkinter library:

Entry Widget: The Entry widget in Tkinter is used to accept single-line text strings
from the user. It provides a simple box that can be used to get user input. It is
commonly used to create input fields for forms and other data entry purposes. The
Entry widget allows users to type in a single line of text and can be used to collect
information from the user during the execution of a program.

Example:

from tkinter import *

root = Tk()

entry = Entry(root, width=30)

entry.pack()

root.mainloop()

RadioButton Widget: The RadioButton widget in Tkinter is used to


implement multiple-choice selections. It allows users to select exactly one
option from a set of options. Radio buttons are typically used in groups,
and only one radio button in a group can be selected at a time. They are
often used in forms and applications where users need to make exclusive
choices.

Example:

from tkinter import *

root = Tk()

v = IntVar()

v.set(1) # initializing the choice

languages = [("Python", 1), ("Java", 2), ("C++", 3), ("C", 4)]


for txt, val in languages:

Radiobutton(root, text=txt, variable=v, value=val).pack(anchor=W)

root.mainloop()

Both the Entry and RadioButton widgets are commonly used in creating
interactive graphical user interfaces (GUIs) in Python applications using
the Tkinter library. They provide a simple way to get user input and
facilitate user interaction with the program.
Q. explain the various of math module
Ans. The math module in Python provides access to various mathematical functions
and constants. Here are some of the main components of the math module:

1. Constants: The math module provides constants such as pi and e, which


represent the mathematical constants π (pi) and e (the base of the natural
logarithm), respectively.
2. Numeric Functions: The math module offers various numeric functions such
as sqrt for square root, ceil for ceiling, floor for flooring, and fabs for absolute
value. These functions help in performing mathematical operations on
numbers.
3. Trigonometric Functions: The math module includes various trigonometric
functions such as sin, cos, and tan for computing the sine, cosine, and
tangent of an angle, respectively. It also includes their inverse functions like
asin , acos, and atan.
4. Logarithmic Functions: The math module provides logarithmic functions
such as log for computing the natural logarithm, log2 for computing the base-
2 logarithm, and log10 for computing the base-10 logarithm of a number.
5. Angular Conversion: The module provides functions to convert between
radians and degrees, including radians for converting degrees to radians and
degrees for converting radians to degrees.
6. Hyperbolic Functions: The math module supports hyperbolic functions such
as sinh, cosh, and tanh for computing the hyperbolic sine, cosine, and tangent
of a number, respectively.
7. Special Functions: It also includes various special functions like gamma for
the gamma function, erf for the error function, and erfc for the
complementary error function, among others.
8. Constants for Special Values: The math module provides special constants
like inf for positive infinity, -inf for negative infinity, and nan for a floating-
point “not a number” value.
Q. explain any 5 method of string in python with example
Ans. Certainly! Here are five commonly used methods of strings in Python with
examples:

upper() method: This method returns a copy of the string with all the characters
converted to uppercase.
my_string = "hello world"

print(my_string.upper()) # Output: HELLO WORLD

lower() method: This method returns a copy of the string with all the characters
converted to lowercase.

my_string = "Hello World"

print(my_string.lower()) # Output: hello world

strip() method: This method returns a copy of the string with leading and
trailing whitespace removed.

my_string = " Hello World "

print(my_string.strip()) # Output: Hello World

split() method: This method returns a list of words in the string, separated by
the specified delimiter. If no delimiter is provided, it splits the string at
whitespaces.

my_string = "Hello,World,How,Are,You"

print(my_string.split(",")) # Output: ['Hello', 'World', 'How', 'Are', 'You']

replace() method: This method returns a copy of the string with all occurrences
of a substring replaced with another substring.

my_string = "Hello, World!"

new_string = my_string.replace("Hello", "Hi")

print(new_string) # Output: Hi, World!

You might also like