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 
  • 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 + y
gets 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.
------------------------------------------------------------------------------
EXERCISE
Q1. From the following, find out which assignment statement will produce an error.
State reason(s) too.

  1. x=55
  2. y=037
  3. z=0o98
  4. 56thnumber=3300
  5. length=450.17
  6. !Taylor='Instant'
  7. this variable=87.E02
  8. float=.17E-03
  9. FLOAT=0.17E-03
Q2. Find out the error(s) the following code fragments:

1. temperature=90       
print(temprature)

2. a=30
   b=20
   print(a And b)

3. X=24
   4=X

Comments

  1. Good Morning Mam I am Derick Joseph of 9D

    ReplyDelete
  2. Good morning ma'am
    This is Arnav Trivedy

    ReplyDelete
  3. ma'am, will there be a google meet session today?

    ReplyDelete
  4. Good Morning Ma'am
    Kavyansh Bagotra 9D

    ReplyDelete
  5. Good morning mam I am Sandarbh of class 9-D

    ReplyDelete
  6. Good morning ma'am! Arjav Jain this side.

    ReplyDelete
  7. Good morning ma'am I'm Rishit Kumar

    ReplyDelete
  8. Good Morning Ma"am i am Soumil Arora

    ReplyDelete
  9. Good morning Ma'am I am Piyush Sharma of class 9-D

    ReplyDelete
  10. Good morning ma’am
    My name is Ryan Rahuel Valentine from class 9-D

    ReplyDelete
  11. Good morning ma'am this is Vincent Zacharias.(9D)

    ReplyDelete
  12. ma"am please confirm the difference between 'hello' AND "hello" as this question was marked wrong in my test

    ReplyDelete
  13. Good morning ma'am I am vansh bharti of class 9D

    ReplyDelete
  14. Good morning ma'am
    I am Teshank Pawar of 9D

    ReplyDelete
  15. Good morning mam I am Kritik Kuraria

    ReplyDelete
  16. Good morning ma'am I am samaksh goswami of class 9-D

    ReplyDelete
  17. Good Morning ma'am
    Aaron John

    ReplyDelete
  18. Good morning Ma’am
    I am Aditya Narayan Padhy
    9D

    ReplyDelete
  19. Good morning Ma'am .This is Advait Mohanty of class 9D

    ReplyDelete
  20. I am sending upgraded result as there was an error. pls check you mail in a while
    There will be no GOOGLE Meet session. I have written clearly in the blog

    ReplyDelete
  21. Good morning ma'am. Samarth raj 9D

    ReplyDelete
  22. Good morning ma'am I am Satwik Pruthi Of class 9D

    ReplyDelete
  23. Good morning mam I am Gunamay prasad of class 9 D

    ReplyDelete
  24. Good Morning Ma'am. This is Joshua Walter of Class 9-D.

    ReplyDelete
  25. Good morning ma'am.
    My name is Ojas Khanna

    ReplyDelete
  26. Good Morning Mam I am Abhishek Dial 9 D

    ReplyDelete
  27. Good Morning ma'am.I am Gracious Benny of class 9-D

    ReplyDelete

Post a Comment

Popular posts from this blog

Introduction

Python-Modes

Python-features