0% found this document useful (0 votes)
37 views19 pages

Topic 2

The document discusses variables in programming and their key characteristics. Variables are used to store and manipulate data in a program and can be assigned different data types. Variables must be given a valid identifier name and are defined using an assignment statement. The document also discusses variable types, identifiers, and provides an example of variable assignment in Python.
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)
37 views19 pages

Topic 2

The document discusses variables in programming and their key characteristics. Variables are used to store and manipulate data in a program and can be assigned different data types. Variables must be given a valid identifier name and are defined using an assignment statement. The document also discusses variable types, identifiers, and provides an example of variable assignment in Python.
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
You are on page 1/ 19

VARIABLES

What is a variable?
• Backbone of any program
• Storage location
• An associated symbolic name which contains
some known or unknown quantity or
information
Variables

What is your name? Juan Tu

yourName
Variables

What value does the variable ‘yourName’ contain?

Juan Tu
Variables and Constants are objects that a
program manipulates. Variable is a name of the
memory location, which stores a value.

Variables can be compared to empty containers.

Age=5
Age=5
It identifies a variable named age, which
stores data. Variables are used to hold
values. The equal sign (=) represents an
assignment statement and its effect is to
store the value 5 in the memory location,
which goes by the named age.
Variable Assignment
>>> score=85
>>>print(score)
Variable Assignment
>>> score=85
>>>print(score)

Try changing the value 85 to 1000


Variable Assignment
>>> score=85
>>>print(score)

Try changing the value 85 to 1000


Variable Types in Python
Variable may be assigned a value of one type and
then, later reassigned a value of a different type.
>>> value=63.12
>>>print(value)
>>>value=“fundamentals of python programming
>>>print(value)
Identifiers
It is a given name to a variable,function,class or
module. Identifiers may be one or more
characters in the following format:
▪It can be a combination of letters in lowercase (a
to z) or upper case (A to Z) or an underscore (_).
Names like myCountry, other_1, and
good_morning –all are valid examples.
Identifiers
It is a given name to a variable,function,class or
module. Identifiers may be one or more
characters in the following format:
▪An identifier cannot start with a digit but is
allowed everywhere else. 1plus is invalid, but
plus1 is perfectly fine.
Identifiers
It is a given name to a variable,function,class or
module. Identifiers may be one or more
characters in the following format:

▪One cannot use spaces and special symbols, like


!,@,#,$,% and etc. as identifiers
Identifiers
It is a given name to a variable,function,class or
module. Identifiers may be one or more
characters in the following format:

▪Keywords cannot be used as identifiers


Identifiers
It is a given name to a variable,function,class or
module. Identifiers may be one or more
characters in the following format:

▪An identifier can be of any length.


Identifiers
It is a given name to a variable,function,class or module. Identifiers
may be one or more characters in the following format:
▪ Variable names and identifiers are case sensitive

planet=“Earth”
Planet=“Saturn”

>>>print(planet,Planet)
Earth Saturn
Exercise
Create a variable named carname and assign the
value Volvo to it.

Then show the value of carname thru a python


program.
Exercise
What will be the output of the following code?
x=10
y=20
x, y = y,x
print(x,y)
Write if the variable is CORRECT or ERROR
1. Username 5. retval 9. xyz123

2. _temp 6. wage_emp 10. !id_num

3. 8myname 7. $try 11. qty price

4. J_1978 8. @ddress 12. midter_

You might also like