Rolex Pearlmaster Replica
  Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
This article is part of in the series
Published: Monday 28th December 2020
Last Updated: Wednesday 29th December 2021

The Scope

In this article, we will learn more about how to run Python scripts. You should be familiar with Python syntax. If you don't know many about Python check our guide here. You need to have Python installed in your machine. To know how to install python on your system check our installation guide.

We will discuss:

  • How to update python
  • Run Python scripts
  • Run python in the terminal
  • Comments in Python

How to update Python:

We discussed in a previous article how to install Python. It's always recommended to have the latest version of Python. This will let you avoid a lot of issues. In each update, Python adds new fixes and enhancements. Updating python will let take the advantage or new fixes. To update the Python version you need to first check your operating system.

Updating Python in widows

  1. Visit Python official page
  2. Download the latest Python version
  3. It'll download a .exe file.
  4. open it and proceed like the installation.
  5. It'll update the Python version installed on your machine.
update python scripts

setup python for windows

Updating Python in Ubuntu

Updating Python in Ubuntu is a simple easy command.

  1. Update apt package manager
    sudo apt update && sudo apt-get upgrade
  2. Install the latest version of Python.
    sudo apt install python3.8.2

This will update the Python version to 3.8.2.

Updating Python in Mac

  1. Visit Python official page
  2. Download the latest Python version
  3. It'll download a file.
  4. open it and proceed like the installation.
  5. It'll update the Python version installed on your macOS.

Updating Python globally might not be a good idea. We learned in a previous article that it's better to use a virtual environment in Python projects. In most cases, you will need to only update the python version in your virtual environment.

python -m venv --upgrade <env_directory>

This will only update the python version inside your virtual environment.

After making sure that you have Python installed. And it is the latest version. Now you can run Python scripts.

Run Python scripts

Usually, a python script will look like this

import os 

def main():
    path = input("Enter folder path: ")
    folder = os.chdir(path)

	for count, filename in enumerate(os.listdir()): 
		dst ="NewYork" + str(count) + ".jpeg"
		os.rename(src, dst) 

if __name__ == '__main__':
	main()

It has the main function that will have the script logic. This code is the main functionality of the script. It actually defines what you need to do. The other part which invokes the main function is

if __name__ == '__main__':
	main()

First, define the function contains your code. Python will enter the if __name__ == '__main__': condition and invoke your main() function. We need to know what happens behind the scene.

  1. The interpreter reads the python source file. interpreter sets the __name__ special variable and assigns it to the "__main__" string.
  2. This will tell the interpreter to run the function called inside it. In our case, it's the main() function.

Scripting is one of the biggest features of Python. To achieve this Python needs to make its code executable in different places.

Run python scripts in the terminal

You can run Python code from the terminal. You can write python <file_name.py>. This will tell the Python interpreter to run the code in this file.

python my_script.py

This will invoke the main function inside the script and execute the logic. A feature like this is useful if you need to automate some of your work from the terminal. Let's say that you're a DevOps and every time you power the machine you need to log the time and the user. You can write Python code that will check the time and the username of logged in user. It'll update the log file with these data each time someone logs in and run the machine. In such a case, you can't open the file manually. You will add to system bootup commands the command that will run this script.

You can open a Python shell in the terminal. It'll help you execute your code directly. With this, you don't need every time you want to test new changes to run the script again. You can write your script and test it in the same place. It'll help you develop the script faster.

Comments in Python scripts

Python is shining for its readability. Commenting helps you explain more logic that could be difficult to understand from code. Adding comments helps your colleagues to understand the complex logic. Comments are very helpful in teams. It helps team members to know the reason for each part of the code. This makes it easy to work and make the development process faster. Adding comments in python is simply by adding hash.

# the below code will print items from 1 to 5
for x in range(1, 5):
    print(x)

Notice that the code after the # symbol will not be run. Python sees this code as a comment. It'll not execute it

conclusion

You can run a Python script from the system GUI or from the terminal. It depends on how you want to use it. The comments in Python used to add human-readable text in the code. Python comments start with a hash symbol. Python interpreter skips the hashed text. Updating python is very important. It makes you up to date with new fixes. You can update Python globally across your entire system. The most efficient is to update the Python version in your virtual environment. It helps in big projects to keep things safe and not break it.

Latest Articles


