Python Basics-I


 1.    What is the output of the following code?

x = 5

y = 2

print(x + y)

Ans: 7

2.    What is the difference between a tuple and a list in Python?

Ans: A tuple is an immutable data structure, meaning its elements cannot be changed once created, while a list is mutable, meaning its elements can be modified

3.How do you define a function in Python?

Ans: A function is defined using the keyword "def" followed by the function name, a set of parentheses, and a colon. The code inside the function is indented. For example:

def greet(name):

    print("Hello, " + name)

4.What is the output of the following code?

x = [1, 2, 3, 4, 5]

print(x[1:4])

Ans [2,3,4]
5.What is the use of the 'import' keyword in Python? 
Ans :The 'import' keyword is used to import modules or packages in Python. It allows you to access the functionality of those modules or packages in your code. For example, you can use the 'import math' statement to use functions like sqrt() and pow() from the math module.

6.What is a variable in Python? 

A: In Python, a variable is a named location in memory that is used to store a value. The value stored in a variable can be of any data type, such as a string, integer, or Boolean. Variables are created by assigning a value to a name using the assignment operator (=).

7.What are the different data types in Python?

Python has several built-in data types, including:

  • Integer (int)
  • Floating-point (float)
  • String (str)
  • Boolean (bool)
  • List (list)
  • Tuple (tuple)
  • Dictionary (dict)
  • Set (set)

8. How do you use a conditional statement in Python?

A: In Python, you can use the if statement to create a conditional block of code that is executed only if a certain condition is true. The basic syntax for an if statement is:
if condition: # code to execute if condition is true
eg if a>b :
            print("first number is greater");

You can also use elif (short for "else if") to add additional conditions that are checked if the first condition is false, and else to specify code that is executed if none of the previous conditions are true.
if condition1: 
 # code to execute if condition1 is true 
elif condition2:
 # code to execute if condition1 is false and condition2 is true 
else: 
 # code to execute if neither condition1 nor condition2 is true


No comments:

Post a Comment