PYTHON BASICS
1. Python Modes
- Interactive Mode: Opens when you launch Python directly.
- Script Mode: Opens when you create a new file.
2. Basic Commands
python
print("Hello") # Output: Hello
a = 36
print(a) # Output: 36
3. Keywords
- Reserved words in Python.
- Examples: True, False, if, or, and, not
4. Identifiers (Variables)
Ram = 4000
print(Ram) # Output: Ram = 4000
5. Naming Rules
- Can use letters, alphabets, or underscores.
- Cannot use special symbols.
6. More Examples
a = 50
Ram = 1
print("Ram ") # Output: Ram
print("a ") # Output: a = 50
7. Data Types
Numeric
1) Int = 5, 469, 0, -15
2) Float = 5.1, 0.0, -15.6
3) Complex = 5, 4j, 36+2j
4) Boolean = `True, False (0, 1)
Sequences
1) String: Enclosed in single ' or double " quotes
2) Tuple: Immutable ordered collection
3) List: Mutable ordered collection
Sets - Similar to sequences but no duplicate values allowed
None - Represents an unknown or missing value
Dictionary : Key-value pairs used for structured data
Operators :
1) Arithmetic` = +, `-, *, /, //, %, **`
2) Assignment =, `+=, -=, *=, /=, //=, %=, **=
3) Logical = and, or, not`
4) Comparison= =, `!=, >, <, >=, <=`
5) Identity` = is, is not`
6) Membership =`in, not in---
Comments - Use # to write comments. These lines are ignored by Python during execution.
Arithmetic Operator Examples
python
a = 10
b=5
print(a + b) # Addition → 15
print(a - b) # Subtraction → 5
print(a * b) # Multiplication → 50
print(a / b) # Division → 2.0
print(a // b) # Floor Division → 2
print(a % b) # Modulus → 0
print(a ** b) # Exponentiation → 100000
Comparison (Relational) Operators
Operator Example Output
Greater than > a = 10, b = 5
print(a> b) True
Less than < print(a < b) False
Greater or equal to >= print(a>= b) True
Less or equal <= print(a <= b) False
Not equal to ! = print(a!= b) True
Equal to == print(a == b) False
Assignment Operator
a = 69 print(a) # Output: 69
a = 10
b=2
1) a += b # a = a + b → 12
print(a) =12
2) a -= b #a=a-b→8
print(a) =8
3) a *= b # a = a * b → 20
print(a) = 20
4) a /= b # a = a / b → 5.0
print(a) 5.0
5) a %= b #a=a%b→0
print(a) =0
6) a //= b # a = a // b → 5
print(a) =5
7) a **= b # a = a ** b → 100
print(a) =100
Logical Operators
a = 10 or 5
b=5
1) And print(a> 5 and b < 5) True
print(a> 10 and b < 5) False
# both are same for true
2) Or print(a> 5 or b < 5) True
print(a> 10 or b> 5) False
# No need both are same
3) Not a=10
print(a>5) True
print(not(a>5)) False
Identity Operators
a = 10
b = 10
1) Is
print(a is b) True
print(a is not b) False
2) a = 10
b=5
is not
print(a is b) True
print(a is not b) False
Membership Operators
a = [2, 4, 6]
1) in # in for use value is present the list
print(2 in a) True
print(3 not in a) False
2) not in
print(2 not in a) False
# Opposite
Punctuation in Python
Common punctuation symbols include:
(), [], {}, :, ., ,, ;, @, =, #, \, etc.
Short answer Question
Q1) Q1: Who developed Python and what inspired its name?
Ans) - Python was developed by Guido van Rossum.
- The name was inspired by the British comedy group Monty Python, specifically their
show "Monty Python’s Flying Circus."
Q2: What is the role of indentation in Python?
Ans) - In Python, indentation is mandatory for defining code blocks.
- It determines the structure of loops, conditionals, functions, and classes.
- Unlike other languages, indentation isn’t just for readability—it’s part of the syntax.
Q3) Q3: Difference between Interactive Mode and Script Mode in Python?
Ans) - Interactive Mode: You enter and execute code line-by-line directly in the interpreter.
Great for quick tests.
- Script Mode: You write code in a file and run the entire script at once. Ideal for larger
programs.
Q4: What is IDLE and its Main Purpose?
Ans) IDLE stands for Integrated Development and Learning Environment. It’s the default IDE
that comes bundled with Python.
Main Purposes:
Easy Python development
- Learning Python interactively
- Organizing and managing code
- Simple yet feature-rich (includes syntax highlighting, code completion, and a debugger)
Q5: Does Python Support Dynamic Typing?
Answer: True.
Python is a dynamically typed language, meaning:
- You don’t need to declare variable types explicitly.
- The type is determined at runtime based on the assigned value.
Example:
python
x = 10 # x is an integer
x = "Hello" # x is now a string
Long Answer Questions
Q1) Explain what you understand by typecasting in Python with the help of examples?
Ans) Typecasting (or type conversion) refers to changing a value from one data type to
another. This is useful when:
- Performing operations that require specific types
- Manipulating data in a format suitable for the task
1. Implicit Type Conversion (Coercion)
- Done automatically by Python when mixing types
- Example:
num_int = 210
num_float = 5.5
result = num_int + num_float
print(f"Result: {result}, Type: {type(result)}")
Output will be a float because Python converts num_int to float during the operation.
2. Explicit Type Conversion (Manual Casting)
- Done by the programmer using built-in functions:
- int(), float(), str(), list(), tuple(), etc.
- Gives full control over how data is converted
Example: Converting an integer to a float
integer_value = 25
float_value = float(integer_value)
print(f"Explicit int to float: {float_value}, Type: {type(float_value)}")
- This shows how to manually convert data types using built-in functions like float(), int(),
str(), etc.
Q2) ‘Comments are an easy way to enhance readability and understandability of a program’
Which operator is used to write a comment in Python? Elaborate with examples.
Comments in Python - Operator Used: # (hash symbol)
- Single-line Comment:
x = 10 # This comment explains the variable assignment
print(x) # This explains the print statement
- Multi-line Comments:
Python doesn’t have a dedicated multi-line comment syntax like /* */ in other languages.
Instead, you use multiple # lines:
# This is line one
# This is line two
# This is line three
- Docstrings:
You can also use triple quotes (""") inside functions or classes to describe their purpose:
python
def some_function():
"""This is a docstring that serves as a multi-line comment."""
Pass
Q3) What are different arithmetic operators used in Python? Write their precedence too.
Ans) Arithmetic Operators in Python
Operator Function Example Result
`Addition`5 + 3 = 8
Subtraction`10 – 4 = 6
`Multiplication`6 * 2 = 12
Division`7 / 2 = 3.5
%` Modulo`7 % 21
**` Exponentiation`2 ** 3 8---
Q4) What are the key features of Python?
Ans) Key Features of Python
Python stands out for its simplicity and versatility. Here are its core features:
- Easy-to-learn syntax: Beginner-friendly and readable.
- Interpreted: Executes code line-by-line without compilation.
- Dynamically Typed: No need to declare variable types explicitly.
- Object-Oriented Programming: Supports classes and objects.
- Large Standard Library: Rich set of built-in modules and functions.
- Cross-platform Compatibility: Runs on Windows, macOS, Linux, etc.
- Open Source & Free: Community-driven and freely available.
Q5) Write a Python program:
1) To accept two integers and print their sum.
2) To input a student’s marks in three subjects (out of 100) and print their percentage.
3) To calculate simple intrest.
Formula:
Simple Interest = (Principal × Years × Rate) / 100
4) To input the temperature in Celsius and convert it into Fahrenheit using the
Formula (F = C*9/5 + 32)
Ans) Formula: Fahrenheit = (Celsius × 9/5) + 32
5) To calculate and display the selling price of an item by inputting the cost price and
profit.
Ans) Program: Calculate Profit or Loss
Logic:
- If Selling Price> Cost Price: It's a Profit
- If Selling Price < Cost Price: It's a Loss
- If both are equal: No profit or loss
6) Write a tipper program where the user inputs the total restaurant bill. The program
should then display two amount : 15 percent tip and 20 percent tip.
Ans)