Python Fundamentals – Class XI – CBSE

Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape
Breadcrumb Abstract Shape

Python Fundamentals

Introduction:

Python has become the most popular programming language for Machine learning and Artificial Intelligence

Program is needed in all the places where you want to transform an input to an output. 

IPO – Input, Process, Output.

Input Process Output
What is a Program?

A Program is a set of instructions that governs the process of converting the input to output.

Python Character Set
  • The character set is a set of valid characters that a language can recognize.
  • Python ➡️ Unicode Encoding standard
  • Unicode encoding standard ➡️ Unique number for a character or symbol
  • All components of a Python Program: Statement, Expression, and other components are created using the Character set.

Tokens:

The smallest individual units in a program are called tokens or lexical units.

Tokens in Python:
  1. Keywords
  2. Identifiers
  3. Literals
  4. Operators
  5. Punctuators

Keywords:

  • Keywords are the reserved words that have a special meaning in a programming language.
  • They cannot be used as normal identifiers.
  • There are a total of 36 keywords in Python.

Identifiers:

Identifiers are the names used to identify variables, functions, objects, classes, lists, and dictionaries. It is a sequence of letters and numbers. 

Rules for identifiers:

Can’s:

  • The first Character must be a letter, Underscore ( _ ) counts as a letter.
  • Case Sensitive – Therefore, Upper and lower-case letters are different.
  • It can be unlimited in length
  • It can have numbers from 0 – 9

Valid Identifiers: Myfile, files_d, _files, file14

Cannot’s:

  • Cannot begin with numbers
  • It must not be a keyword.
  • It cannot contain any special character.

Invalid Identifiers: 1file, for, file.one, file@space

Literals:

Literals are data items that have fixed values.

  • String Literals
  • Numeric Literals
  • Boolean Literals
  • Special Literals
  • Literal Collection
1. String Literals:

String literals are the text enclosed within single/double quotes in python.

E.g. : ‘a’, “a”, ‘abc’, “abc”

String Types in Python: 

  1. Single Line Strings – Strings are enclosed within single/double quotes but terminate in a single line. Eg: a=”This is beautiful”
  2. Multi Line Strings – Strings that do not terminate in a single line. To create a multiline string:

Eg:

Type1: Add a backslash at the end of the first line.

a=”This is\
beautiful”

Type2: Add text within triple quotes.(Either 3 single quotes or 3 double quotes)

a=””” This is                      
beautiful”””

Or

a=”’This is 
     beautiful”’

Size of Strings:

  • Count of characters in the string.
  • An Escape sequence is also counted as one character.
  • The EOL character at the end of the line is also counted in a multiline string with triple quotes.
  • \ – Backslash at the end of the line is not counted in a multiline string with single/double quotes
  • To check the size of the string – len(<string name>)

E.g. :

  1. “\\”- 1 (Escape sequence)
  2. “a\n”- 2 ( a is one character and \n is another character. Totally:2)
  3. “He’s” – 4 
  4. print(len(”’I ↵
    have a↵
    dog”’)) – 17 (including the return key and spaces symbol)
  1. print(len(”’I \
     have a\
    dog ”’))- 15 (without including backslash symbol)
Try Me: Find the length of your name using Python.
Google Colab is an extarordinary platform to practice Python. 

2. Numeric Literals:
  • Integer Literals – Whole numbers without any fractional part

    • Decimal Integer Literals – Sequence of digits without having 0 in the beginning. E.g. : 4578, +457, -45

    • Octal Integer Literals: Sequence of digits joining with 0o. E.g.: 0o15, 0o24. It cannot have the digits 8 and 9. Therefore, 0o458, and 0o89 are invalid.

  • Floating Point Literals –  Numbers having fractional part

      • Fractional form – signed or unsigned digits with a decimal point between digits. E.g. : 2.0, +1.75, -8.5                                                                                                 Invalid: 2.45.7, 78/5, 2,65, 17

      • Exponent form – E.g. : 0.58E01 (Mantissa – 0.58 , Exponent – 1, E01 – 10^1), where 0.58E01 =  0.58 X 10^1 = 5.8

3. Boolean Literals : True or False
4. Special Literal :

None : It is used to indicate the absence of a value or no useful information.

Operators:

  • Operators perform operations on operands. 
  • Operands – Variables and Objects
Types of Operators:
Unary Operators:  Requires one operand to operate upon
+Unary Plus
Unary Minus
~Bitwise Complement
notLogical negation

Binary Operators: Requires two operands to operate upon

