Popular Posts

Tuesday, February 28, 2023

PyQt WIndow setting

Hello Python Developer!

How are you? Hope you're in the best condition today. Because we will exercise PyQt WIndow setup.

-PyQt wIndow Size adjustment

import sys
from PyQt5.QtWidgets import *

class MyWindow(QMainWindow):
    def __init__(self):
app = QApplication(sys.argv)
window = MyWindow()

Python codes running result

- PyQt titlebar & icon setting

* Note : An icon file "PyQt window sample icon.png" must be in the same folder with this Python code.  

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *

class MyWindow(QMainWindow):
    def __init__(self):
        self.setGeometry(200,200, 300,150)
        self.setWindowTitle("PyQt exercise")
        self.setWindowIcon(QIcon("PyQt window sample icon.png"))
app = QMainWindow(sys.argv)
window = MyWindow()

Have you checked above PyQt exercise sample Python codes? Great job!

Let's learn more about PyQt window setup!

Thursday, February 23, 2023

PyQt - Python GUI module

Hello Python developer!

Do you want to code a window base GUI(Graphic User Interface) app like below image?

If you are 'Yes', let's learn about 'PyQt' library(a set of modules) for GUI programming.

'PyQt' made by Riverbank Computing and you can find latest news and more details about PyQt from the following link.

PyQt site : https://riverbankcomputing.com/software/pyqt/

For installation of PyQt library at your PC, please type following command in Windows command prompt.

 pip install pyqt5

Are you ready for the first PyQt coding?

Let's input following code for PyQt sample and run it!

import sys
from PyQt5.QtWidgets import *

app = QApplication(sys.argv)
label = QLabel(" Hello GUI by Python! ")


Can you see the small window with " Hello GUI by Python! " Text? Congratulations!
Now you successfully coded the first Python GUI app, good job!

See you on the next Python course. Please take care and coding everyday! 

Tuesday, February 21, 2023

Python Class Initiate Method

Hello Python developer!

Hope you're good today with bright sunshine like our future!

Today, I would like to study Initializer method in Python Class.

Class initializer method was made for initiating class variables and methods.

Think about this, if you create an instance from the class and you want it to have each initiate values and functions.

For instance, we will make a calculator Python program. 

First, we will initialize the calculator result variable as 0 with initializer method using ' def __init__(self): ' statement.

Let's look into the following codes.

class Calculator:
def __init__(self):
self.result = 0

def add(self, var1, var2):
self.result = var1 + var2
return self.result

def sub(self, var1, var2):
self.result = var1 - var2
return self.result

test_add = Calculator()
test_sub = Calculator()

print(test_add.add(4, 7))

test_sub.sub(7, 2)

If you want to transfer variables into initializer method, refer following statement.

def __init__(self, var1, var2):
self.first_var = var1
self.second_var = var2

Now, are you familiar with the class 'Initializer' method?

Then, let's analyze following Python exercise codes.

# Python initiate method exercise

class Smartphone:
def __init__(self, model, year): # initiate class 'Smartphone' with following variables and function
self.model = model
self.year = year
self.model_year = model + " " + str(year)

Apple_phone = Smartphone("iPhone 14", 2022)
Samsung_phone = Smartphone("Galaxy S23", 2023)

print(Apple_phone.model, Apple_phone.year)
print(Samsung_phone.model, Samsung_phone.year)


