Python Assignment– 1

Python Basics

Basic Questions

  1. Write a script that first sets ‘x = 10’, then reassigns ‘x = “ten”‘, and prints the value and ‘type’ each time to demonstrate dynamic typing.
  2. Create variables of types ‘int’, ‘float’, ‘str’, ‘bool’, and ‘complex’; print each as ‘<value> -> <type>’ using ‘type()’.
  3. Read two ‘int’ values and print their ‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, and ‘**’ results.
  4. Read ‘name’ (string) and ‘age’ (integer) and print ‘Hello, <name>. You are <age> years old.’ using an f-string and again using ‘print(…, sep=…, end=…)’.
  5. Read two ‘str’ inputs like ’12’ and ‘3.5’, convert them to ‘int’ and ‘float’, then print the addition result.
  6. Add a module docstring at the top (triple quotes) and a few ‘#’ comments; then print the docstring via ‘print(doc)’.
  7. Call ‘help(print)’ and ‘dir(“abc”)’; print the length of the list returned by ‘dir(“abc”)’.
  8. Read two numbers and print results of ‘==’, ‘!=’, ‘<‘, ‘<=’, ‘>’, and ‘>=’.
  9. Read three boolean-like inputs (‘True’/’False’), convert to booleans, and print results of ‘(p and q) or (not r)’ and ‘p or (q and r)’.
  10. Read an integer and print its binary form using ‘bin()’, then print results of ‘~n’, ‘n << 2’, and ‘n >> 3’.
  11. Read two integers and print results of ‘x & y’, ‘x | y’, and ‘x ^ y’.
  12. Given ‘a = 256’, ‘b = 256’, ‘c = 10’, ‘d = 10’, print results of ‘a is b’ and ‘c is d’.
  13. Read a word and a character; print whether the character is in the word using ‘in’ and ‘not in’.
  14. Evaluate and print results of ‘3 + 4 * 5’ and ‘(3 + 4) * 5’.
  15. Read three numbers ‘x’, ‘y’, ‘z’; compute and print ‘x ** y ** z’ and ‘(x ** y) ** z’.
  16. Read two integers and demonstrate compound assignments: ‘n += 5’, ‘n -= 2’, ‘n *= 3’, ‘n //= 2’.
  17. Read a string ‘s’ and print results of: ”a’ in s’, ”A’ in s’, and ”a’ not in s’.
  18. Read a float and cast it to ‘int’; print both values and a formatted message.
  19. Use ‘dir(0)’ and ‘dir(0.0)’; print how many attributes each has and whether ‘numerator’ appears in them.
  20. Print results of ‘3 < 4 < 5’ and ‘3 < 4 > 5’.

Intermediate Questions

  1. Build a mini arithmetic reporter: read two numbers and print one line summarizing ‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, and ‘**’ results.
  2. Read three integers ‘a, b, c’ and compute ‘a + b * c’, ‘(a + b) * c’, and ‘a + (b * c)’.
  3. Read two booleans and print evaluations of ‘p and q’, ‘p or q’, ‘not p’, and ‘not q’.
  4. Read an integer and print its binary, octal, and hex representations, then count ‘1’ bits using ‘bin(n).count(“1”)’.
  5. Read two integers ‘x’ and ‘y’; compute ‘(x & y) + (x | y) – (x ^ y)’, then print each bitwise result.
  6. Ask the user for a string and a substring; print membership results using ‘in’ and ‘not in’.
  7. Create variables ‘a = True’, ‘b = 1’, ‘c = 1.0’; print results of ‘a == b’, ‘b == c’, and ‘a is b’.
  8. Read an integer ‘n’; compute ‘((n + 5) * 3) ** 2 // 4 % 7’; print the result and a parenthesized expression showing evaluation order.
  9. Use ‘help(int)’ to display documentation; then print only the first line of ‘int.doc’.
  10. Read a number and a format keyword (‘fixed’ or ‘sci’); print the number using fixed-point or scientific format.
  11. Read two strings representing numbers; convert to ‘float’; add and print result with its ‘type’.
  12. Read a complex number using ‘complex(input())’; print ‘.real’, ‘.imag’, and magnitude.
  13. Read an integer ‘n’ and shift amount ‘k’; print results of ‘n << k’ and ‘n >> k’.
  14. Read string ‘s’ and character ‘ch’; check identity with ‘is’ and print.
  15. Read three booleans ‘p, q, r’; compute ‘p and (q or r)’ and ‘(p and q) or r’.
  16. Read integer ‘n’; compute ‘(~n) + 1’ and check if equal to ‘-n’.
  17. Read a word; test results of ”py’ in word’, ”PY’ in word’, and ”p’ in word or ‘P’ in word’.
  18. Read two values ‘x’ and ‘y’ as strings; print ‘x + y’ and ‘int(x) + int(y)’.
  19. Construct and print an expression using at least three operators that equals ‘7’.
  20. Read an integer and test if ‘(n % 2 == 0) is (n & 1 == 0)’.

Advanced Questions

  1. Build a bitwise dashboard: read two integers and print results of ‘&’, ‘|’, ‘^’, ‘~’, ‘<<‘, ‘>>’, and bit counts.
  2. Create a precedence demonstrator: read three integers and print results of ‘a + b * c’, ‘(a + b) * c’, ‘a ** b ** c’, ‘(a ** b) ** c’, ‘a ** (b ** c)’, and ‘a + b << c’.
  3. Build a membership tester: read a sentence and a token; test ‘token in sentence’, ‘token.lower() in sentence.lower()’, and ‘token not in sentence’.
  4. Identity pitfall demo: assign ‘x = 256’, ‘y = 256’, ‘p = 10’, ‘q = 10’, ‘s1 = “hello”‘, ‘s2 = “he” + “llo”‘; print equality and identity results.
  5. Logical equivalence check: read booleans ‘p’ and ‘q’; verify De Morgan’s laws: ‘not (p and q) == (not p or not q)’ and ‘not (p or q) == (not p and not q)’.
  6. Expression explainer: read ‘a, b, c’ and print ‘a + b * c ** 2 // 3 % 5’; then reprint fully parenthesized.
  7. Mini formatter: read a float and print it in default, fixed (3 decimals), and scientific (2 decimals) formats.
  8. Complex arithmetic: read two complex numbers; print their sum, product, and whether their ‘.real’ parts are equal.
  9. Introspection card: read a type name like ‘int’ as string; use ‘eval’ to get object; print first line of ‘.doc’ and length of ‘dir()’.
  10. One-line calculator: read two numbers and an operator (‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, ‘**’, ‘&’, ‘|’, ‘^’, ‘<<‘, ‘>>’); evaluate safely via mapping and print result.