Python Fundamentals
Python Character Set :A set of valid characters recognized by python.
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
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
Token
Smallest individual unit in a program is known as token/lexical unit
1. Keywords
2. Identifiers-
3. Literals
4. Operators
5. Punctuators/Delimiters
Keywords:Reserve word of the compiler/interpreter which can’t be used as
identifier.
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).
* An identifier can contain digits but cannot begin with a digit
* Python does not allow special characters except underscore
* 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
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)
age = 22
Example of Float Literals in Python(numeric literal)
height = 6.2
Example of Special Literals in Python
name = None
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
Punctuators/Delimiters:Used to implement the grammatical and structure of a
Syntax.Following are the python punctuators.
Barebone of a python program
a. Expression : - which is evaluated and produce result. E.g. (20 + 4) / 4
b. Statement :- instruction that does something.
e.g
a = 20
print("Calling in proper sequence")
c. Comments : which is readable for programmer but ignored by python
interpreter
i. Single line comment: Which begins with # sign.
ii.Inline comment is a comment on the same line as a statement
iii. Multi line comment (docstring): either write multiple line beginning with #
sign or use triple quoted multiple line. E.g.
‘’’this is my first
python multiline comment‘’’
d. Function:a code that has some name and it can be reused.e.g. keyArgFunc in
above program
d. Block & indentation : group of statements is block/suite.indentation at same
level create a block
.e.g. all 3 statement of keyArgFunc function
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
Variable Scope And Lifetime in Python Program
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.
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
print() Function In Python is used to print output on the screen.
Syntax of Print Function - print(expression/variable)
print(122) print('hello India')
Output :- Output :-
122 hello India
print(‘Computer',‘Science') x=10
print(‘Computer',‘Science',sep=' & ') y=20
print(‘Computer',‘Science',sep=' & z=x*y
',end='$') print(x,"*",y,"=",z)
Output :- Output :-
Computer Science 10 * 20 = 200
Computer & Science
Computer & Science$
type()- type(obj) is passed, it returns the type of the given object
x = 10
print(type(x))
<class 'int'>
id() function is used to return a unique identification value of the object stored
in the memory.
a = 10
print('ID of a =', id(a))
ID of a = 9781088
chr() function returns the character that represents the specified unicode.
chr(65) returns 'A'
ord() function in Python is used to convert a single Unicode character into its
integer representation, i.e., it takes a single string character as an input and
returns an integer (representing the Unicode equivalent of the character) as an
output.
ord('A')returns 65