Skip to content

Refresher: Introduction To Python and Data Types


Agenda

  1. Basics of Python
    • Interpreter
    • First Program
    • Case Sensitive
  2. Arithmetic Calculations
  3. Data Types
  4. Variables
  5. Comments
  6. Type Casting
  7. Lots of Quizzes Quizzes
  8. Dashboard Walkthrough

Introduction to Python

  • Python is a high-level language and it is a very simple language.
  • Python is one of the most used languages.

Python Interpreter

For the Python interpreter, we do not need to install any extra software in our system.

  • You just need to open our browser.
  • Search Colab Google in the browser and open the first link.
  • Create a new notebook.
  • Now blank environment will be opened on your screen.
  • You can simply write code on it and run it.

First Python Program

We will write the first program to print Hello World

print("Hello World")

Then we will run it, then colab will automatically run it, we do not need to install Python explicitly.

Output:

Hello World

But colab sometimes works slowly, so we can also use jupyter notebook for Python programs.

We have learnt one thing from the above program:

  • Anything written in " " will be printed as it is in output.

Let us try to run one more code

print("Hi how are you?")

It will print Hi how are you? as it is.

Output:

Hi how are you?

If we try to print any random content by print(), then that will also be printed as it is:

print("hjasghkjelf2176189*4#$sljhadsfghbdas")

Output:

hjasghkjelf2176189*4#$sljhadsfghbdas


Question

What will be the output of this code snippet? print("10")

Choices

  • 10
  • "10"
  • ten
  • Error

Explanation

print() will not print "", it only prints the content written inside it.

So print("10") will print only 10 as output.

If we do not write " " in print then it will print the variable value.

print(10)

Output:

10

If we do not write " " in print then it will perform arithmetic calculation.

print(10 + 30)

Output:

40

If we write " " in print then it will perform string concatenation.

print("10" + "30")

Output:

1030


Question

Predict the output print("10 + 10")

Choices

  • 20
  • "10 + 10"
  • 10 + 10
  • Error

Explanation

Whatever will be written in "", python will print it as it is without changing anything in it, so it will not perform any addition and print it as it is.

So print("10 + 10") will print only 10 + 10 as output.


Python Is a Case-Sensitive Language

The computer is a very dumb machine, it does not understand instructions, as normal people do so If I say what is 10 + 20 then you will say 30, then if I ask you to spell it, you will say t h i r t y, but programming language does not do like that.

  • Programming language is hardware-driven it will do the things that are programmed only.

If we write prent() instead of print(), then normal people can understand it is a print() and a person wants to print something, but Python would not understand. Also, we forgot to add parentheses in the print statement, then also we got an error.


Arithmetic Calculations in print()

  • We can do substractions in print().
    print(10 - 1)
    

Output:

9   

  • We can also do multiplications in print().
    print(10 * 2)
    

Output:

20


Question

print(10 + 20)

Choices

  • 30
  • 30.00
  • 33

Question

What is the output of the following piece of code? Print(10 * 22)

Choices

  • 220
  • 2200
  • Error
  • 10 * 22

Explanation

Here we have Print(10 * 22) and Python is a case-sensitive language, so it will not understand print with P and it will throw a NameError.


Question

What is the output of the following piece of code? print(8 * 6)

Choices

  • 54
  • 48
  • 8 * 6
  • 86

Primitive data types in Python

Every data is in a certain form, for example, a name is a String.

Let us take an example of Student details,

  • Name: string, we never name as 1, 2, etc, it will always be a string.
  • Roll number: integer
  • marks: decimal(float), marks can be decimal value as sometimes there may be half marks
  • is_absent: True/false, the student is absent or present, so it is a boolean value.

Data types: is a type of data we have in Python programming language.

In Python, we have a function named type, if we pass any data to it, it will print the type of that data.

Integer:

For example:

print(type(23))

Output:

<class 'int'>

It is an integer value so it will print int, just ignore the class part.

Example:

print(type(-122))

Output:

<class 'int'>

-122 is also an integer.

So integer can be any numeric value from - infinity to + infinity without a decimal in it.

Float

If we add a decimal into the integer then it will be a float.

For example:

print(type(12.5))

Output:

<class 'float'>

Example:

print(type(12.0))

Output:

<class 'float'>

