Variables and Types (JNNC Technologies)

                                              http://jnnctechnologies.com/
                               https://clanguagetraininginvizagjnnc.blogspot.com/  
                               https://javatrainingjnnctechnologies.blogspot.com/
                                  https://pythontraininginjnnctechnologies.blogspot.com/
                                      https://dotnetjnnctechnologies.blogspot.com/
                                                   https://softwarecoursesselenium.blogspot.com/
                                     https://summerinternshipinjnnctechnologies.blogspot.com/
                                           
https://www.google.com/maps/place/JNNC+Technologies+Pvt.Ltd/@17.725503,83.3016408,17z/data=!3m1!4b1!4m5!3m4!1s0x0:0xd8a1b115babeb73c!8m2!3d17.725503!4d83.3038295




Python is completely object oriented, 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.

This tutorial will go over a few basic types of variables.

Numbers


Python supports two types of numbers - integers and floating point numbers. (It also supports complex numbers, which will not be explained in this tutorial).
To define an integer, use the following syntax:

1
2
myint = 7
print(myint)
To define a floating point number, you may use one of the following notations:

1
2
3
4
myfloat = 7.0
print(myfloat)
myfloat = float(7)
print(myfloat)

Strings

Strings are defined either with a single quote or a double quotes.

1
2
3
4
mystring = 'hello'
print(mystring)
mystring = "hello"
print(mystring)
The difference between the two is that using double quotes makes it easy to include apostrophes (whereas these would terminate the string if using single quotes)

1
2
mystring = "Don't worry about apostrophes"
print(mystring)
There are additional variations on defining strings that make it easier to include things such as carriage returns, backslashes and Unicode characters. These are beyond the scope of this tutorial, but are covered in the Python documentation.
Simple operators can be executed on numbers and strings:

1
2
3
4
5
6
7
8
9
one = 1
two = 2
three = one + two
print(three)
hello = "hello"
world = "world"
helloworld = hello + " " + world
print(helloworld)
Assignments can be done on more than one variable "simultaneously" on the same line like this

1
2
a, b = 3, 4
print(a,b)
Mixing operators between numbers and strings is not supported:

1
2
3
4
5
6
# This will not work!
one = 1
two = 2
hello = "hello"
print(one + two + hello)

Exercise

The target of this exercise is to create a string, an integer, and a floating point number. The string should be named mystring and should contain the word "hello". The floating point number should be named myfloat and should contain the number 10.0, and the integer should be named myint and should contain the number 20.


1
2
3
4
5
6
7
8
9
10
11
12
# change this code
mystring = None
myfloat = None
myint = None
# testing code
if mystring == "hello":
print("String: %s" % mystring)
if isinstance(myfloat, float) and myfloat == 10.0:
print("Float: %f" % myfloat)
if isinstance(myint, int) and myint == 20:
print("Integer: %d" % myint)


0 Comments

'; (function() { var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true; dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js'; (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq); })();