0% found this document useful (0 votes)
8 views

2_1_Console IO, Variables, Data Types (1)

The document provides an overview of Python basics, including console input/output, variables, and data types. It covers how to use the print() function, take user input, and format strings, as well as explains variable assignment and the five standard data types in Python. Additionally, it includes problem sets for practical application of the concepts learned.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

2_1_Console IO, Variables, Data Types (1)

The document provides an overview of Python basics, including console input/output, variables, and data types. It covers how to use the print() function, take user input, and format strings, as well as explains variable assignment and the five standard data types in Python. Additionally, it includes problem sets for practical application of the concepts learned.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 40

Python – Console IO, Variables, Data

Types
Highlights!

• Console IO.

• Variables.

• Data Types.
Console I/O
Running Python in Command-Prompt
Check the current version
of
Python
Opening python

Simple
arithmetic
Print text in the
console
Adding two variables and
Printing their values

Exit
Python
Display values to the Console ()

print() statement prints / displays information in the console.

Literal to be
For instance, printed
print(“Hello Python! This is me trying to learn you!!”)

Output:
Hello Python! This is me trying to learn you!!
Display values to the Console ()
Use the print() Statement to Print a Variable
in Python Output

var = ‘hello world’


hello world
print(var)

Print multiple variables: Use a Comma , to Separate the Variables and


Print Them
Display values to the Console ()
Use the String Formatting With the
Help of % Output
name = ‘Mustafa’
Welcome Mustafa with id 420 to the
id = 420
class
print(“Welcome %s with id %d to the class” %(name,id))

Use the String Formatting With the Help of


{}

name = ‘Mustafa’
Welcome Mustafa with id 420 to the
id = 420
class
print(“Welcome {} with id {} to the class”.format(name,id))
Display values to the Console ()

Use the f-string in the print Statement


Output
name = ‘Mustafa’
Welcome Mustafa with id 420 to the
id = 420
class
print(f“Welcome {name} with id {id} to the class”)
Working with user Input
Taking input from user: input(prompt) function

User input is assigned to this Prompt to


variable user

name = input("Enter your name: ")


print(name)
Input
function
Printing the user input in the
console Output

Enter your name: Jhon


Jhon
Working with user Input
Taking input from user: input(prompt) function
Output

name = input("Enter your name: ") Enter your name: Jhon


print(name) Jhon

How the input function works in Python :

