Python-3.4.3/C3/Basic-datatypes-and-operators/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue
Narration
Slide containing title Welcome to the spoken tutorial on "Data types and operators".
Show Slide

Objectives


In this tutorial, we will learn about-
  • Data types in Python like
    • Numbers and
    • Boolean and
  • Operators in Python like
    • Arithmetic operators and
    • Boolean operators
Show Slide

System Specifications

To record this tutorial, I am using
  • Ubuntu Linux 16.04 operating system
  • Python 3.4.3 and
  • IPython 5.1.0
Show Slide

Pre-requisite slide

To practise this tutorial,
  • you should know how to run basic Python commands on the ipython console.
  • If not, see the relevant Python tutorials on this website.
Show Slide


'Numbers'

There are three built-in data types in Python to represent numbers.
  • int
  • float and
  • complex
[Terminal]

ipython3

Let us start ipython.


Open the terminal.


Type ipython3 and press Enter.


From here onwards, remember to press the Enter key after typing every command on the terminal.

Type, a = 13 Let's first see about int.


Type a is equal to 13

Type a


Highlight a

Type a


Now, we have our first int variable a.

Type type(a)


Highlight the output

To see the type of a, type, type inside parentheses a


This means that a is of integer data type.

Type a.<Tab>


Highlight the output

There are some functions associated with the int data type to manipulate it.


These can be explored by typing a dot and then press the tab key.


Here you can see the list of functions.

Type, b = 99999999999999999999


Highlight the command

int data type can hold integers of any size.


Let us see an example.


Type b is equal to 20 times 9


Here we are storing 9, repeated 20 times in b.

Type type(b)


Highlight the output

Type type inside parentheses b


We can see that type of b is integer.

Type p = 3.141592 Next we will see about float data type.


Type p is equal to 3.141592

Type type(p)


Highlight the output

Type type inside parentheses p


We can see that type of p is float.


<<PAUSE>>

Type c = 3.2+4.6j


Highlight the command


Highlight 4.6j

The last data type in the list is complex number.


Type c is equal to 3.2 plus 4.6j


It is just a combination of two floats.


The imaginary part being defined by j notation instead of i.

Type c.<Tab>


Complex numbers have a lot of functions specific to them.


Type c dot and then press the tab key.

Type, c.real


Let us try some of them.


Type c dot real

Type c.imag


Highlight 3.2 and 4.6

Type c dot imag


c dot real gives the real part and c dot imag gives the imaginary part of the complex number.

Type, abs(c) We can get the absolute value of c by typing, abs inside parentheses c


<<PAUSE>>

Type, t = True Python also has Boolean as a built-in type.


To try it out, just type, t is equal to True, T in capital

Type, f = not t Now you can apply different Boolean operations on t.


For example, type f is equal to not t

Type, f To see the value of f, type f
Type, f or t


Highlight the output

Type f or t


The output is True.

Type, f and t


Highlight the output

Type f and t


The output is False.

Type, a=False What if you want to apply one operator before another?


We can use parentheses for precedence.


Let us write a small code to check this out.


Type, a is equal to False

Type, b=True Type, b is equal to True
Type, c=True Type, c is equal to True
Type, (a and b) or c


Now we will check how precedence changes with parenthesis.


Let us try two expressions and their evaluations.


Type, inside parentheses a and b or c


This expression gives the value True.

Type, a and (b or c) Now type a and inside parentheses b or c


It gives the value False.


<<PAUSE>>

Type, 23 + 74 Let's now look at some operators available in Python3.


Python uses plus sign for addition.


Type, 23 plus 74

Type, 23 - 56 Minus sign for subtraction.


Type, 23 minus 56

Type, 45*76 Asterisk sign for multiplication.


Type, 45 asterisk 76

Type, 384/16 slash sign for division.


Type, 384 slash 16

Type, 87% 6 Let us move ahead with operators.


Percentage sign for modulo operation.


Modulo operator gives reminder value.


Type, 87 percentage sign 6

Type, 7**8 Two asterisks for an exponent.


Type, 7 asterisk asterisk 8

Type, a=73 In case, we may want to use the current value of variable in which the result is stored in the expression.


We can do that by putting the operator before equal to.


Let us say, a is equal to 73

Type, a*=34 Type, a asterisk equal to 34
Type, a To see the value of a, type, a
Type, a=73 Again we will assign the same value to a by typing, a is equal to 73
Type, a = a*34 Type, a is equal to a asterisk 34.
Type, a


Highlight commands and value of a

Type a


You can see that both the expressions are giving the same value.

Show Slide

Exercise 1

Pause the video.


Try this exercise and then resume the video.


Find the square root of 3 using Python.

Show Slide

Solution 1

The solution is on your screen.


3 asterisk asterisk 0.5 gives the square root of 3.

Show Slide

Exercise 2

Pause the video.


Try this exercise and then resume the video.


Will you get the same value for the below expressions?

Switch to terminal Switch to the terminal for the solution.
Type, 3**0.5 Type, 3 asterisk asterisk 0.5


This operation gives a float.

Type, 3**1/2 Type, 3 asterisk asterisk 1/2


This operation also gives a float, but different value.


Hence, we will not get the same value for the expressions.

Type, 3**(1/2) To get the same value, type, 3 asterisk asterisk inside parentheses 1/2


We can see that now the values are same.

Show Slide

Summary slide


This brings us to the end of this tutorial.


In this tutorial, we have learnt about,

  • data types in Python like
    • Numbers and
    • Boolean and
  • Operators in Python like
    • Arithmetic Operators and
    • Boolean Operators
Show Slide

Evaluation


Here are some self assessment questions for you to solve


  1. What are the three built-in data types in Python to represent numbers?
  2. Which operator is used to find exponent?
Show Slide


Solutions

And the answers,
  1. The built-in data types are
    1. int
    2. float and
    3. complex
  2. Two asterisks are used to find exponent.
Slide Forum Please post your timed queries in this forum.
Slide Fossee Forum Please post your general queries on Python in this forum.
Slide Textbook Companion FOSSEE team coordinates the TBC project.
Show Slide

Acknowledgement

Spoken Tutorial Project is funded by NMEICT, MHRD, Govt. of India.


For more details, visit this website.

Show Slide

Thank You

This is Priya from IIT Bombay signing off. Thanks for watching.

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Priyacst