Skip to main content

Python for Data Science

Concepts of Python in Data Science


Numbers:  Using the numbers we can do some arithmetic operations on that.

1. Addition of two numbers using print function:
    
    2+3        print(2+3)
    5            5

2. Subtraction of two numbers using print function:

    3-2         print(2-3)
    1            1

3. Multiplication of two numbers using print function:

    3*2        print(2*)
    6            6

4. Division of two numbers using print function: 
    Note: If we use one ("/") this we can get floating point number. Instead of that we use floor division     that gives quotient.
    
    2/1           3/2            3//2      print(3//2)
    1.0           1.5            1          1

5. Modulo of two numbers using print function:
    Note: We use the ("%")  symbol here. It gives the reminder.
    
    3%2       print(3%2)
    1            1

6. Powers of numbers for that we use  ' ** '. We can take any number in power like points also.

    2**3       print(2**3)        3**2          3**0.3
    8             8                         9               1.39089170

Variables in Python:

-    Names cannot start with a number.
-    Names cannot contain spaces, use _ instead.
-    Names cannot contain any special characters.
-    Avoid using Python built-in keywords like list and str 
Example:

     ## 1. Assigning 5 to variable a         ## 2.  a_number = 89                 
     a=5                                                              print(a_number)                      
                                                                         89                                             
                                                                                                                          
     ##3. Boolean Variable
     true = 91
     b=true
     print(b)    ##It gives the 99

Python Variables:

  • A variable is a storage location or a memory location where you data will be stored.

  • Syntax:

variable_name=”Value”

  • Ex:

word=”Hello World”

Integer_variable = 10

String_variable = ”Hello World”

Float_variable =  120.00

List_variable = [‘a’,’b’,’c’]


Python Operators:

  • Operators are nothing but symbols. It performs some operations or some data processing.

  • We have so many types of operators like Arithmetic Operators, Comparison Operators, Assignment Operators, Bitwise Operators and Logical Operators.

  • Arithmetic Operator: It is used to perform Arithmetic Operations like addition, Subtraction, Multiplication, Division.

  • Comparison Operator: It is used to compare the values of both sides.

  • Assignment Operator: It is used to assign values to variables.

  • Bitwise Operator: It is used to compare the values in binary format. These operators work on integers. When performing these operations on integers it will convert into binary format and then they will use some comparison.


If Statement in Python:

  • It has the ability to verify the conditions based on the behaviour of the  program.

  • Syntax:

If (expression):

Do something

  • Ex:

     a=”Hello World”

If “Hello” in a:

print(“Hurray it exists”)

b=[“hello”,”World”]

If “hello” in b:

print(“It will correct”)


If - else  Statement in Python:

  • We can make decision making using the if - else statement.

  • Syntax:

If (test-expression):

Body of the if

Else:

Body of else

  • Ex:

num=3

If num > 0:

print(“Num is positive”)

Else:

print(“Num is Zero”)


 Break and Continue in Python:

  • Break and continue are loop control statements.

  • Ex:

for var in “string”:

    If var==’i’:

                    break

          print(var)

     else:

        print(var)

print(“Hello”)

  • Ex:

for var in “string”:

    If var==’i’:

                    continue

          print(var)

     else:

        print(var)

print(“Hello”)

     

Loops in Python: For and While

  • Looping is nothing but the mechanism of iterating through sequence data types (which are nothing but String, list, tuple,dict) is called looping.

  • What is the need of looping?

Looping is required to repeat and execution block of code a certain number of times.

  • Syntax:

for i in sequence_types:

print(i)

  • Ex:

a=”Hello World” a=range(10)

for i in a: print(a)

print(i)

  • Syntax of While:

while expression:

statement(s)

pass (“pass is a statement which do nothing”)

  • Ex:

count=0

while (count<5):

print(count)

count=count+1

Strings in Python:

  • String is nothing but a set of characters or a collection of characters. It is denoted by a single quote or double quote.

  • a=”Python”

  • a=’Python’

  • Some methods in Strings.

a=”Hello 123 World”

print(a.capitalize()) # First Character always in Capital.

print(a.count()) # We will take some value in which value counts.

print(a.find(‘e’)) # It will give an index position.

print(a.isalnum()) # We have alpha and normal values. We get  True

print(a.isalpha()) # Give the alpha values. We  get True.

print(a.digit()) # Give the digit in string. We get False.

print(a.len()) # Give the length of Characters.

print(a.upper()) # All are Upper case.

print(a.lower()) # All are lower case.


Lists in Python:

  • List is an in-built data type in python which is used to store a list of items.

  • List is created using square brackets  [ ] and inside each item separated with comma (,) in their item have their own index values.

  • List is mutable. That means we change items in the list once created.

  • We can delete the items in the list using the ‘del’ method with their indexes.

  • Lists have some built-in methods or functions.

a=[1,2,3,4,5,6]

del a[1]

print(a) # Then ‘2’ is deleted in the given list.

print(max(a)) # It returns the maximum value in the list.

print(min(a)) # It returns the minimum value in the list. 

print(a.count(‘2’) # It returns the  item how many times it repeated.

print(a.index(4) # it returns the index value of the existing item.

a.append(“Hello”)

print(a) # It means adding some item inside the list.

a.insert(2, “World”)

print(a) # It inserts the values based on the index position.

a.reverse()

print(a) # It simply reverses the items in the list.

a.sort()

print(a) # It arranges the order by using the sort method. Use a.reverse


Tuples in Python:

  • Tuple is an in-built data type in python which stores the items like a list.

  • Tuple is created using parentheses ( ) and inside each item separated with comma , .

  • Tuple is immutable that means we cannot change the tuple once created.

  • Tuple have some built-in methods or functions.

a = ( 1,2,3,4,5,6,7,”Hello”,123,”Raj”)

print(len(a)) # It gives the length of the tuple.

print(max(a)) # It gives the maximum value.

print(min(a)) # It gives the minimum value.

  • How to convert the list into tuples?

a=(1,2,3,4,5,6,7)

b=list(a)

print(b)

  • How to convert the tuple into a list?

b=tuple(a)

print(b)


Comments