MCQs Of Day 2 – Python Syntax and Variables
MCQs Of Day 2 – Python Syntax and Variables
Assignments Of Day 2 – Python Syntax and Variables
Objective
Outcome
By the end of this session,
students will:
1. Python
Syntax
Python syntax refers to the set
of rules that define the structure of a Python program. Unlike other
programming languages, Python is designed to be highly readable.
Key
Features of Python Syntax:
1. Case
Sensitivity
o
Python is case-sensitive. For example, myVar and myvar
are treated as different variables.
2. No
Semicolons
o
Unlike languages like C or Java, Python does not
require semicolons to terminate statements.
o
Example:
print("Hello, World!") # No semicolon needed.
3. Whitespace
and Indentation
o
Python uses indentation (spaces or tabs) to define
code blocks instead of curly braces {}.
o
A consistent level of indentation is mandatory.
o
Example:
if True:
print("Indented block starts
here.")
4. Comments
in Python
o
Single-line comments: Use the # symbol.
Example:
# This is a single-line comment.
print("Python
comments start with #")
o
Multi-line comments: Use triple quotes (''' or """).
Example:
"""
This is a
multi-line
comment.
"""
2.
Variables in Python
Variables are used to store data
in Python. You can think of a variable as a container for storing values.
Rules for
Defining Variables
1. Variable
names must start with a letter or an underscore (_).
Example: _myVariable, myVariable
2. Variable
names cannot start with a number.
Example: 2variable (invalid)
3. Variable
names can only contain alphanumeric characters and underscores (A-Z, a-z, 0-9, _).
Example: my_var, var123
4. Variable
names are case-sensitive.
Example: name and Name are different variables.
Variable
Declaration and Initialization
Variables in Python are created
when you assign a value to them.
Example:
x = 10 # Integer
y =
3.14 # Float
name =
"John" # String
Dynamic
Typing
Reassigning
Variables
x = 10 # Initially an integer
x =
"Hello" # Now a string
print(x) # Output: Hello
3. Basic
Variable Types
Python has several built-in
types, including:
o
Represents whole numbers.
o
Example:
age = 25
print(age) # Output: 25
o
Represents numbers with decimal points.
o
Example:
price = 19.99
print(price) # Output: 19.99
o
Represents a sequence of characters.
o
Strings are enclosed in either single or double
quotes.
o
Example:
name = "Alice"
print(name) # Output: Alice
o
Represents True or False.
o
Example:
is_python_fun = True
print(is_python_fun) # Output: True
o
Represents the absence of a value.
o
Example:
x = None
print(x) # Output: None
The print() function is used to
output variables.
Example:
name = "Alice"
age = 25
height = 5.5
print("Name:", name)
print("Age:",
age)
print("Height:",
height)
Output:
Name: Alice
Age:
25
Height:
5.5
5. Type
Checking and Conversion
Use the type() function to check the type of a variable.
Example:
x = 10
print(type(x)) # Output: <class 'int'>
Python allows you to convert variables from one type to another.
o Example:
x =
5 # Integer
y =
str(x) # Convert to string
print(y) # Output: "5"
6. Best
Practices for Using Variables
1. Use
meaningful variable names.
o
Instead of x, use age, name, etc.
2. Avoid
using Python reserved keywords as variable names (e.g., if, else, True).
3. Keep
variable names concise but descriptive.
Exercise
1. Create
variables of different types:
o
Integer
o
Float
o
String
2. Print
their values and types using the print() and type() functions.
Example Solution:
# Creating variables
age =
20 # Integer
price =
15.75 # Float
name =
"John Doe" # String
# Printing
variables and their types
print("Age:",
age, "Type:", type(age))
print("Price:",
price, "Type:", type(price))
print("Name:",
name, "Type:", type(name))
Expected Output:
Age: 20
Type: <class 'int'>
Price:
15.75 Type: <class 'float'>
Name:
John Doe Type: <class 'str'>
Summary
MCQs Of Day 2 – Python Syntax and Variables