Curriculum
Course: Ncert - Class 11- Computer Science
Login
Text lesson

Detailed Notes-2 -Chapter 5 -Getting Started with Python

COMMENTS

  1. Purpose:

    • Comments are used to add remarks or notes in the source code.
    • They are not executed by the interpreter but are included to make the source code easier for humans to understand.
    • Comments document the meaning, purpose, input, and output requirements of the source code, aiding in understanding its functionality.
  2. Usage:

    • Comments are particularly important in large and complex software projects, especially when multiple programmers are involved.
    • They help programmers, including those working in teams or maintaining code written by others, to understand how the program functions and how to use it effectively.
  3. Syntax:

    • In Python, comments start with the # (hash sign) character.
    • Everything following the # until the end of the line is treated as a comment by the interpreter, which ignores it during execution.
  4. Example (5.1):

    • Demonstrates the usage of comments in Python code.
    • Comments are used to explain the purpose of variables amount and totalMarks, providing context for their usage.
  5. Example Program (5-4):

    • Illustrates a Python program to find the sum of two numbers.
    • Comments are added to describe the purpose of variables num1 and num2, as well as the calculation of the result.

EVERYTHING IS AN OBJECT

  1. Object Representation:

    • In Python, every value or data item, whether numeric, string, or other types, is treated as an object.
    • Objects can be assigned to variables or passed to functions as arguments.
  2. Unique Identity:

    • Each object in Python is assigned a unique identity (ID), which remains constant throughout the object’s lifetime.
    • This ID serves as a reference akin to the memory address of the object.
  3. Function id():

    • The id() function in Python returns the identity of an object, which is essentially its unique identifier.
  4. Example (5.2):

    • Demonstrates the usage of the id() function in Python.
    • Variable num1 is assigned the value 20, and id(num1) returns its identity.
    • Variable num2 is assigned the expression 30 - 10, and id(num2) returns the same identity as num1, indicating that both variables refer to the same object, which is 20.

DATA TYPES

  1. Definition:

    • Every value in Python belongs to a specific data type, which identifies the type of data a variable can hold and the operations that can be performed on it.
  2. Numeric Data Types:

    • Numeric data types store numerical values and are classified into three types: intfloat, and complex.
    • int: Represents integer numbers, such as -12-301252.
    • float: Represents real or floating-point numbers, such as -2.044.014.23.
    • complex: Represents complex numbers, such as 3 + 4j2 - 2j.
  3. Boolean Data Type (bool):

    • A subtype of integer consisting of two constants: True and False.
    • True represents a non-zero, non-null, and non-empty value, while False represents the value zero.
  4. Example (5.3):

    • Demonstrates the usage of the type() function in Python to determine the data type of variables.
    • Variables are assigned values of different data types (intboolfloatcomplex), and the type() function is used to identify their respective data types.
  5. Complex Data Structures:

    • While simple data types like integers, floats, and booleans hold single values, Python provides data types like tuples, lists, dictionaries, and sets to hold collections of data.
    • These data types are useful for storing and manipulating larger sets of information, such as lists of names, phone numbers, or artifacts.

Sequence

  1. Sequence Data Types:
    • A Python sequence is an ordered collection of items, where each item is indexed by an integer.
    • There are three main types of sequence data types in Python: Strings, Lists, and Tuples.

String (A):

  • A string is a group of characters, including alphabets, digits, or special characters.
  • String values are enclosed in either single quotation marks (') or double quotation marks (").
  • Example
Python

str1 = ‘Hello Friend’ str2 = “452”

List (B):

  • A list is a sequence of items separated by commas and enclosed in square brackets ([]).
  • Lists can contain a mix of data types (e.g., integers, floats, strings).
  • Example
python

list1 = [53.4“New Delhi”“20C”45]

Tuple (C):

  • A tuple is similar to a list, but its elements are enclosed in parentheses (()).
  • Once created, the elements of a tuple cannot be changed (immutable).
  • Example
python

tuple1 = (1020“Apple”3.4‘a’)

Set Data Type:

  • A set is an unordered collection of items separated by commas and enclosed in curly braces ({}).
  • Sets cannot have duplicate entries, and once created, the elements of a set cannot be changed.
  • Example
python

set1 = {10203.14“New Delhi”} set2 = {1213# Duplicate elements are not included in the set

Understanding these data types in Python is crucial for effectively storing and manipulating collections of data in a program. Each data type has its own characteristics and use cases, providing flexibility and versatility in programming tasks.

None

None Data Type (5.7.4):

  • None is a special data type in Python that represents the absence of a value.
  • It has a single value, None, and supports no special operations.
  • Example
python

myVar = None print(type(myVar)) # Output: <class ‘NoneType’> print(myVar) # Output: None

 

 

×

Cart