WebQuest

Programming with Python

Variables and Operators

20200423064753ZydaJ.png

Type of variables
Variables are nothing but reserved memory locations to store values. This means that when you create a variable you reserve some space in memory.

Based on the data type of a variable, the interpreter allocates memory and decides what can be stored in the reserved memory. Therefore, by assigning different data types to variables, you can store integers, decimals or characters in these variables.

Assigning Values to Variables

Python variables do not need explicit declaration to reserve memory space. The declaration happens automatically when you assign a value to a variable. The equal sign (=) is used to assign values to variables.

Integer Variable

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:

Float Variable

The float () method is used to return a floating-point number from a number or a string. Syntax:

float(x)
The method only accepts one parameter and that is also optional to use. Let us look at the various types of argument, the method accepts:
1. A number: Can be an Integer or a floating-point number.

2. A String:
· Must contain numbers of any type.
· Any left or right whitespaces or a new line is ignored by the method.
· Mathematical Operators can be used.
· Can contain NaN, Infinity or inf (any cases)
Values that the float () method can return depending upon the argument passed
· If an argument is passed, then the equivalent floating-point number is returned.
· If no argument is passed then the method returns 0.0.
· If any string is passed that is not a decimal point number or does not match to any cases mentioned above then an error will be raised.
· If a number is passed outside the range of Python float then Overflow Error is generated


Strings Variable

Strings in Python are identified as a contiguous set of characters represented in the quotation marks. Python allows for either pairs of single or double quotes. Subsets of strings can be taken using the slice operator ( [ ] and [:] ) with indexes starting at 0 in the beginning of the string and working their way from -1 at the end.
There are three ways you can declare a string in Python: single quotes ('), double quotes ("), and triple quotes ("""). In all cases, you start and end the string with your chosen string declaration. For example:

>>> print ('I am a single quoted string')
I am a single quoted string
>>> print ("I am a double quoted string")
I am a double quoted string
>>> print ("""I am a triple quoted string""")
I am a triple quoted string

You can use quotation marks within strings by placing a backslash directly before them, so that Python knows you want to include the quotation marks in the string, instead of ending the string there. Placing a backslash directly before another symbol like this is known as escaping the symbol. Note that if you want to put a backslash into the string, you also have to escape the backslash, to tell Python that you want to include the backslash, rather than using it as an escape character.



OPERATORS

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand. For example:


Arithmetic operators

An arithmetic operator is a mathematical function that takes two operands and performs a calculation on them. They are used in common arithmetic and most computer languages contain a set of such operators that can be used within equations to perform a number of types of sequential calculation. Basic arithmetic operators include:

Addition (+)

Subtraction (-)

Multiplication (×)

Division (÷)

Relational operators

These operators compare the values on either side of them and decide the relation among them. They are also called Relational operators.

Logical operators

Logical operators in Python are used for conditional statements are true or false. Logical operators in Python are AND, OR and NOT. For logical operators following condition are applied. For AND operator – It returns TRUE if both the operands (right side and left side) are true For OR operator- It returns TRUE if either of the operand (right side or left side) is true For NOT operator- returns TRUE if operand is false


Assignment operators

Assignment operators are used in Python to assign values to variables. a = 5 is a simple assignment operator that assigns the value 5 on the right to the variable a on the left. There are various compound operators in Python like a += 5 that adds to the variable and later assigns the same. It is equivalent to a = a + 5


Attachments


Web Link

Web Link
  • Website
    Description: Variables in Python

The Public URL for this WebQuest:
http://zunal.com/webquest.php?w=517406
WebQuest Hits: 428
Save WebQuest as PDF

Ready to go?

Select "Logout" below if you are ready
to end your current session.