Python
Fundamentals
PYTHON CHARACTER SET
Character set is a set of valid characters that a
language can recongnize.
Python supports unicode encoding standard
following character set :
Tokens
The smallest individual unit in a program is known as
a token or a lexical unit.
Python has following tokens :
(i) Keywords
(ii) Identifiers (Names)
(iii) Literals
(iv) Operators
(v) Punctuators
Keywords
Keywords are the words that convey a special
meaning to the language compiler/interpreter.
Identifiers (Names)
Identifiers are fundamental building blocks of a program and
are used as the general terminology for
the names given to different parts of the program viz.
variables, objects, classes functions, lists,
dictionaries etc.
Identifier forming rules of Python are being specified below :
Identifiers (Names)
Literals/Values
Literals are data items that have a fixed value.
Python allows several kinds of literals :
(i) String literals
(ii) Numeric literals
(iii) Boolean literals
(iv) Special Literal None
(v) Literal Collections
String Literals
The text enclosed in quotes forms a string literal in
Python.
Unlike many other languages, both single character
enclosed in quotes such as “a” , ‘1’, ’@’, or multiple
characters enclosed in quotes such as “abc” or ‘123’,
‘$ #@’ , “
[email protected]” are treated as string
literals.
Python allows you to have certain nongraphic-
characters in String values.
Nongraphic characters are those characters that
cannot be typed directly from keyboard e.g.,
backspace, tabs, carriage, return etc.
These nongraphic-characters can be represented by
using escape sequences.
An escape sequence is represented by a backslash ( \ )
followed by one or more characters.
String Types in Python
Python allows you to have two string types :
(i) Single-line String
(ii) Multiline String
(i) Single-line Strings (basic strings). The strings that
you create by enclosing text in single quotes
(‘ ‘) or double quotes (“ ”) are normally single-line
string, i.e., they must terminate in one line.
Text1=“vinit”, a=‘r’, name=‘priya’ , id=‘A@101’
(ii) Multiline String . Sometimes you need to store
some text spread across multiple lines as one
single string. For that Python offers multiline strings.
Multiline strings can be created in two ways :
(a) By adding a backslash at the end of normal single-
quote / double-quote strings. In normal strings, just
add a backslash in the end.
text1=‘Hello\
world’
(b) By typing the text in triple quotation marks. (No
backslash needed at the end of line).
Text1=‘’’how Text1=“ ” ”how
are are
you’’’ you” “ “
Comments
Comments are the additional readable information,
which is read by the programmers but ignored
by Python interpreter.
In Python, comments begin with symbol # (Pound or
hash character) and end with the end of physical line.
Multi-line Comments
What if you want to enter a multi-line comment or a
block comment ?. You can enter a multi-line comment
in Python code in two ways :
(i) Add a # symbol in the beginning of every physical
line part of the multi-line comments,
(ii) Type comment as a triple-quoted multi-line string
VARIABLES AND ASSIGNMENTS
Named labels, whose values can be used and
processed during program run, are called Variables.
The variables are called symbolic variables because
these are named labels.
Creating Variables
Python variables are created by assigning value of
desired type to them.
e.g. to create a numeric variable, assign a numeric
value to variable-name ; a =10
to create a string variable , assign a string value to
variable-name b= "vinit" c= 'jain‘
Here a is a,b,c are variables and we defined values to
it
Multiple Assignments
Variable definitions
Dynamic Typing
Output
print
Statement
Features of print statement
It auto-converts the items to strings i.e., if you are
printing a numeric value it will automatically convert
it into equivalent string and print it .
For numeric expressions, it first evaluates them and
then converts the result to string, before printing
Simple
Input & Output
Reading
Numbers
Possible
Errors
When Reading
Numeric Value
Examples
#name,age,sal='vinit',25,2500.50
#print('name is: %s age is: %d salary is: %g'
%(name,age,sal))
#print('name is: {} age is: {} salary is:
{}'.format(name,age,sal))
#n=16; print("even") if n%2==0 else print("odd")
n=15; {print("even"),print("hello")} if n%2==0 else
{print("odd") , print('bye')}
Thank You