Programming with python
Programming with python
Python Variable
Python Variable is containers that store values. Python is not “statically typed”.
An Example of a Variable in Python is a representational name that serves as a
pointer to an object. Once an object is assigned to a variable, it can be referred
to by that name.
Rules for Python variables
A Python variable name must start with a letter or the underscore
character.
A Python variable name cannot start with a number.
A Python variable name can only contain alpha-numeric characters and
underscores (A-z, 0-9, and _ ).
Variable in Python names are case-sensitive (name, Name, and NAME
are three different variables).
Python Data Types
Data types are the classification or categorization of data items. It represents
the kind of value that tells what operations can be performed on a particular
data. Since everything is an object in Python programming, data types are
classes and variables are instances (objects) of these classes.
Python Operators
In Python programming, Operators in general are used to perform operations
on values and variables. These are standard symbols used for the purpose of
logical and arithmetic operations. In this article, we will look into different
types of Python operators.
Arithmetic Operators
Python Arithmetic operators are used to perform basic mathematical
operations like addition, subtraction, multiplication, and division.
Precedence of Arithmetic Operators
The precedence of Arithmetic Operators in Python is as follows:
1. P – Parentheses
2. E – Exponentiation
3. M – Multiplication (Multiplication and division have the same
precedence)
4. D – Division
5. A – Addition (Addition and subtraction have the same precedence)
6. S – Subtraction
Logical Operators
Python Logical operators perform Logical AND , Logical OR , and Logical
NOT operations. It is used to combine conditional statements.
Bitwise Operators
Python Bitwise operators act on bits and perform bit-by-bit operations. These
are used to operate on binary numbers.
Python If Else
The if statement alone tells us that if a condition is true it will execute a block
of statements and if the condition is false it won’t. But if we want to do
something else if the condition is false, we can use the else statement with the
if statement to execute a block of code when the if condition is false
Example 1 :
output
0
2
4
6
8
Python Functions
Python Functions is a block of statements that return the specific task. The idea
is to put some commonly or repeatedly done tasks together and make a
function so that instead of writing the same code again and again for different
inputs, we can do the function calls to reuse code contained in it over and over
again.
Python Dictionaries
Dictionaries are used to store data values in key:value pairs.
A dictionary is a collection which is ordered*, changeable and do not
allow duplicates.
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and
earlier, dictionaries are unordered.
Dictionaries are written with curly brackets, and have keys and values
Example
Create and print a dictionary:
thisdict = {"brand": "Ford",
"model": "Mustang",
"year": 1964}
print(thisdict)
Python Modules
Python Functions
Python Functions is a block of statements that return the specific task. The
idea is to put some commonly or repeatedly done tasks together and make a
function so that instead of writing the same code again and again for different
inputs, we can do the function calls to reuse code contained in it over and over
again.
Some Benefits of Using Functions
Increase Code Readability
Increase Code Reusability