PYTHON variables | tutorial 3

Python is a case sensitive language. So when you define a variable you should consider the uppercase and lowercase.

eg: price = 10 is different to PRICE = 10

Important Variable Types and Declaration

price = 10
# price variable type is INTEGER
# price is an identifier for variable and initialised with the value 10

update_price = 20.5
# update_price variable type is FLOAT

is_published = True
# is_published variable type is BOOLEAN
# either True or False (case sensitive first letter should be capital)

name = 'Jasim'
# name variable type is STRING

# – is a comment line. Python interpreted not read this line.

Let’s do one small task.
Variable Declaration Task: We check in a patient named John Toe. He’s 20 years old and is a new patient.

name = 'John Toe'
Age = 20
is_new_patient = True

As of now we successfully learn how to declare the variable in python. Next, we learn about how to get input from users.

Instead of print() method use input() method to get the input from the user.

Filename: app.py

name = input('What is your name? ')
print('Hey! ' + name)
In your terminal, interpreted python will wait for your input. In my case, I type John Carter and then hit enter.

Task 2: Ask two questions – Person’s name and his favorite color. Print the output like “John Toe like violet”.

name = input('What is your name? ')
Color = input('what is your favourite color? ')
print(name + ' like ' + color);
The output of program – python input/print

Type conversion

Type conversion is used to define a variable for calculation. If the number is in string format we need to convert the number string into the integer type.

Example:

birth_year = input('Your birth year? ')
print(type(birth_year))
age = 2020 - birth_year
print('Your age is ' + age)
You will get an error – TypeError: Unsupported operand type(S) for int and str

You get input from the terminal 1994 and the type of 1994 is string identified with the use of type() method.

You get an error in line 20 in the app.py file. It will be shown in the terminal due to the type differentiation. So how to solve this problem. We need to convert the type from string to integer.

How to convert the string to integer

int() – method is used to convert the typed string to an integer.
str() – method is used to convert the integer type to string type.

Solution:

birth_year = input('Your birth year? ')
print(type(birth_year))
age = 2020 - int(birth_year)
print('Your age is ' + str(age))
output: Type conversion example

In the next article let see the strings in python. Go the next continuation on the menu.

One thought on “PYTHON variables | tutorial 3”

  1. Needed to write you the very small observation to give many thanks again for the gorgeous strategies you have featured here. This is quite extremely generous of people like you to allow unreservedly what most people could have supplied for an ebook to end up making some money on their own, particularly since you might have tried it in case you considered necessary. Those concepts likewise worked to be a great way to realize that the rest have a similar dreams just as my own to realize a whole lot more pertaining to this matter. I am certain there are a lot more pleasant times up front for many who take a look at your blog. Jobi Dougie Chemesh

Leave a Reply

Your email address will not be published. Required fields are marked *