Popular Posts

Showing posts with label Python code. Show all posts
Showing posts with label Python code. Show all posts

Friday, September 11, 2020

Python Beginner: Classes & Objects

 Hello, Developer!


Hope you're good at handling modules in the Python.

Today, we will learn the "Classes & Objects". 

Python is an object-oriented programming language. Class is an object constructor and we can create data types as objects modeling the real world.

For creating an object, we have to define a class first. Because objects can be created from classes.


In the class, "__init__()" function is used for assigning values to each property in objects. Also, we can define functions in the class 


Class Form

class sample_class: # Define class with class name(i.e. sample_class)

    def __init__(self, value_1, value_2): # Define initiated class values 

        self.value_1 = name # Define value matches class value

        self.value_2 = age

   

    def class_function(self): # Define function in the class

        print("This is class exercise!")


Object Form

object_1 = sample_class("Class", "No.1") # Create object with modeling class with values

object_1.class_function() # Using class function


I showed full class & object exercise codes as following.

Class(Student_class.py)

class Student:

def __init__(self, name, major, gpa, is_on_probation):
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation

def print_func(self):
print("This student's name is : " + self.name)

Class & Objects.py

from Student_class import Student # From Student_class file import Student class

student01 = Student("Jim", "Business", 3.1, False)
student02 = Student("Alex", "Sales", 3.5, True)

print(student01.gpa)
print(student02.major)
student01.print_func()
student02.print_func()

Exercise Result

Can you see the same result on your PC? Great job! You can create class & object in Python and you started the object-oriented programming!

See you at the next post, Python developer! 😊


Thursday, September 10, 2020

Python Beginner: Modules & Pip

 Hello, developer!


Good day! Hope you're fine today! 😀

Today, we will learn "Modules" & "Pip". A module is an external Python file that has functions or variables. You can load external Python module file in the Python by "import" command.


Module loading command

import Python_module_exercise # Loading the Python_module_exercise file in the same directory.


You can also refer to the "Python Module Index" in the Python official documents site link(https://docs.python.org) for searching out suitable modules on your program. 


Python Module Index


There are two types of modules.

Built-in modules are automatically loaded so you can use these modules.

External modules are needed to import in order to use these modules. In the Python official module list - "Python Module Index" - "External module" shows "Source code" where it was located in the Python library folder like "Lib/base64.py" but "Built-in" modules don't show "Source code" because it was automatically loaded in the Python program. 


External Module example(random in the Python Module Index"


Built-in Module example(random in the Python Module Index"

Also, you can check external Python modules at the following installed Python folders in the PyCharm.

 


For 3rd-party modules, we can use the "PIP" program for the installation of the 3rd-party module package. "PIP" is pre-installed if you installed the latest Python 3 versions but if you are using an older version of Python, you need to install it manually. You can check the "PIP" installation through the below steps in Windows OS.

1. Press the "Windows" key + "R" key

2. Input "cmd" in the input field and press the "Enter" key

3. Opened "Command Prompt Window", input "pip --version" in the prompt line and press "Enter" key.

 4. If you see the version information of pip, "PIP" was installed on your PC.

"PIP" enables you to manage 3rd-party packages with the following features.

 - install: install package(i.e. pip install sample_package)

 - uninstall: uninstall package(i.e. pip uninstall sample_package) 

 - list: list installed packages(i.e. pip list)

 

You can refer more information of "PIP" from the "pip" site(https://pypi.org/project/pip/)