0% found this document useful (0 votes)
8 views3 pages

Python Basics and Coding Exercises

The document contains questions about Python basics, primitive types, control flow, functions and coding exercises. It includes over 50 questions across different topics in Python like expressions, data types, operators, functions and more. The questions range from basic to more advanced concepts in the language.

Uploaded by

mishraapurv95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Python Basics and Coding Exercises

The document contains questions about Python basics, primitive types, control flow, functions and coding exercises. It includes over 50 questions across different topics in Python like expressions, data types, operators, functions and more. The questions range from basic to more advanced concepts in the language.

Uploaded by

mishraapurv95
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON

Questions
Basics
1. What is an expression?
2. What is a syntax error?
3. What is PEP8?
4. What does a linter do?
5. What is the result of this expression: “*” * 10
6. What is CPython?
7. How is CPython different from Jython?
8. How is CPython different from IronPython?

Primitive Types
1. What is a variable?
2. What are the primitive built-in types in Python?
3. When should we use “”” (tripe quotes) to define strings?
4. Assuming (name = “John Smith”), what does name[1] return?
5. What about name[-2]?
6. What about name[1:-1]?
7. How to get the length of name?
8. What are the escape sequences in Python?
9. What is the result of f“{2+2}+{10%3}”?
10. Given (name = “john smith”), what will [Link]() return?
11. What does [Link]() do?
12. What will [Link](“Smith”) return?
13. What will be the value of name after we call [Link](“j”,
“k”)?
14. How can we check to see if name contains “John”?
15. What are the 3 types of numbers in Python?

Control Flow
1. What is the difference between 10 / 3 and 10 // 3?
2. What is the result of 10 ** 3?
3. Given (x = 1), what will be the value of after we run (x += 2)?
4. How can we round a number?
5. What is the result of float(1)?
6. What is the result of bool(“False”)?
PYTHON
7. What are the falsy values in Python?
8. What is the result of 10 == “10”?
9. What is the result of “bag” > “apple”?
10. What is the result of not(True or False)?
11. Under what circumstances does the expression 18 <= age <
65 evaluate to True?
12. What does range(1, 10, 2) return?
13. Name 3 iterable objects in Python.

Functions
1. What is the difference between a parameter and an argument?
2. All functions in Python by default return …?
3. What are keyword arguments and when should we use them?
4. How can we make a parameter of a function optional?
5. What happens when we prefix a parameter with an asterisk (*)?
6. What about two asterisks (**)?
7. What is scope?
8. What is the difference between local and global variables?
9. Why is using the global statement a bad practice?

Coding Exercises
1. Write a function that returns the maximum of two numbers.
2. Write a function called fizz_buzz that takes a number.
1. If the number is divisible by 3, it should return “Fizz”.
2. If it is divisible by 5, it should return “Buzz”.
3. If it is divisible by both 3 and 5, it should return
“FizzBuzz”.
4. Otherwise, it should return the same number.
3. Write a function for checking the speed of drivers. This function
should have one parameter: speed.
1. If speed is less than 70, it should print “Ok”.
2. Otherwise, for every 5km above the speed limit (70), it
should give the driver one demerit point and print the
total number of demerit points. For example, if the speed
is 80, it should print: “Points: 2”.
3. If the driver gets more than 12 points, the function
should print: “License suspended”
PYTHON
4. Write a function called showNumbers that takes a parameter
called limit. It should print all the numbers between 0 and limit
with a label to identify the even and odd numbers. For example, if
the limit is 3, it should print:
o 0 EVEN
o 1 ODD
o 2 EVEN
o 3 ODD
5. Write a function that returns the sum of multiples of 3 and 5
between 0 and limit (parameter). For example, if limit is 20, it
should return the sum of 3, 5, 6, 9, 10, 12, 15, 18, 20.
6. Write a function called show_stars(rows). If rows is 5, it should
print the following:
o *
o **
o ***
o ****
o *****
7. Write a function that prints all the prime numbers between 0
and limit where limit is a parameter.

Common questions

Powered by AI

The expression 18 <= age < 65 evaluates to True when the variable 'age' holds a value between 18 and 64 inclusive. Python allows chaining of comparison operators, which makes it possible to check if a value falls within a specific range in a concise manner. This is particularly useful in scenarios that require age-based validations, such as ensuring a valid age for employment or service eligibility .

Escape sequences in Python are used to insert special characters in strings that cannot be typed out directly or may interfere with string quotes. Common escape sequences include '\n' for new lines, '\t' for tabs, and '\\' for backslashes. These sequences allow the inclusion of non-printable characters or precise formatting of string outputs, aiding in creating text-based UI or formatted file outputs .

Falsy values in Python are treated as False in conditional statements. They include None, False, zero of any numeric type, and empty sequences or collections like '', (), [], and {}, among others. Recognizing falsy values aids in writing concise conditional statements and error handling, particularly when checking for null or empty data .

CPython is the default and most widely used implementation of Python, written in C and optimized for executing Python code seamlessly. Jython is a Python implementation in Java that runs on the Java Virtual Machine (JVM), allowing Python code to interact with Java libraries. IronPython is another implementation targeting the .NET framework and Microsoft's Common Language Runtime (CLR), enabling integration with .NET libraries. Each implementation interacts with different ecosystems, respectively, and is suited for integration where specific language interoperability is required .

Keyword arguments in Python are specified by identifying each argument by the name of the parameter in function calls, instead of relying solely on their position. They enhance readability by clearly indicating the purpose of each argument. This flexibility allows arguments to be passed in any order and provides a way to set default values that can be selectively overridden, improving the clarity and functionality of complex function calls .

PEP8 is a style guide for Python code, which provides conventions for writing code in a standardized and readable way. It covers various aspects such as indentation, line length, and naming conventions. PEP8 is important because it helps maintain consistency in code across different projects and developers, making it easier to read and understand .

The range object generated by range(1, 10, 2) creates a sequence of numbers starting at 1, ending before 10, with steps of 2 (i.e., 1, 3, 5, 7, 9). This flexibility allows developers to iterate over non-consecutive numbers effectively, such as iterating over alternate elements in a list, or counting in specific intervals within loops .

Three built-in iterable objects in Python include lists, tuples, and strings. Lists are used for flexible, mutable collections of items, ideal for dynamic data storage and iteration. Tuples, being immutable, are suited for fixed data structures like coordinate pairs. Strings are iterated over character-by-character, often utilized for parsing text. These iterable types leverage Python's powerful iteration capabilities for a variety of data manipulation tasks .

In Python, multiplying a string by an integer repeats the string that many times, so '*' * 10 produces '**********'. This operation can be useful for creating repeated patterns of characters, such as visual separators in command-line interfaces or formatting outputs in reports and logs .

Using the global statement is considered bad practice as it makes code harder to understand and maintain by allowing modifications to variables defined outside the function's scope, leading to side effects. This impacts readability and increases the likelihood of bugs. Alternatives include returning values from functions and updating the variable outside, or using class attributes or data encapsulation techniques to manage state .

You might also like