Integers, Floats, and Strings: Session #2
Integers, Floats, and Strings: Session #2
Strings
This Weeks Lecture
• Session-01 Review
• Integers
• Floats
• Strings
• Summary
• Comments
• Variables
• Input Statements
• Output statements
• Processing
• Definitive loop
Reserved Words (Keywords)
• The following words are the keywords in Python, meaning they have
a specific purpose in the system. So they cannot be used as Variables.
Python Datatypes
• Integers – int • Bytes – bytes
• Real numbers – float
• Lists – list
• Complex numbers – complex
• Byte Arrays – bytearray
• Boolean – bool
• Tuples – tuple
• Dictionaries - dict
Numbers
• Integers
• A number without a fractional component.
• A Whole number.
• Ex: 1,2,3,…
• Python can store a large integer value - 2147483647 for 32 bit system.
• 2 to the power of 3
• 2**3 = 8
// - Integer Division
• A normal division of integer can give you a float result.
• Ex: 8/5 = 1.6
• An integer division for
• positive numbers will give you an integer result by throwing away the floating
part of the answer.
• Ex: 8//5 = 1 (truncates)
• Negative numbers will give you an integer value by rounding the float value.
• Ex: -8//5 =-9//5 = -7//5 = -2 (rounding)
% - Modulo
• Modulo return the remainder from the division.
• Ex: 21%6 = 3
• Int(4.6);
• Round(7.6);
Mixed Arithmetic
• Not possible to do arithmetic with mixed data types,
• strings+int (‘2’ + 3) is not pissible.
• Storage
Sub strings
• We can split the strings into substrings.
• Try the following
• Mystr[0:3]
• My_str[2:6]
The ‘in’ operator
• In – Is used to find if something is in the given string or not.
• If ‘b’ in my_str:
Print(‘your string has character b in it’);
In - example
Next session Lecture
• Reading data from Files so that we don’t have to input data all the time.