We
take some examples before like (a=10) in this a=10 (a) is the identifier, (=) is the
operator (10) is the data or value.
Identifiers
The identifier can be Variable, Function, Class, or Module.
Operators
types
Arithmetic
operators
Assignment
operators
Comparison
operators
Logical
operators
Identity
operators
Membership
operators
Bitwise
operators
Data
or Values types…
Text
Type: str
Numeric
Types: int, float, complex
Sequence
Types: list, tuple, range
Mapping
Type: dict
Set
Types: set,
frozen set
Boolean
Type: bool
Binary
Types: bytes, byte-array
None
Type: None
Identifier
Rules:
1.
Identifiers can only a single or a combination of letters in
lowercase
(a to z)
or
uppercase (A to Z)
or
digits (0 to 9)
or
an underscore (_).
2.
An identifier cannot start with a digit but above other three are ok.
3.
An identifier is case sensitive, it means A=10 and a=10 both are
different.
4.
No length limit of the variable name.
5.
Keywords cannot be used as identifiers. Now we have doubted what is keywords
Python
keywords are special reserved words, that have specific meanings and purposes
and can’t be used for anything but those specific purposes. These keywords are
always available—you’ll never have to import them into your code.
Python
keywords are different from Python’s built-in functions and types. The built-in
functions and types are also always available, but they aren’t as restrictive
as the keywords in their usage.
An
example of something you can’t do with Python keywords is assign something to
them. If you try, then you’ll get a SyntaxError. You won’t get a SyntaxError if
you try to assign something to a built-in function or type, but it still isn’t
a good idea.
Python
keywords:
['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']
Total
35 keywords.
Now
we going to practice these points.
1. password…ok
2. p$ssword...fail because $, not an identifier
symbol.
3. password@123..fail because @ not an
identifier symbol.
4. 123_password.. fail because starting
letter must be a letter, not a digit.
5. all passwords..fail because no spaces
on an identifier
6. password2all..ok
7. if.. fail because it’s keyword
8. _password.. ok
9. _password__123…0k
6. password ..
it means it’s a normal variable
_password ..
it means it’s a protected variable
__password ..it
means it’s a private variable
__password__ ..
it means it’s a magic variable.
Keywords:
'False',
'None', 'True',
'and',
'not', 'or','is',
'if',
'elif', 'else',
'while',
'for', 'break', 'continue', 'return', 'in', 'yield'
'try',
'except', 'finally', 'raise', 'assert',
'import',
'from', 'as', 'class', 'def', 'pass',
'global',
'nonlocal', 'lambda', 'del', 'with',
'async',
'await',
All
are contained the alphabet letters only, all are lower case but True,
False and None are Starting with Capital letters. Switch type
and do-while is not in python, int, float like not in keywords
because it’s a dynamically typed programing language. we can see on
windows terminal python keywords. For that, we need to enter python on
terminal. Then it’s going to PVM mode. Now we enter like import keyword and
enter and keyword.kwlist then enter. The result on below.
C:\Users\Surya>
C:\Users\Surya>
C:\Users\Surya>python
Python
3.8.5 (tags/v3.8.5:580fbb0, Jul 20 2020, 15:43:08) [MSC v.1926 32 bit (Intel)]
on win32
Type
"help", "copyright", "credits" or
"license" for more information.
>>>
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']
>>>
Data
types:
Now
we are going to in-depth in data types, how data types in python.
int,
float, complex
str
list,
tuple, range
dict
set, frozen set
bool
bytes, byte array
None
There
are three numeric types in Python:
int
float
complex
Variables
of numeric types are created when you assign a value to them:
Example
x
= 1 # int
y
= 2.8 # float
z
= x + yj, # complex
To
verify the type of any object in Python, use the type() function:
Example
print(type(x)) #
int
print(type(y)) #
float
print(type(z)) #
complex
Int
Data Type:
Int,
or integer, is a whole number, positive or negative, without decimals,
of unlimited length, called an integer number.
Example
Integers:
x
= 1
y
= 35656222554887711
z
= -3255522
print(type(x)) #<class
'int'>
print(type(y)) #<class
'int'>
print(type(z)) #<class
'int'>
x=342342342341235636335345234523452345235423
this
int data type in python 3.x version only but in python 2.x version it’s
long int type. But now we all learning and discuss python 3.x
version only. we not talking about the 2.x version because python support
ended. But some idea or interview purpose.
To
assign the int value to the program we have four types are available
1. Decimal form (base-10) or (0-9)
2. Binary form (base-2) 0r (o or 1)
3. Octal form (base-8) or (o to 7)
4. Hexadecimal form (base 16) (0-9 and
(‘A’ to ‘F’ or ‘a’ to ‘f’))
Decimal
form:
Decimal
form means base 10 or zero to 9 only like whole number system
or normal numbers. Ex: a= 123456975 , b=34577588433545
Binary
form:
Binary
form means base 2 or zero or one only. Ex: a=0101010101 , b=
11111100111
The
important point here is if we are given some number like a =1111, it, not
a binary number, python default the taken number is decimal number
only if we want to give a binary form then we need to add zero
with ‘b’ or ‘B’ and number.
Ex:
a = 0b1111 or a =0B1111
It’s
consider (0b1111) on binary form and calculate like this
1x20=1x1=1
1x21=2x1=2
1x22=4x1=4
1x23=8x1=8
Total
=15
Octal Form:
The
octal number means base 8 or zero or seven only.
Ex:
a=1234567 , b= 123341245564
The
important point here is, it’s only taking zero to seven number only if we are
given to eight or nine number It shows an error. And if we are given some
number like a =34567, it not an octal number, python default is taken
number is decimal number only if we want to give a The octal form
then we need to add zero with ‘o’ or ‘O’ and number.
Ex:
a = 0O1111 or a =0o56455
It’s
considered (0O1111) on Octal form and calculate like this
1x80=1x1=1
1x81=8x1=8
1x82=64x1=64
1x83=514x1=514
Total
=587
Hexadecimal
form:
Hexa
decimal number means the base 16 or zero or 9 and ‘a’ to ‘f’ or ‘A’ to
‘F’.
Ex:
a=12789abdef , b = dedbf
The
important point here is, it’s only taking zero to nine number and (a to
z or A to Z both are equal) and it’s taking (a =1o, b= 11,c =12 , d= 13,e =14,
f= 15) only if we are given to after ‘f’ its show error. And if we are
given some number like a =34567, it not a Hexa a decimal number, python
default taken number is decimal number only if we want to give a hexadecimal
number then we need to add zero with ‘x’ or ‘X’ and number.
Ex:
a = 0x1111 or a =0Xedbf
It’s
considered (0x1111) on the hexadecimal number and calculates like this
1x160=1x1=1
1x161=16x1=16
1x162=256x1=256
1x163=4096x1=4096
Total
=4369
And
some more valid examples are a = 345235b, b=dfead and not
valid examples are a=2342ikl ,b=54658gmp because it contains after ‘f’
alphabets.
Now
we are going to discuss the conversion part
Python
integer other types (Decimal, Binary, Octal, hexadecimal) taking into decimal
form only. But if we want to convert these types, python has default functions
that are there.
1. bin() #for convert
the decimal to binary
2. oct() #for
convert the decimal to octal
3. hex() #for
converting the decimal to hexadecimal
now
we are trying to numbers, convert to binary, octal, and hexadecimal support
with bin function.
bin
(23) # numbers to binary
number
0b10111
#result
bin(0o23) #
octal numbers to binary number
0b10011
#result
bin(0x23) #
hexa decimal numbers to binary number
0b100011
#result
now
we are trying to numbers, convert to binary, octal, and hexadecimal support
with oct function.
oct(23) #
numbers to octal number
0o27 #result
oct(0b11001) #
binary number to octal number
0o31 #result
oct(0o347) #
octal number to octal number (it’s not required but some idea)
0o347
#result also same
oct(0x2345e) #
hexa decimal number to octal number
0o432136 #result
now
we are trying to numbers, convert to binary, octal, and hexadecimal support
with hex function.
hex(23) #
numbers to hexadecimal number
0x17 #result
hex(0b01101)#
binary number to hexadecimal number
0xd #result
hex(0o2467)#
octal number to hexadecimal number
0x537 #result
hex(0x2467f)#hexa
decimal number to hexadecimal number (it’s not required but some idea)
0x2467f #result
Float
Data Type:
Float
is a whole number, positive or negative, with decimals, of unlimited
length called Float number.
Example
Float:
x
= 1.2
y
= 356562.22554887711
z
= -3255.522
print(type(x)) #<class
'float'>
print(type(y)) #<class
'float'>
print(type(z)) #<class
'float'>
Floating
point value we can assign decimal type and exponential form or
scientific notation only. We can’t assign other types. If we are
trying to do it, it will give an error.
a=
3.4e3 # exponential form or scientific notation
3400.0 #Result
The
advantage of this future is we can assign big value in very little
space and readable also very easy.
Ex:a= 456342542345235423543246345456.56734523452354354565754
#Float type
4.563425423452354e+29
#result on exponential form
Complex
datatype:
Complex
datatype only in python not another language like c,c++, or java. It special
data type in python. It can be creating mathematical and
engineering-based applications very easily.
Complex number syntax is (a+bj), in ‘a’ is called the real part and ‘b’ is called the imaginary part and j will be the same. never try to change 'j' letter. If we are trying to change the 'j' with another number or another letter, it’s showing an error. And
j value is (j2=-1) or j equal to square root of minus one.
This
type of representation called a complex number.
Ex:
a= 10+30j
We
can check the function for real number and imaginary number helping with
a=
10+30J
print(a.real)
print(a.imag)
In
real part, we can assign
an int or float or int other form types like (decimal, binary, octal,
hexadecimal) without any issue.
In
imaginary part we
can assign int or float only in int other form types
like (decimal, binary, octal, hexadecimal) not accepted. If trying to do
it’s showing an error.
Ex: a
= 10 + 10j…ok
a
= 10.3+10.3…fail # j is not assign
a
= 0b1101+10.4j…ok
a
= 10 + 0b1001j…fail # imaginary part not expected to binary number
a
= 0o1234+10.63j…ok
a
= 0xedf+10j.. ok
Bool
datatype:
Bool
datatype only represent True, False (or) Yes, No (or) ok,
not-ok like. Its only allowed values are True and False, but True
and False must be starting with a capital letter only.
Ex: a
=10
b
= 20
c
= a>b
print(c)
# result is False
print
(type(c)) # result is <class 'bool'>
in
bool date type True represents ‘1’ and False represent
‘0’ and it’s using for athematic operations
I
will cover athematic operations soon.
String
datatype:
String(str)
datatype means any group of letters or a single letter called
a string(str).
Ex
: (a to z) or (A to Z) of any words. We can assign string on multiple types
Ex: a
= ‘ram’ # single quoted string is valid
a
= “ram’’ #double the quoted-string is valid
a = '''ram''' (or) a =
"""ram"""# the triple string is valid (or)
triple-double string also valid
Triple
quoted strings using
for multi-line string but single line and the double line
quoted strings we can’t use.
Ex: a
= '''rama
rama
rama''' #valid
in python
a = """rama
rama
rama""" #valid
in python
a
= ‘rama
rama
rama’ #
invalid syntax
a
= ''rama
rama
rama'' #
invalid syntax
In
python single letter also considers 'a' as str, but in other languages, it has 'a' char data type. Python doesn’t have a char data type.
Some
advantages of single-quoted, double-quoted, triple quoted strings with some
examples
a
= ‘rama killed by ravana’ #valid
a
= "rama killed by ravana" #valid
a
= '''rama killed by ravana''' #valid
a
= '''rama
killed
by
ravana''' #valid
a
= """rama killed by ravana""" #valid
a
= """rama
killed
by
ravana"""
#valid
a
= '''rama ''killed'' 'by' ravana''' #valid
a
= '''rama
''killed''
'by'
ravana'''
#valid
a
= """rama ''killed'' 'by' ravana""" #valid
a
= """rama
''killed''
'by'
ravana"""
#valid
a
= 'rama \'killed\' by ravana' #valid
If
we are using this type (\'), we can create more examples but I
hope understand all valid points. The string is a very important role in any
language, and python language also the same priority.
Triple
quoted strings uses:
1. To
define multi-line string literals
2. To
use (') and ('') as a normal character in our string
3. To
define doc-string
Indexing:
Take
one string for indexing purpose, name = ‘krishna’ now krishna string as an
indexing table looks like
Negative Indexing |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Memory location |
k |
r |
i |
s |
h |
n |
a |
Positive indexing |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
name
= ‘krishna’
print
(name[0]) # result is k
print
(name[4]) # result is h
print
(name[8]) # result is index error because no memory location is available.
print
(name[-1]) # result is a
print
(name[-7]) # result is k
print
(name[-19]) # result is index error because no memory location is available.
This
future is only available in python, other languages are positive index only.
Slice
operator:
In
name only we can find that ‘slice’ word. We all know slice means, some object
trying to cut parts it means slice. As the same as this operator also.
Now
we are taking one string name = ‘krishna’, we are trying to do a slice of this
string.
Slice
syntax is [begin:end-1]
Ex:
[2:6] #it’s convert to [2:5], below table will help us.
Negative Indexing |
-7 |
-6 |
-5 |
-4 |
-3 |
-2 |
-1 |
Memory location |
k |
r |
i |
s |
h |
n |
a |
Positive indexing |
0 |
1 |
2 |
3 |
4 |
5 |
6 |
name
= 'krishna'
print
(name[2:6]) #result ishn
some
important points:
if
we are taking begin index then it will take from starting string only. And if
we are not taking end index then it will take from mentioned begin index to the
total end of the string.
Ex:
name = 'krishna'
print
(name[2:6]) #result ishn
name = 'krishna'
print
(name[:6]) #result krishn
name = 'krishna'
print
(name[2:]) #result ishna
name = 'krishna'
print
(name[:]) #result krishna #it’s
taking full string.
name = 'krishna'
print
(name[2:100]) #result ishna #
it’s taking 2 to full string.
Note:
slice operator never going to give the error.
name = 'krishna
print
(name[2:1]) #result # it’s blank or
nothing.
Now
we are check some uses of slice operator, for that we take one example of
string like this, company = ‘american_multinational_technology_company’, now we
are trying to do changes with multiple ways in this string
company =
'american_multinational_technology_company'
print
(company[:].upper())
print
(company[0].upper()+company[1:22])
print (company[0:8].upper()+company[8:100])
print (company[0:-1]
+ company[len(company)-1].upper())
print
(company[0].upper() + company[1:len(company)-1]+company[-1].upper())
Result:
AMERICAN_MULTINATIONAL_TECHNOLOGY_COMPANY
American_multinational
AMERICAN_multinational_technology_company
american_multinational_technology_companY
American_multinational_technology_companY
String
with mathematical operator with multiple ways
1.
name = 'tech'+'company'
print(name)
techcompany # Result is ok
2.
name = 'tech'+2
print(name)
TypeError: can only concatenate str (not "int") to
str # Result
3.
name = 'tech'* 2
print(name)
techtech # Result is ok
4.
name = 4 * 'tech'
print(name)
techtechtechtech # Result is ok
5.
name = 'tech'* 'company'
print(name)
TypeError: can't multiply sequence by non-int of type 'str' # Result
Post a Comment