Friday, 7 February 2025

MCQs Of Day 2 – Python Syntax and Variables

MCQs Of Day 2 – Python Syntax and Variables


Assignments Of Day 2 – Python Syntax and Variables

  Assignments Of Day 2 – Python Syntax and Variables


Lecture Notes Of Day 2 – Python Syntax and Variables


 Lecture Notes Of Day 2 
Python Syntax and Variables


Objective

  • Understand Python’s syntax, including indentation.
  • Learn about variables and their basic types in Python.

Outcome

By the end of this session, students will:

  • Understand the rules of Python syntax and indentation.
  • Be able to create and use variables of various types.

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

  • Python does not require you to declare the type of a variable explicitly.
  • The type is inferred based on the value assigned.

Reassigning Variables

  • Variables can be reassigned to a different type at any time.
  • Example:

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:

1.   Integer (int)

o    Represents whole numbers.

o    Example:

age = 25

print(age)  # Output: 25

2.   Float (float)

o    Represents numbers with decimal points.

o    Example:

price = 19.99

print(price)  # Output: 19.99

3.   String (str)

o    Represents a sequence of characters.

o    Strings are enclosed in either single or double quotes.

o    Example:

name = "Alice"

print(name)  # Output: Alice

4.   Boolean (bool)

o    Represents True or False.

o    Example:

is_python_fun = True

print(is_python_fun)  # Output: True

5.   None Type (None)

o    Represents the absence of a value.

o    Example:

x = None

print(x)  # Output: None


4. Printing Variables

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

1.   Checking the Type of a Variable

Use the type() function to check the type of a variable.
Example:

x = 10

print(type(x))  # Output: <class 'int'>

2.   Type Conversion

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

  • Python syntax emphasizes readability and uses indentation to define code blocks.
  • Variables in Python are dynamically typed, meaning they do not require explicit declaration of data types.
  • Python supports basic data types such as int, float, str, bool, and None.
  • The print() function and type() function are essential for working with variables and understanding their types.


Search This Blog

Powered by Blogger.

MCQs Of Day 2 – Python Syntax and Variables

MCQs Of Day 2 – Python Syntax and Variables

Name*


Message*