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

Integers, Floats, and Strings: Session #2

This document discusses various Python data types including integers, floats, strings, and their properties. It covers arithmetic operators and how to perform operations on different data types. String functions like length, concatenation, and accessing characters are described. The use of libraries and importing them is also summarized.

Uploaded by

Congyao Zheng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Integers, Floats, and Strings: Session #2

This document discusses various Python data types including integers, floats, strings, and their properties. It covers arithmetic operators and how to perform operations on different data types. String functions like length, concatenation, and accessing characters are described. The use of libraries and importing them is also summarized.

Uploaded by

Congyao Zheng
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Integers, Floats, and 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

• Strings –str • Sets – set

• 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.

• Floats (decimal numbers)


• A number consisting two parts; a whole part and a fractional part.
• Ex: 1.1, 2.2, 3.3, 0.4, etc

• 32 bit python can store


Arithmetic Operators
• +, -, *, /, **, //, %, abs() ** exponentiation
+ addition
// integer division
- subtraction
% Modulo
* multiplication
abs() absolute value
/ division
** -- Exponentiation
• To the power of
• N to the power of M = N**M

• 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

• ABS(X) is the non negative value of x.


• Ex: abs(-3.5) = 3.5
Some practical work
• We can find the data type of an input by using the type() function.
• Open your “IDLE” shell and try the following
• type(3)
• type (3.0)
• Type(“3.0”)
Lets try some other functions
• Lets try some other functions to convert the given datatypes
• Float(), Int(), and round().

• Ex: type these and see the output


• Float(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.

• Operations between Float and int will result in Float answers


• 2.2 +3 = 5.3
Python Standard Libraries
• Every programming language provides some usable ( most often
used) code so that programmers don’t have to write and rewrite the
same code.
• Number related libraries
• Numbers
• Math
• Cmath
• Decimal
• Fractions
• Random
• Statistics
How to use a library
• Use “import” • How to find all the functions in
• Example using math library a library?
• Use help() function.
• Help(math) will return all the
functions available in math
library.
Import statements
• “import” will establish a link between the calling program and the
standard library.
• Called only once
• In a program
• In the shell
• If you close the shell and open it again you have to run the import
statements again.
string
• Datatype dealing with text (mostly).

• Any input provided to the program is read as a string.


• Try
String basics
• A string is created by enclosing text in quotes.
• “hello”, ‘Hello’,
• “”” this is a long string
Spread over two lines”””

• All inputs to the programs are considered as strings.


• We can change the type of the inputs as needed.
• Empty string: ‘ ‘ - nothing in between the quotes.
String basics
• Length of a string can be identified by using the function “len(<string
name>);”
• Adding two strings (concatenation) use +
• “AB”+”cd” =“ABcd”
• ‘A’+’7’+’B’=‘A7B’
• Repetition use *
• ‘Hi’*2 = ‘HiHi’
String of characters
• My_str = ‘Win college’;
• ‘Win college’ is stored in a string my_str.
• We can access individual characters.
• Print(my_str[0]) → W
• Print(my_str[3]) → ‘ ‘

• 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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy