Lesson 10 - Variables: What Is A Variable?
Lesson 10 - Variables: What Is A Variable?
What is a Variable?
A variable is a name (identifier) that is associated with a value,
Num 10
Value vs. Name
Num = 5
VS.
X = num + 10
Variables are assigned values by assignment operator
X=5
So what does:
Num = num + 1
A note on input()
Input() returns a string!
Can I change a type in Python?
Type Conversion
Python provides a built-in type conversion functions
Int() and float()
Line = input(How many credits do you have?)
Num_credits = int(line)
Line = input(What is your GPA ?)
Gpa = float(line)
Line = input(How many credits?)
Num_credits = int(line)
If you enter 24, it is converted to the equivalent integer value, 24, before being
assigned to variable num_credits
Better yet:
Identifiers
An identifier is a sequence of one or more chracters used to provide a name for a
given program element
What is an identifier?
Variable names
Line
Num_creidts
Gpa
Are each identifiers
What is an identifier?
Python is case sensitive, thus, line is different from line
Identifiers may contain letters and digits, but cannot begin with a digit.
Underscore character _ is also allowed to aid in the readability of long identifier
names
Operators
An operator is :
-
A symbol
Represents an operation
Performed on one or more operands
Types of operators
Unary operator operates on only one operand
Negation operator -12
Binary Operator operates on two operands
Addition a + b