
Hello guys, This is the first post on my new blog. I’m just getting this new blog going, so stay tuned for more. Subscribe below to get notified when I post new updates.
Let’s start our discussion on the basics of python.
Keywords and Identifiers
The Keywords are the same way in python too. The Reserved words in python and are CASE SENSITIVE. The Keywords cannot be used as an function name, variable name or any other identifiers.
Identifier is the name given to entities like class, functions, variables etc.,in Python. It helps differentiating one entity from another.
Rules for Writing Identifiers:
- Identifiers can be a combination of letters in lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an underscore (_).
- An identifier cannot start with a digit. 1variable is invalid, but variable1 is perfectly fine.
- Keywords cannot be used as identifiers.
Comments, Indentation and Statements:
we use (#) hash symbol in python comments, which is nothing but we used to make the code more readable and are ignored by compilers and interpreters.
In case of multi-line comments, we use double or triple quotes ”’ or “””
Unlike most of the programming languages like C, C++, Java use braces { } to define a block of code. Python uses indentation.
Providing instructions to python interpreters are statements. we can use multiple statements in a single line using ; (Semi-colon)
Variables and Datatypes:
Variables are the same as in other programming languages, since we don’t have to declare variable types here which can be handled internally in python.
Every value in Python has a datatype. Since everything is an object in Python programming, data types are actually classes and variables which are instance (object) of these classes.
We can use the type() function to know which class a variable or a value belongs to and the isinstance() function to check if an object belongs to a particular class.
a=5 print(a, "is of type",type(a)) a=2.5 print(a, "is of type",type(a)) a=1+2j print(a, "is of type",type(a))
Operators:
In order to carry out arithmetic and logical computations, we can use operators which are special symbols in python.
The following are the basic operator types in python:
- Arithmetic operators
- Comparison (Relational) operators
- Logical (Boolean) operators
- Bitwise operators
- Assignment operators
- Special operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.
+ , -, *, /, %, //, ** are arithmetic operators
Comparison operators are used to compare values. It either returns True or False according to the condition.
>, <, ==, !=, >=, <= are comparison operators
Logical operators are and, or, not operators.
Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit
&, |, ~, ^, >>, << are Bitwise operators
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.
=, +=, -=, *=, /=, %=, //=, **=, &=, |=, ^=, >>=, <<= are Assignment operators
The special operators in python are Identity operator and Membership operator.
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.
in and not in are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).
Control Flows:
1. IF_ELSE Statement:
The if…elif…else statement is used in Python for decision making. Unlike in other programming languages, python interprets non-zero values as True. None and 0 are interpreted as False.
2. WHILE Loop:
The while loop in Python is used to iterate over a block of code as long as the test expression (condition) is true.
#Find the product of all numbers in the list
my_list=[10,20,30,40,50]
product = 1
index=0
while index < len(my_list)
product * = my_list[index]
index += 1
print(product)
3. FOR Loop:
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called traversal.
We can generate a sequence of numbers using range() function.
- range(10) will generate numbers from 0 to 9 (10 numbers).
- We can also define the start, stop and step size as range(start,stop,step size). step size defaults to 1 if not provided.
This function does not store all the values in memory, it would be inefficient. So it remembers the start, stop, step size and generates the next number on the go.
for i in range(10):
print(i)
Thank you for reading my blog , Hope you enjoyed it:)
Looking forward for your valuable comments.