This article is in a continuation of my previous articles:
Variable and data types in Python
- Variables and data types in Python adhere to standard nomenclature of an alphanumeric name beginning with a letter (A-Z or a-z or 0-9) or underscore ("_").
- Variables do not need to be declared and variable names are case sensitive.
- The datatype of variables in Python is automatically inferred from the assignment statement.
Python currently supports the following data types:
- Boolean
- Integer
- Float
- String
- List
- Tuples
- Dictionary
- Objects
- None
Syntax/Example
Figure 1: Syntax and Example for Variable and data types (input)
Figure 2: Syntax and Example for Variable and data types (Output)
Expressions and Statements in Python
- Some basic statements are allowed in Python as follows:
- print
It is used to output strings, integer or any other datatype.
Example
- print("Jump Start with Python - Part 3")
- Assignment Statement
It is used to assign a value to a statement.
Example
- input / raw_input
It is used to get input values from the user such as int, string, bool and so on.
Example
- x=input("Enter Value")
-
-
- y=int(input("Enter Integer Value"))
-
- Import
It allows importing modules in Python, for example, os, math and so on.
- Passing Statements
Python allows the passing of statements typically in either of two ways and they are explained in the following.
- Single Line Statement
- Multi-Line Statements
- String Quotations
Python allows (') Single, (") Double and (""" or ''') Triple quotes for string literals and they are used at the beginning and ending point of a string respectively.
Example
Triple quotes are used for spanning a string to multiple lines. It is usually used to either get a paragraph as input or output.
Example
Operators in Python
Operators are the most important part of a programming language since they provide and enhance the computational and functional capabilities of a programming language. There are various types of operators available in Python and they are categorized under the following categories.
- Arithmetic Operators
They are used to do arithmetic operations and are as in the following:
- Modulus (%)
- Division (/)
- Multiplication (*)
- Addition (+)
- Subtraction (-)
- Exponent (**)
- Floor Division (//)
Syntax and Example
Figure 3: Syntax and Example for Arithmetic Operators (Input)
Figure 4: Syntax and Example for Arithmetic Operators (Output)
- Relational/Comparison Operators
They are used to relate or compare the values on variables present on both sides of these operators. Relational/Comparison operators are as listed below:
- Equality (==)
- Not Equal (!=)
- Less Than (<)
- Greater Than (>)
- Less Than Equals (<=)
- Greater Than Equals (>=)
Syntax and Example
Figure 5: Syntax and Example for Relational/Comparison Operators (Input)
Note: I will explain about Conditional Programming in Python (If statement) used in this example in another article.
Figure 6: Syntax and Example for Relational/Comparison Operators (Output)
- Bitwise Operators
These operators are also known as Binary Operators. They work on bits and perform bit-by-bit operations and they are listed as in the following:
- Binary AND (&)
- Binary OR (|)
- Binary XOR (^)
- Binary One's Complement (~)
- Binary Left Shift (<<)
- Binary Right Shift (>>)
Syntax and Example
Figure 7: Syntax and Example for Bitwise/Binary Operators (Input)
Figure 8: Syntax and Example for Bitwise/Binary Operators (Output)
- Logical Operators
They are used to perform logical operations and are listed below:
- AND (and)
- OR (or)
- NOT (not)
Syntax and Example
Figure 9: Syntax and Example for Logical Operators (Input)
Figure 10: Syntax and Example for Logical Operators (Output)
- Membership Operators
These operators check for membership in a sequence, such as strings, tuples, dictionary or lists and so on and they are listed as in the following:
Syntax and Example
Figure 11: Syntax and Example for Membership Operators (Input)
Figure 12: Syntax and Example for Membership Operators (Output)
- Identity Operators
These operators are used to compare the memory locations of two objects and they are listed as in the following:
Syntax and Example
Figure 13: Syntax and Example for Identity Operators (Input)
Figure 14: Syntax and Example for Identity Operators (Output)
- Assignment Operators
They are used for assigning values to variables and are listed as in the following:
- Assignment Operator (=)
- Modulus AND (%=)
- Divide AND (/=)
- Multiply AND (*=)
- Add AND (+=)
- Subtract AND (-=)
- Exponent AND (**=)
- Floor Division AND (//=)
Syntax and Example
Figure 15: Syntax and Example for Assignment Operators (Input)
Figure 16: Syntax and Example for Assignment Operators (Output)