My previous post was just an introduction to Python. It just gave the general details about it like who created it, its advantages, etc. Now, I am going to write about the basics of Python. These include syntax, comments, variables, data types etc.
Python can be installed in your Laptop/PC from the official website here for macOS users ,here for windows users, here for Linux/UNIX users and here for other OS users.
1. Syntax
Syntax refers to a set of rules that defines how a program shall be written and interpreted in Python. In simple terms, it is the way code is written in Python.
print("Hello World!")
In the snippet given above, print("Hello World!")
is a piece of
code. It is because of the syntax of Python that this code is written like
print("Hello World!")
, not like
Hello World!("print")
etc.
2. Variables
Variables in Python are used to store values. They are declared by assigning a value to a name. For example-
#Variables- a=2 b=3
In the above code snippet, a
and b
are variables
which have been assigned the values 2
and
3
respectively.
3. Data Types
Python has several built-in data types, including integers (int
),
floating-point numbers (float
), strings (str
), and
lists (list
). For example-
#Data Types #Integers a=175 #Floating point numbers b=12.65 #Strings c="Hello World!" #Lists d=[1, -3, 9.4, 'Hi']
There are many more data types in Python, which have been covered in another post. You can find it here- Data Types in Python
4. Operators
Python has a variety of operators that can be used to perform operations on
variables, including arithmetic operators (+, -, *, /), comparison operators
(>, >=, <, <=, ==, !=
), and logical operators (and, or, not
).
You can find more about operators here- Python Operators: Part-1 and Python Operators: Part-2
For example-
#Operators a=2 b=3 #Arithmetic operators print(a+b) print(a*b) #Comparison operators print(a>b) print(a<b) #Logical operators print(a is not b)
The output-
5 6 False True True
5. Conditional Statements
Python has conditional statements, which allow you to execute different parts of your code based on certain conditions. The most commonly used conditional statements are if and if-else.
Read more about conditional statements here- Conditional Statements in Python
For example-
#Conditional statements a=3 b=7 if a>b: print("a>b.") else: print("a<b.")
The output-
a<b.
6. Loops
Python supports two types of loops: for
loops and
while
loops. For loops are used to iterate over a sequence (such
as a list or a string), and while loops are used to repeat a block of code
while a certain condition is True
.
Read more about loops here- For Loop in Python and While Loop in Python
For example-
#Loops nums=[1, 2, 3, 4, 5] #For loop for x in nums: print(x) #While loop a=1 while a<3: print(a) a+=1
The output-
1 2 3 4 5 1 2
These are the basics of python that you need to know to know to start coding in Python. Hope this article was interesting. Please don't hesitate to drop a comment below if you have any doubts, feedback or suggestions.
Happy Coding,
Aarav Iyer