0% found this document useful (0 votes)
2 views10 pages

unit 1 b)

The document provides an overview of Python, highlighting its features, data types, and modes of execution. It covers fundamental concepts such as variables, expressions, statements, and various operators, along with examples for clarity. Additionally, it explains functions and modules, emphasizing their importance in code reusability and organization.

Uploaded by

kritikanegi.011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

unit 1 b)

The document provides an overview of Python, highlighting its features, data types, and modes of execution. It covers fundamental concepts such as variables, expressions, statements, and various operators, along with examples for clarity. Additionally, it explains functions and modules, emphasizing their importance in code reusability and organization.

Uploaded by

kritikanegi.011
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

DATA, EXPRESSIONS, AND STATEMENTS IN PYTHON

1. Introduction to Python

Python is a high-level, interpreted, object-oriented, and dynamic programming language. It


is widely used for web development, data analysis, artificial intelligence, machine learning,
and automation.

Key Features of Python:

• Easy to Learn – Simple syntax similar to English.

• Interpreted Language – Python executes code line by line (no compilation required).

• Object-Oriented – Supports classes and objects for better code organization.

• Cross-Platform – Works on Windows, Linux, and macOS without modification.

• Extensive Libraries – Provides built-in modules and third-party packages.

2. Python Interpreter

The Python Interpreter is responsible for executing Python code. It works in two modes:

Two Modes of Execution:

1. Immediate Mode (Interactive Mode)

o Type commands directly in the Python Shell (>>>), and it executes


immediately.

o Example:

o >>> print("Hello, Python!")

o Hello, Python!

2. Script Mode

o Write Python programs in a file (.py) and execute them using the interpreter.

o Example: Save the following in script.py and run it using python script.py

o print("Hello, Python!")

3. Values and Data Types

What is a Value?
• A value is any piece of data that a program manipulates.

• Example:

• 42 # Integer value

• 3.14 # Floating-point value

• "Python" # String value

Standard Data Types in Python:

Data Type Description Example

Integer (int), Floating-point (float), Complex


Numbers 10, 3.14, 2+3j
(complex)

Strings Sequence of characters enclosed in quotes "Hello, World!"

Lists Ordered, mutable collection [1, 2, "Python", 3.5]

Tuples Ordered, immutable collection (10, 20, "AI")

{"name": "Alice", "age":


Dictionaries Key-value pairs
25}

4. Python Numbers

Python supports four types of numbers:

1. Integer (int) – Whole numbers

2. a = 10

3. Float (float) – Decimal numbers

4. b = 3.14

5. Complex (complex) – Numbers with imaginary parts

6. c = 2 + 3j

7. Boolean (bool) – True (1) or False (0)

8. d = True

5. Python Strings

Strings are sequences of characters enclosed in single, double, or triple quotes.


String Operations:

• Indexing: Access characters by position (0 for first, -1 for last).

• Concatenation (+) – Combine strings.

• Repetition (*) – Repeat a string multiple times.

Example:

str1 = "Hello"

str2 = "Python"

print(str1 + " " + str2) # Concatenation

print(str1 * 3) # Repetition

Output:

Hello Python

HelloHelloHello

6. Lists in Python

• Lists are ordered, mutable collections.

• Lists can contain different data types.

List Operations:

Method Description

append(x) Adds item x to the end

insert(i, x) Inserts item x at index i

remove(x) Removes first occurrence of x

pop(i) Removes item at index i

sort() Sorts the list

Example:

fruits = ["apple", "banana", "cherry"]

fruits.append("orange")

print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']


7. Tuples in Python

• Tuples are ordered, immutable collections.

• Used for fixed data (e.g., coordinates, RGB color values).

Example:

coordinates = (10, 20)

print(coordinates[0]) # Output: 10

8. Dictionaries in Python

• Dictionaries store data as key-value pairs.

• Keys must be unique, values can be repeated.

Example:

person = {"name": "Alice", "age": 25, "city": "New York"}

print(person["name"]) # Output: Alice

9. Variables in Python

• A variable is a named reference to a value.

• Variables are assigned using =.

Example:

x = 10

name = "Alice"

print(x, name)

Rules for Naming Variables:

Must start with a letter (a-z, A-Z) or _


Can contain letters, digits, and _
Cannot start with a digit
Cannot use Python keywords

Example:

_valid_name = 10 # Valid
2name = "Alice" # Invalid (Cannot start with a digit)

