- Get link
- X
- Other Apps
(1) WHAT IS VARIABLE ?
Variables are like empty containers used for storing data values. Just like we use a lunchbox to store our lunch in it, we use variables to store data.
• phoneNumber
• In the above example, phoneNumber is a variable used to store your phone number in it.
(2) Create Variables
characterName = "Eva"
• In the above example characterName is the variable and Eva is its value.
• '=' in above example assigns the value on the right to the variable on the left.
# Create a variable userAge with value
# <any number> and then run your code
userAge = 15
(3) Update Variables Concept
• The value of a variable can be changed/updated as many times as required after the variable is created.
• For example, in the last chapter you created a variable:
characterName = "Eva"
• Now if you want to update the name to Pixie you can simply change the value by using "=" sign.
characterName = 'Pixie'
• Now the value of the variable character Name is 'Pixie'
Naming Convention
• The name of a variable is very important. We need to give appropriate names to variables so that it becomes easier to remember and understandable by the person reading the code.
• A Variable name can be as short as a single letter either lowercase or uppercase. It can also start with a letter or an underscore (_) and can only contain alpha-numeric characters and underscores. For example
• characterName
• _userAge
•_my_name
(4) • Variables are case sensitive.character Name and charactername are two different variables.
• Two of these conventions can be used to ease the process of naming a variable. Firstly, starting with a lowercase letter and secondly using camel case.
• Camel Case is a naming convention in which a name is formed of multiple words that are joined together as a single word with the first letter of each of the multiplex
• A Variable name can be as short as a single letter either lowercase or uppercase. It can also start with a letter or an underscore (_) and can only contain alpha-numeric characters and underscores. For example
• characterName
• _userAge
•_my_name
(5) • Variables are case sensitive.character Name and charactername are two different variables.
• Two of these conventions can be used to ease the process of naming a variable. Firstly, starting with a lowercase letter and secondly using camel case.
• Camel Case is a naming convention in which a name is formed of multiple words that are joined together as a single word with the first letter of each of the multiple words capitalised except the first word whose first letter is smallcase. This makes up the variable name to be easily read.
• ageOfStudents
• myName
• myCity
(6) Add Variables
• Multiple variables can be added in python to generate a result. For example: first we will create three variables:
mathMarks = 98 scienceMarks = 95 englishMarks = 96
• Now, we want to create a new variable for storing the sum of these variables. We can do it in this way:
totalMarks = mathMarks + scienceMarks
• The value of the variable totalMarks will be: 289 (98+95 +96)
(7) Copy Variable
• Inorder to copy the value of one variable to another we need to use the '=' sign. For example, lets copy the value of the variable classAScore into classBScore. Code for this will be as follows:
classAScore = 97 classBScore = classAScore
• The value of the variable classBScore will be equal to 97.
- Get link
- X
- Other Apps
Comments