0% found this document useful (0 votes)
19 views27 pages

Class 11 Python Fundamentals

The document covers fundamental concepts of Python programming, including character sets, input/output functions, indentation, tokens, keywords, identifiers, literals, operators, variables, dynamic typing, and constants. It explains how to use various Python features with examples, emphasizing the importance of syntax and conventions. Additionally, it highlights the differences between Python 2 and Python 3 regarding input functions and the use of constants.

Uploaded by

sahu292008
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)
19 views27 pages

Class 11 Python Fundamentals

The document covers fundamental concepts of Python programming, including character sets, input/output functions, indentation, tokens, keywords, identifiers, literals, operators, variables, dynamic typing, and constants. It explains how to use various Python features with examples, emphasizing the importance of syntax and conventions. Additionally, it highlights the differences between Python 2 and Python 3 regarding input functions and the use of constants.

Uploaded by

sahu292008
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/ 27

Python Fundamentals

Python Character Set A set of valid characters


recognized by python. Python uses the traditional ASCII character set. The
latest version recognizes the Unicode character set. The ASCII character set is
a subset of the Unicode character set.

Letters :– A-Z, a-z


Digits :– 0-9
Special symbols :– Special symbol available over keyboard [@, +,-, *, {}, (), ==, != etc
White spaces:– Blank space, tab, carriage return, new line, form feed .
Other characters:- Unicode (ASCII)

GDGS Azamgarh
Input and Output
var1=‘Computer Science’
var2=‘Informatics Practices' print(var1,' and ',var2,’ )
Output :- Computer Science and Informatics Practices raw_input() Function In Python
allows a user to give input to a program from a keyboard but in the form of string.
NOTE : raw_input() function is deprecated in python 3 e.g.
age = int(raw_input(‘enter your age’))
percentage = float(raw_input(‘enter percentage’))
input() Function In Python allows a user to give input to a program from a keyboard but returns the value accordingly.
e.g.
age = int(input(‘enter your age’))
C = age+2 #will not produce any error
NOTE : input() function always enter string value in python 3.so on need int(),float()
function can be used for data conversion.
GDGS Azamgarh
Indentation
Indentation refers to the spaces applied at the beginning of a code
line. In other programming languages the indentation in code is for
readability only, where as the indentation in Python is very important.
Python uses indentation to indicate a block of code or used in block of codes.
E.g.1
if 3 > 2:
print(“Three is greater than two!") //syntax error due to not indented

E.g.2
if 3 > 2:
print(“Three is greater than two!") //indented so no error
GDGS Azamgarh
Token
Smallest individual unit in a program is known as token.
1. Keywords
2. Identifiers
3. Literals
4. Operators
5. Punctuators/Delimiters

GDGS Azamgarh
Keywords
Reserve word of the compiler/interpreter which can’t be used as
identifier. False None True
And As assert
break class continue
def del elif
else except finally
for from global
if import in
is lambda nonlocal
or not Pass
raise return Try
while with yield
GDGS Azamgarh
Identifiers
A Python identifier is a name used to identify a variable, function, class,
module or other object.
* An identifier starts with a letter A to Z or a to z or an underscore (_) followed
by zero or more letters, underscores and digits (0 to 9).
• Python does not allow special characters
• * Identifier must not be a keyword of Python.
• * Python is a case sensitive programming language.
• Thus, Rollnumber and rollnumber are two different identifiers in Python.
Some valid identifiers : Mybook, file123, z2td, date_2, _no

GDGS Azamgarh
Identifiers-continue

Some additional naming conventions.


1. Class names start with an uppercase letter. All other identifiers start with a
lowercase letter.
2. Starting an identifier with a single leading underscore indicates that the identifier
is private.
3. Starting an identifier with two leading underscores indicates a strong private
identifier.
4. If the identifier also ends with two trailing underscores, the identifier is a
language-defined special name.

GDGS Azamgarh
Literals
Literals in Python can be defined as number, text, or other data that represent values to be stored
in variables.
Example of String Literals in Python
name = ‘Johni’ , fname =“johny”
Example of Integer Literals in Python(numeric literal)
= 22
Example of Float Literals in Python(numeric literal)
height = 6.2
Example of Special Literals in Python
name = None

GDGS Azamgarh
Literals
Escape sequence/Back slash character constants
\\ Backslash(\)
\’ Single quote(‘)
\” Double quote(“)
\a ASCII Bell (BEL)

GDGS Azamgarh
Operators
Operators can be defined as symbols that are used to perform operations on operands.
Types of Operators
1. Arithmetic Operators.
2. Relational Operators.
3. Assignment Operators.
4. Logical Operators.
5. Bitwise Operators
6. Membership Operators
7. Identity Operators

GDGS Azamgarh
Operators continue

GDGS Azamgarh
Operator continue
Arithmatic operator continue
e. g.
x=5 OUTPUT
y=4 ('x + y =', 9)
print('x + y =',x+y) ('x - y =', 1)
print('x - y =',x-y) ('x * y =', 20)
print('x * y =',x*y) ('x / y =‘, 1
print('x / y =',x/y) ) ('x // y =', 1)
print('x // y =',x//y) ('x ** y =', 625)
print('x ** y =',x**y)

GDGS Azamgarh
Operator continue
Arithmetic operator continue
# EMI Calculator program in Python def emi_calculator(p, r, t):
r = r / (12 * 100) # one month interest
t = t * 12 # one month period
emi = (p * r * pow(1 + r, t)) / (pow(1 + r, t) - 1)
return emi
# driver code
principal = 10000;
rate = 10;
time = 2;
emi = emi_calculator(principal, rate, time);
print("Monthly EMI is= ", emi)
GDGS Azamgarh
Operator continue
Arithmetic operator continue How to calculate GST GST ( Goods and Services Tax ) which is included in net
price of product for get GST % first need to calculate GST Amount by subtract original cost from Netprice and
then apply
GST % formula = (GST_Amount*100) / original_cost
# Python3 Program to compute GST from original and net prices.
def Calculate_GST(org_cost, N_price):
# return value after calculate GST%
return (((N_price - org_cost) * 100) / org_cost);
# Driver program to test above functions
org_cost = 100
N_price = 120
print("GST = ",end=‘’)
print(round(Calculate_GST(org_cost, N_price)),end=‘’)
print("%")
* Write a Python program to calculate the standard deviation
GDGS Azamgarh
Operators continue

GDGS Azamgarh
Operator continue
Comparison operators continue e.g.
x = 101
y = 121
print('x > y is',x>y)
print('x < y is',x=y)
print('x <= y is',x<=y)
Output
('x > y is', False)
('x < y is', True)
('x == y is', False)
('x != y is', True)
('x >= y is', False)
('x <= y is', True)
GDGS Azamgarh
Operator continue
3. Augmented Assignment Operators Used to assign values to the variables.

GDGS Azamgarh
Operator continue
4. Logical Operators
Logical Operators are used to perform logical operations on the given two variables or values.

a=30

b=20
if(a==30 and b==20):
print('hello’)
Output :- hello
GDGS Azamgarh
Operator continue
6. Membership Operators
The membership operators in Python are used to validate whether a value is found within
a sequence such as such as strings, lists, or tuples.
E.g. a = 22
list = [22,99,27,31]
In_Ans = a in list
NotIn_Ans = a not in list
print(In_Ans)
print(NotIn_Ans)
Output :-
True
False
GDGS Azamgarh
Operator continue

GDGS Azamgarh
Operator continue

GDGS Azamgarh
GDGS Azamgarh
Variables
Variable is a name given to a memory location. A variable can consider as a container
which holds value. Python is a type infer language that means you don't need to
specify the datatype of variable.
Python automatically get variable datatype depending upon the value assigned to the variable.
Assigning Values To Variable
name = ‘python' # String Data Type
sum = None # a variable without value
a = 23 # Integer
b = 6.2 # Float sum = a + b
print (sum)
Multiple Assignment: assign a single value to many variables
a = b = c = 1 # single value to multiple variable
a,b = 1,2 # multiple value to multiple variable
a,b = b,a # value of a and b is swaped
GDGS Azamgarh
Variables
Concept of L Value and R Value in variable
Lvalue and Rvalue refer to the left and right side of the assignment operator.
The Lvalue (pronounced: L value) concept refers to the requirement that the
operand on the left side of the assignment operator is modifiable, usually a
variable. Rvalue concept fetches the value of the expression or operand on the
right side of the assignment operator.
example: amount = 390
The value 390 is pulled or fetched (Rvalue) and stored into the
variable named amount (Lvalue);
destroying the value previously stored in that variable.

GDGS Azamgarh
Dynamic typing

Data type of a variable depend/change upon the value assigned to a


variable on each next statement.
X = 25 # integer type
X = “python” # x variable data type change to string on just next line
Now programmer should be aware that not to write like this:
Y = X / 5 # error !! String cannot be divided.

GDGS Azamgarh
Constants
A constant is a type of variable whose value cannot be changed. It is helpful to
think of constants as containers that hold information which cannot be changed later.
In Python, constants are usually declared and assigned in a module. Here, the module
is a new file containing variables, functions, etc which is imported to the main file.
Inside the module, constants are written in all capital letters and underscores
separating the words.
Create a constant.py:
PI = 3.14
Create a main.py:
import constant print(constant.PI)
Note: In reality, we can not create constants in Python. Naming them in all capital
letters is a convention to separate them from variables, however, it does not actually
prevent reassignment, so we can change it’s value.
GDGS Azamgarh
Input and Output
print() Function In Python is used to print output on the screen.
Syntax of Print Function - print(expression/variable) e.g.
print(122)
Output :- 122
print('hello India’)
Output :- hello India
print(‘Computer’, ‘Science’)
print(‘Computer',‘Science',sep=' & ‘)
print(‘Computer',‘Science',sep=' & ',end='.’)
Output :- Computer Science Computer & Science Computer & Science.
GDGS Azamgarh

You might also like