Python Basics-1
Python Basics-1
Python Basics-1
Python is an
interpreter based scripting language, which can be used:
● To create web based application
● To connect to the database systems and to read and modify files.
● To handle big data and perform complex mathematics.
● For rapid application development.
6. For quit python IDLE, simply type the following command on the Python shell
prompt:
quit()
Or
exit()
7. Python recognizes all characters in the ASCII character set as well as the
UNICODE characters.
8. Tokens:
A Token is the smallest unit in a program that makes a program. It is
categorized into 5 parts:
● Identifier
● Keywords
● Delimiters/Punctuators
● Literals/Constants
● Operators
9. Identifier
A. It is a name given to a particular Python object (e.g: variable, function,
module etc) in order to identify it.
B. Rules to be followed while giving an identifier name:
a. It consists of three types of characters:
i. Alphabets (A-Z / a-z)
ii. Digits (0-9)
iii. Underscore(_)
b. It should either start with an alphabet or an underscore.
c. It should not be a keyword
[Note : Python is case sensitive, so the identifiers should be referred in the
same case in which it is specified.]
● Examples of valid Identifiers : Sum, _Sum1, Total_number etc
[Suggestion: Try to give names, starting with uppercase except True, False and
None]
10. Keyword
● A keyword is a reserved word that is pre-defined in the python library
for a particular purpose. [Note that you cannot change its purpose]
● In python, you can get the list of keywords by importing the module
keyword.
>>> import keyword
>>> keyword.kwlist
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', '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 the keywords are in lower cases except True, False and None which
starts with upper case]
11. Delimiters
It includes Grouping, punctuation, and binding symbols. The following is the
list of characters used as Delimiters:
Brackets [ ]
Parenthesis ( ),
Braces { }
Semicolon ;
Colon :
Single quotes '
Double quotes "
Pound Sign #
Triple single quotes ' ' '
Triple double quotes,
Braces { }
Comma quotes " " "
12 Literals
Literals in Python are raw data which are processed using Python statements.
In python, we have three types of literals:
● Numeric Literals
○ Integers
E.g: 23, 0b1001 (binary), 0o17(octal),0x1a(hexadecimal)
[Note: 'zero b' -> (for binary) 0b or 0B both will work. Same goes
for octal and hexadecimal i.e 0o or 0O / 0x or 0X
○ Float Literal
E.g; 15.5, 2.5e3 (It means 2.5 X 103 i.e. 2500.0)
○ Complex Literal
z=3.14j (over here the Real part will be 0 and the imaginary part
will be 3.14. You can see this result by typing z.real and z.imag)
● String Literals
○ A string is a set of characters.
○ As discussed in point IV a string literal can appear within
single/double/triple quotes.
● Boolean Literal
○ A boolean literal in Python can have two values : True or False
Note: True and False are the keywords, which start with an
upper case alphabet.
○ E.g:
>>> a=(5>4)
>>> a
True
>>> type(a)
<class 'bool'>
[Note: type is a built-in function which returns the data type of
the parameter
E.g:
>>> a=10
>>> b=1.5
>>> c="hello"
>>> d=1.5j
>>> e=(3>4)
>>> type(a)
<class 'int'>
>>> type(b)
<class 'float'>
>>> type(c)
<class 'str'>
>>> type(d)
<class 'complex'>
>>> type(e)
<class 'bool'>
]
13. Operators
● An operator is a tool that performs an operation on the basis of
operands passed to it.
● Python divides the operators into following categories:
○ Arithmetic operators
○ Assignment operators
○ Relational operators
○ Logical operators
○ Identity operators (is/is not)
○ Membership operators (in/ not in)
○ Bitwise operators (|, &, <<, >>, ^ and ~)
[Note : Last three operators will be covered later]
** Exponent 1
*,/,//,% Multiplication, Division, Floor 2 (All these operators are
Division and Modulo having Same Precedence)
Examples : [TTT]
>>>2**3
8
>>>5//2
2
>>>5/2
2.5
>>>15%4
3
>>>a=10
>>>b=3
>>>a-b
7
>>>-15%2
1
>>>15%-2
-1
>>>-15%-2
-1
>>>2**3**2
512
>>>-15%7%20
(OPERATION WILL BE Left to Right)
6
[Note : Remainder = Numerator - (Numerator//Denominator)* Denominator]
~Shorthand Notation
/= a /= 2 # It implies a= a / 2
//= a //= 2 # a = a // 2
%= a %= 2 # a = a % 2
-= a -= 2 #a = a - 2
+= a + =2 # a = a + 2
Example: [TTT] [Use Interactive mode]
1) What will be the value of a?
a=2
b=10
a *= b + 20 #it implies a=a*(b+20)
60
2)
>>>a=10
>>>b=20
>>>a, b = b, a
>>>a
20
>>>b
10
16. Relational Operators:
● These operators are used to compare two values, which are passed as an
operand to it.
● The result of the relational expression is always a boolean (i.e. True or
False)
● The precedence of == and != is lesser than the precedence of remaining
relational operators .
● The precedence of relational operator is less than Arithmetic operators
● These operators are:
● Note Python also supports multiple use of relational operators
[e.g: the value a should fall in the range 20-30 can be written as:
20<=a<=30
Note: Only for the VIVA VOCE]
== Equals to >>>a=5
(Note : >>>b=6
'=' >>>a == b
symbol False
has been >>>10==10
written True
twice)
17. Logical Operators : They are used to either negate or combine Relational
expressions and like relational expression the result is either True or False.
These operators are (As per their Precedence)
>>>a=print(‘hello’)
>>>print(a)
hello
None
21 Inbuilt Function input()