Ch 4 Assignment soln
Ch 4 Assignment soln
4. Write a Python program for the Fibonacci series using a for loop.
5. Write a Python program to receive the numbers from the user
through the keyboard until the user gives 0 (to end the input process)
then, the program calculates and displays the sum of given odd
numbers and even numbers, respectively.
EXAMPLE: if the user gives the following sequence of numbers:
7 5 10 14 3 9 11 12
Then the output should be as follows: The Sum of even numbers in the
given input sequence = 36 The Sum of odd numbers in the given input
sequence = 35
6. Write a program that displays a joke. But display the punchline only
when the user presses the enter key. (HINT: you may use input() )
Example:
• Explicit: x = float("10") → Here, you manually convert a string "10"
to a float.
• Implicit: x = 10 + 2.5 → Python automatically converts the integer
10 to a float before performing the addition, resulting in a float
value 12.5.
13. Write down the syntax and use of the following data types: i) List,
ii)Tuple, iii)Dictionary, iv)Set, v)String
1. List
• Syntax:
• my_list = [element1, element2, element3, ...]
• Use:
o Ordered, mutable collection of elements.
o Can store elements of different types.
o Supports indexing, slicing, and modifying elements.
Example:
my_list = [1, 2, 3, "apple"]
my_list[0] = 5 # List is mutable
2. Tuple
• Syntax:
• my_tuple = (element1, element2, element3, ...)
• Use:
o Ordered, immutable collection of elements.
o Can store elements of different types.
o Faster than lists for fixed data.
Example:
my_tuple = (1, 2, 3, "apple")
3. Dictionary
• Syntax:
• my_dict = {key1: value1, key2: value2, key3: value3}
• Use:
o Unordered collection of key-value pairs.
o Keys are unique, and values can be of any type.
o Efficient for looking up values based on keys.
Example:
my_dict = {"name": "Alice", "age": 25}
print(my_dict["name"]) # Accessing value using key
4. Set
• Syntax:
• my_set = {element1, element2, element3, ...}
• Use:
o Unordered collection of unique elements.
o Does not allow duplicate values.
o Supports mathematical set operations like union,
intersection, etc.
Example:
my_set = {1, 2, 3, 4}
my_set.add(5) # Adding an element
5. String
• Syntax:
• my_string = "This is a string"
• Use:
o Immutable sequence of characters.
o Supports various methods for string manipulation like slicing,
concatenation, and formatting.
Example:
my_string = "Hello"
print(my_string[0]) # Accessing the first character
Summary of Usage:
• List: Ordered, mutable collection.
• Tuple: Ordered, immutable collection.
• Dictionary: Key-value pairs, unordered.
• Set: Unordered collection of unique elements.
• String: Immutable sequence of characters.
+ Addition a+b
- Subtraction a-b
* Multiplication a*b
Operator Description Example
** Exponentiation (power) a ** b
• Example:
• x=5
• y=2
• print(x + y) # Output: 7
2. Relational Operators:
• Use: Compare two values and return a Boolean (True or False).
• Operators: ==, !=, >, <, >=, <=
== Equal to a == b
!= Not equal to a != b
• Example:
• x = 10
• y = 20
• print(x < y) # Output: True
3. Logical Operators:
• Use: Perform logical operations and return a Boolean value.
• Operators: and, or, not
• Example:
• x = True
• y = False
• print(x and y) # Output: False
4. Identity Operators:
• Use: Compare the memory locations of two objects.
• Operators: is, is not
is not Returns True if both are not the same object a is not b
• Example:
• a = [1, 2, 3]
• b=a
• print(a is b) # Output: True
5. Membership Operators:
• Use: Test if a value is a member of a sequence (like a list, string, or
tuple).
• Operators: in, not in
• Example:
• fruits = ['apple', 'banana', 'cherry']
• print('apple' in fruits) # Output: True
Summary:
• Arithmetic Operators: Perform mathematical calculations.
• Relational Operators: Compare values and return Boolean results.
• Logical Operators: Combine or invert Boolean expressions.
• Identity Operators: Check if two variables point to the same
object.
• Membership Operators: Check if a value is part of a sequence.
Precedence
Operators Description
Level
Exponentiation (right-to-left
2 ** (Exponentiation)
associativity)
5 +, - Addition, Subtraction
8 ^ Bitwise XOR
Precedence
Operators Description
Level
9 ` `
Relational (Comparison)
10 ==, !=, >, <, >=, <=
operators
13 or Logical OR