Python Fundamentals Class 11 Notes | Getting Started with Python

latest Python Fundamentals Class 11 Notes designed for CBSE Computer Science student covering all concepts concisely to get full marks. This Post cum notes is specially covers Chapter 5 Getting started with Python of NCERT Computer Science Class 11 as per CBSE suggested syllabus.

Introduction to Python

Python is a General Purpose high level Programming language used for developing application softwares.

Features of Python

  • High Level
  • Free and Open Source
  • Case Sensitive
  • Interpreted
  • Plateform Independent
  • Rich library of functions
  • Dynamic
  • General Purpose

Important Programming Terminologies

Instructions : A single step written to carry out an action

Programs: Set of Instructions

Software: Set of Programs

Source Code: A program written in High Level Language

Machine Code: set of instructions in form of binary language (0 and 1) which is understood by computer

Compiler: program which converts code written in high level language into machine language

Interpreter: it is also a program which converts code written in high level language into machine language. An interpreter processes the instructions one by one.

Working with Python

To write and run python programs we need Python Interpreter also called Python IDLE.

Execution Mode

We can use Python Interpreter in two ways:

  • Interactive mode
  • Script mode
Interactive Mode
  • Instant execution of individual statement
  • Convenient for testing single line of code
  • We cannot save statements for future use
Script Mode
  • Allows us to write and execute more than one Instruction together.
  • We can save programs (python script) for future use
  • Python scripts are saved as file with extension “.py”

How to execute or run python program

  • Open python IDLE
  • Click ‘File’ and select ‘New’ to open Script mode
  • Type source code and save it
  • Click on ‘Run’ menu and select ‘Run Module’

Python Keywords

  • These are predefined words which a specific meaning to Python Interpreter.
  • These are reserve keywords
  • Keywords in python are case sensitive

Identifier

Identifiers are name used to identify a variable, function or any other entities in a programs.

Rules for naming Identifier

  • The name should begin with an alphabet or and underscore sign and can be followed by any combination of charaters a-z, A-Z, 0-9 or underscore.
  • It can be of any length but we should keep it simple, short and meaningful.
  • it should not be a python keyword or reserved word.
  • We cannot use special symbols like !, @, #, $, % etc. in identifiers.

Variables

  • It can be referred as an object or element that occupies memory space which can contain a value.
  • Value of variable can be numeric, alphanumeric or combination of both.
  • In python assignment statement is used to create variable and assign values to it.

Data types

These are keywords which determine the type of data stored in a variable. Following table show data types used in python:

Comments

  • These are statement ignored by python interpreter during execution.
  • It is used add a remark or note in the source code.
  • It starts with # (hash sign) in python.

Operators

These are special symbols used to perform specific operation on values. Different types of operator supported in python are given below:

  • Arithmetic operator
  • Relational operator
  • Assignment operator
  • Logical operator
  • Identity operator
  • Membership operator
Arithmetic Operator
Relational Operator
Assignment Operator
Logical Operator
Identity Operator
Membership Operator

Operator Precedence

Expressions

  • An expression is combination of different variables, operators and constant which is always evaluated to a value.
  • A value or a standalone variable is also considered as an expression.

Examples:

  • 200
  • 4.0 + 6.22
  • Var
  • Var + 13
  • 41 + 3*11 – 77/7
  • “techtipnow” + “computers”
Evaluation of Expression

Example1:

23 + 12 + 18

Evaluation:

= (23 + 12) + 18                  #step1
= 35 + 18                            #step2
= 53                                    #step3

Example 2:

56 + (23-13) + 89%8 – 2*3

Evaluation:

= 56 + (23 -13) + 89%8 – 2*3        #step1
= 56 + 10 + (89%8) – 2*3              #step2
= 56 + 10 + 1 – (2*3)                     #step3
= (56 + 10) + 1 – 6                         #step4
= (66 + 1) – 6                                 #step5
= 67 – 6                                         #step6
= 61

Statement

