Variables expressions and statements in python

Posted on 2 June, 2019 by Administrator

Variables expressions and statements in python

1. Value and type

Value is one of the basic things that a program needs to use, such as a letter or a number. The values we have seen so far include ‘1, 2,3’, and 'Hello, World!'.

These values belong to two different types: 1,2 and 3 are an integer, and 'Hello World' is a string, because 'Hello World' is a sequence of characters that join together. You (and the interpreter) can recognize strings because they are enclosed in quotes.

You can display the number command print


If you are not sure what type of value is, the interpreter can tell you.

<class 'int'>
>>>print(type('Hello World'))
<class 'str'>

It is not surprising that the string (string) belongs to type str and integers belong to int type. The less obvious thing is that decimal numbers belong to a type named float, because these numbers are represented in a form called floating-point.

<class 'float'>

What about values like '1080' and '3.2'? They look like numbers, but they are placed in quotes like strings.

<class 'str'>
<class 'str'>

When you type in large integers, you may want to use commas to group each of the three numbers together, like 1,000,000. This is not a valid integer in Python, but is still true in syntax:

1 0 0

Python interpret 1,000,000 as a list of integers separated by commas, and when printed it put a space between them.

This is the first example where we see a semantic error: the code runs without a reported error, but it does not do "right".

2. Variable
Usually programmers choose a variable name - a meaning name . Variable names are of arbitrary length. They may include letters andnumbers, but must start with a letter. Using printed words is fine, but it is best to start the variable with lowercase letters.

Declare the variable with an assignment statement.

>>>a = 1

You can assign multiple types of values (numbers, strings, array, dictionary) to a variable.

>>>a = 1
>>>a = 'hello world' 
>>>a = [1,2,3]
>>>a = [1.2, 'hello world', 5]

If you type in an integer that starts with a 0 like 0238, you may receive an error message

>>>code = 0238

SyntaxError: invalid token

But if you type command 0237, it doesn’t show any error message

>>>code = 0237

Because Python interprets a number starting with the digit 0 as octal (base 8). However, the only valid octal digits are 0-7, so the 9 in your zip code is seen as invalid. Additionally, if you're using Python 3, the format of octal literals was changed so they begin with 0o now (a zero followed by the lowercase letter o), so you'd still get an error even if you tried to input code = 0238, which would be valid in Python 2.

So in python3 the correct command is:

>>>code = 0o237

You can test it here:

Python 2:


An underscore (_) may appear in a name. It is often used in names containing multiple words, such as my_variable or array_of_variable

If you set an invalid variable name, there will be a syntax error:

>>>123designthing = 'hello world'
SyntaxError: invalid syntax
>>>more@ = 'some thing'
SyntaxError: invalid syntax
>>>class = 'domino'
SyntaxError: invalid syntax

123designthing are invalid because it does not start with a letter. more@ invalid because it contains an invalid character, @. But why is the class wrong?

Because the class is one of Python's keywords. The interpreter uses keywords to recognize the structure of the program, and they cannot be used to name variables.

Python has about 31 keywords

If the interpreter complains about a variable name and you don't know why, check if it is in this list.

3. Operator and operand

Python also supports some common math operators such as:

  • + summation

  • - subtracion

  • * multiplication

  • / division

  • % modulo

But the division operator may not do what you expect:

>>>minute = 59
>>>minute / 60

Minute value is 59, and in ordinary algebra 59 is divided by 60 equal to 0.98333, not 0. The reason for the difference is that Python has done rounding down to 2. When both operands are integers, the result will also be an integer; Rounding down cuts off the decimal part, so in this example the result is rounded down to 0. If one of the two operands is a decimal part, Python will perform the decimal division, and the result is a decimal (float):

>>>minute / 60.0

4. Boolean and logical operators

Correct and false values are respectively is: True and False

  • not - to reverse the value.

  • and - logical operations and

  • or - logical operations or

Some common comparisons like <(smaller), <= (less than or equal to),> (greater than),> = (greater than or equal to), == (equal),! = (Other) to compare 2 values. Example:

x = 2
1 < x < 3 #True
10 < x < 30 #False
3 > x <= 2 # True
2 == x < 4 # True

The element test operator in a set: - 'in' checks whether exist - 'not in' does not exist

Tags: python3, python


New blog posts

String and number in python
String and number in python

8 June, 2019 by Administrator

1. String data type All programming...

Variables expressions and statements in python
Variables expressions and statements in python

2 June, 2019 by Administrator

1. Value and type Value is one of...

First python hello world
First python hello world

31 May, 2019 by Administrator

1. What is Python: Python is an Interpreter...

View all blog entries →

Give away $50 Digital Ocean