0% found this document useful (0 votes)
22 views10 pages

Vanisha Y Gowda

The document discusses Python programming concepts like variables, data types, operators, loops, functions and provides examples. It covers topics like what is Python, its advantages, variables and data types, identifiers, slicing, operators, writing functions to find product, sum, check conditions.
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)
22 views10 pages

Vanisha Y Gowda

The document discusses Python programming concepts like variables, data types, operators, loops, functions and provides examples. It covers topics like what is Python, its advantages, variables and data types, identifiers, slicing, operators, writing functions to find product, sum, check conditions.
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/ 10

1. What is Python? Advantage of Python.

Python:

Python is a high-level, interpreted, and versatile programming language created by Guido van
Rossum. It prioritizes readability and simplicity, making it widely adopted for various applications.

Advantages of Python:

1. Readability and Simplicity

2. Versatility

3. Large Standard Library:

4. Community Support

5. cross-Platform Compatibility

6. Integration Capabilities

7. High-Level Language

8.Open Source

9. Community-Driven Frameworks and Libraries

10. Easy to Learn and Teach

2. What is variable. clarify Types with examples.

A variable in programming is a named storage location that can hold data.

1. Integer (int)

Example: age = 25`

2. Float (float):

-Example: salary = 55000.50`

3. String (str):

-Example: name = "John"`

4. Boolean (bool):

-Example: is_valid = True`

5. List:

- Example: `numbers = [1, 2, 3, 4, 5]


6. Tuple:

Example: coordinates = (3, 4)

7. Dictionary:

Example: person = {'name': 'Alice', 'age': 30, 'city': 'New York'}`

8. Set:

Example: unique_numbers = {1, 2, 3, 4, 5}

9. NoneType:

-Example: result = None

3. What is Identifier. List rules of identifiers.

An identifier in programming is a name given to entities such as variables, functions, classes,


modules, or any other user-defined items.

Rules for Identifiers:

1. Character Set:

2. Case Sensitivity

3. Start with a Letter or Underscore

4. No Keywords

5. No Spaces

6. No Special Characters

7. Length Limitations

8. Numbers in Identifier

-9. Descriptive and Meaningful

10. Consistency

4. What is Data types and list them

In programming, a data type is a classification that specifies which type of value a variable
can hold.

1. Integer (int):
2. Float (float)

3. String (str)

4. Boolean (bool)

5. List

6. Tuple

7. Dictionary (dict)

8. Set:

9. NoneType

10. Complex (complex)

5. Explain slicing with example.

In Python, slicing is a technique used to extract a portion of a sequence, such as a string, list,
or tuple. It allows you to create a new sequence containing a subset of the original elements

original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]

Extract elements from index 2 to 5 (exclusive)

sliced_list = original_list[2:5]

print(sliced_list)

Output: [3, 4, 5]

6. Explain operators. list them .

Python, operators are special symbols or keywords that perform operations on one or more
operands.

Arithmetic Operators

a=5

b=2

print(a + b) # Output: 7
print(a - b) # Output: 3

Comparison Operators:

Used to compare values and return a Boolean result

\x = 10

y = 20

print(x == y) # Output: False

print(x != y) # Output: True

Logical Operators:

Used for logical operations on Boolean values

a = True

b = False

print(a and b) # Output: False

Assignment Operators:

Used to assign values to variables

x=5

x += 3 # Equivalent to x = x + 3

print(x) # Output: 8

Bitwise Operators

Used for bitwise operations on integers.

a=5

b=3

print(a & b) # Output: 1 (bitwise AND)

print(a | b) # Output: 7 (bitwise OR)


Membership Operators

Used to test whether a value is a member of a sequence

numbers = [1, 2, 3, 4, 5]

print(3 in numbers) # Output: True

print(6 not in numbers) # Output: True

7. Write a program to find product of numbers.

def calculate_product(numbers): product = 1

for num in numbers:

product *= num

return product

numbers_list = [2, 3, 5, 7, 11, 13]

result = calculate_product(numbers_list)

print(f"The product of numbers {numbers_list} is: {result}")

8. Write a program to find addition by using logical operation.

def add_using_logical(num1, num2):

while num2 != 0:

carry = num1 & num2

