Artificial Intelligence Buildings Design Health Innovation Machine Learning Nature Technology

Getting Started With Python

Pinterest LinkedIn Tumblr

An intriguing name, you may wonder. Is it a snake or something else? Well firstly, let’s take a look at how the name “Python” came about. The programming language “Python” was created in the Netherlands by Dutch Programmer Guido Van Rossum and was first released in February 20,1991. He named this programming language “Python” based on an old BBC television comedy sketch series called Monty Python’s Flying Circus that he loved to watch. It was designed to be easy as well as fun to use.

Python is an interpreted, high level programming language with dynamic semantics and features. It is a versatile programming language available on numerous platforms. The fact that python is very easy to use and understand makes it a great choice for beginners and even experienced programmers. It is also a very popular development tool and that is why as developer’s, we need to know how it works and how to code it. Python tends to align very well with the philosophy of write less and do more. It was designed to be readable and takes a lot of similarities from english language and mathematics.

Python is widely used in various areas, these include areas of business such as web development, data analytics, machine learning, artificial intelligence and business forecasting. In our everyday life, we use Python almost everywhere even when we do not realize or notice it, examples include using Instagram, Facebook, Spotify, Dropbox, Reddit, Quora, Pinterest, Netflix, and so much more.

VISUAL STUDIO CODE

To run Python code, we can make use of the Visual Studio Code. Visual Studio Code is an Integrated Development Environment(IDE) provided by Microsoft for Windows, Linux, macOS and web browsers for free. It is richly accompanied with a wealth of extensions and plugins that makes the life of a developer easy.

HOW TO INSTALL AND SET UP VISUAL STUDIO CODE ON YOUR DEVICE:

For Windows:

1)Search for Visual Studio Code download on the web browser.

2)Click on the website where the download for VSCode is available.

3)Download the 64 bit or 32 bit files depending on your operating system.

4)Click on download. Once the download is complete, open the file to install it.

5)Use the default directory location when prompted where to install.

6)On the “Select Addition Tasks view”, make sure “Add to path” is selected.

7)Click next and the installation will begin.

8)Once complete, the Visual Studio Code will be opened to you.

For Mac:

1)Download Visual Studio Code based on the chipset of your Mac. Apple M1 uses silicon and older Macs use intel. In a case where you are not sure what chipset your Mac uses, choose the universal option.

2)Once VSCode is downloaded, go to your finder app, then check the downloads file to see your latest download.

3)Double click the zip file to extract the content.

4)Drag and drop the .app file to the Applications link in the file explorer.

5)You can now access Visual Studio Code on your Mac.

Python Extension from Visual Studio Code.

Recall it was mentioned earlier that there is a wealth of extensions found in VSCode. One of such extension is the Python extension which can be gotten from Visual Studio Code’s extension marketplace. It can be be added directly from the IDE by following these steps:

1)Open you Visual Studio Code app on your device.

2)Click on the extension icon.

3)In the Search bar, search for Python, make sure you download the Python extension provided by Microsoft.

4)Then click the “Install” button. Your installation will start automatically.

5)You may need to restart your IDE after the installation is complete.

Running Python without using Visual Studio Code.

In a situation where you don’t want to run Python using VSCode, you can install Python paths on your device.

For Windows:

1)Go to Python.org website.

2)Select the latest release.

3)Select either the 32 bit or 64 bit files depending on what suits your Operating System.

ALSO READ  An Introduction to Object Oriented Programming in Python

4)Once downloaded, open the file.

5)Select Install now.

6)Click option “Yes” to allow the app make changes to your device.

7)Your installation process will automatically begin and when successful, close the tab.

8)You now have Python installed on your device.

For Mac:

Installing Python on Windows is quite straight forward and doesn’t require dependencies but installing Python paths on Mac will require some additional dependencies. These are “Xcode” and “Brew”. The first step is to install the first dependency which is “Xcode”.

Xcode: To install brew, you need to install Xcode firstly. Xcode has to be installed for Brew to work correctly. Follow the steps below to install Xccode:

1)Open a terminal.

2)In the terminal, run the following code: “shell xcode-select –install”.

3)Click on the Install Button.

