Soluții

Python PIP

PIP is a package manager for Python packages, or modules if you like.

Note: If you have Python version 3.4 or later, PIP is included by default.

What is a Package?

A package contains all the files you need for a module. Modules are Python code libraries you can include in your project.

Check if PIP is Installed

Navigate your command line to the location of Python’s script directory, and type the following:

Example

Check PIP version:

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>pip –version
[mai mult...]

Python Scope

A variable is only available from inside the region it is created. This is called scope.

Local Scope

A variable created inside a function belongs to the local scope of that function, and can only be used inside that function.

Example

A variable created inside a function is available inside that function:

def myfunc():
x = 300
print(x)
myfunc()

Function Inside Function

As explained in the example above, the variable x is not available outside the function, but it is available for any function inside the function:

Example

The local variable can be accessed from a function within the function:

def myfunc():
x = 300
def myinnerfunc():
print(x)
myinnerfunc()
myfunc()

Global Scope

A variable created in the main body of the Python code is a global variable and belongs to the global scope. Global variables are available from within any scope, global and local.

Example

A variable created outside of a function is global and can be used by anyone:

x = 300

def myfunc():
print(x)

myfunc()

print(x)

[mai mult...]

Python Inheritance

Inheritance allows us to define a class that inherits all the methods and properties from another class.

  • Parent class is the class being inherited from, also called base class.
  • Child class is the class that inherits from another class, also called derived class.

Any class can be a parent class, so the syntax is the same as creating any other class:

  • Example

Create a class named Person, with firstname and lastname properties, and a printname method:

class Person:
def __init__(self, fname, lname):
self.firstname = fname
self.lastname = lname
def printname(self):
print(self.firstname, self.lastname)

#Use the Person class to create an object, and then execute the printname method:

x = Person(“John”“Doe”)
x.printname()

  • Create a Child Class

To create a class that inherits the functionality from another class, send the parent class as a parameter when creating the child class:

Create a class named Student, which will inherit the properties and methods from the Person class:

class Student(Person):
pass
[mai mult...]

Python Lambda

A lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression.

Syntax
lambda arguments expression

The expression is executed and the result is returned:

Example

Add 10 to argument a, and return the result:

x = lambda a : a + 10
print(x(5
[mai mult...]