Python- Variables(Recap)-Application
Good Morning Boys
In the sessions to come, we shall take a recap of the Python programing concepts covered so far.
Our primary focus today will be on the behaviour of the variables in Python.
As we are revising today, so NO GOOGLE MEET class today.
You are all expected to
---------------------------------------------------------------------------------
By the end of this session, you will be able to
Creating Variables
5
John
Sally
Explanation- Intially, when the first command was executed, variable x was given the value 4 but after the second command is executed, the previous value of variable x is over-written by string "Sally"
Orange
Banana
Cherry
Orange
Orange
Orange
Python is awesome
gets executed, "+" behaves like and adhesive and joins the strings (called CONCATENATION in Python) to give the result
z="Python is awesome"
15
In the sessions to come, we shall take a recap of the Python programing concepts covered so far.
Our primary focus today will be on the behaviour of the variables in Python.
As we are revising today, so NO GOOGLE MEET class today.
You are all expected to
- copy the contents of the blog.
- understand the facts shared
- share your doubts,if any in the COMMENT SECTION.
---------------------------------------------------------------------------------
By the end of this session, you will be able to
- Create Valid variables and identify errors in variable names, if any
- Assign values to a variable and also predict the values that a variable may get in due course of program execution
- Understand the working of multiple assignments and its impact on the multiple variables.
Creating Variables
- Variables are containers for storing data values.
- Unlike other programming languages, Python has no command for declaring a variable.
- A variable is created the moment you first assign a value to it.
Example
x = 5
y = "John"
print(x)
print(y)
The Output will be:5
John
- Variables do not need to be declared with any particular type and can even change type after they have been set.
Example
x = 4 # x is of type int
x = "Sally" # x is now of type str
print(x)
The Output will be:Sally
Explanation- Intially, when the first command was executed, variable x was given the value 4 but after the second command is executed, the previous value of variable x is over-written by string "Sally"
String variables can be declared either by using single or double quotes:
Example
x = "John" # is the same as
x = 'John'
Variable Names
A variable can have a short name (like x and y) or a more descriptive name (age, carname, total_volume). Rules for Python variables:- 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)
Example
#Legal variable names:
myvar = "John"
my_var = "John"
_my_var = "John"
myVar = "John"
MYVAR = "John"
myvar2 = "John"
#Illegal variable names:
2myvar = "John"
my-var = "John"
my var = "John"
Remember that variable names are case-sensitive
Assign Value to Multiple Variables
Python allows you to assign values to multiple variables in one line:
Example
x, y, z = "Orange", "Banana", "Cherry"
print(x)
print(y)
print(z)
The Output will be:Orange
Banana
Cherry
And you can assign the same value to multiple variables in one line:
Example
x = y = z = "Orange"
print(x)
print(y)
print(z)
The output will be:Orange
Orange
Orange
Output Variables
The Python
print
statement is often used to output variables.
To combine both text and a variable, Python uses the
+
character:Example-1
x = "awesome"
print("Python is " + x)
The Output will be:Python is awesome
You can also use the
+
character to add a variable to another variable:Example-2
x = "Python is "
y = "awesome"
z = x + y
print(z)
The Output will be:
Python is awesome
-----------------------------------------------------------------------------------
EXPLANATION
We note that the output of Example-1 and Example-2 is same
but in Example-2, there is a a third variable created named "z".
As, variable "x" and variable "y" are holding string values , so when the command
z = x + ygets executed, "+" behaves like and adhesive and joins the strings (called CONCATENATION in Python) to give the result
z="Python is awesome"
So, when
print(z)
is executed, we get the output as:
Python is awesome
For numbers, the
+
character works as a mathematical operator:Example
x = 5
y = 10
print(x + y)
The output will be 15
If you try to combine a string and a number, Python will give you an error:
Example
x = 5
y = "John"
print(x + y)
The Output will be the following ERROR:
TypeError: unsupported operand type(s) for +: 'int' and 'str'
EXPLANATION
As two different data types are being added using "+" which will result in an error.
------------------------------------------------------------------------------
State reason(s) too.
1. temperature=90
print(temprature)
As two different data types are being added using "+" which will result in an error.
------------------------------------------------------------------------------
EXERCISE
Q1. From the following, find out which assignment statement will produce an error.State reason(s) too.
- x=55
- y=037
- z=0o98
- 56thnumber=3300
- length=450.17
- !Taylor='Instant'
- this variable=87.E02
- float=.17E-03
- FLOAT=0.17E-03
1. temperature=90
print(temprature)
2. a=30
b=20
print(a And b)
3. X=24
4=X
Good Morning Mam I am Derick Joseph of 9D
ReplyDeleteGood morning ma'am
ReplyDeleteThis is Arnav Trivedy
ma'am, will there be a google meet session today?
ReplyDeleteGood Morning Ma'am
ReplyDeleteKavyansh Bagotra 9D
Good morning mam I am Sandarbh of class 9-D
ReplyDeleteGood morning ma'am! Arjav Jain this side.
ReplyDeleteGood morning ma'am I'm Rishit Kumar
ReplyDeleteGood Morning Ma"am i am Soumil Arora
ReplyDeleteGood morning Ma'am I am Piyush Sharma of class 9-D
ReplyDeleteGood morning ma’am
ReplyDeleteMy name is Ryan Rahuel Valentine from class 9-D
Good morning ma'am this is Vincent Zacharias.(9D)
ReplyDeletema"am please confirm the difference between 'hello' AND "hello" as this question was marked wrong in my test
ReplyDeleteGood morning ma'am I am vansh bharti of class 9D
ReplyDeleteGood morning ma'am
ReplyDeleteI am Teshank Pawar of 9D
Good morning mam I am Kritik Kuraria
ReplyDeleteGood morning ma'am I am samaksh goswami of class 9-D
ReplyDeleteGood Morning ma'am
ReplyDeleteAaron John
Good morning Ma’am
ReplyDeleteI am Aditya Narayan Padhy
9D
Good morning Ma'am .This is Advait Mohanty of class 9D
ReplyDeleteI am sending upgraded result as there was an error. pls check you mail in a while
ReplyDeleteThere will be no GOOGLE Meet session. I have written clearly in the blog
Good morning ma'am. Samarth raj 9D
ReplyDeleteGood morning ma'am I am Satwik Pruthi Of class 9D
ReplyDeleteGood morning mam I am Gunamay prasad of class 9 D
ReplyDeleteGood Morning Ma'am. This is Joshua Walter of Class 9-D.
ReplyDeleteGood morning ma'am.
ReplyDeleteMy name is Ojas Khanna
Good Morning Mam I am Abhishek Dial 9 D
ReplyDeleteGood Morning ma'am.I am Gracious Benny of class 9-D
ReplyDelete