12/29/21, 9:02 AM 1 Basics of Python
Data Types a) Integer
b) String
c) Boolean
In [1]:
type(10)
Out[1]: int
In [2]:
type("hello")
Out[2]: str
In [3]:
type(True)
Out[3]: bool
VARIABLES
A variable is a location in memory used to store some data (value).
when Assigning a variable we
dont need mention the Data Type.
In [4]:
a = 10
In [5]:
b = 10
In [6]:
c = a + b
In [7]:
c # OPTION TO PRINT THE OUTPUT USING VARIABLE NAME
Out[7]: 20
we are using (=) to Assign the values to a variable we can assing int , string and boolen to variables
In [3]:
a = "hello"
b = 10
c = True
d = "welcome to Data Science"
In [10]:
print(a)
print(b)
print(d)
hello
10
welcome to Data Science
Out[10]: True
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 1/6
12/29/21, 9:02 AM 1 Basics of Python
In [18]: type(d)
Out[18]: str
Printing we can have print the output in different ways
In [9]:
# use the variable name to print the output
Out[9]: 'hello'
In [10]:
# use the print command also print the output
print(a)
hello
In [25]:
# Anything which can be writtein in double string will be printed
print("Welcome to Data Science")
Welcome to Data Science
In [11]:
# we user \n to pring the value in new line
print("Welcome to Data \n Science")
Welcome to Data
Science
NUMBERS
Types in Numbers category
Integers
floating point numbers
They are defined as int for Integers float for floating point numbers
We can use the type() function to know which class a variable or a value belongs to and the
isinstance() function to check if an object belongs to a particular class.
a= 10
In [12]:
a=10
type(a)
Out[12]: int
In [13]:
b = 10.2
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 2/6
12/29/21, 9:02 AM 1 Basics of Python
In [14]:
type(b)
Out[14]: float
In [12]:
import random
import numpy as np
#print(random.randrange(1, 10))
In [ ]:
In [20]:
import pandas as pd
In [21]:
import seaborn as sns
BOOLEAN
boolean represent the values of True and False
In [13]:
a = True
In [14]:
type(a)
Out[14]: bool
Strings
In [19]:
s = "hello"
In [20]:
s
#print(s)
Out[20]: 'hello'
In [21]:
s[0]
Out[21]: 'h'
In [22]:
s[1]
'e'
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 3/6
12/29/21, 9:02 AM 1 Basics of Python
Out[22]:
In [47]:
# Indexing start with 0 and will take the values before one value of Upper Limit
# [0:3] --- 0 ,1 ,2 ( or consider N is upper limit then it will consider the values of
s[0:3]
Out[47]: 'hel'
In [23]:
# Negative indexing - last elements starts with -1
s[-1]
Out[23]: 'o'
In [24]:
# size of the string is 5 if we are giving the size more than string size then we will
s[8]
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-24-7028ea1703e6> in <module>
1 # size of the string is 5 if we are giving the size more than string size then w
e will get an error of out of Range
----> 2 s[8]
IndexError: string index out of range
In [ ]:
String Slicing
You can return a range of characters by using the slice syntax.
Specify the start index and the end index, separated by a colon, to return a part of the string.
In [32]:
b = "Hello Every one!"
print(b[2:5])
llo
In [33]:
b[-8:-1]
Out[33]: 'ery one'
String Concatenation
In [25]:
a = "Hello"
b = "World"
c = a + b
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 4/6
12/29/21, 9:02 AM 1 Basics of Python
In [26]: c
Out[26]: 'HelloWorld'
In [38]:
c = a + " " + b
In [39]:
c
Out[39]: 'Hello World'
In [ ]:
Casting
Casting is process to specify a type on to variable
casing in python used as mention below
int()
float()
str()
In [15]:
x = int(10)
y = float(12.2)
z = str("hello")
In [16]:
type(z)
#y
#z
Out[16]: str
In [1]:
# See Some print statements
print("hello world")
hello world
In [2]:
x = 'Datascience'
In [3]:
print ("welcome to",x)
welcome to Datascience
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 5/6
12/29/21, 9:02 AM 1 Basics of Python
In [18]: x = 2
y = 2
print("value of x :",x)
z= x + y
print("sum of x and y is :",z)
value of x : 2
sum of x and y is : 4
Out[18]: 4
In [ ]:
file:///C:/Users/rgandyala/Downloads/1 Basics of Python.html 6/6