10. Keywords and Identifiers

• Keywords are reserved words (e.g., if, while, return).

• Identifiers are names for variables, functions, classes.

Example:

import keyword

print(keyword.kwlist) # Displays all Python keywords

11. Expressions and Statements

What is an Expression?

• A combination of values, variables, and operators.

• Produces a result.

Example:

result = 10 + 5 # Expression

What is a Statement?

• An instruction that Python executes.

• Does not return a result.

Example:

x = 5 # Assignment statement

if x > 3: # Conditional statement

print("Hello")

12. Operators in Python

Types of Operators:

1. Arithmetic Operators (+, -, *, /, %, **)

2. Comparison Operators (==, !=, >, <, >=, <=)

3. Logical Operators (and, or, not)


4. Bitwise Operators (&, |, ^, >>, <<)

5. Assignment Operators (=, +=, -=, *=, /=)

6. Membership Operators (in, not in)

7. Identity Operators (is, is not)

Example:

x=5

y = 10

print(x > y) # False

print(x in [1, 2, 5]) # True

13. Identity and Membership Operators

• Identity Operators (is, is not) – Check if two variables refer to the same object.

• Membership Operators (in, not in) – Check if a value exists in a sequence.

Example:

x = [1, 2, 3]

y=x

print(x is y) # True

print(2 in x) # True

print(5 not in x) # True


1. Explain the Data Types in Python

Python provides several built-in data types to store different kinds of values.

Data Type Description Example

Integer (int), Floating-point (float), Complex


Numbers 10, 3.14, 2+3j
(complex)

Strings Sequence of characters enclosed in quotes "Hello, Python!"

Lists Ordered, mutable collection [1, 2, "Python", 3.5]

Tuples Ordered, immutable collection (10, 20, "AI")

{"name": "Alice", "age":


Dictionaries Key-value pairs
25}

Sets Unordered collection of unique items {1, 2, 3, 4}

Boolean Represents True or False True, False

Example:

x = 10 # Integer

y = 3.14 # Float

z = "Hello" # String

my_list = [1, 2, 3] # List

my_tuple = (10, 20) # Tuple

my_dict = {"name": "Alice", "age": 25} # Dictionary

my_set = {1, 2, 3, 4} # Set

is_valid = True # Boolean

2. Illustrate Interpreter and Interactive Mode in Python with Example

Python provides two modes of execution:

1. Interactive Mode (Immediate Mode)

• The Python interpreter executes commands line by line.

• You type a command, and Python immediately executes it.


Example:

>>> print("Hello, Python!")

Hello, Python!

>>> 2 + 3

2. Script Mode

• You write Python code in a file (.py) and run it.

• Used for larger programs.

Example (script.py):

# Save this in script.py

print("Hello, Python!")

Run using:

python script.py

Output:

Hello, Python!

3. Explain Function and Module with Suitable Example

Function in Python

• A function is a reusable block of code that performs a specific task.

• Helps in code reusability and modularity.

Example:

def greet(name):

print(f"Hello, {name}!")

greet("Alice") # Output: Hello, Alice!

Module in Python

• A module is a file containing Python code (functions, classes, variables).

• It allows us to reuse code across multiple programs.


Example: Creating a module (mymodule.py):

def add(a, b):

return a + b

Using the module in another script:

import mymodule

print(mymodule.add(5, 3)) # Output: 8

4. Write Short Notes on Types of Operators in Python with Appropriate Example

Python provides several types of operators:

Operator Type Description Example

Arithmetic Operators Perform mathematical operations +, -, *, /, %, **, //

Comparison Operators Compare two values ==, !=, >, <, >=, <=

Logical Operators Combine multiple conditions and, or, not

Bitwise Operators Perform bit-level operations `&,

Assignment Operators Assign values to variables =, +=, -=, *=, /=

Membership Operators Check if value exists in sequence in, not in

Identity Operators Check if two variables reference the same object is, is not

Examples:

# Arithmetic Operators

a = 10

b=3

print(a + b) # Output: 13

print(a ** b) # Output: 1000 (Exponentiation)

# Comparison Operators

print(a > b) # Output: True


# Logical Operators

x = True

y = False

print(x and y) # Output: False

# Membership Operator

print(2 in [1, 2, 3]) # Output: True

# Identity Operator

a = [1, 2, 3]

b=a

print(a is b) # Output: True

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy