Useful Python Scripts" />

Python FAQ

Introduction to Python

What is Python?

Python is an interpreted, interactive, and object-oriented programming language.

What are the advantages of Python?

Unlike other programming languages, Python has a simple and intuitive syntax. It also incorporates many positive aspects from other languages.

Which operating system should I use for Python?

Python is a portable language. It can run on Windows, Linux, Unix, Mac, and more. You just need to install the Python interpreter on your operating system.

Note: If you know the C language, you can also customize the Python interpreter software to suit your programming needs and create your own custom release.

Who created Python?

Python was created in 1989 by Dutch developer Guido Van Rossum. He released it for free on Usenet in 1991.

Why is it called Python?

The language is named after Monty Python, as Van Rossum was a fan of the British comedy group's sketches.

What does "interpreted language" mean?

It means that the source code of a Python program is executed by another software called an interpreter. Unlike C, Python programs do not need to be compiled.

What does "interactive language" mean?

It means that Python instructions can be executed directly from the command prompt without creating a source code file.

What does "object-oriented language" mean?

It means that Python allows the creation of objects with properties, classes, and attributes. This is known as object-oriented (OO) programming.

How do I install Python on a Windows PC?

The Python interpreter can be freely installed on Windows PCs. It can be downloaded and installed for free.

How do I install Python on Linux?

Python is usually pre-installed in Linux distributions. Just go to the command line and type python or python3. 

Are there online Python emulators?

Yes, there are several free online platforms that allow you to program in Python. They are useful for getting familiar with the language without installing anything on your PC. 

How to stop a running program

To stop a running Python program in the shell, simply press CTRL+C on the command line.

How to start debugging a program

To debug a program, you use a special module called pdb (Python debugger). This module pauses program execution and opens a prompt where you can set various debug commands.

import pdb
pdb.set_trace()

Programming in Python

What are exceptions in Python?

During program execution, unexpected events (e.g., entering a number instead of a name) may occur. Exceptions allow programmers to handle these unexpected events, correct them, and prevent the program from returning an error. In Python, you can manage both expected (handled) and unexpected (unhandled) events.

Note: Van Rossum likely incorporated positive aspects from other programming languages when creating Python. In this case, he was inspired by Java's exception handling.

What are Python operators?

Python uses logical, mathematical, boolean, and comparison operators similar to those in other programming languages. However, there are significant differences. For example, exponentiation is calculated using ** instead of ^.

What is indentation?

In Python's conditional structures, a block of instructions is defined using significant indentation. Each instruction is preceded by a certain number of spaces (zero or more), which changes its position in the lines of code. The position of the instruction within the structure affects the program's execution. Unlike C, Python does not use braces or keywords (then, end-if, etc.).

  1. if age<18:
  2. print('Access denied')
  3. access = False
  4. else:
  5. print('Access granted')
  6. access = True

How to add comments in code

To comment on the source code in Python, use the # (hash) symbol. Commented lines are not executed by the interpreter.

# this is a comment

How to create a loop

To create an iterative structure in Python, use the while and for statements.

The while statement allows you to create both conditional and unconditional loops.

While [condition]:
block of instructions

The for statement is used only for unconditional loops.

for [object]:
block of instructions

For more information on iterative structures in Python, click here.

What is introspection?

It is a feature of Python that allows you to inspect the attributes of an object from outside. For example, you can view the functions in a module or the attributes of a class.

How to define a function

To define a function in Python, use the def() statement.

def average(a,b):
c = (a + b) / 2
return c

How to use classes in Python

Python allows the creation of classes and object-oriented programming.

class Name():
....

How to use decorators

Decorators are functions that take another function as input.

How to do profiling

Profiling is a tool that checks the number of times individual lines of a script are executed and their corresponding execution times. It can also be performed on a single function.

import cProfile
cProfile.run('function_name()')

How to create an iterator

An iterator is an object or class that returns the elements of an iterable one at a time. You define the list with the iter() function and read it with the next() function.

list = [1, 2, 3]
list2 = iter(list)
next(list2)

How to create a generator

Generators are functions that return one element of an iterable at a time. They are similar to functions.

def generator():
yield x
object = generator()
next.object()

Useful Python Scripts

A list of useful scripts, examples, and practical solutions.

  • How to write program code on multiple lines
  • How to open a URL with Python
  • How to create a graph
  • How to run a Python script (.py) online with PHP
  • How to measure the execution time of a statement
  • How to pass a variable from PHP to Python
  • How to generate random numbers in Python
  • How to create a display mask
  • How to convert a string to a list/tuple
  • How to create a function library (module)
  • How to program with multithreading in Python

Some useful functions for mathematical calculations:

Here are some handy scripts for working with media files:

 

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

Useful Python Scripts" />

FacebookTwitterLinkedinLinkedin
knowledge base

Python

  1. The Python Language
  2. How to Install Python on Your PC
  3. How to Write a Program in Python
  4. How to Use Python in Interactive Mode
  5. Variables
  6. Numbers
  7. Logical Operators
  8. Iterative Structures (or Loops)
  9. Conditional Structures
  10. Exceptions
  11. Files in Python
  12. Classes
  13. Modules

Miscellaneous

Source