Vanisha Y Gowda
Vanisha Y Gowda
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:
2. Versatility
4. Community Support
5. cross-Platform Compatibility
6. Integration Capabilities
7. High-Level Language
8.Open Source
1. Integer (int)
2. Float (float):
3. String (str):
4. Boolean (bool):
5. List:
7. Dictionary:
8. Set:
9. NoneType:
1. Character Set:
2. Case Sensitivity
4. No Keywords
5. No Spaces
6. No Special Characters
7. Length Limitations
8. Numbers in Identifier
10. Consistency
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
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]
sliced_list = original_list[2:5]
print(sliced_list)
Output: [3, 4, 5]
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:
\x = 10
y = 20
Logical Operators:
a = True
b = False
Assignment Operators:
x=5
x += 3 # Equivalent to x = x + 3
print(x) # Output: 8
Bitwise Operators
a=5
b=3
numbers = [1, 2, 3, 4, 5]
product *= num
return product
result = calculate_product(numbers_list)
while num2 != 0:
return num1
num1 = 5
num2 = 7
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
if is_multiple_of_five(user_input):
else:
def is_uppercase(character):
return character.isupper()
if is_uppercase(user_input):
print(f"The character '{user_input}' is uppercase.")
else:
else:
def is_palindrome(word):
word = word.lower()
if is_palindrome(user_input):
else:
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
if number % 3 == 0:
# 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.
15. Write a program to following output In = [ hai , hello , python , Numbers ] Out = { ‘hai’ : 3 ,
’hello’ : 5 , ’python’ : 6 , ’Numbers’ : 7 }
16. Write a program to following output In = [ hai , hello, python, Numbers ] Out = { ‘hai’ : ’ai’ ,
’hello’ : ’eo’ , ’python’ : ’o’ , ’Numbers’ : ’ue’ }
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:
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.