Ch1 Python Fundamental
Ch1 Python Fundamental
LEARNING OBJECTIVES
Tokens:
keywords, identifiers, literals, operators and delimiter
HISTORY OF PYTHON
Python was developed by Guido van Rossum in 1989 at the National Research Institute
for Mathematics and Computer Science in Netherlands.
Python got its name from a BBC comedy series from seventies- “Monty Python’s Flying
Circus”.
TOKENS
KEYWORDS
● They are the words used by Python interpreter .
● As these words have specific meaning for interpreter,.
● they cannot be used for any other purpose.
['False', 'None', 'True', 'and', 'as', 'assert', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else',
'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or',
'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
NOTE :
All these keywords are in small alphabets, except for False, None, True, which are
starting with capital alphabets.
IDENTIFIERS:
These are the names given to identify any memory block, program unit
LITERALS :
A fixed numeric or non-numeric value is called a literal.
Examples of literals are:
OPERATORS :
They perform some action on data. Examples of operators are:
+, -, **, / etc.
DELIMITERS:
Delimiters are the symbols which can be used as separators of values, or to enclose
some values. Examples of delimiters are:
(){}[],;:
NOTE :
# symbol used to insert comments is not a token.
WHAT IS A VARIABLE?
Variables are like containers which are used to store the values for some input,
intermediate result of calculations or the final result.
Variable names are case sensitive. For eg. num and NUM are treated as two different
variable names.
EXAMPLES
a=17
name='Rachit'
num=10.5
NOTE : Variables do not need to be declared with any particular type and can even
change type after they have been set.
EXAMPLE
num=10
num = 'Raman'
>x=y=z=10
For example:
EXAMPLE
>>>a=1
>>> b = a
>>> 9=a
>>>a + 1 = b
>>>a = p + 5
OUTPUT A VARIABLE
The print statement is used to display the value of a variable.
PRINT STATEMENT
● If an expression is given with the print statement, it first evalutes the expression
and then print it.
● To print more than one item on a single line, comma (,) may be used.
>>>print (x)
welcome
>>> x="Year"
>>> y="New"
>>> print(y + x)
New Year
abcd
a@b@c@d!!
NOTE :
print can also be called with no arguments to print a blank line.
a,b=5,7
print(a)
print()
print(b)
output:
INPUT A VARIABLE
input statement
input() returns this value which is usually stored in a variable.
For example:
NOTE :
● To input numeric data from input() function we can use typecasting, i.e.,
changing the datatype using function.
●
● The string data accepted from the user can be converted to appropriate numeric
data type using int(), float() and eval() functions.
Data entered by the user is converted from string to integer and then assigned to y.
DATA TYPES
Data type states the way the values of that type are stored, the operations that can be
done on that type, and the range for that type.
Different types of data like character, integer and numbers with decimals etc. can be
stored in variables.
1. Number
1. integer
2. float point numbers
3. complex numbers
a) Integer are whole numbers without decimal point. They can be positive or negative
with unlimited length.
For example:
>>>a=20
Integers contain Boolean Type which is a unique data type, consisting of two constants,
True & False. A Boolean True value is Non-Zero, Non-Null and Non-empty. For example:
>>>entry=true
b) Numbers with fractions or decimal point are called floating point numbers. For
example:
>>> k=200.789
>>>x=10+5j
For accessing different parts of variable a; we will use a.real and a.imag.
10.0 5.0
2. STRINGS
For example:
3. LISTS
EXAMPLES
EXAMPLES
tup3=(1,2,3,4,5,6,7,8,9)
5. DICTIONARY
Python dictionary is an unordered collection of items where each item is a key: value
pair.
We can also refer to a dictionary as a mapping between a set of keys and a set of
values.
. Dictionary is mutable.
We can add new items or change the value of existing items.
EXAMPLES
dict3={'num':10, 1:20}
. We can use the type() function to know the data type of a variable. For example:
>>>num=10
>>> type(num)
<class 'int'>
>>> name='Kapil'
>>> type(name)
<class 'str'>
float ()
str ()
bool ()
EXAMPLE
>>> x= 158.19
>>> y= int(x)
158
EXAMPLE
>
>>>y=float(x)
>>>print y
165.0
COMMENTS
In Python, comment start with "#" symbol and extends till the end of the physical line.
Example
>>>n1=10
>>>n2=20
>>>sum= n1 + n2 # sum of numbers 10 and 20
EXAMPLE
'''This is a
multiline comment'''
or
"""This is a
multiline comment"""