Here is above Python source codes in Github(https://github.com/Cevastian/Python-Developer-start-today/blob/master/Python_initiate_method.py)

Hope you get the right answer and level up your Python class understanding.

See you at the next post! Take care! 

Sunday, February 19, 2023

Python Class Inheritance exercise part 2

Hello Python developer!

Today, we will look into class 'inheritance' with my test codes. Because I want to understand about 'class inheritance'.

Let's see following codes and type in Python IDE for execution!

Source codes(Multi_Inheritance.py)

Github link : https://github.com/Cevastian/Python-Developer-start-today/blob/master/Multi_Inheritance.py

class Lion():
color = "yellow"
classification = "animal"

def hunting(self):
print("Lion is hunting")

def attack_claw(self):
print("Claw attack")

class Eagle():
color = "brown"
classification = "bird"

def hunting(self):
print("Eagle is hunting")

def attack_beak(self):
print("Beak attack")

class Shark():
color = "blue"
classification = "fish"

def hunting(self):
print("Shark is hunting")

def attack_teeth(self):
print("Teeth attack")

class Monster(Lion, Eagle, Shark):
color = "green"
classification ="beast"

def playing(self):
print("Monster is playing")

def attack_tail(self):
print("Tail attack")

monster = Monster() # Class instance generating
monster.playing() # monster instance playing method calling
monster.hunting() # monster instance 'Lion' class 'hunting' method by inheritance
monster.attack_teeth() # monster instance 'Shark' class 'attack_teeth' method by inheritance

Executed result

'Monster' class inherited from 'Lion', 'Eagle' and 'Shark' classes. It also has its own method ' playing' and 'attack_tail'. As you know, child class can use parent classes' attributes and methods. Python meet calling of class method, it tried to search in its own class and it fails to search it, Python tried to search it out in the first parent classes' attributes and methods. It still fails to find out the called attributes and method from the first class, Python tried to search it in second parent class.

So, 'monster.hunting' method execution result was 'Lion' classes' method excution result. Because there is no 'hunting' method in 'Monster' class so Python search 'hunting' method from the parent classes by 'Lion', 'Eagle' and 'Shark' sequence by Monster(Lion, Eagle, Shark) class inheritance.

Developer, did you understand 'class inheritance' with above example?

Hope you're fine today with Python codes!

Wednesday, February 15, 2023

Current Date & Time expression

Hello, Python developer!

Hope you're fine today! Do you know how can we express the current time in Python program?

Python has 'datetime' module for dealing with the date and time.

Let's test the it with following codes.

import datetime                                                # Load 'datetime' module
current_date_time = datetime.datetime.now( )        # Get current date & time
print(current_date_time)                                    #Print current date & time

Run result

If you want to add time or date about current date & time by datetime.now( ) function, please use 'timedelta' function. 

Here are 'timedelta' function examples.

import datetime
current_date_time = datetime.datetime.now( )
print( current_date_time + datetime.timedelta(seconds = 75))    # Add 75 seconds
print( current_date_time - datetime.timedelta(minutes = 25))    # Sub 25 minutes
print( current_date_time + datetime.timedelta(hours =7))        # Add 7 hours
print( current_date_time - datetime.timedelta(days=100))        # Sub 100 days
print( current_date_time + datetime.timedelta(weeks = 8))        # Add 8 weeks

Run result

As you recognized the above result, 'timedelta' function automatically converts seconds to minutes, minutes to hours, hours to days and days to weeks.

Please refer following 'datetime' reference documentation from Python official site.

In this reference page, you also find 'datetime' and 'timedelta' function explanation as following.

I hope you get to know and deal with date & time in Python.

Please take care and have a nice day, Python developer!  

Tuesday, February 14, 2023

Bitcoin Price Checker Ver 1.0

Hello Python developer,

Today, I will code Bitcoin price checker with requests module.

Python module is file set of python codes - functions.

I showed module declaration from the Python Glossary


You can use codes in the Python module using 'import' keyword.

import Python module name

We will use 'requests' module for web-scrapping from the 'Binance' cryptocurrency exchange.

Here is the executed result.

'requests.get' code get the current Bitcoin price with USDT symbol from the Binance server.

There are two ways to print out the Bitcoin price.

 - Print content variable from the current_price 

 - Using JSON module with json function to print out the current Bitcoin price 

I also uploaded this code in my GibHub.

I hope you to enjoy above codes.

Have a nice day, Python developer!

Thursday, February 9, 2023

Understanding Python Class

Hello, Developer!

Today, I will exercise about Python 'Class'.

Class is based on OOP(Objective Oriented Programming) and I need to exercise it with several examples about class for clear understanding.

Did you remember about 'Class'?

It consists of two parts - Data and Function as following image. Normally, developers call 'data' in class as 'attribute' and 'function' in class as 'method'.

You can create a class with following construct.

class Class_Name:


class MyClass():
 Color = Green                Data(Attribute) part
 def painting(self):            Function(Method) part

You can created a object(=instance) from class as following.

paint_shape = MyClass()      Create 'paint_shape' object from the 'MyClass' class

You can use a function in the created object(=instance) as following.

paint_shape.painting()         Call painting function(method) from paint_shape object.

You can inherit from parent classes(father & mother). 

If you create a Child class and inherits Class Father & class Mother, Child instance can use data and methods of Father and Mother classes.

Let's see following example.

Class father has attribute "hard" and method "work"
Class mother has attribute "kind" and method "cook"
Calss child inherits Father and Mother classes and has own method "sleep"

'child' class can use "work" method from 'father' class and use "cook" method from 'mpther' class and use its own "sleep" method.

You can easily inherit from other classes by adding class names in class definition line as following.

Class inherite example 
class child(father, mother):       # 'child' class inherits 'father' class 'mother' class

This is how classes work in Python. Let's study more about class together!

Python - Web crawling exercises

Hello Python developer! How are you? Hope you've been good and had wonderful time with Python. There are 10 Python requests module exerc...