Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

2 Python Variables

The document provides an introduction to variables in Python, explaining how to create, print, and change values assigned to them. It covers the syntax for variable creation, the difference between constants and variables, and the importance of naming conventions. Additionally, it emphasizes that variables are case-sensitive and includes examples to illustrate these concepts.

Uploaded by

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

2 Python Variables

The document provides an introduction to variables in Python, explaining how to create, print, and change values assigned to them. It covers the syntax for variable creation, the difference between constants and variables, and the importance of naming conventions. Additionally, it emphasizes that variables are case-sensitive and includes examples to illustrate these concepts.

Uploaded by

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

LEARNING

OBJECTIVES
• Students will be able to learn about the introduction of
variables.
• Learn how to create variables, print variables and change
values assigned to variables
VARIABLES
INTRODUCTION
Storing data allows us to use the
same data later in the program. For
this, we use something
called variables.
PYTHON VARIABLES
In programming, a variable is a container
(storage area) to hold data.

Variables are containers to store data like


numbers and strings.
CREATE VARIABLES
The syntax to create variables is pretty simple.
x = 25
Here,

x is a variable
it is storing an integer value 25
the = operator is used to assign values to variables
x = 25
name = 'Mia‘

Here, we have created two variables:

the x variable is storing an integer value 25


the name variable is storing a string value 'Mia'
PRINTING VARIABLES
 Here's how we print variables in
Python.
Example 1 NOTE: LINE 1
# create a variable
AND LINE 4 ARE
age = 25
# print the variable COMMENTS.
print(age) THEY ARE
IGNORED BY THE
Output: ??? COMPUTER.
 Here,

 age = 25 - the age variable is created and it's storing 25


 print(age) - prints the value stored in the age variable
IF YOU ARE WONDERING WHY PRINT(AGE) IS PRINTING 25
INSTEAD OF AGE, THIS NEXT EXAMPLE WILL HELP CLARIFY
THINGS.

Example
age = 25
print('age')

Output: ???
Example 3
print(age)

Output:???
 print('age') - prints the 'age'
string. The output is age.

 print(age) - prints the Important: Strin


variable (value stored in the g values are
variable). The output is 25. always wrapped
inside quotation
marks, but
variables are not.
CHANGING VALUES STORED
IN VARIABLES
 We can change the value stored in variables as per our needs.

Example 1
age = 25
print(age)

Output: ???
Example 2
age = 26
print(age)

Output: ???
HERE,

 The initial value of age is 25. So, the first


print(age) prints 25.
 Its value is then changed to 26. So, the second
print(age) prints 26.
 Example 3

var = 25
var = 'John'
print(var)
Output: ???
HERE,

 var = 25 - The initial value of var is 25.


 var = 'John' - Its value is changed to 'John'.
 print(var) - When var is printed, its current value is printed.
ANOTHER EXAMPLE.
# create a variable
city1 = 'Paris'
# assign city2 to city1
city1 = city2
print(city1)
Output: ???
 In the program, the line city1 = city2 results in an error.
 It is because we haven't defined the city2 variable before this line.
 So, Python doesn't know what city2 is.

 By the way, city2 is not a string because it's not wrapped inside quotation
marks.
TIP:
Always read the error message
carefully. It may help us fix the error.
In the above output, the error
message is clearly stating that city2
is not defined.
VALUE OF ONE VARIABLE TO
ANOTHER
 It's also possible to assign the value of one variable to another. Let's take an
example.

color1 = "blue“
print(color1)
Output: ???
color2 = "pink"
# Assigning the value stored in color2 to color1
color1 = color2
print(color1)
# Output:???
ASSIGNING MULTIPLE VALUES TO MULTIPLE
VARIABLES

 a, b, c = 5, 3.2, 'Hello'

 print(a) # prints 5
 print(b) # prints 3.2
 print(c) # prints Hello
RULES FOR NAMING PYTHON VARIABLES

1. Constant and variable names should have a combination of letters in


lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore
(_).
For example:
snake_case
MACRO_CASE
camelCase
CapWords

2. Create a name that makes sense. For example, vowel makes more sense
than v.
3. If you want to create a variable name having two words, use underscore
to separate them.
For example:
my_name
current_salary
4. Python is case-sensitive. So num and Num are different variables.
For example,
var num = 5
var Num = 55
print(num) # 5
print(Num) # 55
5. Avoid using keywords like if, True, class, etc. as variable names.
DIFFERENCE BETWEEN CONSTANT AND
VARIABLES

Constant Variables

A constant is a variable or value that cannot be altered once


A variable is a name associated with some memory location.
defined.

A constant is used to hold the fixed values which we can A variable is used to hold some value that can be changed
retrieve later but cannot change. according to the requirement.

The constants are generally stored in the text segment as The variables are stored inside a data segment, heap, or
they are read-only stack depending on the environment it is declared in.

We can only assign a value to the constant while defining it. We can assign value to the variable anytime.

A constant can be defined by A variable can only be defined using the standard variable
using #define or const keyword. definition syntax.

Example: #define pi 3.14 Example: int var = 25;


const int pi = 3.14; var = 10;
SELECTING PROPER VARIABLE NAMES

It's important to build a habit of giving good variable names. Let's see why.

Can you guess which variable name is preferable among these two?
ms = 4969.5
Or
monthly_salary = 4969.5
SO, WHAT MAKES A GOOD VARIABLE NAME?

1. A variable name should be clear and


concise.
2. If the name consists of two or more
words, use underscores to separate
them. By the way, we cannot use spaces
or other symbols in variable names (only
alphabets, numbers and underscore)
VARIABLES ARE CASE-SENSITIVE

By the way, variable names are case-sensitive. Let's take an example to


demonstrate it.
age = 19
print(Age)
Output: ??? NameError: name 'Age' is not defined

Here, print(Age) gives us an error. It's because Python thinks Age is


a different variable than age, and we haven't defined Age.
REVISION: VARIABLE BASICS
 Let's revise what we have learned till now.

Create Variables
 We create variables by assigning values to them.

age = 25
print(age)
Changing Values of Variables
 It's possible to change values stored in variables.

current_salary = 77287.5
print(current_salary)
# Output: 77287.5
# changing value stored in the variable
current_salary = 80000.5
print(current_salary)
# Output: 80000.5
Assign One Variable to Another
 We can also assign one variable to another.

color1 = "blue“
color2 = "pink"
# Assign the value stored in color2 to color1
color1 = color2
print(color1)
# Output: pink
DON'T CONFUSE STRINGS AND VARIABLES

 Strings are always wrapped inside quotation marks but variables are not.

city = 'San Francisco'


# printing the value stored in the city variable
print(city)
# Output: San Francisco
# printing the 'city' string
print('city')
# Output: city

You might also like