Installation to Syntax and Best Practices in Python Programming language (Part 2/6)
Experience the Beauty of Python: The Easiest Language to Learn and Use
Lets start with a joke:
Why did the Python data scientist get arrested at customs?
Because she was caught trying to import pandas!
Table of Content
- Installing Python
- Hello Earth
- Syntax in Python Coding
- Rules in Python Coding
- Good to have
Installing Python
Linux
- Open the terminal
- Update the package list using the command:
sudo apt-get update
- Install Python:
sudo apt-get install python3
- Verify:
python3 --version
orpython --version
MacOS
- Open Spotlight or the Applications folder.
- Serach and run Terminal app.
- Install the Homebrew package manager :
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Use Homebrew to install Python:
brew install python
- Verify:
python3 --version
orpython --version
Window
- Go to the official Python website.
- Download the latest version of Python for Windows.
- Once downloaded, click it and follow the steps to install Python.
- Open Command Prompt
- Verify :
python3 --version
orpython --version
In Python, both “python3” and “python” can be used interchangeably. However, you should use whichever gives you the version number.
Hello Earth
The origins of the “Hello, World!” program can be traced back to the early days of computing when programming languages were first being developed. One of the earliest known examples of the “Hello, World!” program was written by Brian Kernighan in 1972 for the C programming language. The program was used to test the installation of a new operating system.
But I like to call it “Hello Earth” :)
Lets run our first python code, so that we are sure things are good so far.
Open a text editor, and open a new file in it.
Paste the following code into it
print("Hello Earth")
In Python, the
print()
function is used to output data to the console or terminal. Here we are printing “Hello Earth” on our terminal (command prompt in case of window)
Save your file as main.py
Open terminal again (command prompt in case of Window) and type the following command in it
python3 <PATH_TO_YOUR_CODE>/main.py
You should be able to see the following output:
Syntax in Python coding
Indentation: Python uses indentation to indicate a block of code. The standard indentation level is 4 spaces or a tab . For example:
if x > 5:
print("x is greater than 5")
else:
print("x is less than or equal to 5")
Here we using 4 space to define the block of code for if and else statement.
In Python, the standard recommended practice for indentation is to use four spaces. This is specified in the official Python Style Guide, also known as PEP 8, which states that “four spaces should be used for each indentation level”.
While using a tab character for indentation is technically allowed in Python, it is not recommended because different text editors and environments may interpret tabs differently, leading to inconsistent formatting and potentially causing errors in the code.
Comments: Python uses the hash (#) symbol to create comments in the code. Comments are used to explain code or to temporarily disable code. For example:
# This is a comment
print("Hello, World!")
We can also use comment to disable a particular code
# print("Hello, World!")
Variables and Data Types: In Python, variables are created when a value is assigned to them. Python has several data types, including integers, floats, strings, and Booleans. For example:
x = 5 # Integer variable
y = 3.14 # Float variable
name = "John" # String variable
is_male = True # Boolean variable
Variables are used to store data that can be accessed and manipulated throughout the program.
Data type is a classification of data that determines the type of value it hold and operations that can be performed on that data.
Rules in Python coding
- Use valid file extension: Like “.py”, “.py3”. Although these are also valid [‘.p’, ‘.py’, ‘.pyt’, ‘.pyth’, ‘.pytho’, ‘.python’, ‘.p3’, ‘.py3’, ‘.pyt3’, ‘.pyth3’, ‘.pytho3’, ‘.python3’], but don't use these as we follow standard practises so we use only “.py” and “.py3”.
- Follow naming conventions:
- Variable names must start with a letter or underscore (_).
- Variable names can only contain letters, numbers, and underscores (_).
- Variable names are case sensitive, so
my_variable
andMy_Variable
are two different variables. - Variable names should be descriptive and indicate the purpose of the variable.
- Variable names should not start with a number.
- Avoid using reserved words as variable names, such as
if
,else
,while
, etc.
A reserved word is a word in a programming language that has a special meaning and can’t be used as an identifier. They’re used to define syntax or control structures, and are usually reserved for specific things.
Good to have
- Use comments to explain your code: Comments should be used to explain the purpose of your code and how it works. This makes it easier for other developers to understand your code and maintain it in the future.
- Keep lines short and simple: Lines of code should be kept short and simple. Limit lines to a maximum of 79 characters to ensure readability. Break long lines by using parentheses or backslashes.
- Use meaningful variable names: Variable names should be descriptive and indicate the purpose of the variable. This makes the code easier to read and understand. Avoid using single letters or abbreviations unless they are commonly understood.
- Follow the PEP 8 style guide: The Python community has developed a style guide called PEP 8, which provides guidelines for writing Python code. Following these guidelines helps make your code more consistent and easier for other developers to read.
This is part 2, and there will be 4more part. This part was just about the theory type things. In part 3 we will learn about Python data-types.
Link for Part 1: https://medium.com/@siddp6/python-programming-language-part-1-6-8b937f7297bf
Link for Part 2: https://siddp6.medium.com/python-programming-language-part-2-6-403dabaa7c6a
Link for Part 3: https://medium.com/@siddp6/python-programming-language-part-3-6-ab0af8000e27
Link for Part 4: https://medium.com/@siddp6/conditionals-and-loops-python-programming-language-part-4-6-b5b1a8c9521e
Link for Part 5: https://siddp6.medium.com/functions-in-python-programming-language-part-5-6-5c2c5b1df5fe
Link for Part 6: https://siddp6.medium.com/classes-and-object-oriented-programming-oop-in-python-programming-language-part-6-6-4e2fca5e1eb9
Copyright © 2023 Siddhartha Purwar. All rights reserved. Portions of this content were enhanced grammatically and refined with the assistance of ChatGPT, an AI language model by OpenAI.