LEARNING DATA SCIENCE , PART 3
DATA TYPES IN PYTHON
1. NUMBERS -
Integers i.e whole numbers , not decimal
2. FLOAT
Floats are the decimal numbers . Example - 4.3 , 5.7
3. Complex Number
x + iy , here x = real number and y = imaginery
4. Boolean
Boolean is either true or false
5. SET
Set is a collection of data
Set only contains unique elements , repititions are not allowed.
6. DICTIONARY
It states in the from of key and it's value terms.
{name : 'John , Age:'25' , 'city':25 , 'New year'}
7. STRING
It is the most commonly used data type. Write in between of commas " ".
8. LIST
List is collection of items and it can have duplicate items.
my_list = [1,2,3,4,5]
9. TUPLES
Tuples are immutable ( means we can not modify the code once written).
10. NONE
When the type of data does not exist that is known as NONE .
Now, we will move forward to PYTHON OPERATORS
1. ARITHMETIC OPERATORS
#ADDITION (+)
a + b (operand), here a = operant and b = operator.
# SUBTRACTION ( - )
a = 20
b = 8
result = a - b
print (result)
12
# MULTIPLICATION (*)
a = 3
b = 3
result = a* b
print ( result)
#DIVISION (/ )
a= 24
b = 3
result = a/b
print( result)
(Python always returns answers in float i.e decimals)
# MODULUS (%)
a = 17
b = 4
result = a % b
print ( result)
1 ( output )
#EXPONENTIATION ( ** )
a= 5
b = 2
result = a**b ( **= power)
print (result)
25
~ COMPARSION OPERATORS
# Equal to ( ==)
This operator determines if 2 values are equal or not .
a = 10
b = 5
result = a ==b
print (result)
False.
# Not Equal to ( !=)
a = 3
b = 3
result = a!=b
print(result)
False
# GREATER THAN ( > ):
a = 12
b = 8
result = a > b
print (result)
True
# Less than (<):
a = 5
b = 8
result = a < b
print ( result)
True
# GREATER THAN OR EQUAL TO (>=)
a = 12
b = 12
result = a >= b
print ( result)
# LESS THAN OR EQUAL TO ( <= )N
a = 10
b = 15
result = a < = b
print( result)
True
~ LOGICAL OPERATORS
# Logical AND (and)
x = True
y = False
result = x and y
print ( result)
# Logical OR
x = True
y = False
result = x or y
print (result)
True
# LOGICAL NOT ( NOT)
x = True
result = not X
print(result)
False
~ ASSIGNMENT OPERATORS
Assigning some value to a value
# Assignment (=)
a = 20
# Additional assignment ( +=)
a += 5
print (a)
25
# Multiplication assignment (*=)
a+= 4
print(a)
80
#Division Assignment (/=)
a /= 3
print(a)
33.33333333
~MEMBERSHIP OPERATORS
This tries to search or look up and it returns BOOLEAN ( True or False)
# ' in' operator
text= "Data Science Course is available in PWskills"
result = "Data Science" in text
print(result)
True
# not in operator
text= "Data Science course is available in PWskills "
result = "Data Science" not in text
print (result)
False
# IDENTITY OPERATORS
# 'IS' operator
It is used to check if two items are same ( T or F )
x = 'PWskills'
y = x
result = x is y
print (result)
True
# 'is not' operator
x = 'PWskills'
y = x
result = x is not y
print (result)
False
PYTHON OPERATORS PRECEDENCE AND ASSOCIATIVITY
1. PARANTHESIS ()
It is used to make calculations easier
Example 1 - Using paranthesis to change order of operations
result = (2+3)*4 , paranthesis ensure addition is done first.
Example 2 - Grouping expressions for clarity
total = (10+5) + (8/2)
print (total)
19.0
2. EXPONENTIATION **
This indicates power of the number
Example 1 - Calculating 2 to the power of 3
result = 2 ** 3
print(results)
8
3. BITWISE SHIFT OPERATORS <<(Left shift operator), >> ( Right shift operator)
1. Example - Left Side
result = 8 << 2
print (result)
32
#shifts 8 two bits to the left
2. Example - Right shift
Value = 32
Shifted = Value >> 32
print(shifted)
#shifts 32 two bits to right .
PYTHON CONVERSION
1. Converting integer ( int())
#Convert a float to an integer
float_num = 10.5
int_num = 10.5 (float_num)
print(int_num)
10
# Convert a string to an integer
str_num = "42"
int_str = int(str_num)
print (int_str)
42
----- If you type str_num = 'FIVE' and then try to convert it to integer, it will show error as python can convert word to number.
2. Converting to float (float ())
#Convert an integer to a float
int_num = 34
float_num = Float (int_num)
print (float_num)
type(float_num)
#Convert a string to a float
str_num = "2.12"
float_str = float (str_num)
print(float_str)
2.12
3. Converting to string (str())
# Convert an integer to a string
int_num = 32
str_num = str (int_num)
print(str_num)
32
#Convert a float to a string
float_num = 2.89
str_num = str(float_num)
print (str_num)
2.89
4. Converting to Boolean (bool())
# Convert an integer to a boolean
int_num = 0
bool_num = bool (int_num)
False
#Convert a non- empty string to a boolean
non_empty_str = "PWskills"
bool_str = bool(non_empty_str)
print (bool_str)
#Convert an empty string to a boolean
empty_str = ' '
bool_empty_str = bool(empty_str)
print (bool_empty_str)
-- CONDITIONAL STATEMENTS
1. IF
course = "Data Science"
if course == 'Data Science'
print( "You are on the right track!")
You are on the right track!
Example -
1. Pwskills_ grades = 8
If Pwskills_grades >= 7
print ( "Impressive Pwskills!")
Impressive PWskills
2. marks = 90
if marks >= 90
print ("You got an 'A' grade)
3. number = 42
if number %2 == 0
print ( "The number is even")
4. age = 15
if age >= 18
print( "You are not an adult)
5. Score = 69
passing_score = 70
if score >= passing _score
print ("Congratulations , you passed)
else :
if score >= passing_score
print("You did not pass.")
You almost passed,
2. 'if - elif -else' statement
It is used in situations where you have to choose among multiple choices .
Course = "CS"
if course == 'Data Science'
print ("You are in PWskills Course")
elif Course == "Physics"
print (" You are in physics wallah's course)
else
print ("You are not enrolled in any course")
3. Nested 'if else' Statement
Course = 'Data Science '
grades = 7
if course = = "Data Science"
if grades >= 7
print("Impression skills in Data Science at Pwskills!")
LOOPS
LOOPS are used when we want to repeat some code or to do things multiple times.
1. 'for' loop
# Printing a list of courses
Courses = [ "Data Science" , "Data Analytics" , "Python" , "Javascript" ]
for courses in course:
print(Course)
#Generating a number sequence
Variations of range
1. range(n)
for i in range (5) ....this indicates that it will iteriate 5 times
i = 0 , 1, 2 , 3 , 4
range (n) ---- [ 0,1 ,2 , n-1]
2. range (2,7)
2 - this indicating starting and 7 indicates last one
= [2,3,4,5,6] 5 elements
#Generating a number sequence
for i in range (1,6):
print("Number" , i )
Number : 1
Number : 2
Number : 3
Number : 4
Number : 5
3. range (1, 10, 2) , (It will start from 1 , 2 indicates how many jumps will be there in sequence)
(1,3,5,7,9)
Courses = ["Data Science " , "Data Analytics" , "Python" ]
print (f"Course {count}: {course}"]
count +=1
print(count)
course 1 : Data Science
course 2 : Data Analytics
course 3 : Python
2. Nested 'for' loop
When we introduce a new loop under a loop it is called nesting.
#Multiplication table
for i in range (1,6)
for j in range (1,1,1)
product = i * j
print (f " {i} * {j} = { product}")
print ()
# Printing a pattern
for i in range (5)
for j in range (i + 1)
print(* , end = " " )
print()
for i in range (5):
print (i , end ", ")
0,1, 2, 3, 4
#Printing a pattern
for i in range (5)
for j in range ( i + 1)
print ("*" , end = " " )
print ()
#Creating a matrix -- 2 Dimensions
rows = 3
cols = 3
matrix = []
for i in range(rows)
row= []
for j in range (cols)
row.append ( i * cols + j )
matrix.append (row)
for row in matrix
\print (row)
[ 0,1,2 ]
[3,4 ,5 ]
[6,7,8]
# Matrix multiplication
matrix A = [ [1,2,3] ,[4,5,6] , [7,8,9] ]
matrix B = [ [9,8,7] , [ 6,5,4] , [ 3,2,1]
result = [ [ 0,0,0] , [0,0,0] , [0,0,0]
for i in range (len (matrix - A )
for j in range (len (matrix - B ) [0])
for k in range (len (matrix - B )
result [i] [j] += matrix - A [i][k] * matrix -B [k][j]
for row in result
'WHILE' LOOP
#Printing course names until it meets the conditions
Course = [ " Data Science" , "Data Analytics" , " Python" ]
index = 0
while index < len (courses)
print( courses [index]
index +=1
#Counting Down
count = 5
while count > 0
print (count)
count = 1
# Right triangle
row = 1
while row <= 4
col = 1
while [col < = row :]
print ( '*' , end = " ")
col +=1
print()
row+= 1
#Square
side = 1
while side = 1
width <= 1
while width <= 3
print ("*" , end = " ")
width += 1
print ()
side += 1
So in this
Comments
Post a Comment