Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
3 views9 pages

Core Python 6 PM

The document provides an overview of Python identifiers, including rules for their definition, case sensitivity, and reserved keywords. It also covers Python data types, distinguishing between static and dynamic types, and detailing fundamental and collection data types, along with type casting. Additionally, it explains the characteristics of various data types such as int, float, complex, bool, and str, as well as mutable and immutable types.

Uploaded by

kumargpc7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views9 pages

Core Python 6 PM

The document provides an overview of Python identifiers, including rules for their definition, case sensitivity, and reserved keywords. It also covers Python data types, distinguishing between static and dynamic types, and detailing fundamental and collection data types, along with type casting. Additionally, it explains the characteristics of various data types such as int, float, complex, bool, and str, as well as mutable and immutable types.

Uploaded by

kumargpc7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Identifiers:

==========
-->A name in the python program is called as identifiers.
-->It can be a class name or function or module name or variable name.
Ex:
x = 10
def f():
class Test:

Rules to define identifiers in python:


1.Alphabates(a-z/A-Z)
2.Digits(0-9)
3.Underscore(_)

IDLE:Integrated Development and Learning Environment

Ex:
cash = 100 #Valid
cas$h = 100 #Invalid

-->Identifier should not start with digit.


total123 = 100 #valid
123total = 100 #invalid

-->Identifiers are case sensitive:


total = 100
TOTAL = 300

Note:
--------
1.There is no length limit for python identifiers. But not recommended to use too
lengthy identifiers.
2.Dollar($) symbol is not valid in python.
3.If identifiers with underscor(_) then it indicates as protected.
4.If an identifiers starts with two underscores(__) then it is private.
5.We can't use reserved word as identifiers.

Reserved words/keywords
----------------------------------------
>>>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']

6.If any method before and after two underscores(__) then those methods are known
as magic methods.
Ex:
__add__(),__sub__(),__mul__()..................

Data Types:
==========
1.Static DT's:statically typed programming language
int a;
float b;
string c;
-->C,C++,Java,.net languages supports static data types.
2.Dynamic DT's:dynamically typed programming language
x = 10==>int
y = 10.0==>float
z = '10'==>str
-->Python and java script supports dynamic data types.

Python data types:


1.Fundamental DT's:
int,float,bool,complex,str

2.Collection DT's:
bytes,bytearray,list,tuple,set,range,frozenset,dict,None

22/10/24
--------------
Python & Full Stack Python @ 6:00 PM (IST) by Mr.Mahesh
Day-1 https://youtu.be/M4HiepjgZS0
Day-2 https://youtu.be/kcBYDKzi8gE
Day-3 https://youtu.be/INvYBqTzGt4
Day-4 https://youtu.be/WIEVYsfcLVo

Data Types:
1.Static DT's:statically typed programming language
int a,
float b;
string c;
C,C++,Java, .Net are supports static DT's

2.Dynamic DT's:dynamically typed programming language


x = 10==>int
y = 1.0==>float
z = 'sunny'==>str
Python and Javascript supports dynamic DT's

Python data types:


1.Fundamental DT's:
int,float,bool,complex,str

2.Collection DT's:
bytes,bytearray,list,tuple,range,set,frozenset,dict,None

1.int data type:


We can use int data type to represent whole numbers(integral values)
Ex:
a = 10
type(a)

We can represent int values in 4-ways.


1.Decimal Form
2.Binary Form
3.Octol Form
4.Hexa Decimal Form

1).Decimal Form(Base-10):
---------------------------------------
It is the default number system in python
The allowed digits are:0 to 9
Ex:
x = 100

2).Binary Form(Base-2):
-----------------------------------
The allowed digits are:0 & 1
Literal value should be prefixed with 0b or 0B.
Ex:
a = 0b1010 #Valid
a = 0B1010 #Valid
a = 0b123 #Invalid

3).Octol Form(Base-8):
----------------------------------
The allowed digits are:0 to 7
Literal value should be prefixed with 0o or 0O.
Ex:
a = 0o10
a = 0O10
a = 0o786 #Invalid

4).Hexa Decimal Form(Base-16):


------------------------------------------------
The allowed digits are:0 to 9,a-f/A-F
Literal value should be prefixed with 0x or 0X.
Ex:
a = 0XFACE
a = 0xBeef
a = 0XBeer #Invalid

Note:
Being a programmer we can specify literal values in decimal, binary, octol
and hexa decimal forms, but PVM will always provide values in decimal form
Ex:
a = 10
b = 0b10
c = 0o10
d = 0x10
a #10
b #2
c #8
d #16

Base Conversions
--------------------------
bin(),oct(),hex()

bin(10)#'0b1010'
oct(10)#'0o12'
hex(10)#'0xa'
hex(20)#'0x14'

2.float data type:


--------------------------
To represent floating point values(decimal values)

f = 1.234
type(f) #<class 'float'>
-->We can also represent floating point values by using exponential form(Scintific
notation)
Ex:
f = 1.2e3
print(f)#1200.0

Note:Instead of 'e' we can use 'E'


Ex:
f = 1.2E3
-->The main advantage of exponential form is we can represent big values in less
memory.

3.complex data type:


Acomplex is the form of
a + bj
a-->Real part
b-->Imaginary part
j--->sqrt(-1)
-->a & b contains integers or floating point values.
-->In the real part if we use int values then we can specify that either by
decimal, octol binary or hexa decimal form.
-->But imaginary part should be specified only by using decimal form.

Ex:
a = 3 + 4j
a = 1.5 + 2.5j
a = 3 + 0b1010j #Invalid
a = 0b1010 + 3j
Ex:
c = 10.5 + 3.6j
c.real #10.5
c.imag #3.6