4)Then make sure to agree to the license agreement.

To install the second dependency which is “Brew”, you do the following:

1)/bin/bash-c”&(curl-fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)”

Once the dependencies have been installed, you can now begin to install Python on your Mac. Run the command “Brew install python”. When you make use of the dependency “Brew” to run Python, it will automatically install the version 3 of Python. Apple originally has Python with version 2.7 installed but it is advisable to install the version 3.0 on your Mac. To verify you have both version installed, run the command “python –version” and “python3 –version” to verify you have the correct version.

HOW TO RUN YOUR CODE USING THE COMMAND LINE IN CONTRAST WITH USING AN IDE.

There are two main ways to run a Python code. The first way is using the Python shell which processes codes in bits and allows the running of code without creating new “.py” files. The second way is by using the command line or terminal. As long as a file has the file extension “.py”, it can be run from the terminal or command line.

Yet another important and effective way of running a Python code is using Visual Studio Code which is an IDE. In fact, using VSCode is more effective and advisable than the other methods. It comes with a lot of additional improvements and facilities that make coding in Python a better experience for developers. VSCode has features like debugging, code syntax, whitespace, completion, highlighting and more that really helps developers.

If you want to run a Python command from a terminal window on Mac, here is how to go about it:

1)Click on the terminal menu in the IDE and click new terminal.

2)Then type Python followed by the name of the file then click the Enter key. Your result is “Hello World”.

Another option while using the terminal is to use the python shell, to do this, follow the following steps:

1)After opening your terminal, only type “Python” in the terminal then click “Enter”. With this, you can write code within the shell and run it directly on the terminal window.

2)To leave or exit the shell, type “exit” then “{}” then press the “Enter” key and you automatically leave the shell.

Finally, you can run your Python code using Visual Studio Code by following these steps:

1)Open your IDE which is Visual Studio Code.

2)There are a series of icons in the top right hand corner of your IDE.

3)Select the button that has a drop down option of “Run Python file ” or “Debug Python file”.

4)Click on “Run”.

4)Result is “Hello World”.

HOW MAKING PROPER USE OF THE PYTHON SNTAX, WHITESPACE AND INDENTATION CAN AFFECT YOUR CODE

Syntax is the way a code is written. Every programming language has a syntax which you must learn and abide by. It’s the way in which you write a programming language to produce results.

Whitespace is the spacing between your code. It is very okay and you can give as much space as you want between your code. Any amount of space on a single line is okay. However, when you want to indicate a new line, you use the “force line” to do this. You make use of a backlash “/” at the end of the first line.

ALSO READ  What's a Debugger?

Indentation means the positioning of your code in the IDE. It’s also very important. The IDE in a way does this for you but it’s also very important to take note of it also.

Making Use Of Comments In Your Code

Commenting or adding comments to your code as a developer helps you greatly. In fact, it not only helps you also helps other members of your team. Reasons to add comments to your code include explaining what the code is intended to do, other developers have a better understanding of your code, and you can also add comments that help you keep track of tasks you need to do for later. It is also important to always have a reason for a comment, it should supply information that makes the code more relatable.

Types Of Comments

1)Single-line Comments: To Comment in Python, you use the symbol “#”. For a Single-line Comment, when you use this symbol, it tells Python to ignore everything after the symbol till the end of the current line.

2)Multiple-line Comments: For Multiple-line Comments, you could make use of the symbol on multiple lines of code.

3)Inline Comments: With Inline Comments, you can place a Comment within the code itself.

VARIABLES

Variables store various types of data. And as Developer’s , we need to understand the concept of variables and how it is used. In Python, to declare a variable, you declare a name(which is the name of your variable) then assign a value.

The more advanced you become as a Developer, it becomes more difficult to know what this variable means or what you intended to use it for. Therefore, it is important to give meaningful names to your variable. This will help you and also your colleagues to understand your code better.

Variable means something that can’t be changed or reversed. But in a situation where you already assigned a value to your variable and want to change the value, you can reassign or the redeclare the variable with your new value.

Naming Conventions For Variables

