Variables and Types – Python

We already started Python and covered some interesting topics! Please read the previous posts before starting with Variables and its exercises. Please refer to this link >> Introduction to Python – Say Hello World! << to get a start up!!

Now-a-days we do not have time to waste. We all are busy! And we required some automation tools to reduce the work load and save the time. Python is a good and simple language for this purpose. Bookmark the Index page and read more about Python.

In this post I am going to explain about variables and its usages in Python scripting for SysAdmins. Variables are a temporary storage for any values.

Python is completely object oriented language, and not statically typed. You do not need to declare variables before using them, or declare their type. Every variable in Python is an object.

Dealing with Numbers

Python supports two types of numbers, integers and floating point. Python also support complex numbers, however, this is not necessary as a beginner.

I am using ipython to illustrate it.

You can directly assign any integer to a variable. The common syntax is pasted below:

x = 10
y = 5

Now the value of x is 10 and the value of y is 5. In your script, if you want to use the numbers 10 and 5 in any instance, you can use x and y instead of the actual digit.

See the example pasted below:

x = 10
y = 5
z = x + y
print z

Floating point numbers

You can simply manage floating point number as we did the integer. Please see the example pasted below:

x = 10.04
y = 12.99
z=x+y
print z
23.03

Inserting number while executing the script

You can simply use the function “input” in your Python script to insert an integer to a variable while executing the script. Please see the example pasted below:

x = input("Enter the first no: ")
Enter the first no: 20

y = input("Enter the second no: ")
Enter the second no: 10

z = x + y

print "The sum of 1st and 2nd no is:"
The sum of 1st and 2nd no is:

print z

Dealing with string

In Python, you can use either single or double quote to define a string variable. See the samples explained below:

Tom = 'A nice guy!'

print Tom
A nice guy!
Tom = "A nice guy!"

print Tom
A nice guy!

Advantage of double quote? Inserting strings with apostrophes!!

Yup! Consider this example. You want to insert the following sentence as a variable:

Hey, this is Tom's lab

You can not assign this sentence with single quote. Because, it will break at the apostrophe section..

Please see the example below:

If you wish to use single quotes with apostrophes, you need to use back slash before the actual apostrophe. Please see the example pasted below:

lab = 'Hey, this is Tom\'s lab'

print lab
Hey, this is Tom's lab

Another usage; words with double quote:

cb = "He said, \"Hello\" to me"

print cb
He said, "Hello" to me

Cool!!!

Other examples and usages

Mixing operators numbers and string won’t work together. See the example below:

a = 10  // is integer 
b = 20  // is integer 
c = 'Tom'  // is string 
print (a + b + c)

Assigning more than one variables in the same line

You can assign multiple variables in one line.

x, y, z = 10, 20, 30
print x
10

print z
30

print (x,y,z)
(10, 20, 30)

print (x,z)
(10, 30)

That’s it!!

Post navigation

Arunlal A

Senior System Developer at Zeta. Linux lover. Traveller. Let's connect! Whether you're a seasoned DevOps pro or just starting your journey, I'm always eager to engage with like-minded individuals. Follow my blog for regular updates, connect on social media, and let's embark on this DevOps adventure together! Happy coding and deploying!

2 thoughts on “Variables and Types – Python

Leave a Reply

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