a. When input(prompt) function executes program flow will be stopped until the user has given an
input.
b. The text or message display (i.e., the prompt) on the output screen is optional.
c. Whatever you enter as input, input function convert it into a string.
d. if you enter an integer value still input() function convert it into a string.
e. You need to explicitly convert it into an integer in your code using typecasting.
Comments in your program
✔ A hash sign (#) that is not inside a string literal is the beginning of a comment.
✔ All characters after the #, up to the end of the physical line, are part of the
comment.
✔ The Python interpreter ignores them while executing the code.

Output

#First comment Hello, Python!


print ("Hello, Python!") # second comment

Comments are not


printed!!
Variables
Variable
✔ Variables are nothing but reserved memory locations to store values.
✔ It means that when you create a variable, you reserve some space in the memory.

#Two variables in python


X = 42
Y = 78
Containers
X and Y to
store data
Variable

x=
42 x=
42
y=
42
Variable

x=
42 x=
y= When we change the value 42
42 y=
78
Variable

x = 42
x = “This is text now.”

✔ y variable now points to a new object holding the text.


✔ Previously pointing variable is deleted / removed by
python.
Assigning value to Variable

Outpu
t

Here, 100, 1000.0 and "John" are the values


assigned to counter, miles, and name variables,
respectively.
Multiple value Assignment
Python allows you to assign a single value to several variables simultaneously.

Here, an integer object is created with the value 1, and all the three
a=b=c=1 variables are assigned to the same memory location.
print(a) a
print(b) b 1 Memor
print(c) y
c

You can also assign multiple objects to multiple variables.

1
a, b, c = 1, 2, “john” a
print(a) b 2 Memor
print(b) y
c joh
print(c)
n
Reserved words in Python

✔ This list shows the Python keywords.

✔ These are reserved words, and you


cannot use them as constants or
variables or any other identifier
names.

✔ All the Python keywords contain


lowercase letters only.
Data Types
Standard Data Types

Data Can be of different types! For example,


✔ A person's age is stored as a numeric value
✔ Name is stored as String ..

Therefore, data stored in memory (i.e., variable) can be of many types!

Python has five standard data types-


✔ Numbers
✔ String
✔ List
✔ Tuple
✔ Dictionary
Numbers in Python

Python supports three different numerical types −


✔ int (signed integers)
✔ float (floating point real values)
✔ complex (complex numbers)
Numbers in Python
✔ Integers can be of any length, it is only limited by the memory available.
✔ A floating-point number is accurate up to 15 decimal places.
✔ Integer and floating points are separated by decimal points. 1 is an integer, 1.0 is a floating-point
number.
✔ Complex numbers are written in the form,
x + yj
where x is the real part and y is the imaginary part.
Numbers in Python
✔ Use the type() function to know which class a variable or a value belongs to.
✔ Use the isinstance() function is used to check if an object belongs to a particular
class

a=5
print(a, "is of type", type(a))

a = 2.0
print(a, "is of type", type(a))

a = 1+2j
Outpu
print(a, "is complex number?", isinstance(1+2j,complex))
t
5 is of type <class 'int'>
2.0 is of type <class 'float'>
(1+2j) is complex number? True
String in Python
Strings are arrays of bytes representing Unicode characters.
A string is a collection of one or more characters put in

✔ a single quote name = ‘This is Python Course’


✔ double-quote name = “This is Python Course”
✔ triple quote. name = ‘‘‘This is Python Course’’’

In python there is no character data type, a


character is a string of length one. It is represented
by str class.
String in Python

name1 = ‘This is Python Course’


name2 = “This is Python Course”
name3 = ‘‘‘This is Python Course’’’
Outpu
t
This is Python Course
print(name1)
This is Python Course
print(name2) This is Python Course
print(name3)

Then what is the difference between these Three type of


Quotes??!!!!
String in Python
Then what is the difference between these Three type of
Quotes??!!!!
✔ We can use single quotes or double quotes to represent
strings.
✔ Multi-line strings can be denoted using triple quotes, ''' or """

name1 = ‘This is Python Course’ name1 = “This is Python Course” name3 = '''A multiline

print(name1) text = “This is Jhon’s Laptop” string'‘’

print(name1)

print(text) print(name3)

Outpu
t
This is Python Course This is Python Course A multiline
This is Jhon’s Laptop
string
String in Python
Slice operator ([ ] and [:] ) Beginning index at 0 and Backward index -1.

str = 'Hello World' Outpu


t
print (str) # Prints complete string Hello World!
print (str[0]) # Prints first character of the string H
print (str[2:5]) # Prints characters starting from 3rd to 5th llo
print (str[2:]) # Prints string starting from 3rd character llo World!
print(str[-1]) # # Prints the last character d
print(str[-3:]) # # Prints the last 3 character rld
print(str[:-3]) # # Prints the character until last 3 characters hello wo

0 1 2 3 4 5 6 7 8 9 10

str H e l l o w o r l d
=
-11 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1
Datatype conversion
You can explicitly check and convert the type of a
variable (for built-in data types).

Outpu
t
x = 10
print(type(x)) <class
'int'>
y = ‘Python’ <class
print(type(y)) 'str'>
Checking input types / Typecasting
How the input function works in Python :

Whatever you enter as input, input function convert it into a string.


a. if you enter an integer value still input() function convert it into a string.
b. You need to explicitly convert it into an integer in your code using typecasting.

num = input ("Enter number :") Outpu


print(num) t
name1 = input("Enter name : ")
print(name1)

# Printing type of input value


print ("type of number", type(num))
print ("type of name", type(name1))
Datatype conversion

Outpu
x = input(‘1st number: ’) t
y = input(‘2nd number: ’)
1st number: 10
2nd number: 20
z=x+y
print(‘sum is: ’, z) Sum is: 1020
<class 'str’>
print(type(x)) <class 'str'>
print(type(y))

What went
wrong!!
Datatype conversion

Outpu
x = input(‘1st number: ’)
t
y = input(‘2nd number: ’)
1st number: 10
x = int(x) 2nd number: 20
y = int(y)
Sum is: 30
z=x+y
print(‘sum is: ’, z) <class ‘int’>
<class ‘int'>
print(type(x))
print(type(y))
Problem Set
Problem 1

Print out your full name, your semester, your ID number, your hobby and your favorite movie name. All
of the information should be in separate lines and should appear formatted like below:

Full name: Your full name


ID: Your ID
Semester: Your semester
Hobby: Your hobby
Favorite movie: Movie name
Problem 1-b
Take input from user their name, address, email and phone number. Then print the information on the
screen. Following is the sample input and output:

Name: Mahbubul Syeed


Address: Dohs, Mirpur
Email: [email protected]
Phone: 12345678

Your information is as follows:


Hello Mahbubul Syeed! Your address is: Dohs, Mirpur. You will receive a confirmation email at:
[email protected] and a message to your registered mobile number (12345678).
Problem 2
2. Write a program to simulate a brief chat with an imaginary friend. You and your imaginary friend (let’s call it Replika) will have
about 8-10 sentences worth of conversation combined. You can hardcode what Replika will say to you in the program, however, you
must provide your responses as input using the input() function. The entire conversation should be simulated in one run of the
program. You are not required to use loops. You must print out the conversation like a dialog, as shown below.

Replika: Hey
Enter your response: Hi there
You: Hi there

Replika: How are you today?


Enter your response: I am fine. What about you?
You: I am fine. What about you?

Replika: I am fine. Thank you!


Enter your response: Glad to hear it!
You: Glad to hear it!

… (continue for 3 to 4 more sentences)


Problem 2 - b
Make a simple calculator for two numbers. Take two numbers as input and print the result of simple
arithmetic between then. Following is the simple input and output.

Enter Two numbers..


Enter number 1: 35
Enter number 2: 45

Addition (35 + 45) = 80


Subtraction (35 - 45) = -10
Division (35 / 45) = .777
Multiplication (35 * 45) = 1575
Problem 3
Suppose that you have gone out to hang out with 5 of your friends. You guys ordered pizza which had N slices. Total price of the pizza
is BDT. Y. However, all of you did not share equally e.g. Some ate more slices than the others. And now you’re scratching your heads as
the waiter brings you the bill and you don’t know how much each should pay. Write a program that helps you to figure this out.

N and Y should be inputs to the program. After that, the program will ask how many slices each of you ate. Finally, the program will
output the amount each should pay. Sample output:

Enter the number of slices: 12


Enter the total price of the pizza: 550 Calculating, please wait…

Enter the number of slices you ate: 3


You should pay: BDT. 137.49
Enter the number of slices friend 1 ate: 2
Enter the number of slices friend 2 ate: 3 Friend 1 should pay: BDT. 91.66
Enter the number of slices friend 3 ate: 1 Friend 2 should pay: BDT. 137.49
Enter the number of slices friend 4 ate: 2 Friend 3 should pay: BDT. 45.83
Enter the number of slices friend 5 ate: 1 Friend 4 should pay: BDT. 91.66
Friend 5 should pay: BDT. 45.83
Problem 4
Write a program that takes 5 numbers as input. Display the following information:
a. Sum of the numbers
b. Average of the numbers
c. The quotient of the first number divided by the fifth number
d. The product of the second number with the fourth number divided by the third number
e. (True/False) If any of the numbers are greater than 5
f. (True/False) If all of the numbers are divisible by 13
g. (True/False) If any of the numbers is equal to 42
Problem 5
Suppose you have started working on an hourly basis as a librarian in your institution. You want
to know how much you will earn a month given that you know how many hours you will work per
week and how much you receive per hour. However, whatever amount you earn per month will
have 20% of it deducted and that amount will be instead used to pay part of your tuition fees.
Write a program that finds out the earnings before and after the deduction. Also find out the
yearly earnings (without deduction) and how much of your tuition fees you are paying yourself
annually. Assume that there are 4 weeks per month.

Enter the number of hours you will work per week: 20


Enter the amount you earn per hour: BDT. 150
Monthly earning: BDT. 12000
Monthly earning after deduction: BDT. 9600
Yearly earning: BDT. 144000
Amount paid as tuition fees per year: BDT. 28800

You might also like