4).bool data type:


The only allowed values are:True and False
Ex:
10 < 20 #True
10 > 20 #False

-->Internally python represents True as 1 and False as 0.


>>>True + True #2
>>>True - False #1

23/10/24
---------------
Python & Full Stack Python @ 6:00 PM (IST) by Mr.Mahesh
Day-1 https://youtu.be/M4HiepjgZS0
Day-2 https://youtu.be/kcBYDKzi8gE
Day-3 https://youtu.be/INvYBqTzGt4
Day-4 https://youtu.be/WIEVYsfcLVo
Day-5 https://youtu.be/6uloo1wwWIk

Python Data Types:


------------------------------
1.Fundamental DT's:
int,float,bool,complex,str

2.Collection DT's:
bytes,bytearray,list,tuple,range,set,frozenset,dict,None
1.int data type:
a = 10
-->Decimal Form:Base-10; 0 to 9
-->Binary Form:Base-2; 0 & 1; 0b/0B
-->Octol Form:Base-8; 0 to 7; 0o/0O
-->Hexa Decimal Form:Base-16; 0-9, a-f/A-F; 0x/0X

Base conversion:
bin(), oct(), hex()

2.float data type:


f = 1.234
f = 1.2e3
f = 1.2E3

3.complex data type:


a + bj

4.bool data type:


True and False
True==>1
False==>0

5.str data type:


-----------------------
-->str represents String data type.
-->A string is a sequence of characters within the single quotes or double quotes
or triple quotes.
Ex:
s = 'sunny'
s = "sunny"
s = '''sunny'''
s = """sunny"""

Slicing of string:
-------------------------
-->Slice means a piece.
-->[:] is called as slice operator, whuch can be used to retrieve parts of string.
-->In python string follows zero based index.
-->The index can be either +ve or -ve.
-->+ve index means forward direction from left to right.
-->-ve index means backward direction from right to left.
Ex:
s = 'python'
s[0] #p
s[-1] #n
s[10] #IndexError

Syn:
s[begin:end]===>begin to end-1

Ex:
s = 'learning python is very easy'
s[1:5]#'earn'
s[:5]#'learn'
s[5:]#'ing python is very easy'
s[:]#'learning python is very easy'
s[::]#'learning python is very easy'
s[::-1]#'ysae yrev si nohtyp gninrael'
s[0:100]#'learning python is very easy'

Type Casting:
--------------------
We can convert one type to other type. This conversion is called as type casting or
type coersion.
int(), float(), bool(), complex(), str()
1.int():
To convert values from other types to int.
Ex:
int(12.99) #12
int(True)#1
int(False)#0
int("10")#10
int(10+3j) #Invalid
int('10.5') #Invalid
int('0B1010')#Invalid
int(0B1010)#10

2.float():
To convert other type values to float type.

float(10)#10.0
float(True)#1.0
float(False)#0.0
float('10')#10.0
float('10.5')#10.5
float(10+3j)#Invalid

3.bool():
To convert other type to bool type.

-->0 means False


-->non-zero means True
-->Empty string always False

bool(1)#True
bool(0)#False
bool(10)#True
bool(0.1)#True
bool(0.0)#False
bool(10+3j)#True
bool(0+0j)#False
bool(' ')#True
bool('')#False
bool(None)#False
bool('True')#True

24/10/24
--------------
Python Data Types:
-----------------------------
1.Fundamental DT's:
int,float,bool,complex,str

a = 10
type(a)#<class 'int'>
b = 1.2
type(b)#<class 'float'>
c = 10+3j
type(c)#<class 'complex'>
d = True
type(d)#<class 'bool'>
e = 'sunny'
type(e)#<class 'str'>

Type Casting:
int(), float(), bool(), complex(), str()

4).complex():
-------------------
Form-1:complex(x):
To convert x into complex number with real part x and imaginary part 0.

complex(10)#(10+0j)
complex(10.5)#(10.5+0j)
complex(True)#(1+0j)
complex(False)#0j
complex('10')#(10+0j)

Form-2:complex(x,y):
To convert x and y into complex number such that x will be real part and y
will be imaginary part

complex(10,3)#(10+3j)
complex(10,-2)#(10-2j)
complex(True,False)#(1+0j)

5.str():
To convert other type values to str type.

str(10)#'10'
str(10.5)#'10.5'
str(True)#'True'
str(10+3j)#'(10+3j)'

Fundamental Data Types vs Immutability:


All fundamental data types are immutable.

2).Collection DT's:
bytes,bytearray,list,tuple,range,set,frozenset,dict,None

1.bytes data type:


To represents a group of byte numbers just like an array.

x = [10,20,30,40]
b = bytes(x)
type(b)#<class 'bytes'>
b[0]#10
b[-1]#40
for i in b:print(i)

Conclusion-1:
The only allowed values for byte data type are 0 to 256.

x = [10,20,30,300]
b = bytes(x)#ValueError: bytes must be in range(0, 256)

Conclusion-2:
Once we create bytes data type, we can't change it values.
It is immutable.

x = [10,20,30]
b = bytes(x)
b[0] = 100 #'bytes' object does not support item assignment

2).bytearray data type:


It is exactly same as bytes data type except that it is mutable.

x = [10,20,30,40]
b = bytearray(x)
type(b)
for i in b:print(i)
10
20
30
40
b[1] = 100
for i in b:print(i)
10
100
30
40

3).list data type:


-------------------------
-->Values should be enclosed with square brackets [ ].
-->Insertion order is preserved.
-->Hetrogenious objects are allowed.
-->Duplicates are allowed.
-->Indexing and slicing are supported.
-->Growable in nature.
-->It is mutable.

You might also like