SoFunction
Updated on 2024-11-20

Recognizing and Manipulating Variables in Python

Today I'm going to introduce you to the Number variable in python, which is a bit different from c++ and java, so let's introduce it to you.

You don't have to declare the type of a variable in python, but you do have to assign a value to it before you can use it; without a value it's meaningless, and the compiler won't pass it.

I : Integer -----int.

The usage of int in python is more or less the same as in c++: the

a=12

print a

The printout is :12

I'd like to start by introducing you to a couple of functions

type(): returns the type of the data in parentheses.

a='hello'
print type(a)
>>>><type 'string'>

len(): Returns the length of the variable in parentheses.

Note: You can't use int with the len() method.

a='hello world'
print len(a)
>> >11

II : Floating point type ------float

Floating point consists of an integer part and a decimal part, which can also be expressed in scientific notation.

a=1.23
print type(a)
>> <type 'float'>

III: Plural

Complex numbers are made up of a real part and an imaginary part,you can use a+bj or complex(a,b),the real part a and the imaginary part b of a complex number are floating point type

IV: Data type conversion

int(x) : convert x to an integer

float(x) : converts x to a floating point number

complex(x) : convert x to a complex number with real part x and imaginary part 0

a=1.23

print complex(a)

>>>(1.23+0j)

complex(x,y) : converts x and y to a complex number with the real part being x and x and y being numeric expressions

V: Constants

Two of the more common constants in python are PI and E

PI: the mathematical constant pi (pi).

E: mathematical constant e. i.e. natural number

summarize

The above is a small introduction to the Python language to recognize and operate the variables, I hope to help you, if you have any questions please leave me a message, I will reply to you in time. Here also thank you very much for your support of my website!