2_1_Console IO, Variables, Data Types (1)
2_1_Console IO, Variables, Data Types (1)
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 ()
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
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 ()
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
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.”
Outpu
t
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
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
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
name1 = ‘This is Python Course’ name1 = “This is Python Course” name3 = '''A multiline
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.
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 :
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:
Replika: Hey
Enter your response: Hi there
You: Hi there
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: