Python Assignment Questions: Variables, Operators, Datatypes¶
Note
Do not use any conditional statements (if, elif, else) or loops in your solutions.
1. Swapping Variables
Write a Python program to swap the values of two variables entered by the user.
Hint: Use a temporary variable or Python’s tuple unpacking to swap values.
2. Simple Calculator (No Conditions)
Create a program that takes two numbers and an operator (+, -, *, /) as input from the user and prints the result.
Hint: Use eval() or separate conditions for operators, but since no conditions allowed, use eval() with the expression.
3. Type Conversion
Ask the user to input a number as a string. Convert it to an integer and a float, then print both values and their types.
Hint: Use int(), float(), and type() to show the types.
4. Quadratic Equation Roots (No Conditions)
Write a program that takes three numbers as input: a, b, and c for the equation ax^2 + bx + c = 0. Calculate the roots using the formula.
Hint: Use ** for power and complex() if you get a negative square root. Do not use any modules or conditions.
5. Custom Data Type Formatter
Take input of a person’s name, age, and height (in cm). Print a formatted string: “Name: <name>, Age: <age> years, Height: <height> cm”. Also, make sure age is an integer and height is a float.
Hint: Use input(), int(), float(), and f-strings for formatting.
6. Complex Expression Evaluator
Write a program that takes three numbers from the user and calculates the result of the expression: ((num1 + num2) * num3) / (num1 - num2). Print the result rounded to 2 decimal places.
Hint: Use parentheses for the calculation and round() for formatting. If you get an error (like dividing by zero), Python will show it.
7. Sum of Two Numbers
Write a program to take two numbers as input and print their sum.
Hint: Use the + operator to add the numbers.
8. Uppercase String
Take a string as input and print it in uppercase.
Hint: Use the .upper() method on the string.
9. Square of a Number
Input a number and print its square.
Hint: Use the ** operator with exponent 2 or multiply the number by itself.
Greeting Message Take a name as input and print “Hello, <name>”.
11. String Concatenation
Input two strings and print them concatenated together.
Hint: Use the + operator to join the strings.
12. Float to Integer Conversion
Take a float as input and print its integer equivalent using int().
Hint: Use int() to convert the float to an integer, which truncates the decimal part.
13. Age Display
Input an age as a number and print “You are <age> years old”.
Hint: Convert the input to int if needed and use f-string for formatting.
14. Product of Two Numbers
Take two numbers as input and print their product.
Hint: Use the * operator to multiply the numbers.
15. String Length
Input a sentence and print its length using len().
Hint: Apply len() directly to the input string.
16. Average of Three Numbers
Take three numbers as input and print their average.
Hint: Sum the numbers and divide by 3, use parentheses for clarity.
17. Absolute Value
Input a number and print its absolute value using abs().
Hint: Use the built-in abs() function.
18. Difference of Two Numbers
Take two numbers as input and print their difference.
Hint: Subtract the second number from the first using the - operator.
19. String Reversal
Input a string and print it reversed using slicing.
Hint: Use slicing with [::-1] to reverse the string.
20. Height Conversion
Input height in centimeters and print it converted to meters.
Hint: Divide the height by 100 to convert cm to meters.
21. Quotient of Two Numbers
Take two numbers as input and print their quotient.
Hint: Use the / operator for division.
22. Quoted Name
Input a name and print it surrounded by double quotes.
Hint: Use f-string like f’”{name}”’ or concatenation with quotes.
23. Formatted Number
Take a number as input and print it rounded to 2 decimal places.
Hint: Use round(number, 2) to round to 2 decimal places.
24. String Separation
Input two strings and print them separated by a space.
Hint: Use f-string like f”{str1} {str2}” or concatenation with a space.
25. Type of Input
Take any input and print its type using type().
Hint: Apply type() to the input variable.
26. Sum and Product
Input three numbers and print their sum and their product.
Hint: Calculate sum with + and product with *, print both.