A statement is unit of code that the python interpreter can execute.

Example:

  • var1 = var2                                          #assignment statement
  • x = input (“enter a number”)      #input statement
  • print (“total = “, R)                           #output statement

How to input values in python?

In python we have input() function for taking user input.

Syntax:

Input([primpt])

How to display output in python

In python we have print() function to display output.

Syntax:

print([message/value])

Example:

python program to input and output your name

var = input(“Enter your name”)
print(“Name you have entered is “, var)

Example: Addition of two numbers

Var1 = int(input(“enter no1”))
Var2 = int(input(“enter no2”))
Total = Var1 + Var2
Print(“Total = “, Total)

Example: Program to convert degree Celsius into Fahrenheit

C = float (“Enter degree in Celsius”))
F = (9*C) /5 + 32
print (“Degree in Fahrenheit = , F)

Type Conversion

  • Type conversion refers to converting one type of data to another type.
  • Type conversion can happen in two ways:
    • Explicit conversion
    • Implicit conversion
Explicit Conversion
  • Explicit conversion also refers to type casting.
  • In explicit conversion, data type conversion is forced by programmer in program

Syntax:

(new_data_type) = (expression)

Example: Program of explicit type conversion from float to int

x = 12
y = 5
print(x/y)                            #output – 2.4
print(int(x/y))                    #output – 2

program of explicit type conversion from string to int

x = input(“enter a number”)
print(x+2)                                            #output – produce error “can only concatenate str to str
x = int(input(Enter a number”))                               
print(x+2)                                            #output – will display addition of value of x and 2

program of explicit type conversion from string to float

x = ’10.2’
y = ’12.3’
r = x+y
print(r)                                                 #output – 10.212.3
r = float(x) + float(y)
print(r)                                                 #output – 22.5

Implicit conversion
  • Implicit conversion is also known as coercion
  • In implicit conversion data type conversion is done automatically.
  • Implicit conversion allows conversion from smaller data type to wider size data type without any loss of information

Example: Program to show implicit conversion from int to float

var1 = 10                              #var1 is integer
var2 = 3.4                             #var2 is float
res  = var1 – var2              #res becomes float automatically after subtraction
print(res)                             #output – 6.6
print(type(res))                     #output – class ‘Float’

Debugging

Process of identifying and removing errors (also called bugs) produced in program is called debugging.

Errors

Error refers to mistake that occurs while writing a program and due to which program may not execute or may not generate desired output.

Types of error

Errors occurring in programs can be categorized as :

  • Syntax error
  • Logical error
  • Runtime error
Syntax Errors
  • Syntax errors refers to writing code against programming rules of python such as using wrong function name, wrong indentation, wrong expressions etc.
  • Program can not be executed until it is syntactically incorrect

Examples:

  • (7 + 11                                         #absence of right bracket           
  • X = inpt(“enter a number”)         #wrong function name
  • If x>y:
    print(x)                                        #wrong indentation
Logical Error

Logical errors refers to

  • Incorrect execution of program
  • Not producing desired output

Example:

If x==’a’ or ‘e’ or ‘i’ or ‘o’ or ‘u’:                 #line1 : Logical Error
     print(“Vowel”)                                                
Else:
    Print(“Consonant”)

#line1 : is logically written wrong as the program will only display ‘a’ as vowel and rest will be displayed as consonant, because they will not be compared. It should be written as:

If x==’a’ or x==’e’ or x==’i’ or x==’o’ or x==’u’:
     print(“Vowel”)                                                   
Else:
    Print(“Consonant”)

Runtime Error

Runtime Error causes abrupt termination of program during its execution. It appears during execution of program only.

Example: “Division by zero”

x = int(input(“Enter Dividend”))
y = int(input(“Enter Divisor”))
print(x/y)                                            #if user enter 0 in y, it generate runtime error and program is terminated abruptly

Leave a Comment

Your email address will not be published. Required fields are marked *

error: Content is protected !!