Python Variables

Variable Usage

They are used by defining variables in almost all programming languages. In Python, there is no need to define a variable to use a variable.

A variable is created the first time it is used.

x = 5

y = 6

print( x + y )

There is no need to specify the type to use the variable . The type of the variable is automatically determined according to the type of data assigned. Moreover, if the same variable is assigned a different type of value later in the program, the type of the variable will also change.

x = 10

x = "Victory"

print( x )

Above, the numerical value of 10 was assigned to the variable x at the first time, and the type of the variable was created as a number. Then, when a string value is assigned to the same variable x, the type of the variable will automatically change and become a string.

The rules for naming variables in Python are similar to other programming languages.

  • Variable name can contain letters, numbers, or underscore characters.
  • The first character cannot be a number. It must begin with a letter or underscore character.
  • Variable names are case sensitive . X and x can be used as different variables.

The + operator is used to combine text type data.

x = "Victory"

print("Welcome" + x )

If the + operator is used between numbers, the addition operation takes place.

When we use the + operator between a text and a number, we get an error.

a = 5

b = "No:"

print ( b+a ) # will cause an error.

Data in Text (String) Type

We can use single or double quotes to indicate textual expressions in Python. There is no separate type (char type) for characters. Even if the data consists of a single character, its type is string.

Although it is not a character type, the texts are used with the logic of a series of characters, and we can reach the character in the desired position of the text by using square brackets.

x = "itLessons"

print( x[3] )

When the above codes are run, "e" will be written on the screen. Index numbers also start at zero in Python.

A usage like the one below is used to get a part from the text . It is similar to the SubString method in many languages, but with some differences.

x = "itLessons"

print( x[2:6] )

Here, the first number indicates the starting position of the part to be imported. The second number indicates the ending position, but the character at the ending position is not included in the result. Including the start position, the ending position is not. When the above example runs, "Course" is written on the screen. The letter "l" in position 6 is not included in the result.

We will describe other text functions in later topics.

Python Number Types

There are 3 different number types in the Python language.

  • int (used for integers)
  • float (used for decimals)
  • complex (used for complex numbers)

We do not specify this type while writing, it is determined automatically according to the value we give.

Int (Integer) Number Type

It is used for all positive and negative integers. There is no length limit.

number1 = 10

number2 = -15

total = number1 + number2

The type of all three variables above will be set to int.

Float Type (Decimal - Floating Point Numbers) 

Used for positive and negative decimals. A period (.) is used to separate the decimal part.

pi = 3.14

r = 5.0

We can assign values ​​to variables in scientific format . The "e" character is used for this. A whole or decimal number written before the "e" character will be multiplied by 10 to the power specified after the "e" character. For example;

z = 524e4

k = 2.87e3

The value assigned to the variable z above is 524 times 10 to the fourth. As a result, the value of the variable z is 5240000.

The value of the variable k will be 2870.

Complex Type

It allows us to express complex numbers used in mathematics. The character "j" is used when writing the virtual part.

x = 4 + 6j

y = -4j

Casting Operation (Data Type Conversion)

During programming, we may sometimes need to convert a data to a different type from the current one. This is called Casting, and the data types' own constructor methods are used for this operation.

Converting to int type

With the int( ) method, we can convert decimal numbers or text type data to integer type . When decimal numbers are converted to integers, the decimal part will be discarded, that is, it will be rounded down. 

In order for the texts to be converted to integers, they must not contain any characters other than numbers.

x = int(3.9) # variable x will be 3

y = int("55") # variable y will be 55

z = int("3a") # error

a = 4.7

k = int( a ) # variable k will be 4

Converting to float type

With the float( ) method, we can convert integers or strings to float type. Integers are converted to have their decimal parts 0. Texts must have an expression suitable for the decimal number structure in order to be converted.

x = float( 2 ) # variable x will be 2.0

y = "36"

z = float( y ) # variable z will be 36.0

k = float("3.1") # variable k will be 3.1

Converting to String type

The str( ) method is used to convert various types of data to string type.

x = str(55) # variable x will be "55"

y = str(2.5) # variable y will be "2.5"

 

python variable usage, python data type conversion, variable type conversion, python variable definition, variables in python language, python decimal use, python complex number usage

EXERCISES

There are no examples related to this subject.



COMMENTS




Read 1028 times.

Online Users: 225



python-variables