joe,

We have been looking at a lot of new things on the blog, lately (React, web components, etc). I figured that it was time to add was time to add Python to the list. Today’s post is going to go over the basics but you can expect a series of posts on the topic over the next few weeks. Python is a lot like Node. It is a general-purpose programming language that is not specialized for any specific problems. You can use it to build websites and software, automate tasks, and analyze data.

How to run a Python script

If you want to write Python, you will probably want to also run what you write. If you are developing on a Windows computer and don’t yet have Python installed, you can run the command python3 from PowerShell to install it from the Microsoft Store. If you don’t know if you have it installed, you can run python3 --version from the same place. If you are using MacOS, you can download the Python installer from the Python website and run it to install Python. If you are a MacOS user, Python3 is likely already there from previously using Homebrew.

With Python installed, you be able to open the Python shell and run simple commands.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-12.01.36%E2%80%AFPM.png?resize=1024%2C856&ssl=1

Next, let’s install Jupyter. Jupyter Notebook is a popular way to write and run Python code, especially for data analysis, data science, and machine learning. Jupyter Notebooks are easy to use because they let you execute code and review the output quickly. From the MacOS terminal, you can install Jupyter by running brew install jupyterlab (assuming that you have Homebrew installed).

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-12.47.11%E2%80%AFPM.png?resize=1024%2C856&ssl=1

With Jupyter installed, you can now run Jupyter Notebook from the terminal.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-12.55.32%E2%80%AFPM.png?resize=1024%2C856&ssl=1

You should see Jupyter open in a new browser window.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-4.21.48%E2%80%AFPM.png?resize=1024%2C966&ssl=1

If you click on New and then Python 3, you can create a new notebook.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-4.23.38%E2%80%AFPM.png?resize=1024%2C966&ssl=1

From within the notebook, you can run code like you can in the Python shell.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-4.28.08%E2%80%AFPM.png?resize=1024%2C966&ssl=1

This is pretty useful if you are just trying to play around with Python.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-4.36.56%E2%80%AFPM.png?resize=1024%2C966&ssl=1

Each cell in the notebook is going to be numbered, you can get as complex with it as you want, you can add markdown into cells, and you can use “Save and Export Notebook As…” to share your work. It is kind of cool.

Python Identifiers and Literals

When you name a variable, function, or class in Python, it can’t start with a number, be a reserved word, or contain special characters. It is also case-sensitive. The function type(x) (where x is an identifier) returns the data type for the object passed into it. Let’s take a look at a few data types.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-5.08.06%E2%80%AFPM.png?resize=651%2C1024&ssl=1

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-5.08.29%E2%80%AFPM.png?resize=635%2C226&ssl=1

As you can see, you have a lot of options. Just remember that True and False values with booleans are case-sensitive.

Conditions and If Statements

Conditions are pretty easy with Python.

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

If statements are just if x > y: on the first line and what you want to conditionally run below it. Just remember to put an indentation after if x > y:.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-5.26.50%E2%80%AFPM.png?resize=925%2C422&ssl=1

Loops

Loops are pretty easy as well. You just need to remember to indent, again.

https://i0.wp.com/jws.news/wp-content/uploads/2024/04/Screenshot-2024-04-17-at-5.38.32%E2%80%AFPM.png?resize=768%2C672&ssl=1

https://jws.news/2024/python-the-basics/

  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • kavyap
  • thenastyranch
  • ethstaker
  • DreamBathrooms
  • osvaldo12
  • magazineikmin
  • tacticalgear
  • Youngstown
  • everett
  • mdbf
  • slotface
  • ngwrru68w68
  • rosin
  • Durango
  • JUstTest
  • InstantRegret
  • GTA5RPClips
  • tester
  • cubers
  • cisconetworking
  • normalnudes
  • khanakhh
  • modclub
  • anitta
  • Leos
  • megavids
  • provamag3
  • lostlight
  • All magazines