Python Basics
We know that you already know a lot about Python and it’s
I have deliberately put screenshots so that you people have to type these commands to practice the syntax of Python 😛
To make sure everyone is on the same page, we will quickly go through the basics of Python:-
1. print(“Hello World”) – print() command to print anything
2. print(“Hello”+” World”) – Plus(+) operator to add two strings
3. Python will throw an error if you do not follow indentation in your code
4. In Python you do not have to declare any variable by a data type
5. There are three types of numeric types supported in Python:-
a. int
b. float
c. complex
Use the type() command to know the data type
6. Multiple occasions will come when you have to
a. int()
b. float()
c. str()
7. Some basic string operations:
8. Following are the operators used in Python:
a. Arithmetic Operator – These include +,-,*,/,%,**(Exponential),//(Floor division)
b. Assignment Operator – These include =,+=,-=, etc.
c. Comparison Operator – These include >,<,<=,>=,!=,etc.
d. Logical Operator – These include and, or, not.
e. Identity Operator – These include is and is not operator
f. Bitwise operator – These include &, |, ^, ~, << and >>
9.
A list can contain any data type.
Functions for List:-
a. len() – To get the length of the list
b. append() – To add an element to the end of a list
c. insert() – To add an element at a desired position
d. remove() – To remove specified element
e. pop() – It removes the last index if nothing is specified
f. del – The keyword del removes the specified index
g. clear() – It empties the list
10. Tuples is an unchangeable and ordered collection.
You can completely delete the tuple, but
11. Set is another data structure in Python which is unordered and unindexed. Sets are defined by curly brackets.
You can add new items. To add one item you can use add() method, and to add multiple items you can use update() method.
len() function is used to get the length of the set
We can use the set() constructor to make a set
12. Dictionary is one of the most important and used
The first element is called key and the second is the value. If you have the same key for two different values then it will not get printed. Refer to the example below:
Three important ways to access an element in a dictionary are:-
1.
for x,
print(x, y)
2.
for x in name.values():
print(x)
3.
for x in name:
print(x)
Keep creating tuple, dictionary, and set for the rest of your life 🙂
Keep learning 🙂
XtraMous