π Variables and Objects
Contents
π Variables and Objects#
Variables are containers for storing data values.
Python does note require that you declare variables, they are created once assigned with the =
operator
x = 5
y = "Drexel"
print(x)
5
print(y)
Drexel
You do not need to declare the DataType of a variable it is inferred
x = 5
print(type(x))
<class 'int'>
x = 5.0
print(type(x))
<class 'float'>
x = 'Drexel'
print(type(x))
<class 'str'>
Assigning Strings#
Strings can be assigned with β or β quotes
x = "Drexel"
y = 'Drexel'
You can check if two variable have the same value using the ==
operator
x == y
True
Case Sensitive#
Variables are case sensitive
a = "Drexel"
A = 5
print(a)
print(A)
Drexel
5
Variable Names#
A variable name must start with a letter or the underscore character
A variable name cannot start with a number
A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ )
Variable names are case-sensitive (age, Age and AGE are three different variables)
myvar = "Drexel"
my_var = "Drexel"
_my_var = "Drexel"
myVar = "Drexel"
MYVAR = "Drexel"
myvar2 = "Drexel"
Assigning Multiple Variables#
Python allows you to assign values to multiple variables in one line:
x, y, z = "Drexel", "University", "Engineering"
print(x)
print(y)
print(z)
Drexel
University
Engineering
Objects#
All Python variables are objects who have behaviors based on their datatype.
An object could be a:
Integer
String
Floating point number
Method
Or any other data type
String Objects#
When you define a string there are built-in method that can be applied.
DU = "Drexel University Engineering"
# splits based on a character
print(DU.rsplit(" "))
['Drexel', 'University', 'Engineering']
print(DU.upper())
DREXEL UNIVERSITY ENGINEERING
Float Objects#
num = 500.
num.is_integer()
True
print(type(num))
<class 'float'>