If we add .0, then also it is considered as a floating number.

String

Any value inside double inverted " ", or single inverted ' ' commas will be considered as a string.

print(type("abcd"))

Output:

<class 'str'>

Example:

print(type('abcd'))

Output:

<class 'str'>

But you can't do it in a way that starts with a single inverted and ends with double inverted commas. It will give us a syntax error.

Example:

print(type('abcd"))

Output:

ERROR!
File "<string>", line 1
    print(type('abcd"))
               ^
SyntaxError: unterminated string literal (detected at line 1)

Either use both single inverted commas or both double inverted commas.

Multi-line strings

If we write any paragraph or multiple line strings, then it is also considered as a string

Example:

print(type(
"""
Hey!!!
Welcome everyone
"""
))

Output:

<class 'str'>

Boolean

There are two boolean values, True and False, and True means 1 and False means 0.

Example:

print(type(True))
print(type(False))

Output:

<class 'bool'>
<class 'bool'>

None

If we print the type of None, then it will be NoneType.

Example:

print(type(None))

Output:

<class 'NoneType'>


Question

What will be the output of the following? print(type(true))

Choices

  • True
  • False
  • Bool
  • Error

Question

What is the output of the following piece of code? print(type(5.0))

Choices

  • int
  • float
  • str
  • bool

Question

What is the output of the following? Print(type(False))

Choices

  • bool
  • int
  • str
  • Error

Explanation

Print(type(False))

Here p is capital P in print so it gives an error.


Comments

A comment is a way to explain what a code does. In Python comments start with the symbol #.

  • Anything written in comments would not get run.

Example:

# print('hello world')

Nothing will be printed in the output, as we have commented print statement.

Multi-line comments

If we want multi-line comments in Python, then we need to write the # symbol in every line.

Example:

# this
# is
# a multi-line comment

You can also make multi-line comments by writing multi-line strings.

Example:

"""
this
is
a multi-line comment
"""

Variables

Variables are like containers, which allow to store values, or we can say variables are like storage.

Example:

a = 10
Here a is a variable.

We can also change the value of a.

Example:

a = 10
a = 20

But when we print the value of a, it will print the latest value of a.

Example:

a = 10
a = 20

print(a)

Output:

20

And if we check the type of variable a, then it will be an integer.

Example:

a = 10
a = 20

print(a)

print(type(a))

Output:

20
<class 'int'>

In Python, variables take the type of their value

So if we assign a float value to a variable, then its type becomes float.

Example:

pi = 3.14

print(type(pi))

Output:

<class 'float'>

Rules for Naming Variables in Python

  1. A variable name must start with a letter(a-z, A-Z) or the underscore(_) character.
  2. A variable name cannot start with a number.
  3. A variable name can only contain alpha-numeric characters and underscores(means can only have A-Z, a-z, 0-9, and _ ).
  4. Variable names are case-sensitive (age, Age and AGE are three different variables).

a1s = 1, is the correct variable as it starts with a character, does not start with a number, it only has alphanumeric characters and underscores.

Example for last rule:

a = 1
A = 2
print(a)
print(A)

Ouput:

1
2
Both a will be treated differently and print their values.


Question

Which of the following is a valid variable name?

Choices

  • hELLO
  • 1_name_Input
  • a * b * c
  • Cod3$#

Cases for Variables

Cases are just a way to write variables. Let us assume we want to create a variable for storing Shubham marks, then we can have

Camel Case:

shubhamMarks =  10

Title Case:

ShubhamMarks =  10

But in the above two cases, the camel case is preferred more.

Snake Case:

shubham_marks =  10

Example:

thisisavariable = 1234
thisIsAVariable = 1234 # It is more readable in comparison to the above variable name
this_is_a_variable = 4567 # can have an underscore in a variable name
print(thisIsAVariable)
print(this_is_a_variable)

Output:

1234
4567


Type Casting

Casting is converting a variable of one type to another type. For example, if we have some value in a string, but we want it in an integer or float, then we need type casting.

Converting String To integers

If we have some integer value in string form, but we want to convert it to an integer for performing some arithmetic operations, then we have a int() function for it.

For converting string to integer, we just need to wrap in int().

Example:

# Converting String
# To integers
a = "1234"
b = int(a)
print(type(b))

Output:

<class 'int'>

But if we try to convert a string have some string value into an integer then it will give an error.

Example:

a = "welcome"
b = int(a)
print(type(b))

Output:

ValueError Traceback (most recent call last)
Cell In[48], line 2
 1 a = "welcome"
----> 2 b = int(a)
 3 print(type(b))
ValueError: invalid literal for int() with base 10: 'welcome'

Example:

a = "12a"
b = int(a)
print(type(b))

Output:

ValueError Traceback (most recent call last)
Cell In[48], line 2
 1 a = "12a"
----> 2 b = int(a)
 3 print(type(b))
ValueError: invalid literal for int() with base 10: '12a'

Converting String To Float

For converting string to float, we just need to wrap in float().

Example:

# Converting String
# To float
a = "12"
b = float(a)
print(type(b))

Output:

<class 'float'>

Example:

a = "12.5"
b = float(a)
print(b)
print(type(b))

Output:

12.5
<class 'float'>

But if we have two decimal points in the string, then it will give an error.

Example:

a = "12.5.0"
b = float(a)
print(type(b))
print(b)

Output:

ValueError Traceback (most recent call last)
Cell In[53], line 2
 1 a = "12.5.0"
----> 2 b = float(a)
 3 print(type(b))
 4 print(b)
ValueError: could not convert string to float: '12.5.0'

Converting String To Bool

Converting string to bool is a little tricky: - Empty string is False, - Everything else is True.

bool() will convert string to bool.

Example:

a = ""
b = bool(a)
print(type(b))
print(b)

Output:

<class 'bool'>
False

If we even write space in a string, then also its bool value will be true.

Example:

a = " "
b = bool(a)
print(type(b))
print(b)

Output:

<class 'bool'>
True

Converting Integer To String

str() is used for converting Integer To String.

Example:

a = 1234
b = str(a)
print(type(b))
print(b)

Output:

<class 'str'>
1234

If the integer value is 0, then also it will be converted to a string.

Example:

a = 0
b = str(a)
print(type(b))
print(b)

Output:

<class 'str'>
0

Converting Integer To Float

float() is used for converting an integer to float, and it will simply add .0 to the integer value for converting it into float.

Example:

a = 1234
b = float(a)
print(type(b))
print(b)

Output:

<class 'float'>
1234.0

Converting Integer To Bool

bool() is used for converting an integer to bool, - 0 is False, - Everything else is True.

Example:

a = 1234
b = bool(a)
print(type(b))
print(b)

Output:

<class 'bool'>
True

Negative value is also True, as 0 is only False.

Example:

a = -1
b = bool(a)
print(type(b))
print(b)

Output:

<class 'bool'>
True

Converting Float To String

str() is used for converting Float To String.

Example:

a = 3.14
b = str(a)
print(type(b))
print(b)

Output:

<class 'str'>
3.14

Converting Float To Integer

int() is used for converting integers to float, and it will remove everything after decimal.

Example:

a = 3.14
b = int(a)
print(type(b))
print(b)

Output:

<class 'int'>
3

Converting Float To Bool

bool() is used for converting float to bool, - Everything that is 0.0 is False, - Everything else is True.

Example:

a = 0.00000001
b = bool(a)
print(type(b))
print(b)

Output:

<class 'bool'>
True

Because 0.00000001 is also something value above 0.0.

Converting Bool To Integer

  • True will be converted to 1.
  • False will be converted to 0.

Example:

a = True
b = int(a)
print(type(b))
print(b)

Output:

<class 'int'>
1

Example:

a = False
b = int(a)
print(type(b))
print(b)

Output:

<class 'int'>
0

Converting Bool To String

  • True will be converted to True in string.
  • False will be converted to False in the string.

Example:

a = False
b = str(a)
print(type(b))
print(b)

Output:

<class 'str'>
False

Example:

a = True
b = str(a)
print(type(b))
print(b)

Output:

<class 'str'>
True


Question

What is the output of the following? print(bool("0"))

Choices

  • True
  • False
  • Error

Explanation

print(bool("0"))

"0" is a string and only the empty string is false, everything else is true, here we have a string having some value so it will be True.


Question

What is the output of the following? print(bool("apple"))

Choices

  • True
  • False

Question

What is the output of the following? # print("101")

Choices

  • 101
  • "101"
  • Nothing
  • Error

Explanation

# print("101")

Here this print statement is commented, so nothing will be printed in the output.


Question

What is the output of the following? print(int(15.99))

Choices

  • 15
  • 16
  • 15.99
  • Error

Input Statement

Not every value is always available in the program, many times we need to take the values input from the user.

input() is used in Python for taking user from input.

We can also pass the message in input() which will be printed for taking input from the user.

Example:

a = input()
print(a)

Output:

67
67

We can also pass the message "Enter an integer value "

Example:

a = input("Enter an integer value ")
print(a)

Output:

Enter an integer value 67
67

But it should not always be guaranteed that the user will enter an integer value, the user can also enter ten as input.

So it's our responsibility to typecast the user input value according to our requirements.

input() will always return a string value.

Example:

a = input("Enter an integer value ")
print(a)
print(type(a))

Output:

Enter an integer value 67
67
<class 'str'>

If we enter any floating value, then it also be taken as a string.

Example:

a = input("Enter an integer value ")
print(a)
print(type(a))

Output:

Enter an integer value 67.7
67.7
<class 'str'>

If we want to change the type of input value, then we have to do type casting.

Example:

a = input("Enter an integer value ")
b = int(a)
print(b)
print(type(b))

Output:

Enter an integer value 12
12
<class 'int'>

Now if the user gives string value as input then it will throw an error during type casting.

Example:

a = input("Enter an integer value ")
b = int(a)
print(b)
print(type(b))

Output:

Enter an integer value john
ValueError Traceback (most recent call last)
Cell In[82], line 2
 1 a = input("Enter an integer value ")
----> 2 b = int(a)
 3 print(b)
 4 print(type(b))
ValueError: invalid literal for int() with base 10: 'john'


Problem Statement

Take two numbers as input and print the sum of those numbers.

Solution

  1. First we need to take two input user

    a = input()
    b = input()
    

  2. The next step is to do the sum of it.

    a = input()
    b = input()
    print(a + b)
    

Output:

10
20
1020

As input() will take everything as a string, so if we try to add input value, then the string concatenation will be performed by it. 3. So we need to convert the input value into int first, and then we will add those values.

a = int(input())
b = int(input())
print(a+b)

Output:

10
20
30


Question

What is the output of the following?

b = input() # input value = 234
print(type(b))

Choices

  • int
  • string
  • not sure

Extra - Print Statement

Example:

a = 10
b = 20
print(a, b)

Output:

10 20

Example:

a = 10
b = 20
c = 30
print(a, b)
print(c)

Output:

10 20
30

Sep and End

We can pass the sep and end values in the print statement.

  • sep is what should come between the values of the print statement.
  • end: is what should come after the print statement content.

default value of sep is ' '(space) and end is \n(new line character).

Example:

print(13, 134, 134, 1324,134, sep = ' ', end = '\n') #default values

Output:

13 134 134 1324 134

If we pass - as a separator value, then a dash will be added between the values.

Example:

print('John', 'Doe', sep = '-')

Output:

John-Doe

Example:

a = 10
b = 20
print(a, b, sep = '@')

Output:

10@20

\n will work as a new line character, if we pass it as a separator then every value will be printed in a new line.

Example:

a = 10
b = 20
print(a, b, sep = '\n')

Output:

10
20

If we pass ' '(space) as a value of end, then after print statement values it will not go to a new line, it will simply add space after that.

Example:

a = 10
b = 20
c = 30
print(a, b, end = ' ')
print(c)
print(40)

Output:

10 20 30
40

In the above code, we have not mentioned any sep value, so it will be space automatically.

Example:

print(1, 2, 3, 4, sep = '-', end = '+')
print(5)

Output:

1 - 2 - 3 - 4 + 5


Question

What will the output of the following?

a = 5
b = 6
print(a, b)

Choices

  • 56
  • 5
    6
  • 5 6
  • Error

Question

What is the output of the following?

a = 5
b = 6
print(a)
print(b)

Choices - [ ] 56 - [x] 5 6 - [ ] 5 6 - [ ] Error


Question

What is the output of the following?

print("I", "Love", "Robin Dhole Blog", sep = "-")

Choices

  • I Love Robin Dhole Blog
  • I-Love-Robin-Dhole-Blog
  • I-love-robin-dhole-blog
  • Error