Variable declaration and User inputs



👈Previous Topic


Today we are going to learn
        1. How to declare, initialize and use variables
        2. How to get input from user

Printing the the output

  • You have to just write print(".....")
  • The text to be printed is written within the brackets. Example: print ("Hello World!!!"). 
  • To print the value of a variable just write the variable name within the brackets, ie., print(a). 
  • Note that for printing a variable you must not write them within quotes.
There is no semicolon at the end in python codes !!!! 
Open the jupyter notebook and code the examples given side by side.

Variables

  • Variables are containers that hold values. 
  • They are declared with specific type and they can hold only that type of value. 
  • Here also variables mean the same except that while declaring, type need not be specified. 
  • Here declaration and initialization are combined into a single step. For Example look below.

variable declaration in python
  • This is how we declare and initialize a value in python.
  •  Here, '#' denotes comment line. The lines after '#' are ignored  and are not executed. It is a good practice to write appropriate comments while coding for future reference. 
What happens when you declare a variable....? ever wondered...? 
  • The memory space is reserved for the variable and the name which you give uniquely identifies that memory space till your program is in execution. 
  •  Here 'a' refers to a memory space, 'b' refers to a memory space and so on. 
  •  Since we do not specify the data type, the interpreter allocates memory to the variable according to the value you initialize it. 
  • Here 'a' is an integer so, it would point to a 4 byte memory location. 
  • Character data types take 1 byte and string is an array of characters. 
  • Thus 'c' points to a 4 byte memory location. This is static way of giving values to variables. Now let us see how to get input in run-time.

Reading inputs in Python:

Python is not much complicated as C and C++. No need of  "%d" or '&' symbol to read value for a variable. In python 3 we use input( ) method to read input from users. For example:


The text which is to be displayed to user while getting input is written within either double quotes or single quotes in the brackets. 

NOTE: 
  • whatever inputs you read using the method input( ), are of String type by default. 
  • So to perform arithmetic operations one need to do type conversion. 
  • If you don't perform type conversion, then '+' will result in concatenation instead of adding two values.In the below example, the result will be '53' instead of '8'.


To do type conversion and apply arithmetic operations, just write int ( input( ) ) as below:


Now the output is numeric value '8'. Similarly float values can also be read as inputs using float( input ( ) ). It is not mandatory that you must type convert when you get input. You can convert it later when required also. The code for doing so is :


NOTE: The print( ) method takes string arguments mostly. But in python 3, type conversion in printing step is not necessary. In case of any error while printing an integer or float value, try modifying the code as print(str(variable_name)). Here Str( ) used to convert the given variable into String.

Multiple variable declarations and initialization in single step:

Multiple variable can be initialize with values in a single step as :


For getting input in run-time, we use the method split( )
  • The split() method is used to take multiple inputs.
  •  It splits the input on specified separator. 
  • The separator can be either a space( ), a comma(,), a semicolon(;), a colon(:) or anything. In the example below, it is split on space. A single space( ) is the separator here.


  • You can even split on an alphabet or numbers. In the example below wherever 'a' is encountered, the input is split and stored in consecutive variables. Thus after splitting the variables and their corresponding values are:
a = zzz
b = yyy
c = xxx
                                  


Exercises:
  1. Get 3 integer inputs and apply +,-,* on them and store in different variables. Display them one by one with the the text as addition,subtraction and multiplication.
  2. Try getting inputs using split( ) method and store them on different variables. 
  3. Get the below given sequence as input. Split them on '@' and store them on 4 variables. Now on a new variable concatenate them and display. input: This @is @ python @program.

Try these 3 exercises and let me know if you get any error. I will help you out. Post the errors you get in comment section with your name, so that it might help other people who get the same error.

See you tomorrow with another content!!!


Next Topic👉

Comments