Python Fundamentals PPT 7 19
Python Fundamentals PPT 7 19
PYTHON 7
FUNDAMENTALS OF PYTHON
a) KEYWORDS :
Reserved words in the library of a language. There are 33 keywords in python.
as elif if or yield
b) IDENTIFIER:
The name given by the user to the entities like variable name, class-name, function-name etc.
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
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.
( 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.
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
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.
PYTHON 11
COMMENTS
Comments are non executable statements . Comments explain a program and make a
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”
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
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
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
Example:
a=input("Enter your Name : ")
print("Welcome : ",a)
TYPE CASTING
Note : During Input if value of wrong data type is entered , then ValueError
will be generated.
PYTHON 17
PRINTING OUTPUT USING PRINT COMMAND
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