num1 = num1 ^ num2

num2 = carry << 1

return num1
num1 = 5

num2 = 7

result = add_using_logical(num1, num2)

print(f"The sum of {num1} and {num2} is: {result}")

10. Write a program to check the given integer number is multiple of 5 or not.

def is_multiple_of_five(number):

return number % 5 == 0

user_input = int(input("Enter an integer: "))

if is_multiple_of_five(user_input):

print(f"{user_input} is a multiple of 5.")

else:

print(f"{user_input} is not a multiple of 5.")

11. Write a program to check the given charactor is uppercase or not.

def is_uppercase(character):

return character.isupper()

user_input = input("Enter a character: ")

if len(user_input) == 1 and user_input.isalpha():

if is_uppercase(user_input):
print(f"The character '{user_input}' is uppercase.")

else:

print(f"The character '{user_input}' is not uppercase.")

else:

print("Please enter a valid single character.")

12. Write a program to check the given word is palindrome or not

def is_palindrome(word):

word = word.lower()

word = word.replace(" ", "")

return word == word[::-1]

user_input = input("Enter a word: ")

if is_palindrome(user_input):

print(f"The word '{user_input}' is a palindrome.")

else:

print(f"The word '{user_input}' is not a palindrome.")

13. Write a program to print all integer numbers which are divisible by 3 between the range 1 to
150 using while loop

number = 1

while number <= 150:

if number % 3 == 0:

print(number, end=" ")


number += 1

# Output:

# 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 93
96 99 102 105 108 111 114 117 120 123 126 129 132 135 138 141 144 147 150

14. Write a program to print cube of all even numbers which are divisible by 4 between range m
and n using for loop.

# Input the range [m, n]

m = int(input("Enter the starting number (m): "))

n = int(input("Enter the ending number (n): "))

for number in range(m, n + 1):

if number % 2 == 0 and number % 4 == 0:

print(f"The cube of {number} is: {number ** 3}")

15. Write a program to following output In = [ hai , hello , python , Numbers ] Out = { ‘hai’ : 3 ,
’hello’ : 5 , ’python’ : 6 , ’Numbers’ : 7 }

input_list = ['hai', 'hello', 'python', 'Numbers']

output_dict = {word: len(word) for word in input_list}

print("In =", input_list)

print("Out =", output_dict)

16. Write a program to following output In = [ hai , hello, python, Numbers ] Out = { ‘hai’ : ’ai’ ,
’hello’ : ’eo’ , ’python’ : ’o’ , ’Numbers’ : ’ue’ }

input_list = ['hai', 'hello', 'python', 'Numbers']

output_dict = {word: len(word) for word in input_list}

print("In =", input_list)


print("Out =", output_dict)

9. Difference between while loop and for loop.

The `while` loop and `for` loop are both control flow structures in programming that allow
repetitive execution of a block of code. Here are five key differences between them:

1. Initialization and Condition:

While Loop: Requires explicit initialization of a variable before the loop, and the loop
continues as long as the specified condition is `True`.

For Loop: Typically used when iterating over a sequence (e.g., a range, list, or string), and
the loop automatically handles the iteration without explicit initialization.

2. Structure:

While Loop: Has a more flexible structure as it relies on a condition that can be placed
anywhere in the loop block. It is suitable when the number of iterations is not known in
advance.

For Loop Has a more concise and structured syntax, especially when iterating over a
sequence. It is appropriate when the number of iterations is known or easily determined.

3. Use Cases

-While Loop: Preferred when the loop termination condition is not based on the number
of iterations but on a specific condition (e.g., user input or external events).

For Loop: Preferred when the number of iterations is known or when iterating over a
sequence like a list or range.

4. **Termination:

While Loop: Requires careful management of the loop control variable to ensure the
termination condition is eventually met; otherwise, it can result in an infinite loop.

-For Loop: Automatically handles iteration and termination based on the length of the
specified sequence, making it less prone to infinite loops.
5. Control Variable Modification:

- While Loop: Requires explicit modification of the control variable inside the loop to
ensure progression toward the termination condition.

-For Loop: The control variable (iteration variable) is automatically updated in each
iteration based on the sequence, eliminating the need for explicit modifications within the
loop.

You might also like