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

3 Data Types and Numbers

This document discusses Python data types, numbers, and casting. It identifies the main built-in data types in Python including text, numeric, sequence, mapping, set, boolean, and binary. It explains integers, floats, complex numbers, and how to get and set data types. It also covers strings, including string literals, multiline strings, strings as arrays, slicing strings, concatenation, formatting strings, escape characters, string length and common string methods.

Uploaded by

earl bagain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

3 Data Types and Numbers

This document discusses Python data types, numbers, and casting. It identifies the main built-in data types in Python including text, numeric, sequence, mapping, set, boolean, and binary. It explains integers, floats, complex numbers, and how to get and set data types. It also covers strings, including string literals, multiline strings, strings as arrays, slicing strings, concatenation, formatting strings, escape characters, string length and common string methods.

Uploaded by

earl bagain
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

PYTHON DATA

TYPES, NUMBERS
AND CASTING
At the end of this lesson…

• Identify and list the different built-in data types


• Get and set the data type of a variable
• Differentiate Int, Float and String
Built-in Data Types

• In programming, data type is an


Text Type:
important concept.
Text type: str
• Variables can store data of
Numeric types: int, float, complex
different types, and different
types can do different things. Sequence types; list, tuple, range
Mapping type: dict
Set types: set, frozenset
•Python has the following data
types built-in by default, in these Boolean type: bool
categories: Binary types: bytes, bytearray, memoryview
Getting the Data Type

You can get the data type of any object by using the type()
function.
Setting the Data Type

In Python, the data


type is set when you
assign a value to a
variable:
Setting the Specific Data Type
Python Numbers

There are three numeric types in Python:


•int
•float
•complex
Variables of numeric types are created when
you assign a value to them:
Example

Example:
x = 1 # int
y = 2.8 # float
z = 1j # complex

To verify the type of any object in Python,


use the TYPE function.
int

Int, or integer, is a whole number, positive or negative, without


decimals, of unlimited length.
float

Float, or "floating point number" is a number, positive or negative,


containing one or more decimals.
float

Float can also be scientific numbers with an "e" to


indicate the power of 10.
complex

Complex numbers are written with a "j" as the imaginary part:


Type Comparison

You can convert from one type to another with the


int(), float(), complex() methods:
Random Number

Python does not have a random() function to make number, but


Python has a built-in module called random that can be used to
make random numbers:

Example
Import the random module, and display a random number
between 1 and 9:
Python Casting
Specify a Variable Type

• There may be times when you want to specify a type on to a variable.


This can be done with casting. Python is an object-orientated
language, and as such it uses classes to define data types, including its
primitive types.
• Casting in python is therefore done using constructor functions:
• Int()- constructs an integer number from an integer literal, a float literal (by rounding down to
the previous whole number), or a string literal (providing the string represents a whole
number)
• Float() - constructs a float number from an integer literal, a float literal or a string literal
(providing the string represents a float or an integer)
• Str()- constructs a string from a wide variety of data types, including strings, integer literals
and float literals
Example
float
str
Python Casting
STRING LITERALS

String literals in python are surrounded by either single quotation


marks, or double quotation marks.

'hello' is the same as "hello".


You can display a string literal with
the print() function:
Assign String to a Variable

String literals in python are surrounded by either single quotation


marks, or double quotation marks.
Multiline String

You can assign a multiline string to a variable by using three


quotes:
Strings are Arrays

• Like many other popular programming languages, strings in Python


are arrays of bytes representing unicode characters.
• However, Python does not have a character data type, a single
character is simply a string with a length of 1.
• Square brackets can be used to access elements of the string.
Looping Through a String
Slicing

• You can return a range of characters by using the slice syntax.


• Specify the start index and the end index, separated by a colon, to return
a part of the string.

Example
Get the characters from position 2 to position 5 (not included):
Slice From the Start

• By leaving out the start index, the range will start at the first
character.
Slice To the End

• By leaving out the end index, the range will go to the end:
Negative Indexing

Use negative indexes to start the slice from the end of the string:

Example
Get the characters from position 5 to position 1 (not included), starting the
count from the end of the string:
String Concatenation

To concatenate, or combine, two strings you can use the + operator.


Ex: Merge variable a with variable b into variable c:
Format String

Strings and numbers cannot be combined.


Format String

But we can combine strings and numbers by using the format()


method!
The format() method takes the passed arguments, formats them,
and places them in the string where the placeholders {} are:
Format String

The format() method takes unlimited number of arguments, and are


placed into the respective placeholders:
Format String

You can use index numbers {0} to be sure the arguments are placed
in the correct placeholders:
Format String

Format the price to be displayed as a number with two decimals:


Format String more examples
Escape Character

To insert characters that are illegal in a string, use an escape


character.
An escape character is a backslash \ followed by the character you
want to insert.

VS
Escape Character
String Length

To get the length of a string, use the len() function.

Example
The len() function returns the length of a string:
String Methods

Python has a set of built-in methods that you can use on


strings.

The strip () method removes any whitespace from the


beginning or the end:

Example:
String Methods

The lower() method returns the string in lower case:


Example:

The Upper() method returns the string in upper case:


String Methods
THANK YOU!

You might also like