When it comes to naming your variables, there are different options available. The first is the “camelCase” where the first word is lower case and the subsequent words have their first letters in upper case with no spacing in between. The second is the “snake_case” where everything is in lower case but you use an underscore “_” in between. Always remember though that while there are different options, it is advisable and good to stick to one and make use of it throughout your program.

Deleting a Variable

To delete a variable, you use the keyword “del” space then the name of your variable. Then when you “print” then “run”, it says “variable not defined”. It has deleted successfully.

BASIC DATA TYPES

A Data Type is an attribute associated with a piece of code that tells a computer system how to interpret its value. Data can come in different types in programming. The five main data types are numeric, sequence, dictionary, boolean and set.

1)Numeric: This further consists of types, integer, float and complex numbers.

a)Integers: These are whole numbers with no decimal places. They can be positive or negative.

b)Float: Contains decimal places e.g 7.9.

c)Complex numbers: Complex numbers are made up of both real and imaginary numbers e.g 12j.

2)Sequence: Sequence has three types. These are strings, lists and tuples.

a)Strings: This is a sequence of characters enclosed in either single quote(”) or double quote(“”). It is represented by a string class or “str” for short.

-Declaration and the usage of strings in Python: To declare a string, you can either use a single quote or double quote. You can type the variable name followed by an equal sign and then type the character enclosed in either single quote or double quotes. It’s your choice but its best to choose one and consistently use it.

ALSO READ  Git And GitHub: A Concise Roadmap On How They Both Work

-Using Index in String: Just like in a list or array, you can use index to access each character in a string. You can access the first character with a number 0 enclosed in square brackets. The first character is always represented with index number “0”.

-Checking the length of a String: It is also possible to check the length of your string. “len” is a Python functionality that does that. You simply apply it to a variable holding a string value.

-Concatenation: This means the joining of separate strings. You can do this using the arithmetic operator (+).

b)List: List is an array and holds any type of data within square brackets ([]). Index is used to access an item in a list.

c)Tuple: Very similar to lists but are immutable meaning contents of a tuple cannot be changed or modified. It is represented by a tuple close and contents are enclosed in parenthesis({}).

3)Dictionary: They can store any data type. They store data in a key value object structure.

4)Boolean: Boolean simply represents as true or false. They are used to check whether a condition is true or false combined with a logical operator.

5)Set: Set is an unordered and non-indexed collection of non-repeated values.

COMPARISON OPERATORS

Equals – “==”

Not Equal – “!=”

Less than – “<“

Greater than – “>”

Less than or equal to – “<=”

Greater than or equal to – “>=”

FUNCTIONS IN PYTHON

BUILT-IN FUNCTIONS

1)Print(): This looks for the default output device like your terminal and displays the value passed to it.

2)Input(): This looks for the default input device and captures it based on what the user entered. This can be assigned or used.

3)String(), int() and float(): They can be used to convert the provided value into a string, integer or float.

Creating Functions

You can also create your own functions in a Python code. You do this using the keyword “def” to define followed by an identifier(a name) which is the function signature and then you add the body which contains the code to run when the function is called on.

TYPE CASTING

Type casting is the process of converting one data type to another. Situations will arise when coding where you will need to convert one data type to another, this is why you need to get familiar with type casting. Python has two different types of conversion. These are implicit and explicit.

1)Implicit: This is performed automatically by the Python compiler to prevent data loss but the downside is that it only occurs if the data types are compatible, if not, an error occurs.

2)Explicit: For this type of conversion, you use the provided python function. For example you can use:

a)To change to a string data type, you can use “str” followed by the value you want to convert in parenthesis.

b) To change to an int data type, you can use “int” followed by the value you want to convert in parenthesis.

c)To change to a float data type, you can use “float” followed by the value you want to convert in parenthesis.

THE PRINT AND INPUT FUNCTION

User Input and Console Output

1)Input function: The input function is designed to get data from a source of inputs and this data collected can be used in different ways. For example, you use the input function to get an input from a user.

2)Print function: This function is used for all outputs in Python. It is used to print all different types of data. It has a number of arguments, comma is used to print numbers in sequence and arithmetic operator “+” can be used to print an equation. Key words include sep, files, flush and objects. There is also room for direct formatting inside the print statement.