June 23, 2021

TriStudy

TriStudy Provide Free Online Tutorials about Programming languages,Digital Marketing, web designing, online earning, web hosting, WordPress etc

Python variables and types.

Python is totally object oriented, and not “statically composed”. You don’t have to announce factors before utilizing them, or proclaim their sort. Each factor in Python is an item.This instructional exercise will go over a couple of fundamental sorts of factors.

print(“Hello, World!”)

output: (“Hello World”)

python.learn.

Numbers.

Use the following syntax to define an integer .

myint = 7 OUTPUT: 7
print(myint) OUTPUT:In [1]:

Use the following syntax to define a floating point number .

myfloat = 9.0 Output:9.0
print(myfloat) Output :9.0
myfloat = float(9) Output : In [1]:
print(myfloat) Output: In [1]:

Strings.

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

mystring = ‘inuya’ Output: inuya
print(mystring)
mystring = “inuya” Output:inuya
print(mystring)

The contrast between the two is that utilizing twofold statements makes it simple to incorporate punctuations (though these would end the string if utilizing single statements).

mystring = “Don’t worry about inuya” OUTPUT: Don’t worry about inuya
print(mystring)

There are extra minor departure from characterizing strings that make it simpler to incorporate things, for example, carriage returns, oblique punctuation lines and Unicode characters. These are past the extent of this instructional exercise, yet are shrouded in the Python documentation.

Basic administrators can be executed on numbers and strings:

two = 2
seven= 7
nine = two + seven
print(nine) OUTPUT : 9

hello = “hello”
world = “world”
helloworld = hello + ” ” + world
print(helloworld) OUTPUT: helloworld

Programs can be done on more than one variable “simultaneously” on the same line like this .

y, i = 2, 9
print(y,i) OUTPUT:2,9

Numbers and strings is not supported when mixing operators are between.

This will not work! ERROR

one = 1
two = 2
hello = “hello”
print(one + two + hello OUTPUT:

Traceback (most recent call last):
File “”, line 6, in
print(one + two + hello)
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

The objective of this activity is to make a string, a whole number, and a coasting point number. The string ought to be named mystring and ought to contain “hi”. The gliding point number ought to be named myfloat and ought to contain the number 10.0, and the whole number ought to be named myint and ought to contain the number 20.

mystring = “hello”
myfloat = 10.0
myint = 20

testing code exercise.

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)

output:
String: hello
Float: 10.000000
Integer: 20

Create constant.py

  • PI = 3.14
  • GRAVITY = 9.8

Create main.py

  • import constant
  • print(constant.PI)
  • print(constant.GRAVITY)

When we run this program, the output will be:

3.14
9.8

In the above program, we make a constant.py module document. At that point, we allot the consistent incentive to PI and GRAVITY. From that point onward, we make a main.py record and import the steady module. At long last, we print the consistent worth.

Note: truly, we don’t utilize constants in Python. The globals or constants module is utilized all through the Python programs.