Python Number and Strings (Python Datatypes)
Data-types in python
- Integer
- String
- List
- Tuple
- Set
- Dictionary
Python Number
Python Number types Integer, Float, Long, Complex, Binary, Octal, Hexadecimal.
Integer number in Python
a=5
b=50
c=555
d = a+b+c
print(d)#--Output--
610
Floating number in Python
A=10.1
B=50.31
C=500.518
D=A+B+C
print(D)#--Output--
560.928
Binary(0000–1111) number in Python
To access binary value in python you need to put keyword 0b___ and values for binary code
#Direct accessing of binary values
print(0b101)
print(0b1010)#Accessing and converting binary to integer values
aa = int(0b111)
print(aa)#--Output--
5
10
7
Octal(00–07 10–17) number in Python
To access octal value in python you need to put keyword 0o___ and values for octal code
#Direct accessing of octal values
print(0o03)
print(0o07)#Accessing and converting octal to integer values
ab = int(0o11)
ac = int(0o15)
print(ab)
print(ac)#--Output--
3
7
9
13
Hexa(00–07 a–e) number in Python
To access hexa-decimal value in python you need to put keyword 0x___ and values for hexa code
#Direct accessing of hexa values
print(0x01)
print(0x06)#Accessing and converting octal to hexa values
ad= int(0xa)
ae= int(0xe)
af= int(0x7)
print(ad)
print(ae)
print(af)
#--Output--
1
6
10
14
7
Type conversion of Python Numbers
On below we are gonna to convert one format of code to another.
#Converting floating value to integer value
a=10.5
b=int(a)
print(b)#Converting integer value to floating value
c=125
d=float(c)
print(d)#Converting through operation
e=float(c/a)
f = int(c/a)
print(e)
print(f)#Converting integer value to binary, octal and hexa-decimal value
g=5
h=6
i=7
print(bin(g))
print(oct(h))
print(hex(i))#--Output--
10
125.0
11.904761904761905
11
0b101
0o6
0x7
Assignment operations in Python
The result of assignment operations will be in Boolean format.
a=9
b=7
#You can change the value a=9 and b=9
print(a>b)
print(a<b)
print(a>=b)
print(a<=b)
print(a!=b)
print(a==b)
print(a is b)
print(a is not b)#--Output--
True
False
True
False
True
False
False
True
Random Function in Python
You can also create random number using Random function
Note: you need to import that module.
import random
#Print random number
print(random.random())#Print random number with range of 0 - 25
print(random.randrange(0,25))#Print random number multiplying by 10
print(random.random()*10)#Print random number multiplying by 10 and type conversion
print(int(random.random()*10))#--Output--
0.7850906069113407
16
8.79172399680762
0
Maths Function in Python
You can use maths function in Python. There are many math function in python you can search on python math function.
Get Narayanan’s stories in your inbox
Join Medium for free to get updates from this writer.
Note: you need to import that module.
import math
#Ceil round up Floor round down
print(math.ceil(10.02))
print(math.ceil(9.99))
print(math.floor(10.02))
print(math.floor(9.99))
print(round(10.02))
print(round(9.99))
print(round(3.1417326,3))#Finding power
print(math.pow(5,5))#Finding square root
print(math.sqrt(36))#Constant number
print(math.pi)
print(math.e)#--Output--
11
10
10
9
10
10
3.142
3125.0
6.0
3.141592653589793
2.718281828459045
Python String
Strings is are collection of unicode character that are enclosed in single quotation(‘’) or double quotation(“”)
a=”Naren”
b=’Naren’
#c=’Naren” Wrong format
print(a,b)#Using quotation inside quotation
print(‘Hello,”Good Morning”’)
print(“Hello,’Good Morning’”)
# print(“hello”Everyone””) Wrong format#--Output--
Naren Naren
Hello,"Good Morning"
Hello,'Good Morning'
String Querying
c=”Naren”
print(c)
#To select that particular value
print(c[0])
print(c[1])#To select within limit
print(c[0:2])#[:6] from start to 6
#[3:] from 3 to rest value
print(c[:3])
print(c[2:])#To print all values
print(c[:])#--Output--
Naren
N
a
Na
Nar
ren
Naren
String Concatination
Joining of two or more strings are string concatination
aa=”Hello”
ab=”Everyone”
print(aa+ab)
print(aa+” “+ab)
ac=aa+” “+ab
print(ac)#--Output--
HelloEveryone
Hello Everyone
Hello Everyone
String Output Methods in Python
aa=”Hello”
ab=”Everyone”
ac="Hello Everyone"
#Direct printing variable
print(ac)#Using normal output method
print(“The concatination of aa and ab is “,ac)#Using format method
print(“{},Have a nice day”.format(ac))#Using f string method
print(f”String concatination is {ac}”)
message = (f”{aa},” f”{ab}.”)
print(message)#--Output--
Hello Everyone
The concatination of aa and ab is Hello Everyone
Hello Everyone,Have a nice day
String concatination is Hello Everyone
Hello,Everyone.
The / command
#Use single quot using \'
ba = ‘how you doi\’n ‘
print(ba)#New line \n
bb = ‘how \n you doi\’n ‘
print(bb)#Tab space \t
bc = ‘how \t\t you doi\’n’
print(bc)#Back space \b
bd = ‘how \b\b\byou doi\’n’
print(bd)#--Output--how you doi'n
how
you doi'n
how you doi'n
hoyou doi'n
The String Methods
da = “hello world”#Upper case, Lower case & Title
print(da.upper())
print(da.lower())
print(da.title())#To find length
print(len(da))#To find length
print(da.find(‘wor’))#To split element
print(da.split(‘ ‘))#Replace the element
print(da.replace(‘hello’, ‘hola’))#Join element
print(‘+’.join(da))#Strip element
db = “ hello space “
print(db)
print(db.strip())#--Output--HELLO WORLD
hello world
Hello World
11
6
['hello', 'world']
hola world
h+e+l+l+o+ +w+o+r+l+d
hello space
hello space
Our next topics Python List, Python Tuple, Python Set, Python Dictionary
Follow us on
Instagram:- https://www.instagram.com/Coder_033/
Github:- https://github.com/Coder033
Skillshare:- https://www.skillshare.com/profile/Coder033/26410885 Youtube:-https://www.youtube.com/channel/UCjTUcwOms1NJDo2UiIvGfyg










