Python Assignment– 1
Python Basics
Basic Questions
- Write a script that first sets ‘x = 10’, then reassigns ‘x = “ten”‘, and prints the value and ‘type’ each time to demonstrate dynamic typing.
- Create variables of types ‘int’, ‘float’, ‘str’, ‘bool’, and ‘complex’; print each as ‘<value> -> <type>’ using ‘type()’.
- Read two ‘int’ values and print their ‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, and ‘**’ results.
- 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=…)’.
- Read two ‘str’ inputs like ’12’ and ‘3.5’, convert them to ‘int’ and ‘float’, then print the addition result.
- Add a module docstring at the top (triple quotes) and a few ‘#’ comments; then print the docstring via ‘print(doc)’.
- Call ‘help(print)’ and ‘dir(“abc”)’; print the length of the list returned by ‘dir(“abc”)’.
- Read two numbers and print results of ‘==’, ‘!=’, ‘<‘, ‘<=’, ‘>’, and ‘>=’.
- 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)’.
- Read an integer and print its binary form using ‘bin()’, then print results of ‘~n’, ‘n << 2’, and ‘n >> 3’.
- Read two integers and print results of ‘x & y’, ‘x | y’, and ‘x ^ y’.
- Given ‘a = 256’, ‘b = 256’, ‘c = 10’, ‘d = 10’, print results of ‘a is b’ and ‘c is d’.
- Read a word and a character; print whether the character is in the word using ‘in’ and ‘not in’.
- Evaluate and print results of ‘3 + 4 * 5’ and ‘(3 + 4) * 5’.
- Read three numbers ‘x’, ‘y’, ‘z’; compute and print ‘x ** y ** z’ and ‘(x ** y) ** z’.
- Read two integers and demonstrate compound assignments: ‘n += 5’, ‘n -= 2’, ‘n *= 3’, ‘n //= 2’.
- Read a string ‘s’ and print results of: ”a’ in s’, ”A’ in s’, and ”a’ not in s’.
- Read a float and cast it to ‘int’; print both values and a formatted message.
- Use ‘dir(0)’ and ‘dir(0.0)’; print how many attributes each has and whether ‘numerator’ appears in them.
- Print results of ‘3 < 4 < 5’ and ‘3 < 4 > 5’.
Intermediate Questions
- Build a mini arithmetic reporter: read two numbers and print one line summarizing ‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, and ‘**’ results.
- Read three integers ‘a, b, c’ and compute ‘a + b * c’, ‘(a + b) * c’, and ‘a + (b * c)’.
- Read two booleans and print evaluations of ‘p and q’, ‘p or q’, ‘not p’, and ‘not q’.
- Read an integer and print its binary, octal, and hex representations, then count ‘1’ bits using ‘bin(n).count(“1”)’.
- Read two integers ‘x’ and ‘y’; compute ‘(x & y) + (x | y) – (x ^ y)’, then print each bitwise result.
- Ask the user for a string and a substring; print membership results using ‘in’ and ‘not in’.
- Create variables ‘a = True’, ‘b = 1’, ‘c = 1.0’; print results of ‘a == b’, ‘b == c’, and ‘a is b’.
- Read an integer ‘n’; compute ‘((n + 5) * 3) ** 2 // 4 % 7’; print the result and a parenthesized expression showing evaluation order.
- Use ‘help(int)’ to display documentation; then print only the first line of ‘int.doc’.
- Read a number and a format keyword (‘fixed’ or ‘sci’); print the number using fixed-point or scientific format.
- Read two strings representing numbers; convert to ‘float’; add and print result with its ‘type’.
- Read a complex number using ‘complex(input())’; print ‘.real’, ‘.imag’, and magnitude.
- Read an integer ‘n’ and shift amount ‘k’; print results of ‘n << k’ and ‘n >> k’.
- Read string ‘s’ and character ‘ch’; check identity with ‘is’ and print.
- Read three booleans ‘p, q, r’; compute ‘p and (q or r)’ and ‘(p and q) or r’.
- Read integer ‘n’; compute ‘(~n) + 1’ and check if equal to ‘-n’.
- Read a word; test results of ”py’ in word’, ”PY’ in word’, and ”p’ in word or ‘P’ in word’.
- Read two values ‘x’ and ‘y’ as strings; print ‘x + y’ and ‘int(x) + int(y)’.
- Construct and print an expression using at least three operators that equals ‘7’.
- Read an integer and test if ‘(n % 2 == 0) is (n & 1 == 0)’.
Advanced Questions
- Build a bitwise dashboard: read two integers and print results of ‘&’, ‘|’, ‘^’, ‘~’, ‘<<‘, ‘>>’, and bit counts.
- 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’.
- Build a membership tester: read a sentence and a token; test ‘token in sentence’, ‘token.lower() in sentence.lower()’, and ‘token not in sentence’.
- Identity pitfall demo: assign ‘x = 256’, ‘y = 256’, ‘p = 10’, ‘q = 10’, ‘s1 = “hello”‘, ‘s2 = “he” + “llo”‘; print equality and identity results.
- 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)’.
- Expression explainer: read ‘a, b, c’ and print ‘a + b * c ** 2 // 3 % 5’; then reprint fully parenthesized.
- Mini formatter: read a float and print it in default, fixed (3 decimals), and scientific (2 decimals) formats.
- Complex arithmetic: read two complex numbers; print their sum, product, and whether their ‘.real’ parts are equal.
- Introspection card: read a type name like ‘int’ as string; use ‘eval’ to get object; print first line of ‘.doc’ and length of ‘dir()’.
- One-line calculator: read two numbers and an operator (‘+’, ‘-‘, ‘*’, ‘/’, ‘//’, ‘%’, ‘**’, ‘&’, ‘|’, ‘^’, ‘<<‘, ‘>>’); evaluate safely via mapping and print result.