PYTHON PROGRAMMING
INTRODUCTION TO PYTHON
Write short answers to these questions.
1) What is python? Write its advantages and disadvantages.
Python is a high-level programming language that is easy to debug and understand. The advantages of python are:
i) It is simple and easy to understand.
ii) It is free and open source.
iii) It has large standard library.
Disadvantages;
i) It is slower than compiled languages like c++.
ii) It consumes high memory.
iii) Less suitable for mobile app development.
2) What are input and output statements in python? Discuss with examples.
Input/Output (I/O) statements allow interaction with the program. The input() function captures user data, such as number = input("Enter number: ").
3) What is the use of 'sep' and 'end' with print statement?
The sep (separator) parameter is used to specify the character or string placed between multiple values printed by the print() function. Example: print(10, 20, 30, sep="-")
Output:
10-20-30
In Python, the end parameter in a print() statement specifies what to print at the end of the line instead of the default newline. For example, print(a, b, end=" ") prints a space after the output instead of moving to a new line.
4) What is the use of f-string and format() function in string format?
An f-string is used for string formatting by allowing expressions to be embedded directly inside string literals using curly braces, such as print(f"Result: {result}"). The string formatting can also be done using the % operator for random number display, but they do not explicitly define the format() function.
5) Describe local and global variable used in python with example.
A local variable is declared inside a function and is only accessible within that function's code block. A global variable is declared outside any function (at the top level) and can be accessed from anywhere in the program.
6) What is the purpose of indentation in python?
Indentation in Python is used to define blocks of code. For instance, statements must be indented to be considered part of a function's body; statements outside that indentation are not part of the function.
7) What is looping? Write the role of the range() function in python for loop.
Looping (Iteration) is the process of repeating a task until a specific condition is satisfied. The range() function defines a sequence of numbers, allowing a for loop to repeat a block of code a specific number of times.
8) Write the differences between break and continue statement.
In Python, the break statement completely stops a loop, while the continue statement skips the current iteration and moves to the next one.
9) How is built-in-function help in reducing code complexity.
Built-in functions reduce complexity by providing organized, reusable code for common tasks like sum() or len(). They are always available in memory, allowing for better modularity without the need for manual programming of standard operations.
10) What is the primary function of the input() function in Python?
The primary function of the input() function is to allow a user to provide data to the program during execution.
11) Provide an example of a string literal in Python.
A string literal consists of text or alphanumeric values enclosed in double quotes. An example is "hello".
12) What is an identifier in the context of Python programming?
An identifier is a user-defined name given to various program units, including variables, functions, or classes.
13) Explain the difference between the division operator (/) and the floor division operator (//) in Python.
The division operator (/) performs standard division, such as 20 / 10 = 2. The floor division operator (//) performs division and returns only the whole number part of the result, such as 9 // 2 = 4.
14) Define the concept of data types in Python.
Data types define the kind of information a variable can store, such as whole numbers (integers), decimals (floats), text (strings), or Boolean values (True/False).
15) Describe the functionality of the else block in an if-else statement.
In an if-else statement, the else block contains the code that will be executed if the condition in the if statement is False.
16) What is a nested if statement?
A nested if is a programming construct where an if statement is placed inside another if statement.
17) When would you typically use a for loop instead of a while loop?
We typically use a for loop when we know exactly how many times a block of code should repeat. A while loop is preferred when you want to repeat a task as long as a certain condition remains true.
18) What is the role of the range() function often used with for loops?
The range() function provides the sequence of items that the for loop iterates through.
19) Define a Python list and provide a simple example.
A Python list is a built-in data type used to store multiple items in a single variable. A simple example is thislist = ["Computer", "Science", 20, True].
20) Discuss the concept of iteration in programming.
Iteration refers to the process of doing things repeatedly in a program until a target condition is met. It is fundamental for managing repetitive tasks through structures like loops.
Post a Comment
Thank you for your comment