Automation Crunch 😎
January 16, 2022

Python Expressions & assignment statements

Posted on January 16, 2022  •  4 minutes  • 814 words

This article offers the basics of Python, python expressions, operators, operations and assignment statements in python. It also covers basic python variables and data types.

Python Expression

>>> 1
1

Errors: Programs will crash if they contain code the computer can’t understand, which will cause Python to show an error message.

For example:

The second line doesn’t follow the rules of English. Similarly, if you enter a bad Python instruction, Python won’t be able to understand it and will display a SyntaxError error message, as shown here:

>>> 1 +
    File "<stdin>", line 1
    1 +
      ^
SyntaxError: invalid syntax
>>> 11 + 1 + * 1
    File "<stdin>", line 1
    11 + 1 + * 1
             ^
SyntaxError: invalid syntax

Math Operators from Highest to Lowest Precedence

The ** operator is evaluated first; the *, /, //, and % operators are evaluated next, from left to right; and the + and - operators are evaluated last (also from left to right). You can use parentheses to override the usual precedence if you need to.

Data Types

A data type is a category for values, and every value belongs to exactly one data type.

Common Data Types:

Data type Examples
Integers -1, 0, 1
Floating-point numbers -1.0, 0.0, 1.0
Strings ‘a’, ‘Hello!’, ‘11 candies’

For example:

String operations:

+ is used on two string values to joins the strings as the string concatenation operator.

For example:

>>> 'Joy' + 'Shen'
'JoyShen'

Note : can only concatenate str (not “int”) to str

The * operator can be used with only two numeric values (for multiplication), or one string value and one integer value (for string replication).

For example:

>>>'Joy' *35
'JoyJoyJoy'

Note : String replication is not used as often as string concatenation. Python will just display an error message, like the following:

>>> 'Joy' * 'Shen'
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
    'Joy' * 'Shen'
"TypeError: can't multiply sequence by non-int of type 'str'"
>>> 'Joy' * 3.0
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
    'Joy' * 3.0
"TypeError: can't multiply sequence by non-int of type 'float'"

Assignment Statements

An assignment statement consists of a variable name, an equal sign (called the assignment operator), and the value to be stored. If you enter the assignment statement elem = 11, then a variable named elem will have the integer value 11 stored in it.

For example:

>>> elem = 11
>>> elem
11
>>> candies = 1
>>> elem + candies
12
>>> elem = elem + 1
>>> elem
11

Variable names:

Valid variable names Invalid variable names
_11 11 (can’t begin with a number)
ACC_BAL ACC_$al (special characters like $ are not allowed)
hello ‘hello’ (special characters like ' are not allowed)
acc_bal acc-balance (hyphens are not allowed)
accBalance acc balance (spaces are not allowed)
elem4 4elem (can’t begin with a number)

Program to print “Hello World” and ask for information such as name

>>> print('Hello, world!')
>>> print('What is your name?')    # ask for their name
>>> myName = input()    # for taking input as a string
>>> print('It is good to meet you, ' + myName) 
>>> print('The length of your name is:')
>>> print(len(myName))    # to check length of string
>>> print('What is your age?')    # ask for their age
>>> myAge = int(input())     # for taking input as integer
>>> print('You will be ' + str(myAge + 1) + ' in a year.')

Comments:

print() Function:

input() Function:

len() Function:

Follow me

You can find me on