Q1 What is python?
ans> 1. High-level programming language: Python is a high-level language, meaning it abstracts
away many low-level details, allowing developers to focus on logic and syntax without worrying
about memory management and other details.
2. Interpreted language: Python code is interpreted rather than compiled, which means that
code is executed line by line, rather than being compiled into machine code beforehand.
3. Object-oriented language: Python supports object-oriented programming (OOP) concepts,
such as classes, objects, inheritance, polymorphism, and encapsulation, making it easy to write
reusable and modular code.
4. Scripting language: Python is often used as a scripting language, meaning it's used to write
short programs or scripts to automate specific tasks, like data analysis, file manipulation, or
web development.
5. Cross-platform language: Python can run on multiple platforms, including Windows, macOS,
Linux, and mobile devices, making it a versatile language for development.
6. Easy to learn: Python has a simple syntax and is relatively easy to learn, making it a great
language for beginners and experts alike.
7. Large standard library: Python has an extensive collection of libraries and modules for
various tasks, such as data analysis, web development, and more.
8. Dynamic typing: Python is dynamically typed, meaning variable types are determined at
runtime, rather than at compile time.
9. Extensive community: Python has a large and active community, with many resources
available, including libraries, frameworks, and tutorials .Overall, Python is a versatile, easy-to-
learn language that's widely used in various domains, including web development, data science,
automation, and more.
Q2.Write a short note on syntax error, Logical error with example?
Ans> Syntax Error: A syntax error, also known as a parsing error, occurs when the
code violates the rules of the programming language's syntax. This means that the
code is not written in a way that the compiler or interpreter can understand.
Example:
print("Hello, world! # forgot to close the string
This code will result in a syntax error because the string is not closed with a
matching quotation mark.
Logical Error:
A logical error, also known as a semantic error, occurs when the code is
syntactically correct but does not produce the desired output or behavior. This
means that the code is written correctly, but the logic is flawed.
Example:
x=5
y=2
print(x + y) # intended to print the product, not the sum
This code will result in a logical error because the code is written to add x and y,
but the intended behavior is to multiply them.
Q3.what is operator? Explain bitwise operator and arithmetic
operator.
Ans> An operator is a symbol used in programming to perform operations on
variables, values, or expressions. There are various types of operators, including:
- Arithmetic operators
- Bitwise operators
1. Arithmetic Operators:
Arithmetic operators perform mathematical operations on numbers. Examples
include:
- Addition (+)
- Subtraction (-)
- Multiplication (*)
- Division (/)
- Modulus (%)
- Exponentiation (**)
Example: a = 5 + 3 (adds 5 and 3 and assigns the result to a).
2. Bitwise Operators:
Bitwise operators manipulate the binary representation of numbers (bits). They
operate on the individual bits of a number, performing operations like shifting,
AND, OR, and XOR. Examples include:
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- Left shift (<<)
- Right shift (>>)
Example: a = 5 & 3 (performs a bitwise AND operation on 5 and 3, resulting in 1)
Here's a breakdown of the bitwise AND operation:
- 5 in binary: 101
- 3 in binary: 011
- Result: 001 (which is 1 in decimal)
Q4. State and explain variable in python along with rule of variable.
Ans> In Python, a variable is a name given to a value. It allows you to store and
manipulate data in your program.
Rules of Variables in Python:
1. Variable names can only contain letters, digits, and underscores: You can use
any combination of letters (a-z or A-Z), digits (0-9), and underscores (_) to create a
variable name.
2. Variable names must start with a letter or underscore: You can't start a variable
name with a digit.
3. Variable names are case-sensitive: Python distinguishes between uppercase
and lowercase letters, so name and Name are treated as different variables.
4. Reserved words cannot be used as variable names: You can't use Python's
reserved words (like if, else, for, etc.) as variable names.
5. Variable names should be descriptive and concise: Choose variable names that
clearly indicate what the variable represents, but avoid overly long names.
Declaring Variables in Python:
In Python, you don't need to declare variables before using them. Instead, you
can assign a value to a variable directly:
variable_name = value
Example:
x = 5 # integer variable
y = "hello" # string variable
z = 3.14 # floating-point variable
Data Types in Python:
Python has various built-in data types, including:
- Integers (int): whole numbers, like 1, 2, 3, etc.
- Floating-point numbers (float): decimal numbers, like 3.14 or -0.5
- Strings (str): sequences of characters, like "hello" or 'hello'
- Boolean (bool): true or false values
- List (list): ordered collections of values, like [1, 2, 3] or ["a", "b", "c"].
Q5. Define control statement? Explain pass, continue, break
statement.
Ans> Control statements are used to control the flow of a program's execution
based on conditions, loops, or other factors. They allow you to determine the
order in which statements are executed, skip or repeat certain statements, and
transfer control to different parts of the program.
1. Pass Statement:
The pass statement is a null operation; it does nothing when executed. It is used
as a placeholder when a statement is required syntactically but no execution of
code is necessary.
Example:
if condition:
pass # do nothing.
2. Continue Statement: The continue statement skips the rest of the current
iteration in a loop and moves on to the next iteration.
3. Example:
for i in range(5):
if i == 3:
continue # skip this iteration
print(i) # prints 0, 1, 2, 4
3. Break Statement: The break statement exits the current loop or switch
statement and transfers control to the statement immediately following
the loop or switch.
Example:
for i in range(5):
if i == 3:
break # exit the loop
print(i) # prints 0, 1, 2
Q6. Write a program to print ‘*’ prymid .
Ans> for i in range(5):
print(' ' * (5 - i - 1) + '*' * (2 * i + 1))
This program uses a for loop to iterate over the rows of the pyramid. The print
function is used to print each row, which consists of:
- A string of spaces (' ' * (5 - i - 1)) to indent the row
- A string of asterisks ('*' * (2 * i + 1)) to print the pyramid shape
The number of spaces and asterisks is calculated using the loop variable i and
some simple arithmetic.
Here's the output of the program:
***
*****
*******
*********
Q7. State and explain type of if with suitable example.
Ans> There are several types of if statements in Python, including:
1. Simple if statement:
if condition:
code to execute
Example:
x=5
if x > 10:
print("x is greater than 10")
2. if-else statement:
if condition:
code to execute
else:
code to execute
Example:
x=5
if x > 10:
print("x is greater than 10")
else:
print("x is less than or equal to 10")
3. if-elif-else statement:
if condition:
code to execute
elif another_condition:
code to execute
else:
code to execute
Example:
x=5
if x > 10:
print("x is greater than 10")
elif x == 5:
print("x is equal to 5")
else:
print("x is less than 5")
4. Nested if statement:
if condition:
if another_condition:
code to execute
else:
code to execute
else:
code to execute
Example:
x=5
if x > 5:
if x > 10:
print("x is greater than 10")
else:
print("x is greater than 5 but less than 10")
else:
print("x is less than or equal to 5")
Q8. Difference between for loop and while loop
Iteration
Sequence
Loop Variable
Condition
Usage