Tags

  • Parsing
  • tail
  • merge sort
  • Programming language
  • remove python
  • concatenate string
  • Code Editors
  • unittest
  • reset_index()
  • Train Test Split
  • Local Testing Server
  • Python Input
  • Studio
  • excel
  • sgd
  • deeplearning
  • pandas
  • class python
  • intersection
  • logic
  • pydub
  • git
  • Scrapping
  • priority queue
  • quick sort
  • web development
  • uninstall python
  • python string
  • code interface
  • PyUnit
  • round numbers
  • train_test_split()
  • Flask module
  • Software
  • FL
  • llm
  • data science
  • testing
  • pathlib
  • oop
  • gui
  • visualization
  • audio edit
  • requests
  • stack
  • min heap
  • Linked List
  • machine learning
  • scripts
  • compare string
  • time delay
  • PythonZip
  • pandas dataframes
  • arange() method
  • SQLAlchemy
  • Activator
  • Music
  • AI
  • ML
  • import
  • file
  • jinja
  • pysimplegui
  • notebook
  • decouple
  • queue
  • heapify
  • Singly Linked List
  • intro
  • python scripts
  • learning python
  • python bugs
  • ZipFunction
  • plus equals
  • np.linspace
  • SQLAlchemy advance
  • Download
  • No
  • nlp
  • machiine learning
  • dask
  • file management
  • jinja2
  • ui
  • tdqm
  • configuration
  • deque
  • heap
  • Data Structure
  • howto
  • dict
  • csv in python
  • logging in python
  • Python Counter
  • python subprocess
  • numpy module
  • Python code generators
  • KMS
  • Office
  • modules
  • web scraping
  • scalable
  • pipx
  • templates
  • python not
  • pytesseract
  • env
  • push
  • search
  • Node
  • python tutorial
  • dictionary
  • csv file python
  • python logging
  • Counter class
  • Python assert
  • linspace
  • numbers_list
  • Tool
  • Key
  • automation
  • website data
  • autoscale
  • packages
  • snusbase
  • boolean
  • ocr
  • pyside6
  • pop
  • binary search
  • Insert Node
  • Python tips
  • python dictionary
  • Python's Built-in CSV Library
  • logging APIs
  • Constructing Counters
  • Assertions
  • Matplotlib Plotting
  • any() Function
  • Activation
  • Patch
  • threading
  • scrapy
  • game analysis
  • dependencies
  • security
  • not operation
  • pdf
  • build gui
  • dequeue
  • linear search
  • Add Node
  • Python tools
  • function
  • python update
  • logging module
  • Concatenate Data Frames
  • python comments
  • matplotlib
  • Recursion Limit
  • License
  • Pirated
  • square root
  • website extract python
  • steamspy
  • processing
  • cybersecurity
  • variable
  • image processing
  • incrementing
  • Data structures
  • algorithm
  • Print Node
  • installation
  • python function
  • pandas installation
  • Zen of Python
  • concatenation
  • Echo Client
  • Pygame
  • NumPy Pad()
  • Unlock
  • Bypass
  • pytorch
  • zipp
  • steam
  • multiprocessing
  • type hinting
  • global
  • argh
  • c vs python
  • Python
  • stacks
  • Sort
  • algorithms
  • install python
  • Scopes
  • how to install pandas
  • Philosophy of Programming
  • concat() function
  • Socket State
  • % Operator
  • Python YAML
  • Crack
  • Reddit
  • lightning
  • zip files
  • python reduce
  • library
  • dynamic
  • local
  • command line
  • define function
  • Pickle
  • enqueue
  • ascending
  • remove a node
  • Django
  • function scope
  • Tuple in Python
  • pandas groupby
  • pyenv
  • socket programming
  • Python Modulo
  • Dictionary Update()
  • Hack
  • sdk
  • python automation
  • main
  • reduce
  • typing
  • ord
  • print
  • network
  • matplotlib inline
  • Pickling
  • datastructure
  • bubble sort
  • find a node
  • Flask
  • calling function
  • tuple
  • GroupBy method
  • Pythonbrew
  • Np.Arange()
  • Modulo Operator
  • Python Or Operator
  • Keygen
  • cloud
  • pyautogui
  • python main
  • reduce function
  • type hints
  • python ord
  • format
  • python socket
  • jupyter
  • Unpickling
  • array
  • sorting
  • reversal
  • Python salaries
  • list sort
  • Pip
  • .groupby()
  • pyenv global
  • NumPy arrays
  • Modulo
  • OpenCV
  • Torrent
  • data
  • int function
  • file conversion
  • calculus
  • python typing
  • encryption
  • strings
  • big o calculator
  • gamin
  • HTML
  • list
  • insertion sort
  • in place reversal
  • learn python
  • String
  • python packages
  • FastAPI
  • argparse
  • zeros() function
  • AWS Lambda
  • Scikit Learn
  • Free
  • classes
  • turtle
  • convert file
  • abs()
  • python do while
  • set operations
  • data visualization
  • efficient coding
  • data analysis
  • HTML Parser
  • circular queue
  • effiiciency
  • Learning
  • windows
  • reverse
  • Python IDE
  • python maps
  • dataframes
  • Num Py Zeros
  • Python Lists
  • Fprintf
  • Version
  • immutable
  • python turtle
  • pandoc
  • semantic kernel
  • do while
  • set
  • tabulate
  • optimize code
  • object oriented
  • HTML Extraction
  • head
  • selection sort
  • Programming
  • install python on windows
  • reverse string
  • python Code Editors
  • Pytest
  • pandas.reset_index
  • NumPy
  • Infinite Numbers in Python
  • Python Readlines()
  • Trial
  • youtube
  • interactive
  • deep
  • kernel
  • while loop
  • union
  • tutorials
  • audio
  • github
  • Python is a beautiful language.