Learning Programming Using Python 3.8 With Tkinter Lecture - 1 Week 6-7
Learning Programming Using Python 3.8 With Tkinter Lecture - 1 Week 6-7
Prepared by : DaBudz
Objective
def foo():
"""This function does nothing."""
pass
print(foo.__doc__) # This function does nothing.
The docstring of a function should contain a (one-line) short description of its purpose,
followed by paragraphs describing the conventions of calling the function.
2. f-string
You might be used to formatting strings using % or .format().
name = 'World'
'Hello %s' % name # Hello World
'Hello {}'.format(name) # Hello World
Ditch them. Once you need to print multiple variables in longer strings, the
code will soon become messy and less readable. These formatting methods
are not straightforward and intuitive anyway.
Python f-string is a game-changer introduced in Python 3.6. It is a readable and elegant
string formatting syntax that embeds expressions inside strings. This is done by syntax
f'{expr}' where an expression is enclosed by curly brackets inside an f-string with an f at the
beginning before the single quote.
name = 'World'
print(f'Hello {name}') # Hello World
You can also put any syntactically valid expressions inside the curly brackets and it works
just fine, you can even call functions in the expressions!
a = [1,2.2,3]
print(f'Sum of squares of {a} is {sum_of_squares(a)}')
# Sum of squares of [1, 2.2, 3] is 14.84
3. Naming Convention
Naming things is one of the most difficult things in Computer
Science. You run out of ideas. You do not know how to name a
temporary intermediary variable. You are not alone.
Comments are an integral part of any program. They can come in the form of module-level
docstrings, or even inline explanations that help shed light on a complex function.
Before diving into the different types of comments, let’s take a closer look at why
commenting your code is so important.
Consider the following two scenarios in which a programmer decided not to comment their
code.
Lets integrate TKinter
First we Open a new text using the text editor in Python.
The last thing to do is create an event loop. An event loop will continually
show the widget on the screen it will keep track of everything on the
screen .even the motion of the mouse and everything....
Summary of the Code
'Creating a window'
root = Tk()
'Creating a Package'
mylabel.pack()