CS111 2020 SWPart Lect9 Python Datatpes
CS111 2020 SWPart Lect9 Python Datatpes
Data Types
Sabah Sayed
Topics
1. Data Types.
2. Values and Operators.
3. Input Function.
4. Comments.
5. Data Types Conversion.
6. Error Types.
7. Examples.
Remember
print("Seven")
Leaving empty
Notes: The print() function has two default behaviors: lines inside a
•It will always print a "line break" character at the end of program, has no
a line
•It will always print a "space" character between multiple effect in the o/p
arguments
Data types
• Data type
– describes the kind of data being stored
– allows you to perform certain operations on the value (e.g., you can add
or subtract two Integer values)
• Basic Data type In python
– Strings (set of characters)
• str (e.g., xyz, hello123, welcome)
– Numbers (set of digits)
• int (e.g., 5, -5, 100, 10032)
• float (e.g., 5.0, -5.0, 100.99, 0.232132234)
– Logical values(True/False)
• bool
Values and types
• Value: may be number or characters
• Type: category of values
examples:
• 2 is int
• 2.0 is float
• ‘hello world’ is string
– Strings can contain 0 or more printed characters. A
string with zero characters is called an "empty string".
– Specify string in python using single quotes, double
quotes, or triple quotes
– Surround a string between triple-quotes, if you want
to use double quotes inside it
• Note:
– type is a built-in function
Number Operations
• i+j , i-j, i*j
If i and j are both of type int the result
is an int.
If either of them is a float the result is a
float.
• i/j
The result is a float.
Note:
print('h'+'w')
print('h','w')
String Operations
• * : Repeat the original string 'n' times >>> ’atg’ * 3
– i*j If i is of type str and j is of type int, the ’atgatgatg’
result is a str which repeats the string i (‘j’ times) >>> ’tg’ in ’atgatgatg’
– if j is a str or float then an error will appear True
• In : Checks if first string IS in second string >>> ’tc’ in ’atgatgatg’
• not in : Checks if first string IS NOT in second False
string.
Priority of Operators
(i.e., order of execution)
Comments
• Comments: are lines that are not interpreted by the interpreter
• Use # symbol to add Single line comment
• Use of the triple quote delimiter """ for multi line comments
Example Commenting
""" These set of lines makes it easier
will be ignored, which represents multi-line comment for someone to
"""
# the following line will NOT be ignored, this is a single line comment
quickly
print ('hi there') understand how a
program is
designed to
operate.
o/p:
input function
• input is a built-in function:
– accepts a single string as an argument/parameter (i.e., input variables),
then it prompts the user with that string and waits for them to type in a
series of characters
– returns a string
• e.g., user_age = input("How old are you?")
• This is a problem. How can we solve it??
e.g.,
# you can write the previous two lines as one line this is called “Nested calls”
monthly_salary_float = float(input('how much do you make in a month?'))
a = "hello world!"
b = float(a)
Types of Errors
• Syntax errors:
– The code that you wrote does not follow the rules of the language.
– e.g., a single quote is used where a double quote is needed; a keyword is used as a
variable name.
– With a Syntax error your program will not run at all
• Runtime errors:
– your code is fine but the program does not run as expected (it "crashes" at some
point as it is running).
– e.g., if your program is meant to divide two numbers, a run-time error would occur
when the program attempts to divide by zero.
• Logical(semantic) errors:
– the program is correct from a syntax perspective; and it runs; but the result is
wrong.
– For example, if your program prints "2 + 2 = 5" the answer is clearly wrong and your
algorithm for computing the sum of two numbers is incorrect.
What’s type of this error? Syntax Errors
• In a print statement, what happens if you
leave out one of the parentheses?
• Solution:
Examples
1 fruit_one = "Apple"
fruit_two = "Pear"
fruit_three = "Peach"
solution
city1 = "NYC" # assign first variable to "NYC"
city2 = "Chicago" # assign second variable to "Chicago"
city3 = "San Francisco" # assign third variable to "San Francisco"