Python Variable and Data type

python variable and data type

Python is an object-oriented programing language. Here we learn Python data types and variable from basic. We can’t learn advance python on the first day so we need to learn basic first. In this article, we are learning very beginning of python code.

python code is written in a different editor. If you write python code on text editor then it saves .py extension and runs the code by the following command on terminal.

python3 filename.py

I recommend you to use Jupiter notebook for run python code. because it’s very to use. On Jupiter notebook, you simply run your current cell by press shift enter. If you have any problem to run Jupiter notebook please visit my blog here is a link. And if there any problem with installing python you can also visit here is link of articale. Now let’s begain to code if you have any problem feel free to comment.

Print function

on python3 print, the vale is a very simple task. We just use the print() function to print the value. For example, print Return Script following code is convenience. We can use either ‘ ’, or “ ” on print function according to need.

print('return script')
print("return script")

if you want to print text like hello I’m a Boy use following code.

print("hello I'm a boy")
print('''hello i'm a boy'''

Escape Sequence

An escape sequence is to escape the word by some rules following table show mostly use escape sequence in python.

Escape SequenceManning
\\Back slash
\nNew line
\tTab
\bBack Space
List of Escape Sequence

Comment

Two comments are practices on python. One is at last of code. Which is also call inline comment.’#’ this symbol is used for inline comment. Another for whole code by press Ctrl+/. Here is an example of this.

print("return script")# code for print value

Print emoji

One simple trick to display emoji by python is using emoji code. Every emoji have their code with the help of this code we will print this emoji. The emoji code is available here. You can copy the emoji which you want to print and on print function past it. One thing you need to remember is +sign attach with the emoji code is remove with 000 before print it. Here is an example to print emoji.

print("\U0001F600")
print emoji

Operator

In python different operators practice. Like Addition, Subtraction, Multiplication etc. following table of the operator is most useful in python

OperatorDescriptionExample
+Additionprint(2+3)
Substractionprint(5-2)
*Multiplicationprint(2*3)
/Float Divisionprint(4/2)
//Integer Divisionprint(4//2)
%Moduloprint(6%2)
**Exponentprint(2**3)

Python Variable

One of the most powerful things about a programming language is the ability to manipulate variable. A variable is a name that refers to the value. The assignment operator is used to assign value to a variable. Following example to show how to create and assign variable in python.

variable1='return script'
variable2=12

We can also check the data type of the variable by using type() function. Check the data type of variable1 and 2 as result we get ‘str’ for variable 1 and ‘int’ for variable 2

print(type(variable1))
print(type(variable2))

Following rules are use to define variable name.

  • It’s should start with a letter.
  • It allows the combination of letter, number and character.
  • We can’t create a variable name by number but alphabet follow by number is allowed.
  • it’s allowed to start variable name by _.
  • it does not allow to use the keyword for the variable.

If you give an illegal variable name you got a syntax error. Python allows you to create a dynamic variable. The dynamic mean same variable can use to assign a different value. Following code shows the dynamic variable.

a=2
print(a)
a='return script'
print(a)

Keyword

The keyword is a reserved word python reserve 35 words for a keyword. They are using by an interpreter to recognized the pattern of code. Here is a list of python keyword.

anddelfromNoneTrue
aselifglobalnonlocaltry
assertelseifnotwhile
breakexceptimportorwith
classFalseinpassyield
continuefinallyis asyncraise
defforlambdareturnawait
Python Keyword

Python Data Types

In python, Data type plays an important role. A variable store data of different type. we can check type of the variable by type() function. In the table below mention python data type

Text Typestr
Numeric Typeint, float, complex
Sequence Typelist, tupple, range
Mapping Typedict
Set Typeset
Boolen Typebool
Binary Typebyte, bytearray
python data types

User input

in python data are taken from a user by using input() function. It’s an inbuilt function of python. In general, data taken from a user are in string format. If we need in another format we need to change the data type. Which is also called typecast. A different way to casting data in python. Following example show the simple data input from the user and print the data.

name=input("what is your name?")
print("Hello "+name)

We can also take multiple data from the user in a single line of code. But splitting technique is used here to split different data. We can also pass the split symbol while taking data from the user. For example, if you take name and age from a user by, split then you can throw message by following way “Please Enter Your Name, Aage” and split by using .split(“,”) function. Following code show this problem.

name, age=input("Please Enter name , age").split(',')
print(name)
print(age)
print name and age

Project

Overview of the project takes two data from the user in a single line and convert it into an integer. Because by default it’s in str format. Then Calculate the sum of two number and again display it.

num1,num2=input("please enter two number").split()
print(type(num1),type(num2))
num1=int(num1)
num2=int(num2)
total=num1+num2
print("Sum of Two Number Is "+str(total))
sum of two number

Conclusion

This article is the first part of a python tutorial. If you have any error regarding this feel free to comment below or contact us. I hope you learn something from my article. if you want to free python course here is a link you can visit. And here is a link of AI-related project you can also visit it thank you!.

Leave a Reply