0% found this document useful (0 votes)
24 views

Python Fundamentals PPT 7 19

sgoiwskg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Python Fundamentals PPT 7 19

sgoiwskg
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

FUNDAMENTALS OF PYTHON

a) PYTHON CHARACTER SET : It is a set of valid characters that a language


recognize.

Letters: A-Z, a-z


Digits : 0-9
Special Symbols
Whitespace
ASCII & Unicode Characters.
2. TOKENS

Smallest individual unit in a program is known as token.

There are five types of token in python:


a. Keywords
b. Identifier
c. Literal
d. Operators
e. Punctuators

PYTHON 7
FUNDAMENTALS OF PYTHON

a) KEYWORDS :
Reserved words in the library of a language. There are 33 keywords in python.

False class finally is return break

None continue for lambda try except

True def from nonlocal while in

and del global not with raise

as elif if or yield

assert else import pass

b) IDENTIFIER:
The name given by the user to the entities like variable name, class-name, function-name etc.

Rules for identifiers:


 It can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or
an underscore.
 It CANNOT start with a digit , but CAN start with _(underscore)
 Keywords cannot be used as an identifier.
 No special symbols like !, @, #, $, %, + etc. in identifier.
 Commas or blank spaces are not allowed within an identifier.

PYTHON 8
FUNDAMENTALS OF PYTHON

c) LITERALS :
Literals can be defined as a data that is given to a variable or constant.
Five Types : Numeric Literals , String Literals , Boolean Literals, Special Literal ,
Literal Collections .
 Numeric literals: Numeric Literals are values in form of integer numbers , decimal numbers
and complex numbers

Eg: 8, 4.7, 7+5j

 String Literals : String literals can be formed by enclosing a text in the quotes. We can use both single as
well as double quotes for a String.

Eg: "info" , '45678‘

( Note : A basic string must be completed in one single line , if needed to be continued in multiple line , place \
as the last char acter of line1 and continue the string in next line)

 Boolean literal: A Boolean literal can have any of the two values: True or False.

 Special literals: Python contains one special literal i.e. None.

 Literal Collections: Collections such as TUPLES, LISTS and DICTIONARY are used in Python.

PYTHON 9
FUNDAMENTALS OF PYTHON

d) OPERATORS :
An operator performs the operation on operands. Basically there are two types of operators in python according to
number of operands:
A. Unary Operator
B. Binary Operator

A. Unary Operator: Performs the operation on one operand.

Example:
+ Unary plus
- Unary minus
~ Bitwise complement
not Logical negation
B. Binary Operator: Performs operation on two operands. (Arithmetic , Logical , Relational , Identity etc )

e) PUNCTUATORS : , ; , ( ), { }, [ ] ,: etc

PYTHON 10
BLOCKS AND INDENTATION
 GROUP of one or more statements.
 NO BRACES to indicate blocks of code for class and function definition or flow
control.
 Blocks of code are denoted by LINE INDENTATION, which has to be strictly
followed
 The number of spaces in the indentation is not fixed , but all statements within the
block must be indented the same amount.

for example : if a<b:


print(“True”)
else:
print(“False”)

PYTHON 11
COMMENTS
Comments are non executable statements . Comments explain a program and make a

program understandable and readable.


Single line comment
This type of comments start in a line and when a line ends, it is automatically ends.
Single line comment starts with # symbol.
Example: if a>b: # Relational operator compare two values

PYTHON 12
VARIABLES
 Named location that refers to a value and whose value can be used and processed
during program execution.
 Variables in python do not have fixed locations.
 The memory location they refer to changes every time their values change.
CREATING A VARIABLE:
A variable is created the moment you first assign a value to it.
Example:
x=5
y = “hello”

VARIABLES DO NOT NEED TO BE DECLARED WITH ANY PARTICULAR TYPE , AND

CAN EVEN CHANGE DATA TYPE AFTER THEY HAVE BEEN SET. IT IS KNOWN AS

DYNAMIC TYPING.

x = 4 # x is of type int
x = "python" # x is now of type str
PYTHON 13
Lvalue & Rvalue

Lvalue and Rvalue:

An expression has two values. Lvalue and Rvalue.

Lvalue: the LHS part of the expression


Rvalue: the RHS part of the expression

Python first evaluates the RHS of the expression and then assigns to LHS.
Example:
P =5

PYTHON 14
ASSIGNING VALUES TO VARIABLES

Normal Assignment :
a=5 # the variable a is assigned the value 5
b=“Anoop” # the variable b is assigned the value Anoop
c=7.8 # the variable c is assigned the value 7.8

ASSIGNING SAME VALUES TO VARIABLES


a=b=c= 30 # the variable a, b and c is assigned the value 5

ASSIGNING MULTIPLE VALUES TO MULTIPLE VARIABLES

a,b,c=10,12,14 # a is assigned the value 10 , b is assigned the value 12


and c is assigned the value 14

PYTHON 15
VARIABLE INTERNALS

 type ( ) :
Determines the datatype of the variable
Syntax: type (variable-name)
Example:
x=6
type(x)
The result will be:
<class ‘int’>
id( ) function of VARIABLE
 id ( )
Retrieves the memory address or location of the variable
Syntax: id (variable-name)
Example:

>>>id(5) 1561184448
>>>b=5
>>>id(b) 1561184448

PYTHON 16
ACCEPTING INPUT FROM THE USER

input( ) method is used to take input from the user.

Example:
a=input("Enter your Name : ")
print("Welcome : ",a)

Note : INPUT() FUNCTION ALWAYS RETURNS A VALUE OF STRING TYPE

TYPE CASTING

To convert one data type into another data type.

Casting in python is done using int( ) , float ( ) and str( ) functions

Note : During Input if value of wrong data type is entered , then ValueError
will be generated.

PYTHON 17
PRINTING OUTPUT USING PRINT COMMAND

print( ) method is used to provide output to the user.

Syntax : print(objects/values[,sep=‘<seperator>’ , end =‘<end string>’)

By default : the seperator is single space and end is \n

Sl No Command Output
1 print("hello") hello
print("world") world # end is \n
2 print("hello",end='$') hello$world # end replaced
print("world") by $
3 print("Trial","Programs") Trial Programs # single space
4 print("Trial","Programs", sep ='&') Trial&Programs
5 print("hello") hello
print()
print("world") world

PYTHON 18
EXAMPLES OF PRINT COMMAND

Sl No Command Output
6 print("hello") hello
7 a=int(input("enter your age : ")) enter your age : 5
print("Your Age is " ,a ) Your Age is 5
8 a=int(input("enter your age : ")) enter your age : 5
print("Your Age is " + str(a)) Your Age is 5
9 print(5*2) 10
10 a,b,c=10,20,30 10 20 30
print(a,b,c)
11 print("Trial","Programs") Trial Programs # single space
12 print("Trial"+"Programs") TrialPrograms # no space
13 a=5 30
b=6
print(a*b)
14 a,b,c= 3,5,7 9 15 21
print(a*a,a*b,a*c)
15 PYTHON 19

You might also like