Python basics is just an introduction to the over view of python as a programming language that every programmer should know in order to work well with python.
Python is an interpreted language, meaning the code is executed as soon as its written.
Just like other languages have extensions, python’s extension is .py
To have python run on the local machine, one has to install python from https://www.python.org/
Or from any other source.
Note: during python installation, an earlier version is preferred to the latest version.
During python installation, it should be added to the path.
Once python is installed, Editors should too be installed on the computer. Examples of editors include;
Sublime, vscode, anaconda and extra.
Writing a small program in python
A program that outputs “lux-academy”
where print is a defined function in python used when outputting information, followed by paranthesis inside which a string to executed is place and in quotes.
Variables are reserved memory locations for storing values;
int a = 9;
a is a variable
int is the data type
9 is the value
These are classifications of data items
Data types include the following;
• Sequence Type
These are special reserved words. And thses include the following;
For, if, def, or, else, class, while, break ….
Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.
Operator include the following;
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
Operator Meaning Example
- Add two operands or unary plus x + y+ 2
- Subtract right operand from the left or unary minus x - y- 2
- Multiply two operands x * y / Divide left operand by the right one (always results into float) x / y % Modulus - remainder of the division of left operand by the right x % y (remainder of x/y) // Floor division - division that results into whole number adjusted to the left in the number line x // y ** Exponent - left operand raised to the power of right x**y (x to the power y)
Comparison operators are used to compare values. It returns either True or False according to the condition.
Operator Meaning Example
Greater than - True if left operand is greater than the right x > y
< Less than - True if left operand is less than the right x < y
== Equal to - True if both operands are equal x == y
!= Not equal to - True if operands are not equal x != y
= Greater than or equal to - True if left operand is greater than or equal to the right x >= y
<= Less than or equal to - True if left operand is less than or equal to the right x <= y
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
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.
is and is not are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.
A data structure is a specialized format for organizing, processing, retrieving and storing data.
Lists: these are mutable or items in the list can be changed
Tuples: these are immutable. Meaning items in the tuple cannot be changed.
Dictionary: is an ordered collection of data values, used to store data values like a map, which, unlike other Data Types that hold only a single value as an element, Dictionary holds key:value pair. Key-value is provided in the dictionary to make it more optimized.
Sets: A Set is an unordered collection data type that is iterable, mutable and has no duplicate elements
User-defined data structures;
Functions in python
These are are blocks of organized re-usable set of instruction that are used in performing some related actions.
Recursive function. These are ones where the function calls itself. An example is when dealing with factorials.