0% found this document useful (0 votes)
2 views23 pages

Lecture 3

Uploaded by

afsara3217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views23 pages

Lecture 3

Uploaded by

afsara3217
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Global Variables

If you create a variable with the same name inside a


function, this variable will be local, and can only be
used inside the function.
The global variable with the same name will remain as
it was, global and with the original value.
Global Variables
Var
Global Variables
The global Keyword
Normally, when you create a variable inside a
function, that variable is local, and can only be used
inside that function.
To create a global variable inside a function, you can
use the global keyword.
Global Variables
Var
Python Data Types
Built-in Data Types
• In programming, data type is an important
concept.
• Variables can store data of different types, and
different types can do different things.
• Python has the following data types built-in by
default, in these categories:
Python Data Types
Built-in Data Types
Python Data Types
Getting Data Types
• You can get the data type of any object by using
the type() function.
Python Numbers
There are three numeric types in Python:

Variables of numeric types are created when you


assign a value to them:
Python Numbers
To verify the type of any object in Python, use the
type() function:
Python Numbers
Int
• Int, or integer, is a whole number, positive or
negative, without decimals, of unlimited length.
Python Numbers
Float
Float, or "floating point number" is a number, positive
or negative, containing one or more decimals.
Python Numbers
Float can also be scientific numbers with an "e" to
indicate the power of 10.
Python Numbers
Complex
Complex numbers are written with a "j" as the
imaginary part:
Python Numbers
Type Conversion
You can convert from one type to another with the
int(), float(), and complex() methods:
Python Numbers
Random Number
Python does not have a random() function to make a
random number, but Python has a built-in module
called random that can be used to make random
numbers:
Python Strings
Strings
Strings 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:
Python Strings
Quotes Inside Quotes
You can use quotes inside a string, as long as they
don't match the quotes surrounding the string:
Python Strings
Assign String to a Variable
Assigning a string to a variable is done with the
variable name followed by an equal sign and the
string:
Python Strings
Multiline Strings
You can assign a multiline string to a variable by using
three quotes:
Python Strings
Multiline Strings
Or three single quotes:
Python Strings
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.
Python Strings
Strings are Arrays
Python Strings
Strings are Arrays

You might also like