Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
56 views
ubuntu2204
Kernel: Python 3 (system-wide)

Assignment 1

  1. Assign the integer 5 to the variable a. Assign the integer 7 to variable b. Now interchange the values of a and b, without using numbers.

a = "5" b = "7" a, b = b, a
  1. Write the code to find out if a variable x is odd or even and then to print "Even number" or "Odd number" based on value of x.

a = 5 b = 7 if a % 2 == 0: print("a is an Even number") else: print("a is an Odd number") if b % 2 == 0: print("b is an Even number") else: print("b is an Odd number")
a is an Odd number b is an Odd number
  1. The rule of divisibility by 3 is that the sum of the digits should be divisible by 3. The rule of divisibility by 4 is that the last two digits are divisible by 4. Write code to check if a 3 digit number is divisible by 4 and 3 only using the rules of divisibility.

num = 314 # divisibility by 3 sum_of_digits = sum(int(digit) for digit in str(num)) is_divisible_by_3 = sum_of_digits % 3 == 0 # divisibility by 4 last_two_digits = int(str(num)[-2:]) is_divisible_by_4 = last_two_digits % 4 == 0 if is_divisible_by_3 and is_divisible_by_4: print(f"{num} is divisible by both 3 and 4.") elif is_divisible_by_3: print(f"{num} is divisible by 3 only.") elif is_divisible_by_4: print(f"{num} is divisible by 4 only.") else: print(f"{num} is not divisible by 3 or 4.")
314 is not divisible by 3 or 4.
  1. With only the numbers 10 and 2, use mathematical and string operations to end up with the number 5540.

num1 = 10 num2 = 2 result = ((( 2* 2 * (num1 * num1 *num1)) + (num1 * num1)) + (num2 * num1 * num2 * num1 * num2) + (num1 * num1 *num2 *num2)+ (num1 *num2 *num2 * num2 *num2) + (num1 * num2 *num2) + (num1 *num2 *num2) ) print(result)
5540
  1. Without running this code, tell us what the output will be (you can run it after submitting the answer!):
    x = 5
    y = x + 3
    y = int(str(y) + "2")
    print(y)

Write the answer for Exercise 5 here: 82