Arithmetic Operators:
+Addition
Subtraction
*Multiplication
/Division
%Remainder
**Exponent
//Floor Division
Bitwise Operators:
&Bitwise AND
^Bitwise exclusive OR
|Bitwise OR
Shift Operators:
<<Shift Left
>>Shift Right
Identity Operators:
isIs the identity the same?
is notIs the identity not the same?
Relational Operators:
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
==Equal to
!=Not equal to
Assignment Operators:
=Assignment
/=Assign Quotient
+=Assign Sum
*=Assign Product
%=Assign Remainder
-=Assign Difference
**=Assign Exponent
//=Assign Floor Division
.Logical Operators
andLogical AND
orLogical OR
Membership Operators
inWhether variable in sequence
not inWhether variable not in sequence

Punctuators

Punctuators are the symbols that are used in programming languages to organize programming sentence structures.

Common Punctuators: ‘ “ # \ ( ) { } @ , : . ` =

Barebones of a Python Program:

1. Expressions – Legal combination of symbols that python evaluates and produces a value. E.g. : b = a+5

2. Statements – A unit of programming instruction that performs any task.  E.g. : print(“Hello”)

3. Comments – Comments are lines of text within the code that are not executed by the interpreter. They are additional readable information.       E.g. : b = “Hello”  #The data type of variable b is string.

4. Functions – A function is a reusable code that has a name. It can be reused by calling its name. 

     E.g.:

 def name():

           print(“Maya”)

5. Blocks and Indentation – A block is a group of statements that logically belong together.

Indentation is the number of spaces (or tabs) at the beginning of a line of code. Python uses indentation (whitespace at the beginning of a line) to define the boundaries of blocks.

Eg:

if b<5:

       print(“B is lesser than 5”)

else:

       print(“B is greater than 5”)

Variables and Assignment in Python:

Variables: Variables are named labels that store data or values that can be used, modified, and processed during the execution of a program.

Eg:

name=”Keerthana”

b=60

c=3.4

d=True

LValue: Expression that comes to the LHS(Left Hand Side) of the Assignment Statement.

RValue: Expression that comes to the RHS(Right Hand Side) of the Assignment Statement.

LValues are the variables while Rvalues are the values assigned to the variable.

LValues from the Example: name, b, c, d

RValues from the Example: “Keerthana”, 60, 3.4, True

Multiple Assignments:
  • Assigning same value to multiple variables:

Same value can be assigned to multiple variables in a single statement.

Eg: a=b=c=20

The value of a, b and c will be assigned as 20.

  • Assigning multiple values to multiple variables:

Multiple variables can be assigned with a value in a single statement. The values will be assigned order wise.

Eg:

a, b, c = 5, 10, 15

b, c, a= a+1, b+2, c-1

Chain Assignment in Python

Variable definition in Python:

A variable is created only when you assign a value to it. 

print(x)

This statement will produce an error – “ name x not defined ”

x=0 #This is the right way to define a variable

print(x) 

Even if you do not want any value for the variable in the beginning, just assign its value as 0 and then change the value of the variable.

x=0 

print(x) 

x=5 #The value of x changes to 5.

print(x)

It is not that while changing the value of the variable we should have the same data type as the previous one. We can also assign a value with different data types. This feature of not raising errors even when the data type changes is dynamic typing.

E.g. : 

x=10

x=”Megha”

Here the integer type changes to string type. It does not create any error.

To find the type of data you can use:

x=”Hello”

type(x)

<class ‘str’>

Simple input and output method in Python:

input() – This function interactively gets input from the user. The system stores user input as a string data type by default. 

Syntax:

variable_name=input(“prompt”)

variable_name=int(input(“prompt”)) #Converts the user input to integer

E.g. :

>>>name=input(“What is your name?”)

What is your name? Megha

Output – print():

The print() function in python is used to send the output to the output devices like monitors.

Syntax:

print(“statement”) .

E.g. :

print(“Python is a kid-friendly programming Language.”)

a=5

print(“My favourite number is “,a)

Output:

Python is a kid-friendly programming Language.

My favourite number is 5

Features of print()  Function:
1. It auto converts the items to strings and prints it. 

E.g. : 

print(5+6)

Output:

11

2. The default value of the sep() argument is space(‘ ’). The print() function automatically inserts spaces between items when no specific value for sep() is given.

Eg 1:

print(“My name” , “is” , “John”)

Output:

My name is John                           # It inserts space between items automatically when no sep() value is given.

Eg 2:

print(“My name” , “is” , “John”, sep=’+++’)

Output:

My name +++ is +++ John             # Inserts +++ in between items as sep() value is given as +++

3. It inserts a new line character at the end of every statement unless you provide the end argument. 

Eg 1 : 

print(“My name is John.”)

print(“I am 10 years old.”)

Output:

My name is John.

I am 10 years old.

In the above output, it automatically added a new line character at the end of each line.

Eg 2 :

print(“My name is John”,end=”@”)

print(“I am 10 years old”)

Output: My name is John @ I am 10 years old      # Output when the end argument is given.

 

× We're here to help!