Download as pdf or txt
Download as pdf or txt
You are on page 1of 364

IN

Skip navigation

9+

hey

0:00 / 12:00:00

1.Python tutorial for beginners

Python Full Course for free 🐍


Fundraiser

Bro Code
1.24M subscribers
Subscribed
654K
Share
12,830,545 views Feb 15, 2021 Full Courses
Python tutorial for beginners full course #python #tutorial #beginners ⭐️Time
Stamps⭐️ #1 (00:00:00) Python tutorial for beginners 🐍 #2 (00:05:57) variables ✘ #3
(00;17;38) multiple assignment 🔠 #4 (00:20:27) string methods 〰️ #5 (00:25:13)
type cast 💱 #6 (00:30:14) user input ⌨️ #7 (00:36:50) math functions 🧮 #8
(00:40:58) string slicing ✂️ #9 (00:51:52) if statements 🧮 #10 (00:58:19) logical
operators 🔣 #11 (01:04:03) while loops 🔄 #12 (01:07:31) for loops ➰ #13 (01:13:04
) nested loops ➿ #14 (01:17:08) break continue pass ⛔ #15 (01:21:06) lists 🧮 #16
(01:26:58) 2D lists 📜 #17 (01:30:47) tuples 📄 #18 (01:33:47) sets 🍴 #19 (01:40:03)
dictionaries 📖 #20 (01:47:20) indexing 📑 #21 (01:53:23) functions 📞 #22 (02:02:03)
return statement 🔙 #23 (02:04:51) keyword arguments 🔑 #24 (02:07:09) nested
function calls 🖇️ #25 (02:09:40) variable scope 🔬 #26 (02:13:23) *args 📦 #27
(02:16:58) **kwargs 🎁 #28 (02:21:17) string format 💬 #29 (02:33:22) random
numbers 🎲 #30 (02:36:43) exception handling ⚠️ #31 (02:43:40) file detection 📁 #32
(02:47:28) read a file 🔍 #33 (02:51:00) write a file 📝 #34 (02:53:45) copy a file 🖨️
#35 (02:57:05) move a file 🗃️ #36 (03:01:20) delete a file 🗑️ #37 (03:06:15) modules
💌 #38 (03:10:26) rock, paper, scissors game 🗿 #39 (03:18:32) quiz game 💯 #40
(03:35:45) Object Oriented Programming (OOP) 🐍 #41 (03:45:06) class variables 🚗
#42 (03:48:54) inheritance 👪 #43 (03:55:30) multilevel inheritance 👴 #44 (03:58:32)
multiple inheritance 👨👩👧👦 #45 (04:01:49) method overriding 🙅 #46 (04:04:14)
method chaining ⛓️ #47 (04:08:08) super function 🧮 #48 (04:12:09) abstract classes
👻 #49 (04:19:12) objects as arguments 🏍️ #50 (04:23:20) duck typing 🧮 #51
(04:27:38) walrus operator 🧮 #52 (04:31:45) functions to variables 📛 #53 (04:35:21)
higher order functions 👑 #54 (04:41:06) lambda λ #55 (04:45:44) sort 🗄️ #56
(04:53:22) map 🗺️ #57 (04:57:17) filter 🍺 #58 (05:00:10) reduce ♻️ #59 (05:04:54)
list comprehensions 📰 #60 (05:10:54) dictionary comprehensions 🕮 #61 (05:18:59)
zip function 🧮 #62 (05:23:41) if name == '__main__' ❓ #63 (05:29:21) time module
⌚ #64 (05:39:58) threading 🧮 #65 (05:53:31) daemon threads 😈 #66 (05:58:19)
multiprocessing ⚡ #67 (06:07:15) GUI windows 🖼️ #68 (06:14:38) labels 🏷️ #69
(06:24:24) buttons 🛎️ #70 (06:30:44) entrybox ⌨️ #71 (06:40:15) checkbox ✔️ #72
(06:49:08) radio buttons 🔘 #73 (07:00:47) scale 🌡️ #74 (07:10:24) listbox 📋 #75
(07:24:41) messagebox 💭 #76 (07:37:17) colorchooser 🎨 #77 (07:43:10) text area
📒 #78 (07:48:38) open a file (file dialog) 📁 #79 (07:55:33) save a file (file dialog) 💾
#80 (08:05:17) menubar 🧮 #81 (08:15:23) frames ⚰️ #82 (08:21:30) new windows 🗔
#83 (08:25:32) window tabs 📑 #84 (08:30:52) grid 🏢 #85 (08:39:52) progress bar 📊
#86 (08:49:48) canvas 🖍️ #87 (09:01:18) keyboard events ⌨️ #88 (09:05:54) mouse
events 🖱️ #89 (09:11:00) drag & drop 👈 #90 (09:18:18) move images w/ keys 🏎️
#91 (09:29:13) animations 🧮 #92 (09:41:31) multiple animations 🎞️ #93 (09:53:04)
clock program 🕒 #94 (10:01:03) send an email 📧 #95 (10:07:37) run with command
prompt 👨💻 #96 (10:09:53) pip 🏗️ #97 (10:13:30) py to exe 🏃 #98 (10:17:13)
calculator program 🖩 #99 (10:31:38) text editor program ✏️ 100 (11:05:51) tic tac toe
game ⭕ 101 (11:26:25) snake game 🐍 🎮 GAMES & PROJECTS 🎮 rock, paper,
scissors game 🗿 quiz game 💯 clock program 🕒 calculator program 🖩 text editor
program ✏️ tic tac toe game ⭕ snake game 🐍 Here's the link to the full
playlist: • Python tutorial for beginners 🐍 Code for each topic can be found
pinned to each video's comments section music credits 🎼 :
=========================================================== Beat
Your Competition - Vibe Tracks • Beat Your Competition – Vibe Tracks (...
===========================================================
Twelve Speed -
Slynk • Slynk - Twelve Speed (Alone Time Vol. 2) ====================
======================================= Up In My Jam (All Of A Sudden)
by - Kubbi https://soundcloud.com/kubbi Creative Commons — Attribution-
ShareAlike 3.0 Unported— CC BY-SA 3.0 Free Download / Stream:
http://bit.ly/2JnDfCE Music promoted by Audio
Library • Up In My Jam (All Of A Sudden) – Kubb... ===================
========================================
Chapters
View all
1.Python tutorial for beginners
0:00

2.variables
5:57

4.string methods 〰️
20:27

5.type cast
25:13

6.user input ⌨️
30:14

7.math functions
36:50

8.string slicing ✂️
40:58

9.if statements
51:52

Featured playlist

6 videos
Full Courses
Bro Code

Music
SONG
Twelve Speed
ARTIST
Slynk
ALBUM
Twelve Speed
LICENSES
Get YouTube Premium

Music

Transcript
Follow along using the transcript.

Show transcript

Bro Code
1.24M subscribers

Videos

About

Show less

18,851 Comments
Sort by

Add a comment...

Pinned by Bro Code


@BroCodez

2 years ago
LET'S GET THIS VIDEO TO #1! Like this video and leave a random comment to break the

Youtube algorithm I appreciate all of you ️

️Time Stamps ️ #1 00:00:00 Python tutorial for

beginners #2 00:05:57 variables ✘ #3 00:17:38 multiple assignment

#4 00:20:27 string methods ️ #5 00:25:13 type cast

#6 00:30:14 user input ️ #7 00:36:50 math functions

#8 00:40:58 string slicing ️ #9 00:51:52 if


statements #10 00:58:19 logical operators #11

01:04:03 while loops #12 01:07:31 for loops #13

01:13:04 nested loops #14 01:17:08 break continue pass

#15 01:21:06 lists #16 01:26:58 2D lists

#17 01:30:47 tuples #18 01:33:47 sets

#19 01:40:03 dictionaries #20 01:47:20 indexing

#21 01:53:23 functions #22 02:02:03 return statement


#23 02:04:51 keyword arguments #24 02:07:09

nested function calls ️ #25 02:09:40 variable scope

#26 02:13:23 *args #27 02:16:58 **kwargs #28

02:21:17 string format #29 02:33:22 random numbers

#30 02:36:43 exception handling ️ #31 02:43:40 file

detection #32 02:47:28 read a file #33 02:51:00 write

a file #34 02:53:45 copy a file ️ #35 02:57:05 move


a file ️ #36 03:01:20 delete a file ️ #37 03:06:15

modules #38 03:10:26 rock, paper, scissors game #39

03:18:32 quiz game #40 03:35:45 Object Oriented Programming (OOP)

#41 03:45:06 class variables #42 03:48:54 inheritance

#43 03:55:30 multilevel inheritance #44 03:58:32

multiple inheritance #45 04:01:49 method overriding

#46 04:04:14 method chaining ️ #47 04:08:08 super function


#48 04:12:09 abstract classes #49 04:19:12 objects as

arguments ️ #50 04:23:20 duck typing #51 04:27:38

walrus operator #52 04:31:45 functions to variables

#53 04:35:21 higher order functions #54 04:41:06 lambda λ #55 04:45:44

sort ️ #56 04:53:22 map ️ #57 04:57:17 filter

#58 05:00:10 reduce ️ #59 05:04:54 list

comprehensions #60 05:10:54 dictionary comprehensions 🕮 #61


05:18:59 zip function #62 05:23:41 if name == '__main__'

#63 05:29:21 time module #64 05:39:58 threading

#65 05:53:31 daemon threads #66 05:58:19

multiprocessing #67 06:07:15 GUI windows ️ #68

06:14:38 labels ️ #69 06:24:24 buttons ️ #70

06:30:44 entrybox ️ #71 06:40:15 checkbox ️ #72

06:49:08 radio buttons #73 07:00:47 scale ️ #74


07:10:24 listbox #75 07:24:41 messagebox #76

07:37:17 colorchooser #77 07:43:10 text area #78

07:48:38 open a file (file dialog) #79 07:55:33 save a file (file dialog)

#80 08:05:17 menubar #81 08:15:23 frames

️ #82 08:21:30 new windows 🗔 #83 08:25:32 window tabs

#84 08:30:52 grid #85 08:39:52 progress bar

#86 08:49:48 canvas ️ #87 09:01:18 keyboard


events ️ #88 09:05:54 mouse events ️ #89 09:11:00

drag & drop #90 09:18:18 move images w/ keys ️

#91 09:29:13 animations #92 09:41:31 multiple animations

️ #93 09:53:04 clock program #94 10:01:03 send an

email #95 10:07:37 run with command prompt #96

10:09:53 pip ️ #97 10:13:30 py to exe #98 10:17:13

calculator program 🖩 #99 10:31:38 text editor program ️ #100 11:05:51


tic tac toe game #101 11:26:25 snake game

GAMES & PROJECTS 03:10:26 rock, paper,

scissors game 03:18:32 quiz game 09:53:04 clock

program 10:17:13 calculator program 🖩 10:31:38 text editor program

️ 11:05:51 tic tac toe game 11:26:25 snake game

Here's the link to the full playlist:


https://www.youtube.com/playlist?list... Code for each topic can be found pinned to each
video's comments section
Read more
7K
Reply
167 replies
@Strawbed
1 year ago
Look at this man : -He make an actual good tutorial -He is explaining correctly -He makes a
whole a*s 12 hours tutorial -He goes straight to the point -he actually teach important things -
he got a gigachad pfp
Read more
18K

Reply
104 replies

@markhackney3305
6 days ago
This tutorial was not only better than my college material, but time-lined almost identically.
After wasting days of frustration in college simulators: fired up vs-code, followed along with
you. I'm in a much stronger place now for my next project hand-in. Thank you!
36
Reply
3 replies

@nicholasparks500
1 month ago
3 hours in. So far so good. Clear, quick and concise. 9 more to go.
413
Reply
25 replies

@koariko-ie_lle-lv2vq
1 day ago (edited)
for future ref!! useful timestamps i js wanna save 2:30:12 string format 2:33:05 2:42:00
exception handling 3:18:57 quiz game 4:05:25 method chaining 4:31:20 Walrus operator
4:39:50 higher order operations 4:45:00 lambda 4:59:40 filter
Read more
2
Reply
@kasp9290
1 month ago
You learn more in 12 hours than my school did in 1 year of teaching python! Thanks a lot!
83
Reply
3 replies

@keshandrafreeman5908
2 days ago
You have helped me understand these variables faster than the 2 months I have been in
school learning them. It’s straight to the point without all the book extras. Thank you so

much!
2
Reply

@copcilufaranume
1 month ago
Very fast and concise. You're doing this better than most paid courses!
34
Reply

@valkyrierayne9143
2 months ago
First off, I really appreciate how concise this tutorial is. Also, it's nice having a tutorial that
doesn't make you feel stupid for following it along! A lot of times I struggle trying to figure
out what IDE to use so I can even test code, so this was a tremendous help. Thank you for
your work!
42
Reply

@Daniel-bu4cl
1 month ago
finally been learning this for 3 years and i've forgotten to thank bro code for all the effort he
done
2
Reply
@simonthakur6733
4 weeks ago (edited)
Day1 : 5:47:46 Day2 : 11:24:12 (Was mostly practising today) Day3: 11:29:26 Day4:
11:32:11 Day5: 11:39:34 (Did a lot today) Day6: 11:42:39 Day7: 11:42:56 Day8: 11:47:54
(Started taking notes today) Day9: 11:51:21 Day10: 11:52:04 (Yes day 10!) Day11: 11:55:59
Day12: 11:56:00 Day13: 11:59:11 (49 more seconds!!)
Read more
340
Reply
13 replies

@sangchowmein
1 month ago (edited)
DAY 1 (14-08-2023) : 58:19 (logical operators) DAY 2 (15-08-2023) : 02:21:17 (string
format) DAY 3 (16-08-2023) : 02:36:43 (exception handling) DAY 4 (17-08-2023) :
02:51:00 (write a file) DAY 5 (19-08-2023) : 02:57:05 (move a file) DAY 6 (20-08-2023) :
03:06:15: ( modules) (skipped 2 games) DAY 7 (25-08-2023) : 04:23:20 (duck typing)
Read more
85
Reply
7 replies

@1Chitus
8 months ago
Let's just take a moment of appreciation for this man who made a 12-hour video to help us in
life, for completely free. Thank you.
2.3K
Reply
18 replies

@veerendraguntupalli1282
1 month ago
I want to acknowledge just how easy it is to use your tutorial. I had no prior knowledge of
any coding language going into this, and this course is great!
17
Reply

@domino5162
1 month ago (edited)
Day 1 25:13 Day 2 2:02:03 Day 3 2:33:23 Day 4 2:53:56 Day 5 2:57:05 + Beginner projects
for fun small break Day 6 3:18:32 Day 7 - Day 8 3:26:56
Read more
30
Reply
4 replies

@seven368
3 weeks ago

2 years later and still so useful


21
Reply

@goranbrannstrom
1 month ago
Everything about this course is super. The only thing that is a bit tough is when you just want
to back off 20 secs because you missed something. The time line is really compressed since
it's one long video. I wish it had been broken up separate videos for each chapter. But besides
that, it rocks!
6
Reply
2 replies

@kmo5412
11 hours ago
Day1: 1:27:07 #16 (2D Lists) 12pm - 8pm (idk if i retained any of that)
Reply

@Alex-ur3vt
2 years ago
This guy is single handedly teaching the next generation of developers
5.6K

Reply
76 replies

@shinzo.mp3228
1 day ago (edited)
Day 1: 41:30:01 (String Slicing) Day 2: 1:16:05 (Nested Loops + Completed the Rock,
Paper, Scissors Game)
Reply
Transcript
1.Python tutorial for beginners
0:00
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to help you get started with python so
0:06
sit back relax and enjoy the show if you wouldn't mind please like comment
0:13
and subscribe one like equals one prayer for the youtube algorithm i'm gonna tell you
why you need to learn
0:20
python it's the easiest programming language to learn as well as being the most
popular in the world right now
0:26
and according to glassdoor the salary for new python developers in the united states
is
0:32
64 000 if any of this sounds good to you well then let's get started if you need to
0:37
download python go to python.org downloads and click this yellow download python
0:44
button open when done check add python 3.9 or another version to path
0:51
install now and then give it a little bit it states here that setup was successful so let's
close out of this
0:58
our next step is to download an ide that's an acronym for integrated development
environment think
1:04
of it as software that helps us write other software one ide that i recommend
1:09
is pycharm you can find it at jetbrains.com pycharm go to download if you would like
1:17
to pay for a professional version you can click this download button but since i don't
like to pay for things i'm going to use
1:23
the free community version at this point we just have to follow the standard
installation procedure
1:29
click next you can pick a destination folder but i'll keep the default next if you would
like a desktop
1:37
shortcut then check this i would also recommend updating the path variable next
1:44
install and then give it a minute or two then after finishing i have a desktop
1:50
shortcut so i can just click on this to run pycharm let's create a new project name this
1:56
project whatever you like i'm going to name this project hello world and i recommend
not creating
2:03
a main dot pi welcome script right now i'll show you how to do that manually and let's
create
2:09
if you need a python file to work with this is how to create one go to file new python
file and i'm going to
2:17
name this main and then click python file
2:23
all right ladies and gentlemen we now have an empty python script that we can use
to begin coding
2:28
now if you checked this box create a main.pi welcome script and then created a new
project
2:34
then your main python file will probably look a little something like this but that's
completely fine we can just
2:40
delete this i don't know about you guys but the font is very small for me so let's
change that we can change that
2:47
by going to file settings editor font and you can
2:52
increase the font size let's say 25 you can also change the font too
2:57
what's a good one uh how about that then click apply and
3:04
okay much better we can actually see something to run a python script just click the
3:09
screen play button in the corner alternatively you can go to the run tab and then go
to run
3:15
okay so this box that appeared at the bottom this is referred to as a terminal window
this will display
3:21
any output that your program has and this line at the end process finished with exit
code zero
3:27
that just means that there were no errors in this program so currently this program
doesn't do anything but let's change that let's
3:34
print something let's print a message so to print a message to the terminal window
3:39
type print and then add a set of parentheses and then you can type a message but it
has to be within quotes
3:46
either double quotes or single quotes it doesn't matter so let's think of a message i
don't know
3:52
i love pizza then let me run this and in my terminal window it says i love
4:00
pizza to print another line just add another print statement print and then your
message within
4:06
quotes it's really good
4:13
and well what do you know it printed i love pizza it's really good so ladies and
gentlemen
4:19
you just wrote your first python program by the way you can change the font as well
as the font color of your terminal
4:26
window by going to file settings then go to editor color scheme console font and
console
4:34
colors let's change the font let's say i would like whatever this font is then click apply
4:43
let's change the font color too by going to console colors for any output you can click
number two
4:51
and select a color let's say i would like bright green that should be decent
4:59
and for anything else let's say this will be a dark color so i can barely see it
5:05
so then let's click apply and okay oh yeah that's much better well
5:10
everybody that was your very first python program i'll post any useful links and
timestamps for this
5:16
video in the comments section down below but yeah welcome to coding with python
5:22
hey you yeah i'm talking to you if you learned something new then help me help you
in three easy
5:28
steps by smashing that like button drop a comment down below and subscribe if
you'd like to become a
5:34
fellow bro [Music]
5:56
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to explain how
2.variables
6:02
variables work in python so sit back relax and enjoy the show
6:09
if you find this video helpful please remember to like comment and subscribe your
support will
6:15
help keep this channel running all right let's do this a variable is a
6:20
container for a value it behaves as the value that it contains it's much like in algebra
lessons back
6:26
in the day where we had to solve for whatever value x was and then we can reuse x
and it
6:31
behaved exactly as that value well with programming we can do that plus we can
assign variables of
6:37
different data types so they are not limited to just numbers we can assign whole
words
6:42
numbers and even these things called booleans which are either true or false but i'll
get more in depth into data
6:48
types a little bit later on in this video so let's create a variable and this is how to do so
we need a unique name for
6:54
this variable let's say name name equals some value now the first data type that we'll
cover
6:59
is strings a string is a series of characters so to create a string we can either use
single quotes or
7:06
double quotes in python and we can assign this a value of whatever your own name
is
7:11
so i will assign this variable name a value of bro now this variable will behave exactly
as
7:18
if it was this value this name and then we can reuse this variable for something so
let's print whatever our name is
7:25
print name and this will print whatever your name is to the console window now
when you print your variable make
7:32
sure you don't put it within quotes because what we're doing then is literally printing
7:37
the word name as you can see here so if you need to use your variable for
7:42
something make sure it's not within quotes now we can combine our string variable
7:48
with another string so within our print statement let's print a string literal such as hello
space
7:55
plus name so we are combining strings and then we're printing the results to the
console window so this will print
8:02
hello bro now you can actually check the data type of a variable so i'm going to turn
this line into a
8:08
comment so what you'll do is take the name of the variable surround this with a set of
parentheses
8:14
and then precede the set of parentheses with the type function and this will print the
data
8:20
type of this variable to the console window and you can see that it says class str
8:25
short for string a string is a series of characters a name is a good example of a string
8:30
a series of characters so that is how to check the data type of a variable just use the
type function
8:37
now you can actually combine variables together as long as they're of the same data
type so let's change our name to
8:44
maybe first name and we'll create a second variable called lastname now a common
naming convention with
8:49
variables if it has two words separate each word with an underscore it's not
necessary but it's common
8:56
practice for python and honestly i sometimes forget about that but don't tell anybody
so let's create another variable called
9:02
last name last name equals whatever your last name is and then let's create a third
variable
9:09
called full name full underscore name and let's combine first name plus last
9:18
name and let us display full name along with hello plus full name
9:25
now these two names were combined together to create a whole new variable
actually what i think i'll
9:30
do is add a space within my variable so i'm doing a bunch of string concatenation i'm
combining
9:37
the variable first name plus a space character plus last name so the result is hello
9:43
whatever your first name and last name is so that is a variable of the string data
9:48
type it is a series of characters now with strings we cannot normally use these for
any sort of math
9:55
that's where the next data type comes in for the time being i just turned all of these
lines into comments and we are
10:00
going to move on to the int data type short for integer this time let's create a variable
called
10:06
age and we will assign this a value of whatever your age is let's just say that i'm 21
now when you
10:12
assign a value of the int data type make sure that it is not within quotes because
then
10:18
technically it would be a string then and you can see that the color scheme for this
number actually changed
10:23
so if this was a string we would treat it different than an int data type because with
10:28
strings we cannot normally use them for any sort of math so we now have a variable
called edge
10:34
and we can actually increase or decrease or do whatever we want with the edge let's
increase our age by one let's say
10:41
it's our birthday to do so we would say age equals age plus one and then let's print
10:46
our edge to the console window print edge and you can see that it says i am 22.
10:52
now there's a shorthand way of writing this what you'll do is type age plus equals one
so that's kind of
10:58
like a shortcut and this would work the same too now let's print the data type of our
inch variable because we can within a
11:05
print statement i will print the edge and add my edge variable to this type
11:10
function and this will print the data type of my edge variable to the console window
and i will turn this line into a comment
11:17
just so that it doesn't get in the way now if i were to print the data type of my age
variable
11:23
as you can see it is of the int data type short for integer it only stores a whole integer
number
11:30
now what if this was a string so what i'm going to do is surround my value within a
set of
11:36
quotes and i will turn this line into a comment so that it does not get in the way you
can see that the data type of my age
11:43
variable is now a string and with strings we cannot normally use them for any sort of
math
11:48
so let's attempt to increase our age variable by one and we ran into an error a type
air can
11:54
only concatenate string to string not end now the point being is that
12:00
it's important to use the appropriate data type because with strings we cannot
normally use them
12:05
for any sort of math you'll want to use a data type of a numeric value an int or a float
which we'll discuss a
12:11
little bit later now here's a situation to consider what if we would like to display our
edge variable along with a string
12:17
literal much like what we did with this line involving a string literal such as hello plus
the user's full name so let's
12:25
attempt to do so and this is not going to work and i'll explain why so within our print
statement let's say
12:30
something such as your age is plus edge now we're going to run into a type
12:38
error that's because we attempted to use string concatenation with a string literal
12:43
and a variable of a different data type a variable of the int data type in order to
display our
12:50
edge along with the string we would need to convert our edge variable to the string
data type and one way in which we can do
12:57
that is by type casting and we'll cover this more in the next video so we're going to
surround
13:02
our age variable that is of the in data type with a set of parentheses and precede this
with str short for
13:09
string and this will convert our variable to a string that will allow us to display it with
another string
13:15
now if we were to run this program this way this will now display your age is and in
this case 22 so if
13:22
you need to display a variable of a different data type along with the string you would
just
13:27
need to use a stringcast to convert that data type to a string but we'll cover this more
in the next
13:32
video on typecasting so that is the int data type it stores a
13:37
whole integer number and the next data type is the float data type and i'm going to
turn
13:43
these lines into comments and we can move on now the float data type is a numeric
value that can store
13:48
a number that includes a decimal portion an int data type cannot store a decimal
13:54
portion that's why it's in short for integer it only stores a whole number this time let's
say we have a
14:00
height and i will assign this a perfectly normal height of maybe 250.5
14:06
centimeters so this decimal portion is important that's indicating that this is a
14:11
floating point number a float for short so let's print our height to the console window
print
14:17
height and this will print whatever my height is and now let's check the type of our
height using
14:23
the type function so within a print statement i'm going to add my height
14:28
surround this with another set of parentheses and precede this with the type function
and the data type
14:35
of my variable height is a float short for a floating point number a numeric value that
contains a decimal
14:42
portion and now for practice let's print our height along with a string literal so let's
create a string literal
14:48
such as your height is plus height but guess what we're going
14:55
to do some type casting so surround height with a set of parentheses and precede
this with
15:00
str so that we convert height to a string so that we can use string concatenation
within this print
15:06
statement and this will print the message your height is 250.5 actually let's add
15:12
centimeters to the end so plus cm
15:17
and there we go and that is what a floating point number is float for short it is a
numeric value
15:22
that contains a decimal portion now the last data type that we're going to cover is the
boolean
15:28
data type it is a variable that can only store true or false let's say we have a variable
15:34
called human if we were to assign this a boolean value this would be either true or
15:40
false and we can print whatever this value is let's print whatever human is and this
will display false now
15:47
the reason that you may want to use booleans instead of strings even though you
can still store
15:52
a string representation of the word false or true is that these are very useful when we
15:58
get to if statements we can check to see if some statement is true so that is a good
use of
16:04
boolean values now let's check the data type of my human variable so let's print
16:11
type human and this will print to the console window bool short for boolean now
make sure
16:18
you're not putting this within quotes because then it's technically a string and they
behave differently
16:23
now the data type is a string and you may have noticed as well that the font color
has changed between
16:29
strings and boolean values so that's another good indicator now what if you need to
display your
16:35
value along with a string using string concatenation well guess what we're going to
typecast again so let's print something
16:44
r u a human so we need to use string concatenation
16:50
so i will convert my boolean variable of human to a string
16:55
and i have been programmed to inform you that yes i am in fact a human that is true
17:01
well everyone that is the basics of variables it is a container for a value and they
behave
17:07
as the value that they contain there are four basic data types strings which store a
series of
17:13
characters inc which store a whole integer floats which are floating point numbers a
numeric
17:20
value with a decimal and booleans which only store true or false
17:25
and they're very useful with if statements so that is it for variables if you would like a
copy of all this
17:31
code i will copy and paste all of this to the comments section down below but yeah
that is how variables work in
17:37
python hey people what's going on it's bro hope you're doing well and in this video
i'm going to explain
17:43
multiple assignment in python so sit back relax and enjoy the show
17:50
well then since you clicked on this video i should probably explain what multiple
assignment is now multiple assignment allows us to
17:57
assign multiple variables at the same time using one line of code
18:02
here's an example of us using standard assignment let's say we have a variable
name and i will set this to a value of
18:09
my name but you can use your name for this example let's say edge equals 21 and
how about a
18:15
variable called attractive i think i'm going to set this to true
18:20
okay so we have a bunch of variables and then we can print the value of these
variables with some
18:25
print statements so let's print name age and attractive
18:30
so we have name age attractive and as you would expect this prints bro
18:37
21 and true now another way in which we could write the same code is to use
18:42
multiple assignment and this allows us to assign multiple variables at the same time
using one line of code
18:48
so i'm going to turn all of these lines into comments and this time we will only use
one line of code but to
18:55
do this we're going to list all of our variables separated with a comma so that would
be
19:00
name comma edge comma attractive and we will set them equal to those
19:08
values but in the same order separated by commas so that would be bro comma 21
comma true
19:15
and this would do the same thing only using one line of code here's another example
let's say we have
19:21
four variables spongebob patrick sandy and squidward and let's pretend that they're
all around
19:27
maybe 30 years old at least it seems like they're around 30 years old but i could be
wrong i'm not too familiar with spongebob lore
19:34
so let's set them all equal to 30. now we could write this using four lines of code or
19:40
we could use multiple assignment to write the same thing using one line of code so if
all of
19:45
these variables are all equal to the same value there's a little bit of different syntax
here
19:51
so let's begin with the first variable of spongebob equals patrick
19:59
equals sandy equals squidward then we're going to set them
20:06
all equal to 30 and this should work just fine using only
20:11
one line of code well everybody that's pretty much it for multiple assignment it allows
us to assign multiple variables at the same time
20:18
using one line of code i will post all of this code in the comments section down below
20:23
and well yeah that's how multiple assignment works in python hey what's going on
everybody it's bro
4.string methods 〰️
20:29
hope you're doing well and in this video i'm going to demonstrate some useful string
methods in python so
20:34
sit back relax and enjoy the show all right let's do this in this video
20:42
i'm going to be explaining and demonstrating a few useful methods available to us
for strings
20:47
now let's create a variable called name for this example and you can use your own
name if you want so we now have a variable called name
20:54
and for my first example let's print the length of our name variable
21:00
so to do that we can surround our name variable with the length method and this
21:05
will print and return how long the length of our string is and for me the length of my
name
21:11
variable is three for three characters if i were to increase the size of my name
21:17
well then that's going to change to a so that is the length method and i'm just going
to
21:22
turn this line into a comment and let's move on so next we have the find method
21:27
i just copy and pasted print name so i can reuse it name dot and actually if you have
21:33
intellisense setup if you add dot after your variable you'll get a suggestion or a few
21:39
suggestions of different methods and other features that you have access to so let's
find find which is
21:48
uh screw this i'll just type find find then we can find the first index of
21:54
where this character is let's say i would like to find where capital b is well that is at
index zero because
22:01
computers always start with zero so this first character would be zero then r
22:06
would be one and then o would be two for this example all right so that
22:12
is how to find a character within a string moving on so let's
22:17
capitalize our name and i should probably make this lowercase
22:23
so name dot capitalize and this will make my name capitalized
22:29
but if there's any spaces and then additional words it's not going to capitalize that
only the first letter and your string
22:37
next we have upper this will make your string all uppercase name upper and now
22:45
it is uppercase guess what's next lower name dot lower
22:52
and i should probably make this capital again and everything is lowercase
23:00
we're making some good progress today next we have is digit this will return true or
false
23:07
depending on if our string is a digit which it's not so this will print false
23:14
if i changed my string to a bunch of numbers like 1 2 3 then this will return true let's
23:19
change that back and move on next we have
23:25
is alpha are these alphabetical characters
23:33
oh make sure you add the parentheses afterwards my bad false because we added
a space within
23:41
here now it's true so you can use is alpha to
23:46
check to see if your string contains only letters alphabetical letters
23:51
next we have count we can count how many characters are
23:57
within our string i would like to count how many o's are in here and this should return
two that's right okay we're almost at
24:06
the end we can replace characters within our string so we use the replace method
24:13
and we pass in two arguments the character we would like to replace and the
character we would like
24:20
to replace our character with i would like to change all o's to a's and now my name is
brock hod oh you guys are
24:28
gonna like this next one this isn't technically a method but it's a really neat feature
24:33
so one thing that we can do that's available to us with python is that we can display a
string multiple times by multiplying that
24:40
string by a given number so let's say i would like to display my name three times i
can just take my
24:46
string variable then add times three and this will print my name three times bro bro
bro like i said not
24:53
technically a method but it's a useful feature that you might be interested in that's
related all right everybody so that is a few
25:01
useful methods available to us for strings if you would like a copy of all this code i will
post this in the comments section
25:07
down below but yeah those are a few useful methods available to us for strings
5.type cast
25:13
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to teach you guys all about typecasting in python so
25:19
sit back relax and enjoy the show all right welcome back everybody i have
25:26
a super quick video for you guys today on the subject of typecasting now typecasting
is the ability to convert
25:32
the data type of a value to another data type here's an example that i've written
already i'm just going to paste it
25:38
i have three variables for this example x y and z x contains one this is a whole
25:45
integer into for short y contains two point zero this is a floating point number
25:50
float four short you can tell it's a float because it ends with a decimal portion and z
contains a string of three a
25:57
string is a series of characters and you can tell it's a string because it's surrounded
with a set of quotes
26:03
and remember with strings we cannot normally perform math on strings so i have
three print statements for x y
26:11
and z and as you would expect they contain the values of one two point zero and
three now let's
26:17
begin by converting both y and z to the integer data type via typecasting in order to
perform
26:23
typecasting on a value or variable type that value or variable and surround this with a
set
26:28
of parentheses whoops i missed and then pre-save this with the data type you'd like
to convert
26:34
this value or variable to i would like to convert y to an integer and then you can use
this for whatever
26:40
you need so i'm going to replace y within my print statement with y after we typecast
it as
26:47
an integer and this will display 1 2 not 2.0 it's the whole integer
26:54
of 2 and then 3. so this is not a permanent change if you need to make this as a
permanent
27:00
change to y you'd have to reassign it so let's say y equals y and then we're going to
27:07
typecast y so let's round this with a set of parentheses and precede this with the data
type we'd like to convert
27:14
y2 so now y is a whole integer and then we will print y without
27:20
typecast this time and our result is one two three now z z is storing a
27:27
string and with strings we cannot normally perform any math on strings for example
within my print statement i
27:33
will state z times three you would think that the result would be nine right
27:39
well you're wrong it's going to print three three times if you need to convert a string to
a int
27:46
or a float you would just have to typecast it so this time let's type cast z as an integer
z
27:53
int and we will type cast z as an integer so then if we have z times three
28:00
the result is now 9 instead of 333 so that's the basic formula for type
28:06
casting now let's convert these to floating point numbers but let's include x as well
because
28:12
x is feeling left out so let's convert x to a floating point number so float
28:17
parenthesis and we will surround this with x and display x
28:22
and actually let's convert y and z to floats as well because why not
28:29
the results are now 1.0 2.0 and 9.0 remember that we multiplied z times
28:35
three and then if we need to convert these values to a string
28:41
we would use the string cast
28:46
so the results now are 1 2.0 and 333 now you might be thinking to
28:52
yourself when would you need to convert an integer or a float to a string here's a
situation let's say within our
28:59
print statements we need to print these variables along with another string so within
our print statement
29:06
let's say x is plus x we would like to display these
29:11
together and let me copy this and do the same thing for y we'll say y is plus y
29:18
you cannot normally display a string along with an integer or a float because we're
using some
29:24
string concatenation so this will result in a compilation error can only concatenate
29:30
string not int to string so that's a situation in which you might need to use
29:36
typecasting to convert an integer or float to a string so let's display
29:41
x and y as strings via typecasting so we'll surround x and y with a set of
29:47
parentheses and precede this with the data type we'd like to convert these values to
we would like to convert x and y to both
29:53
strings and now this will compile and run just fine well everybody that's the basics of
typecasting
30:00
if you would like a copy of all my code i will post everything in the comments down
below if you found this video helpful don't be
30:06
afraid to help me out by smashing that like button drop a comment down below and
subscribe
30:11
if you'd like to become a fellow bro hey what's going on everybody it's bro
6.user input ⌨️
30:16
hope you're doing well and in this video i'm going to teach you guys how we can
accept some user input in python so sit
30:22
back relax and enjoy the show ladies and gentlemen this is where the
30:27
fun begins in this video i'm going to explain how we can accept some user input in
python and we do so by using the input function
30:35
and then we type in our input to the console window but it would probably be good
practice to let the user know what we want them
30:41
to type in exactly so let's ask for somebody's name within quotes inside the input
function i'm
30:47
going to type what is your name and now our program will wait for us to enter in
30:54
some user input so you can use your own name for this example i'm going to type
bro now to submit some user input you just
31:00
type or press enter now you can see that my process finished with exit code zero
that means
31:06
the program is done running so we should probably do something with this value we
can actually assign it to a variable so we can use it elsewhere in
31:13
our program so to do so we precede input with a variable like name works name
equals
31:20
input and when we accept some user input we will assign it to this variable that we
called them
31:25
and now we can do something with it so let's print a message involving our name
variable that we have
31:31
so how about hello plus name now when i run this program we can
31:37
accept some user input and then do something with that input such as display
message that says
31:42
hello bro and then you can type in whatever you want for your name let's say i'm
dude what is your name
31:48
dude hello dude now this next part is very important when we accept user input it is
always
31:54
of the string data type a series of characters what if we need to accept a number
32:00
well you can but you can't normally perform any math on it because it's of the string
data type
32:05
we would need to cast it as either of the integer data type or of the float data type so
here's an
32:11
example of why we can't perform any math on a string let's say we have a variable
32:16
called edge age equals input and let's ask how old somebody is how old
32:23
are you now i will add one to somebody's age let's pretend it's their birthday
32:30
age equals age plus one this will add one to their age now let's try and run this what
is your
32:35
name bro how old are you let's say that i'm 21. okay we ran into an error a type error
32:42
can only concatenate string to string with what i mentioned previously we cannot
normally use
32:49
strings for any sort of mathematical equation or operation we'll want to cast it to the
integer or
32:55
the float data type so let's cast our user input to the integer data type and we will
33:01
surround our user input with a cast surround your input with a set of parenthesis and
precede this with
33:08
int and now we can use this user input in a mathematical operation so let's try it even
though this won't
33:15
display age yet so what is your name bro 21 and our program finished with
33:20
an x a code of zero there were no problems now let's attempt to display this edge so
i will print
33:28
u r plus age plus years
33:35
old we're doing some string concatenation here what is your name bro how old are
you 21
33:42
all right we ran into a type error again can only concatenate string to string you
cannot normally display a variable
33:50
of the integer data type along with strings because we're doing string concatenation
we're adding
33:55
strings together we would need to convert this variable back to the string data type
so we can cast it we'll
34:02
surround our edge variable with a cast now this is what's going to happen we will
accept some user input it will be of the string
34:09
data type and then we will immediately cast it to be of the integer data type so that
we can treat it
34:15
as a number instead of a character and then if we need to display this edge we will
need to convert it back to
34:21
a string so that we can use some string concatenation and display all of these strings
together
34:27
and now let's try this theory so what is your name bro how old are you let's say i'm
21 hello
34:33
bro you are 22 years old here's a situation what if we enter a number that contains a
decimal portion
34:39
so let's try this again let's say i'm 21.5 years old well now we ran into a different issue
a
34:47
value error invalid literal for int with base 10 that means if we cast a string as an
34:54
integer it can only be a whole number a portion that does not contain a decimal and
that is where the float
35:01
data type would come in it is a data type that can contain a decimal portion
35:06
for this example let's create a third variable called height and we will ask for
somebody's height
35:12
input we'll create a prompt that says how tall are you
35:21
and we will cast this to be of the float data type so that our string our user input
35:28
can contain a decimal portion and then we can treat it as a number instead of a
35:33
series of characters and i think i will delete this line we won't really need it anymore
and then let's print their height along
35:40
with a message u r plus then height but we need to cast this back to
35:47
being a string so we'll surround our height with a cast
35:54
plus let's say cm tall all right let's test this
36:00
what is your name bro how old are you let's say i'm 21 how tall are you let's
36:06
pretend that i'm 250.5 centimeters tall
36:12
i'm adding the 0.5 just to test to see if this will accept a floating point number
36:17
hello bro you are 21 years old you are 250.5 centimeters tall
36:23
not really all right everybody so that is the basics of user input in python normally
with user
36:29
input it will give you a value of the string data type if you need to use that value for
any sort of
36:35
math you'll probably need to cast it to be of the integer or the float data type so if you
would like a copy of this
36:42
code i will post this in the comment section down below but yeah that's how to
accept user input
36:47
in python hey what's going on everybody it's bro hope you're doing well and in this
video
7.math functions
36:53
i'm going to demonstrate a few useful functions related to numbers in python so sit
back relax and enjoy the show
37:03
okay let's begin in this video i'm going to be demonstrating a bunch of useful
functions related to numbers
37:09
and a bunch of these functions are located within the math module now let's create a
variable for
37:15
this example let's say pi equals 3.14 just the first few digits
37:21
of pi is fine for this example and now here's a bunch of useful functions that you guys
would probably
37:26
be interested in the first is round round is a built-in function and all we do is type
round
37:32
then add a set of parenthesis and then we can pass in a value or a variable as an
argument and
37:39
the round function will round that number for us so pi rounded to the nearest whole
integer
37:45
is you guessed it three now we can round a number up by using the seal function of
the math
37:51
class seal short for ceiling so let's print our result and in order to access this
37:58
function within the math module we would type the name of the module dot and
here's a bunch of functions that we
38:04
have access to so we are looking for seal and it's actually at the top here for me and
at seal we'll round a number up to
38:11
the nearest whole integer so pi rounded up is four then on the flip side we can round
a
38:18
number down by using floor and that is also located within the math module so let's
type math dot floor and
38:25
pi rounded down would be three up next i have the absolute value
38:32
function but it's abs for short abs will give you the absolute value of a number
38:38
so the absolute value of pi actually let's make this negative 3.14 the absolute value
will tell you how far
38:45
a number is away from zero so if you pass in a negative number it will give you a
positive number so
38:52
that is abs short for absolute value next we have the pow function pow will raise
38:59
a base number to a power let's raise pi to the power of two so we pass in
39:06
two arguments a base and an exponent pi to the power of two is nine point
39:11
eight and some change so that is the pow function next we have square root
39:20
so this is located within the math module we'll type math dot s q r
39:27
t short for square root and the square root of pi is let's find out oh i guess we can't
39:34
pass in pi uh let's round a different number like 420
39:39
oh i guess that's because i made pi negative okay so the square root of 420 is 20.4
and some change so that is the
39:47
square root function all right we have two left we have the max function the max
function will find the largest
39:54
of a varying amount of values so let's create three variables for this example
40:00
let's say x equals one y equals two and z equals three we can use the max
40:07
function to find the largest of these values so within a print statement let's use the
max function and pass in
40:15
all of the different values or variables that you would like to compare so i would like
to find the largest
40:21
value between x y and z and the result in this example
40:26
is three now the min function will find the lowest so min and then pass in your values
or
40:34
variables so i would like to find the minimum value between x y and z and in this
case it is one
40:42
well everybody those are a few useful functions that you guys might be interested in
40:47
if you guys would like a copy of this code i will post this in the comments section
down below but yeah those are a few useful
40:54
functions related to numbers in python hey what's going on everybody
8.string slicing ✂️
41:00
it's bro hope you're doing well and in this video i'm going to explain string slicing in
python so sit back
41:06
relax and enjoy the show all right then let's talk about slicing
41:12
slicing can be used to create a substring by extracting elements from another string
the same principle can also be applied
41:19
to different collections as well but in this video we're going to be focusing on slicing
strings
41:24
now to slice a string we can either use the indexing operator which is a set of square
brackets or by
41:31
use of the slice function to create a slice object this is a little more complex
41:36
we'll discuss the slice function near the end of this video so with slicing there are
three optional
41:43
arguments three fields that we can fill in depending on where and how we want to
slice our string a
41:49
starting index a stopping index and a step let's begin with the starting index
41:54
now for this example let's say we have a string variable called name and this will
store
42:00
a value of whatever your own full name is yes i legally change my name to bro code
you can use your own
42:06
name for this if you want so what i would like to do is to create a substring based off
a sliced portion of my full
42:13
name so what i would like to do is create a substring based off of just the first
42:18
part of my name just bro so let's begin by creating a substring
42:23
maybe i'll call this first name equals now to slice a string
42:30
type in a string or the name of a variable that contains your string and follow this with
the indexing
42:36
operator and like i said we'll discuss the slice function a little bit later so we need
42:42
a starting index and a stopping index but we'll begin with the starting index
42:47
where do we want to begin our slice so computers they always start with zero
42:52
so the first character in your string will have an index of zero i would like my slice to
begin
42:58
at index 0 so i'll list that for the starting position the starting index so let's test this just
to be sure it
43:05
works i'll just print first name and let's see if this prints
43:10
capital b which it does so following that same pattern the next character
43:15
within my string would have an index of one that would be r then the next index two
would be
43:22
o and three should be an empty space all right so that is the starting index
43:29
within string slicing if we would like to slice an entire portion of our string
43:34
not just one letter we need a stopping index as well and both of these fields are
separated
43:40
with a colon so let's say i would like to slice the first three characters of my string
43:46
so this would have an index of zero one two and this isn't going to work as you
43:52
would imagine and i'll explain why so you would think that indexes 0 through 2 would
give us three letters
43:59
right but it only gives us two so this first index is inclusive
44:05
the stopping index is exclusive so if i want the first three characters the first three
letters
44:12
in the string i would need the starting index to be zero and the stopping index to be
three and
44:18
now this should print my first name and not anything else there is a shorthand way of
writing this
44:24
too so if you were to leave this first index blank for start well python is going to
assume
44:30
that this is zero the beginning of your string so this would be no different than what
we had previously
44:37
just having colon three now let's try and create a separate substring for a last name
so last
44:45
name name now we need a starting index and a stopping index so
44:52
we need the starting index at least so this would be zero one two three four
44:59
four colon and where do we want this to stop zero one two three four five
45:05
six seven and remember that this is exclusive so we're going to place eight here and
45:11
now i would like to display last name and this should just display code
45:16
and a shortcut is that if you were to leave stop blank but you still have that colon
45:22
this is a shorthand way of writing i would like every character starting at index four
45:28
and everything after until the end so there would be no change to this as well you
can write it either way okay
45:34
so we still have to discuss step step is an optional field that we can set a value to
45:40
so step is how much we're increasing our index by between starting and stopping so
it is
45:46
entirely possible to create a substring that will count only every second character
after the first
45:53
normally step is one by default but if we were to set that to two we're going to count
only
45:58
every second character so let's try this maybe i'll create a new variable called funky
name because i
46:05
really don't know what else to call this for this example let's say funky name equals
name and i'm
46:10
going to count only every second character including the first so the indexes would
be zero colon
46:18
eight for my example but if you're using your own name that's probably going to be
something different so the default first step is one and if
46:26
i were to display funky name as it is currently this will just display my full name but if
we set step to two this will only
46:33
display every second character including the first so funky name is now for me at
least b o c d now if step
46:42
was three this will print only every third character including the first where it's b space
d a shorthand way of
46:50
writing this is that you could leave start and stop empty but the important thing is to
have
46:55
these two colons python is going to assume that if you leave your starting index
empty you're referring to index 0 the
47:03
beginning if your second index your stopping index is empty python will assume this
will be the very
47:10
end of your string so just having two colons and then step three would be no different
than what we had previously
47:16
or this next part i'm going to explain how we can reverse a string in python so
47:22
let's create a new substring variable called reversed name equals name i would like
47:27
to use the entire string so i can just use two colons python will fill in the rest
47:33
so for the step this is going to be negative one it's kind of like we're counting
backwards and we're going to create
47:40
a new sub string based off my name but in reverse so if i were to print my reversed
name
47:47
this is now edok orb i think that's how you pronounce it so that's how to reverse a
string in
47:53
python using slicing now let's move on to part two of this video i'm going to be
explaining the
47:59
slice function and we can use the slice function to create a slice object which is
actually reusable so
48:05
that's somewhat convenient let's take a different scenario this time let's say we have
a website url
48:11
website equals http colon2forwardslashes google.com but use whatever website you
48:18
want what i would like to do for this program is remove and create a substring based
48:23
off the website name and not anything else i would like to exclude the http portion as
well as the
48:30
dot-com portion of this website url so let's break it down step by step our first step is
to create a slice
48:37
object so i will name the slice object as slice slice equals and i will invoke the slice
48:43
function so within the parentheses of the slice function we can add up to three values
a start a stop and a step
48:52
exactly the same as what we did with indexing however with the slice function we
separate each of these values with a
48:59
comma instead of a colon but it works the same way so we would like our substring
to begin where the name of
49:05
the website begins so we would like our start to be zero one two three four five six
seven
49:13
so i will pass in seven for where my substring will begin and now each value within
the slice
49:19
function will be separated with a comma instead of a colon like what we did with the
indexing operator
49:25
now for the stop index that's going to be a little bit tricky and let me explain why one
issue that
49:31
we're going to run into is that not all website names are consistent with their length
they can
49:36
really vary so it is not possible for us to use the same stopping index
49:41
for each of these website urls so one way in which we can account for that is to use
what is called a negative
49:48
index each character within a string has a positive index as well as a
49:53
negative index and a negative index works almost exactly the same way
49:58
except the character most on the right begins with a negative index of minus one and
the character on the
50:06
left of that would be negative two negative three and negative four it's as if you're
counting backwards so
50:12
with indexing we can use some combination of positive and negative indexing so i
50:18
would like my substring to end where this dot is so this would have a index of minus
one minus two
50:26
minus three minus four and remember that this is exclusive so i would like my slice
to begin at
50:32
index seven and a negative index of four for the stopping position
50:38
so that will give me just the website name and remove the http portion as well as the
dot com
50:44
portion of my website url and now we have a slice object and we can reuse this
50:50
so to apply your slice object type in the name your string index operator and place
your slice
50:57
right within here and let's print this so print our website and apply the slice to it
51:03
and now this should give us just the website name and now let's test this with maybe
another website
51:09
i'll rename this as website one and let's create website2
51:14
and let's try a different website name for goodmeasure so let's try wikipedia so we
can reuse
51:22
our slice object and we will apply our slice to website two this time and this should
remove the url portions
51:30
http as well as com all right everybody so that is slicing strings
51:36
in python you can either use the indexing operator or the slice function to create a
slice
51:42
object if you would like a copy of all this code i will post all of this in the comments
section down below
51:48
but yeah that is how string slicing works in python hey what's going on people it's
you bro
9.if statements
51:54
hope you're doing well and in this video i'm going to explain if statements else if
statements and else statements and python so
52:01
sit back relax and enjoy the show all right people let's talk about if
52:08
statements an if statement is a block of code that will execute only if its condition
52:13
is true it's a very basic form of decision making with programming for this example
let's create a prompt
52:21
where we will ask a user for their age and depending on what their age is we will
print a response
52:26
so let's say age equals input and then we will add a prompt how old are you now this
will return a
52:33
string so we should probably cast this as an integer data type so we now have a
variable called age
52:39
where we will accept some user input let's check somebody's age with a variety of if
statements
52:45
so let's check to see if somebody's age is greater than or equal to 18 so to create an
if statement type if and
52:51
then some sort of condition if age is greater than or equal to 18 to finish this if
statement add a colon
52:58
at the end now pay attention to this if i move down to the next line you'll notice that
my cursor is now
53:04
indented any indented code underneath an if statement is the block of code for
53:10
that if statement so if this condition is true we will execute some block of code if it's
false we'll skip
53:17
over it so if somebody's age is greater than or equal to 18 let's print you are an
53:24
adult and let's try this program how old are you let's say i'm 21 so my condition when
we
53:31
get to the if statement is going to check to see if this condition is true if it is true we're
going to
53:36
execute this block of code if it's not we skip over it so how old are you let's say i'm 21
i'm
53:42
going to hit enter and my if statement is true so we execute this block of code which
prints
53:48
you are an adult so what if this condition is false let's say i'm 12. well then we're just
53:53
going to skip over this block of code and continue on with the rest of the program if
you would like your program to take
53:58
some other course of action if this condition is false we can add an else statement so
after the if statement add
54:06
else make sure you get the indentation right you don't want this within the block of
code for the if statement
54:11
so if this condition is false we can take some other course of action so if
54:17
somebody's age is less than 18 let's print
54:22
you are a child so let's try this again i'm going to
54:28
enter that i am 12 years old not really but let's just pretend i am so this will now print
you are a child
54:35
if this condition is false perform this block of code for the else statement
54:40
so with if statements we can check more than one condition before reaching the else
statement by using an
54:46
else if statement and we add that after an if statement so it's shortened to e l
54:53
i f for else if so let's check to see if somebody's age is less than zero for some
reason
55:00
so let's print you haven't been born yet
55:08
so with if statements we start at the top we first check the if statement if this is false
we
55:14
move down to the next statement to check then we check our else if statement if this
is also false then we resort to
55:21
the else statement the else statement is sort of like a last resort if all conditions
55:27
evaluate to be false then we execute the else statement so let's try this how old are
you let's
55:33
say i'm negative 1 years old you haven't been born yet so with if statements we
always begin
55:40
with an if statement and we check that condition if that condition evaluates to be
false
55:46
then we will skip this block of code and move down to the else if statement if there is
one it's optional if this
55:53
condition is also false then we will use our else statement as a last resort
55:58
if there is one if there is no else statement then we just skip over all of this then and
continue on with the rest
56:04
of the program so you can add more than one else if statement so let's check
something else
56:10
let's check to see else if age is equal to 100 so if you need to check
56:17
to see if a value is equal to a particular value make sure you use double equal signs
56:24
this is the comparison operator for equality if you use just one equal sign that's the
assignment
56:31
operator and python thinks you're attempting to set age equal to 100 so if you want
to check to see if age is
56:38
equal to 100 then use double equals so let's check to see if somebody's age is
56:44
equal to 100 so let's print you are a century
56:52
old now pay attention to this if i were to run this program how old are you let's say i'm
100. so
56:59
this is printing you are an adult that's because we first check our if statement and
well we're 100
57:06
years old and 100 is greater than or equal to 18. so we print this block of code we
execute this block
57:12
of code and then we skip everything else even though age is equal
57:17
to 100 within our else if statement so the order of your if statements
57:23
does matter so let's change this around let's add this to the very beginning and this
will be
57:30
our if statement and we will set age is greater than or equal to 18 to be an
57:35
else if statement so we're first going to check to see if age is equal to 100
57:40
if not we will check to see if age is greater than or equal to 18 and we just follow that
order so let's
57:47
try this again how old are you i am 100 years old you are a century old so we first
check our if
57:54
statement if this condition is true we will execute this block of code if it's false we just
go down the order
58:01
till we reach our else statement well everybody that is the basics of if
58:06
statements we have if statements else if statements and else statements so if you
would like a copy of this code
58:12
i will post this in the comments down below but yeah that is how if statements work
in python
10.logical operators
58:19
hey what's going on everybody it's bro hope you're doing well in this video i'm going
to explain logical operators in python so sit back
58:26
relax and enjoy the show all right everybody let's talk about
58:32
logical operators these are used to check if two or more conditional statements
58:37
are true so we'll be discussing the and as well as the or logical operators there's also
a third
58:44
one called not but that works a little bit different and i'll explain why so let's create a
program to demonstrate
58:50
this let's create a program where we will ask somebody for the temperature outside
and then depending on the temperature if it falls within a
58:56
certain range we will print a message so we'll need to use and as well as or logical
operators
59:01
to do this so here i have a prompt temp for temperature equals input what is the
temperature outside
59:08
so i'm going to cast this as an integer data type so what if we want to check to see if
59:13
our temperature is within a certain range we can use the and logical operator to
59:19
check two or more conditional statements this is how we might do that let's check if
temp
59:26
is greater than or equal to zero zero degrees celsius and we can check another
condition too
59:32
if temp is less than or equal to 30 degrees celsius
59:38
so we need to add a colon at the end to finish this if statement and let me just fix
some of the spacing
59:44
okay so with the and logical operator in order for this entire condition to be true
59:51
both conditions must be true this needs to be true and this needs to be true so if our
59:57
temperature is equal to or above 0 as well as being less than or equal to 30 that
1:00:04
means our temperature falls within a certain range so let's print a message such as
the temperature
1:00:12
is good today print go
1:00:19
outside all right let's try it so what is the temperature outside let's
1:00:25
say it's 20 degrees celsius well the temperature is good today go outside
1:00:30
this condition is true 20 is greater than or equal to zero and temperature is less than
or equal to
1:00:37
30. let's try a different temperature like negative 20 degrees celsius so it's really
freaking cold outside right now
1:00:44
as you can see nothing happened because our first condition was false our
temperature is not greater
1:00:50
than or equal to zero but our temperature is less than or equal to 30 so
1:00:56
our second condition is true but our first condition was false and in order for this
entire statement
1:01:01
to be true since we're using the and logical operator both conditions must be true in
order
1:01:06
for this statement to be true now let's discuss the or logical operator we can check to
see if our temperature
1:01:13
is below or above a certain range so let's write else if temp is less than zero or
1:01:21
temp is greater than 30. so that means it's going to be either very cold outside or
very hot outside so
1:01:29
let's print a message the temperature
1:01:35
is bad today print stay
1:01:44
inside all right let's try this again
1:01:49
what is the temperature outside it is negative 20 degrees outside the temperature is
bad today stay inside
1:01:55
so with the orological operator as long as one of these conditions is true then the
entire statement is
1:02:02
true it doesn't matter if one of them is false as long as one of them is true then the
entire statement is true last but not least we have the
1:02:09
not logical operator this works a little bit different from the and as well as the or
logical operator
1:02:14
because we can check one or more conditional statements instead of two or more
conditional
1:02:20
statements so what the not logical operator will do is that it'll take a conditional
statement if it's true
1:02:26
it's going to flip it to false if it's normally false it's going to flip it to true so for this
example we
1:02:31
have another way in which we could write this although it's a little more complex so if
our statement
1:02:37
is true what we can do is that we can flip it to false using the not logical operator if it's
normally
1:02:43
false we can change it to true so what we'll do is surround one or more conditional
statements with a set of
1:02:48
parentheses and precede everything within the parentheses with the not logical
operator so if the statement is
1:02:55
normally true it's going to become false using the not logical operator if it's normally
false it's going to be
1:03:01
true then let's do the same with the else if statement so surround one or more
conditional
1:03:06
statements with the not logical operator so now these roles are going to be kind of
reversed so let's
1:03:12
try this again what is the temperature outside it is negative 20 degrees outside the
temperature is good today go outside
1:03:18
so what we should probably do is kind of reverse these rolls so i'm just going to swap
1:03:23
everything within these if statements with each other and now this should work the
same as it
1:03:29
did before but we just wrote it a little bit different what is the temperature outside it is
15
1:03:34
degrees outside 15 degrees celsius the temperature is good today go outside so with
the not logical operator you can
1:03:41
surround one or more conditional statements with the not logical operator and what
this will
1:03:46
do is flip it from being false to true or from true to false well everybody that's the
basics of
1:03:52
logical operators in python if you would like a copy of all this code i will post this in
the comments
1:03:58
down below but yeah that's the basics of logical operators in python hey what's going
on everybody
11.while loops
1:04:05
it's bro hope you're doing well and in this video i'm going to explain while loops in
python so sit back
1:04:10
relax and enjoy the show so while loops a while loop is a
1:04:17
statement that will execute its block of code as long as its condition remains true
1:04:23
later on in this video for this example we're going to create a program where we will
prompt a user to
1:04:28
enter their name if they attempt to skip that prompt then we will continually ask the
user to
1:04:33
enter their name and they can't continue on with the rest of the program until they do
so the key thing to keep in mind with while loops
1:04:39
is that you'll want some way to eventually escape the while loop if you don't have any
way to escape the while loop that's referred to as an
1:04:46
infinite loop so let's create an example of an infinite loop because it's fun so to create
a while loop type while and
1:04:52
then some sort of condition so for our condition let's say while one is equal to one so
this will always be true now what do
1:04:59
we want to do when we execute this block of code let's print something print help i'm
1:05:06
stuck in a loop all right so let's try this while one is equal to one print this
1:05:13
statement and we will just continue on and on within this while loop because we
have
1:05:19
no way to escape this while loop so now let's use this concept and write a program
where we will prompt a user to
1:05:26
type in their name if they don't type in anything then we will continue to prompt them
to type in at least something
1:05:32
so let's say name is equal to and then to just keep this blank we'll just set this to a
set of quotes
1:05:39
and then let's write a while loop here while name and let's check the length of name
if
1:05:45
the length of our name is equal to zero then we will ask the user to
1:05:52
type in their name name equals input enter your name
1:06:00
so then once we escape the while loop let's print something print hello plus
1:06:07
name whoops hello plus name alright so when i run this we are giving
1:06:12
the user a way to escape the while loop they have to type in something for their
name as long as the length of my name is
1:06:19
equal to zero keep on printing this prompt enter your name so i'm just going to hit
enter a bunch
1:06:25
of times enter your name no enter your name no enter your name no so we are stuck
within this loop until
1:06:32
we enter something for our name so this time let me type in a name then hit enter
1:06:37
so therefore our condition is now false the length of my name is now one two three
so
1:06:43
three is not equal to zero therefore our while statement our while loop is false and we
continue on with the rest of the
1:06:49
program there are a few variations in which you could write the same program so
let's write this a different way what we
1:06:55
could do is say name is equal to none while not name and this will pretty much do
1:07:03
the same thing it's just another way of writing this well everybody that's the basics of
while loops a while loop is a statement
1:07:10
that will execute its block of code as long as its condition remains true
1:07:15
for our example we asked the user to type in their name if they were to leave it
empty or blank we would keep on asking the user to type
1:07:22
in their name so if you would like a copy of this code i will post this in the comments
section down below
1:07:27
but yeah that's the basics of while loops in python hey what's going on everybody it's
bro
12.for loops
1:07:33
hope you're doing well and in this video i'm going to explain for loops in python and
at the end of this video we're going
1:07:38
to create a countdown timer so sit back relax and enjoy the show
1:07:45
all right guys and gals let's talk about for loops a for loop is a statement that will
execute its block of code a
1:07:52
limited amount of times it's similar but different from a while loop because a while
loop
1:07:57
could iterate an unlimited or infinite amount of times depending on the condition a for
loop
1:08:04
will only iterate a limited amount of times and before we start executing its block of
code we already know how
1:08:10
many times we're going to repeat this block of code so for this example let's create a
for loop that will simply count it to 10
1:08:16
and then we'll create a few more sophisticated examples so to create a for loop that
will count up to 10 this is what we'll type
1:08:22
four and then we need some sort of counter people usually either write index or they
shorten it to just i i for
1:08:29
index four i in and we can set a range range ten so we will execute this for loop ten
1:08:36
times and to finish this for loop just add a colon at the end so the next line or lines
will be
1:08:43
indented so that's the block of code for the for loop what do we want to do during
each iteration let's just print what
1:08:49
i is and let's take a look to see what would happen so we executed this for loop ten
times
1:08:55
except with computers computers always start with zero so when we said four i in
range ten
1:09:00
we're counting numbers zero through nine so it's as if this number is exclusive so
there's two ways we can
1:09:07
change this we could say range ten plus one or what i would do is say print i
1:09:12
plus one so this will give us numbers one through ten so that's a for loop it will
1:09:18
execute a limited amount of times this time let's count a range between two numbers
1:09:23
not necessarily 0 to 10 but something else so let's say 4i in range maybe the
numbers 50 through
1:09:31
100 so within my range function i will pass in two numbers the first number will be
the starting
1:09:37
point and the second number is the ending point this first number is inclusive and the
second number is exclusive
1:09:43
so let's print this print whatever i is so this will count 50 through 99 actually if you
wanted to
1:09:51
include this last number since it's exclusive what we could do is just add one to the
end so this would give
1:09:56
us 50 through 100 except that this will iterate 51 times since we're
1:10:02
counting 50. so you just have to pay attention to your range and the numbers that it's
counting now one thing that you can do
1:10:08
with the range function is that you can add a third argument and this will function as
the step how much you want to count up or down by
1:10:15
so this time let's count up by two so i'll add comma two we're passing in a
1:10:20
third argument this time so this program will now count up by two starting at fifty and
go all the way
1:10:27
to one hundred a benefit of for loops is that we can iterate through anything that is
considered
1:10:33
iterable this could include a string the letters in a string or any sort of collection so
this time
1:10:39
let's create a for loop that will iterate once through each letter in maybe a name
1:10:44
so for i in and let's type a name here type in whatever your first name and last name
1:10:49
is and then we will print whatever i is for i in whatever your name is print
1:10:56
each letter in the string so when i run this each letter within my name will be printed
to a new
1:11:03
line all right people for my last trick we're going to create a program where we will
simulate a countdown
1:11:09
starting at 10 and count down to zero and then maybe once we reach zero we can
print something such as
1:11:15
happy new year so to create this program we'll need an import so at the top we're
going to import the time module
1:11:21
because we'll be waiting one second after each iteration of this for loop so to create
this program let's say four
1:11:27
you could say i but you can really write anything here and to demonstrate that let's
say four seconds four seconds
1:11:35
in range so the starting point is going to be ten the ending point will be zero and we'll
1:11:41
add a step what do we want to count up or down by let's set this to negative one so
this will be a countdown
1:11:47
starting at ten and ending at zero so after each iteration let's print whatever i is or in
this
1:11:54
case seconds and then we can sleep have our thread sleep for a number of seconds
too so after we
1:12:01
print seconds type time dot sleep and pass in
1:12:06
how long you want to sleep for i would like to sleep for one second and then at the
end let's print happy
1:12:12
new year make sure this isn't within the for loop print happy new year and well
1:12:18
that's it let's test this so we'll begin at 10 and count down to zero and you can see
that my thread that is running this
1:12:25
program is sleeping for one second after each iteration of this for loop then once we
reach zero it will display
1:12:32
happy new year well everybody that's the basics of for loops a for loop is a statement
1:12:37
that will execute its block of code a limited amount of times it's different from a while
loop because
1:12:43
a while loop could execute an infinite or unlimited amount of times with for loops they
execute limited
1:12:50
amounts of times and before we enter the for loop we already know how many times
we're going to iterate this for loop
1:12:56
if you'd like a copy of all this code i will post this in the comments down below but
yeah that's how for loops work in
1:13:02
python hey what's going on everybody it's you bro hope you're doing well and in this
13.nested loops
1:13:07
video i'm going to explain nested loops in python so sit back relax and enjoy the
show
1:13:15
hey all you people let's talk about nested loops a nested loop is a general concept of
having
1:13:20
one loop inside of another loop and that's really it it doesn't matter if it's a for loop or
a while loop
1:13:26
so the inner loop will finish all of its iterations before we finish one iteration of the
1:13:31
outer loop and to best demonstrate this we're going to create a program where we
will draw a rectangle made out of a certain symbol
1:13:38
that we choose we need to set a width and a height and to best do this we'll need to
use a
1:13:43
nested loop we'll create a few prompts one for rows columns and a symbol that we
want to make a rectangle of
1:13:49
so let's begin with rows rows equals input how many
1:13:56
rows and we'll need to cast this as an integer data type because we're working with
numbers okay
1:14:04
let's do the same thing for columns so replace rows with columns for this
1:14:10
next line input how many columns
1:14:15
and we'll create a symbol variable symbol equals input and this we don't have to
1:14:21
cast enter a symbol to
1:14:26
use now it's time for the nested loops we're going to create an outer for loop as well
as an inner for loop
1:14:33
the outer for loop will be in charge of the rows the inner for loop will be in charge of
the columns
1:14:38
so let's begin by creating our outer loop so we'll write 4 i in range
1:14:46
rows we would like our outer for loop to iterate as many times as we have rows and
we're going to
1:14:52
create an inner for loop that will iterate as many times as we have columns so within
the block of code for the
1:14:58
outer for loop we're going to create another for loop this time it'll be four and a
common
1:15:04
convention for inner loops is to write j as an index because j comes after i i guess so
for j
1:15:12
in range columns
1:15:18
so our inner for loop will iterate for as many times as we have columns all we'll do
within
1:15:24
the inner for loop is print our symbol that we have except we'll have one issue using
a
1:15:31
print statement after we use the print statement we will enter a new line character
and move down
1:15:36
to the next line we can actually prevent that by adding comma and equals quotes so
after using a print
1:15:45
statement this will prevent our cursor from moving down to the next line so pay
attention to the indentation too
1:15:52
so there's kind of two levels of indentation we're currently within the inner for loop so
that's pretty much it for the
1:15:58
inner for loop now we're going to print a new line once we exit the inner for loop
1:16:03
and you can see that the indentation is on this line right here we're within the
1:16:08
outer for loop now and that's pretty much it so let's try this program how many rows
let's say five rows how
1:16:16
many columns perhaps six what symbol do we want to use let's say the dollar sign
1:16:22
and let's try it there we go there's our rectangle so we have let's see six columns one
1:16:30
two three four five six and five rows one two three four five in summary
1:16:36
a nested loop is really just this concept of having one loop inside of another loop it
doesn't matter if it's a while loop or a
1:16:43
for a loop it's really situational the inner loop will finish all of its iterations before
finishing
1:16:49
one iteration of the outer loop so what we did to demonstrate that is that we had the
outer loop in charge the rows and an
1:16:56
inner loop in charge of the columns for our rectangle program so if you would like a
copy of this program i will post this in the comment
1:17:03
section down below but yeah that's the basics of nested loops in python
14.break continue pass
1:17:09
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to explain
1:17:14
loop control statements in python so sit back relax and enjoy the show
1:17:21
all right ladies and gentlemen let's talk about loop control statements these are used
to change a loop's
1:17:26
execution from its normal sequence and there are three we're going to discuss break
continue
1:17:32
and pass let's begin with break break is used to terminate the loop entirely now
here's a good example of where a
1:17:39
break would be useful let's say while true we're going to continually ask
1:17:44
for somebody's name if they don't type in anything then we'll continue the while loop
over and over again
1:17:49
so let's say name equals input
1:17:55
enter your name now if name does not equal
1:18:02
a set of quotes that means they don't type in anything then we will break and break
out of this
1:18:08
while loop and let's test it enter your name nah i don't think so i'm too lazy to enter
my name
1:18:14
all right i can see that i'm not going to escape this while loop so i'm going to type in
my name because i give up
1:18:19
and hit enter therefore i will break and exit out of this while loop so a break is used to
terminate the loop
1:18:26
entirely when it's encountered next we have continue continue skips to the next
iteration of the loop
1:18:32
now let's say we have a phone number phone underscore number and i will set the
1:18:38
sequel to let's say a random phone number including dashes 123-456-7890
1:18:45
what i would like to do is to display this number within the console window without
these dashes
1:18:51
this is how to do that using a continue control statement for i in phone number
1:19:00
so for each character within our string of phone number what we'll do is check to see
if we
1:19:06
encounter a dash character if i i is our index is
1:19:11
equal to a dash then we want to continue and skip over this iteration of
1:19:18
the loop so continue then i would like to print
1:19:26
whatever our indexes whatever character we're working on so if i were to run this as
it is it's
1:19:32
going to print each digit on a new line so with print statements they will add a new
line character to
1:19:39
the end of your string and you can actually change that by adding to the end comma
1:19:44
end equals and then a set of quotes so this will print my phone number without any
dashes
1:19:50
one two three four five six seven eight nine zero so that's a use of the continue
control statement it skips
1:19:56
to the next iteration of the loop last but not least we have pass pass does nothing
1:20:01
it acts as a placeholder let's say that i would like to print the numbers 1 through 20
using a for loop for i
1:20:09
in range 1 through 21 and remember that the second digit is
1:20:14
exclusive so this will iterate once through the digits of 1 through 20. let's pretend that
i'm
1:20:20
highly superstitious and i don't want to print the number 13 because 13 is considered
an unlucky number
1:20:26
if i is equal to 13 what we'll do is pass and pass acts as a
1:20:31
placeholder and it won't do anything else print
1:20:37
i that's one i there we go this will print the numbers 1 through 20
1:20:42
and skip 13 because we used the past control statement which does nothing it acts
as a placeholder
1:20:49
so everybody those are loop control statements they change a loop's execution from
its normal
1:20:54
sequence we have break continue and pass so if you would like a copy of all this
code
1:21:00
i will post all of this in the comments section down below but yeah those are a few
loop control statements in python what's
15.lists
1:21:07
going on everybody it's bro hope you're doing well and in this video i'm going to
explain lists in python so sit back relax
1:21:15
and enjoy the show all right ladies and gentlemen let's get
1:21:21
down to business i got to explain lists to you all now a list is used to store
1:21:26
multiple items within a single variable for example let's say we have a variable
1:21:32
called food and i will store a string value of pizza because i like pizza
1:21:37
what we could do is that we can store multiple items within this variable by turning
this variable into a
1:21:44
list and in order to do so we're going to surround all of the values that we would like
to add
1:21:50
to our list with a set of square brackets and boom there you have it we now have a
1:21:56
list called food it is no longer a variable so what we could do is add
1:22:01
multiple items to this list of food so let's add some other food items that we might like
1:22:07
let's see i also like maybe some hamburgers and a hot dog
1:22:15
and maybe some spaghetti i think i smelled spaghetti right okay so we now have a
list called food
1:22:22
now what happens if we attempt to print this list of food so print food so this will
1:22:30
print all of the elements found in this list each item in a list is referred to as an
1:22:37
element if we need to access a certain element of this list we have to list the index so
1:22:44
next to our list we'll add a set of square brackets again and we need to list the
numbered index
1:22:49
of the element that we're trying to access now computers they always start with zero
so the first
1:22:55
element in our list would be element number zero the next one would be one two
and three
1:23:02
so if we need to access element zero which is in the first position we would
1:23:07
say food square brackets and then within here list the index so the first position
1:23:13
is zero and this will now print the first element of our list which is pizza let's attempt
to access
1:23:19
some of these other elements so the element at index one would be you guessed it
hamburger two
1:23:28
would be hot dog and three would be spaghetti now what happens if we attempt to
access
1:23:34
the element at index four well currently that's out of range because we did not assign
1:23:40
a value so we're experiencing an error an index out of range error but if i were to add
1:23:46
another element let's say pudding alright well then
1:23:51
the element at index 4 would be putting then one important concept with lists is that
1:23:57
you can always update and change the elements found within a list later on in the
program after you
1:24:03
declare one so let's say we would like to immediately change one of these elements
let's say
1:24:09
food at index zero i want to replace with maybe sushi because sushi
1:24:15
is great so if i were to print the element at index 0 this would no longer be pizza
because
1:24:22
we updated element 0 to be sushi then and this prints sushi now if
1:24:28
you need to display all of the elements found within a list you can easily do so with a
standard for
1:24:34
loop what we'll do is say for x in the name of our list which is food
1:24:41
print x and let me turn this line into a comment all right so this for loop will print
1:24:49
all of the elements found within our list of food and the results are sushi
1:24:55
hamburger hot dog spaghetti and pudding all right people for this next portion i'm
going to demonstrate a few useful
1:25:01
functions of lists and to access some of these functions type the name of your list dot
and there's a bunch to choose from
1:25:08
here let's begin with event we can add an element to this list let's say at the end i
would like to add
1:25:14
ice cream so then if i were to display my list we now have ice cream at the end
1:25:20
because we appended this value to my list of food so let's go over a few others
1:25:26
so we can also remove a value food dot remove let's say i would like to
1:25:32
remove hotdog so i'm going to type that within my function and this will remove
hotdog
1:25:39
and that is no longer here next we can pop food.pop
1:25:46
pop will remove the last element so this will remove
1:25:52
pudding we can insert a value at a given index food dot insert we need to list an
index
1:26:01
let's say zero that would be the first position technically that's where pizza is
currently
1:26:06
and i would like to add cake and at index 0 we now have cake
1:26:14
so next up we have sort food dot sort this will sort a list
1:26:20
alphabetically so in the first position we have hamburger hot dog pudding spaghetti
1:26:26
then sushi we can also clear a list food dot clear and this will remove
1:26:34
all of the elements of a list so this will not print anything because we cleared the list
1:26:40
well everyone in conclusion a list is really just a variable that can store
1:26:45
multiple values think of it that way so if you would like a copy of all the code that we
have written
1:26:51
here today i will post all of this in the comments down below but yeah that's how lists
work in python
16.2D lists
1:26:58
how's it going everybody it's bro hope you're doing well and in this video i'm going to
explain 2d lists in python so sit back
1:27:05
relax and enjoy the show i have a super quick video for you all
1:27:12
today on 2d lists also referred to as multi-dimensional lists
1:27:17
all it is is a list of separate lists so let's begin by creating a few separate lists let's say
we have a list
1:27:24
called drinks and let's assign a few values let's say we have coffee as well
1:27:32
as soda and maybe some tea all right so we currently have one list
1:27:37
let's make maybe two more let's make a list called dinner and maybe in here we'll
place
1:27:44
pizza then hamburger
1:27:50
and hot dog okay then let's create one more for this example let's say dessert
1:27:59
and maybe we'll add two items they all don't need the same amount of items or
elements so we have cake
1:28:06
and ice cream all right so we have three lists one
1:28:12
called drinks dinner and dessert so what we can do is add all of these lists to one list
1:28:19
and let's create a list called food food equals and for each of these elements we're
going to place
1:28:25
our lists so we have drinks dinner and dessert check this out what if i
1:28:32
were to print my 2d list of food so what ends up happening is that this
1:28:38
will print all of the elements found within each individual list and they're all grouped
together
1:28:44
so this first portion is my drinks list which contains coffee soda tea this next
1:28:51
element is my dinner list which contains pizza hamburger hot dog and the last
element
1:28:57
dessert contains cake and ice cream if i need to access just one of these lists i will
add an
1:29:04
index after my food 2d list and set an index number so index zero is
1:29:11
referring to my first list of drinks and this will display all of the elements found within
my first
1:29:17
list and if i need just one of these elements i will add a second set of square brackets
and list
1:29:24
the index of the item that i'm trying to access so this very first item coffee would be
1:29:31
index zero and then index zero again so we need two sets of square brackets and if i
1:29:36
were to print this this will display coffee so if i go down the list the next element
would be soda
1:29:44
and then t if i were to change the index within this first set of square brackets well
1:29:50
now we're working with a different list this time we're working with our dinner list so
the element at index 1
1:29:58
index 2 that would be our dinner list and this will display the element of hot
1:30:03
dog if i change this to the next list which would be dessert well we're going to receive
an index out
1:30:10
of range error because we only have two values within this list cake and ice cream
there is no element
1:30:18
at index 2 because we only added two elements to this list of dessert all right
1:30:24
everybody so that is 2d lists also referred to as multi-dimensional lists
1:30:30
it's a list of separate lists if you need to access one of the elements within your 2d list
1:30:36
you need two sets of square brackets so if you would like a copy of this code i will
post this in the comments down
1:30:43
below but yeah that's how 2d lists work in python hey what's going on people
17.tuples
1:30:48
it's you bro hope you're doing well and in this video i'm going to explain tuples in
python so sit back relax and enjoy the show
1:30:58
so tuples they are collections which are ordered and unchangeable they're very
similar to
1:31:03
lists but they're ordered and we can't change them they're useful for grouping
together related data
1:31:09
let's say we would like to create some sort of student record so we can create a tuple
to take care of
1:31:14
that for us so let's say we would like to create a tuple called student the process of
creating a tuple is very
1:31:20
similar to lists but instead of using a set of square brackets to place all of the values
1:31:26
within we're going to use a set of parentheses and now we can add a bunch of
values related to this collection of student
1:31:33
so let's add some student information let's say that this student's name is bro let's
give him an age as well as a
1:31:40
gender all right so there you have it we now have a tuple called student and let's take
a look at some of the
1:31:46
functions related to tuples and you can access them by typing student dot and then
there are a few here not as
1:31:53
many as lists we have count and we have index so we can find the count of how
many times a value appears
1:32:00
let's count how many times the value of bro appears so we'll need to put this within a
print
1:32:06
statement so we can actually see it so bro appears
1:32:12
one time within this tuple there is also an index method as well so type the name of
your tuple dot
1:32:19
and we will use the index method we can find the index of a certain value
1:32:24
let's say we would like to find the index of mail and then i will just print this to the
console window
1:32:31
so the index of the value mail would be at two zero one
1:32:37
two that's it for the methods available to tuples a few tricks that you can do with
tuples
1:32:42
is that you can display all of the contents within a tuple using a for loop
1:32:48
let's say 4x in student we will print x so
1:32:55
this will iterate once through all the values found within our tuple of student and we
can also check to see
1:33:02
if a certain value exists within our tuple using an if statement let's say if
1:33:07
bro in student if this is true then what do we want to
1:33:13
do let's say print bro is here all right so since bro is within here
1:33:21
this will be true and this will print our statement of bro is here all right
1:33:27
everybody like i said this is a super quick video on tuples they're very similar to lists
except
1:33:34
they are ordered and unchangeable if you'd like a copy of all this code i will post all
of this in the comments
1:33:39
down below and don't forget to smash that like button drop a comment down below
and subscribe if you'd like to become a
1:33:46
fellow bro hey what's going on everybody it's you bro hope you're doing well
18.sets
1:33:51
and in this video i'm going to explain what a set is in python so sit back relax and
enjoy the show
1:34:00
a set is a collection which is unordered as well as being unindexed they do not allow
any
1:34:06
duplicate values for example let's create a set of silverware so in order to create a
set we need a
1:34:12
name for this set let's call it utensils equals and in order to create a set we need to
1:34:19
surround all values with a set of curly braces now let's add a few utensils
1:34:24
to the set called utensils let's add a fork a spoon and maybe a knife and that
1:34:32
should be good and let's display all of the values for x in utensils
1:34:41
print x now with sets they're different from lists because
1:34:46
they're unordered as well as being unindexed if i were to print all of the elements
1:34:51
in utensils they might not necessarily be in the same order in which we placed them
1:34:56
for example when we printed all of the elements found within utensils first we had a
knife a fork and then a
1:35:03
spoon if i were to run this again they might be in a different order kind of like that
1:35:08
so a set is actually faster than a list if you need to check to see if something
1:35:14
is within a set compared to a list and they do not allow any duplicate values
1:35:19
let me add a bunch of knives and let's see what happens so i'll have a total of three
knives but
1:35:26
when we print all of the elements of the set only one knife appears so here's a few
useful methods of sets
1:35:34
the first method is that we can add an item to our set so type in the name of the set
in this
1:35:40
case it's utensils dot and here's a bunch of methods that you can use i am looking for
add and we can add an
1:35:47
element to the set i would like to add a napkin and then if we were to print the set we
1:35:54
do have a napkin within the set we can also remove an element utensils dot remove
1:36:02
and i would like to remove the fork
1:36:08
and now our fork is gone we can also clear utensils dot clear
1:36:16
and all the elements within our set should be gone so let me turn these lines into a
1:36:22
comment all right for this next example we'll need a second set let's create a second
set called dishes
1:36:30
and then we need a set of curly braces and let's add a bowl
1:36:36
a plate and a cup our next method is that we're going to
1:36:43
add one set to another by using the update method let's say we would like to add our
1:36:48
dishes set to our utensil set so let's type utensils dot and use the
1:36:54
update method and within this method we will pass in the set that we would like to
add
1:36:59
so utensils update dishes this will add all of the elements found within dishes
1:37:04
to our utensil set and you can see that when we print our utensils we have a bowl a
plate and a cup in here
1:37:12
as well and then if i were to switch these around let's say dishes update utensils and
then i will
1:37:20
print everything found within dishes now there's a fork a spoon and a knife in here as
well we can also
1:37:28
join two sets together and create a new set entirely so let's create a set called dinner
1:37:34
table like we're setting up a dinner table and we need a fork a spoon a knife a bowl a
plate and a cup
1:37:40
so we have a dinner table set we're going to set the sequel to either
1:37:46
utensils dot union dishes or we could do dishes
1:37:54
union utensils either way would work and let's display all the elements found within
dinner table
1:38:00
and we should now have elements from both sets utensils and dishes as well there
are also some
1:38:07
methods in which we can compare the similarities as well as the differences between
1:38:12
the elements found within two sets let's say i would like to check to see what utensils
has that dishes doesn't
1:38:19
and for this example i think i'll add a knife to my dishes set just so that they have
1:38:24
at least one thing in common so i would like to see what utensils has that dishes
doesn't so we can do so
1:38:31
using the difference method and i will print the results with a print statement so i'm
going to compare utensils against
1:38:40
dishes utensils dishes and this will print what utensils
1:38:48
has that dishes doesn't which is a fork as well as a spoon
1:38:54
and we can reverse the rolls too we could say dishes difference utensils what do
dishes have that
1:39:01
utensils doesn't and dishes has a cup a bowl and a plate
1:39:06
they both have knives so that's why knife isn't appearing we can also check to see if
there's
1:39:12
anything that they have in common using the intersection method so i'll turn this line
into a comment
1:39:20
we'll print utensils dot intersection
1:39:27
dishes and this will return whatever element that they have in common which is a
knife all right everybody in
1:39:35
conclusion a set is a collection which is unordered as well as being unindexed they
do not allow any
1:39:42
duplicate values and you can do things like compare two sets see if there's anything
that they
1:39:47
have in common any differences and we can add or remove elements to a set if
you'd like a copy of all this
1:39:53
code i will post all of this in the comments down below don't be afraid to smash that
like button drop a comment down below
1:40:00
and subscribe if you'd like to become a fellow bro hey what's going on everybody it's
you bro hope you're doing well and
19.dictionaries
1:40:06
in this video i'm going to explain how dictionaries work in python so sit back relax
and enjoy the show
1:40:15
all right let's get down to business a dictionary is a changeable unordered collection
of
1:40:22
unique key value pairs they're fast because they use hashing
1:40:27
and they allow us to access a value quickly now to create a dictionary it's very
1:40:32
similar to creating a set based on the last video except we're going to store unique
key value pairs let's create a
1:40:39
dictionary of countries and their capitals we can store those as key value pairs so
let's call this
1:40:46
dictionary capitals capitals equals then we need a set of curly braces much like what
we
1:40:53
use with sets now we need a key and a value let's say we have the usa as
1:40:59
a key and the capital of the usa would be washington dc so in order to associate a
value with
1:41:06
the key we'll follow the key with a colon and then add some value and the data type
really doesn't matter but
1:41:12
in this case we're just using strings so the capital of the usa would be washington dc
1:41:19
and to add another key value pair we'll separate each with a comma and let's add a
few others for good
1:41:25
measure so let's say we have india next as a key and the value would be
1:41:31
new delhi and we'll add two more let's say we have china
1:41:38
and the capital of china is beijing and lastly let's add russia
1:41:46
and the capital of russia is moscow and there you have it ladies and
1:41:51
gentlemen we now have a dictionary called capitals that has unique key value pairs
so
1:41:57
remember with dictionaries they are unordered in order to access one of these
values instead of using a numbered index we're
1:42:04
going to use the associated key with that value here's an example let's say i would
like to print the capital
1:42:10
of russia so i need to use this key of russia so in order to do this type in the name
1:42:16
of the dictionary followed by a set of straight brackets and then instead of an index
number like 0 or 1 2 3
1:42:25
i'm going to use the key so i would like to print the value that's stored or associated
1:42:31
with the key of russia so when i run this this will print
1:42:36
that value which is moscow so this isn't always safe let's say we have a key that
doesn't
1:42:41
exist like germany germany is not currently within my dictionary so if i were to run
this
1:42:47
well then my program is going to encounter an error and this will interrupt the normal
flow
1:42:52
of my program a much safer way to access a key to check to see if it's there or not is
1:42:59
to use the get method of dictionaries so i'm going to turn this line into a comment and
let's try
1:43:04
this again but this time we'll use the get method so type in the name of the dictionary
dot get
1:43:12
and list the key that you want to use so i would like to see if germany
1:43:18
is within my dictionary currently there isn't so this will return none and we will not
encounter an error
1:43:25
so this is a much safer way of checking to see if there is a key within your dictionary
1:43:30
here's a few other useful methods there is a method to print only the keys so we type
in the name of the dictionary
1:43:38
capitals and here's a listing of all of the methods that we have access to i'm looking
for the keys method and this
1:43:45
will print only the keys and not the values we can also print just the values and not
the keys so type
1:43:53
in the name of the dictionary again capitals dot values
1:43:59
and this will print only the values or you could print everything both the keys and the
values and you do
1:44:06
so by using the items method capitals dot items
1:44:15
and this will print your entire dictionary one other way in which we can display all of
the key value pairs in a
1:44:21
dictionary is to use a for loop so at the end let's create a for loop for
1:44:26
key comma value in capitals dot items
1:44:33
this is going to iterate once for each key value pair in my dictionary for each key
value in
1:44:40
capitals dot items print key comma value
1:44:47
and this will print my entire dictionary a feature of dictionaries is that they are
mutable that means we
1:44:54
can change them or alter them after the program is already running so one way in
which we can do that
1:44:59
is to use the update method of dictionaries so let's add germany as a key and give
1:45:06
it a value of berlin so type in the name of the dictionary capitals
1:45:11
and we'll use the update method and within the parentheses of this method we'll add
a set of curly braces and now
1:45:18
we can add a new key value pair so let's add germany
1:45:23
and a value of berlin so if i were to run this and display
1:45:28
all of the key value pairs found within my dictionary we now have germany as a key
and berlin as its value
1:45:35
not only can you use the update method to add a new key value pair but you can
update an
1:45:41
existing one let's say we would like to change the capital of usa so capitals
1:45:47
dot update parentheses curly braces list the key usa colon
1:45:54
and then we can give the usa a new value let's pretend that the united states
government has decided to relocate
1:46:01
their capital to las vegas so this will now update our key of usa
1:46:08
with a new value and if i were to print all of the key value pairs found within
1:46:14
my dictionary of capitals the usa has a new value of las vegas so the last two
methods i'm
1:46:21
going to cover are both pop and clear you can use the pop method to remove a key
value pair so capitals
1:46:29
dot pop parentheses and list the key of the key value pair that you would
1:46:34
like to remove so let's say we would like to remove china so we'll list that key and
then when we
1:46:39
use the pop method that will remove this key value pair from my dictionary
1:46:45
and then of course we have clear as well which will just remove everything capitals
dot clear
1:46:52
and this will clear my dictionary all right everybody so in conclusion a dictionary is a
1:46:58
changeable unordered collection of unique key value pairs they're fast because they
use hashing
1:47:05
and they allow us to access a value quickly well that's the basics of dictionaries
1:47:10
in python if you would like a copy of all this code i will post all of this in the comments
down below
1:47:16
but yeah that's how dictionaries work in python hey what's going on everybody it's
bro
20.indexing
1:47:22
hope you're doing well and in this video i'm going to teach you guys all about the
index operator in python so sit back relax
1:47:29
and enjoy the show all right people let's begin i'm going to be explaining the index
operator
1:47:36
which is represented by a set of square brackets now they give access
1:47:41
to a sequences elements they include but are not limited to strings lists and tuples
for this
1:47:48
example we're going to be working with strings because strings are easy to work with
let's say we have a name and name equals
1:47:55
whatever your first and last name is we can use the index operator to access
1:48:00
an element of the sequence our string so let's check to see if the first
1:48:05
letter in our name is lowercase and i should probably change that to lowercase for
this
1:48:11
example so within an if statement if name and we can add a set
1:48:16
of square brackets after our sequence which is a string and we can check
1:48:22
a given element of our sequence let's check to see if the first letter is lowercase
1:48:29
now with computers they always start at zero if you need to access the first element
in a sequence that would be index zero
1:48:36
and then the next sequence well the next element in the sequence would be one
then two then you followed that pattern
1:48:42
so i would like to check to see if name at index zero that would be the first character
1:48:48
is lowercase so there is a method to check that dot is lower this will return
1:48:55
true or false if the first letter is lowercase then our if statement is going to be
1:49:00
resolved to true and what i could do is that i can
1:49:06
perhaps change it to uppercase then so let's reassign name to equal name dot
1:49:14
and there is a capitalize function here and then let's print our name print
1:49:21
name so when we run this the first letter in my name is now
1:49:27
capitalized so if you need to access an element within a sequence a string or
1:49:32
list or a tuple you would add a set of square brackets afterwards and then
1:49:38
you list an integer of the element that you're trying to access so here's a few other
examples
1:49:43
i'm going to turn this as well as this into a comment and let's say we would like to
create
1:49:49
some substrings and we can do so using our index operator so i would like to
1:49:54
create a substring from the first part of my name so what i'll do is create a new
variable
1:50:00
first name equals name index operator
1:50:05
so you can specify a range for an index operator so you would set the starting
position
1:50:12
on the left hand side followed by a colon and then your ending position so i would
like the substring to begin
1:50:18
at index 0 and end at 3 so that would be 0 colon then three all right and then i would
1:50:25
like to make this all uppercase for some reason and then let's print my first name
1:50:33
so with the index operator we specified a range we would like to access elements 0
1:50:39
through 3. turn them up your case and assign them to a new variable of first name
here's a shortcut
1:50:45
that you might find useful if the index for your range begins with zero you can
actually delete that you
1:50:50
don't even necessarily need that zero now we just have a colon three and this would
work the same as it did before
1:50:57
so that's a useful shortcut and we're going to bring it up later if we need to access
the last element in a sequence
1:51:03
this time let's create a substring of last name so last
1:51:09
name equals name index operator this time i would like to access the
1:51:16
element at index let's see 0 1 2 3 4. all right so i'm going to say
1:51:24
for colon and we're not really sure where our name our last name is going to
1:51:29
end so what you could do is just say colon and then leave the next index blank so
that will be
1:51:36
the index beginning at four and everything afterwards and let's say two lower to
make this all
1:51:42
lowercase for some reason so let's print our last name print
1:51:48
last name so this should take everything starting at index 4
1:51:56
and after that and turn it into its own substring of last name now you can access the
last element in a
1:52:03
sequence by using what's called negative indexing so let's add
1:52:08
a exclamation point to the end of our name so let's say we have a new variable
called last
1:52:16
character and i would like to store the last character in my name to this
1:52:23
new variable this new substring so to access the last element in a sequence within
the index
1:52:31
operator we can use negative indexing so the last element would be negative one all
right and then let's
1:52:38
print whatever our last character is so this should print an exclamation point
1:52:45
yep which did so then if this was negative 2 that would be the second to last element
1:52:51
in a sequence so now that would be e because e is in the second to last
1:52:56
element in my sequence all right everybody so that's the basics of the index operator
1:53:02
you can add a set of square brackets after a string a list or a tuple and then list an
1:53:08
integer or a range of the elements that you're trying to access so if you would like a
copy of
1:53:13
all this code i will post everything in the comments down below don't be afraid to help
me out and smash that like button drop a comment down
1:53:20
below and subscribe if you'd like to become a fellow bro hey what's going on
everybody it's you
21.functions
1:53:26
bro hope you're doing well and in this video i'm going to explain how functions work
in python so
1:53:31
sit back relax and enjoy the show if you find this video helpful please
1:53:37
remember to like comment and subscribe your support will help keep this channel
running
1:53:43
okay let's do this thing a function is a block of code which is executed
1:53:49
only when it is called this process is also known as invoking a function
1:53:54
with programming we try not to repeat code if we don't have to that's why functions
are useful because
1:53:59
our function can perform some specific task for us some block of code for us
whenever it is called so we only need to
1:54:06
write that block of code once and if we need to repeat it we just call this function one
more time
1:54:12
so let's define and create a function we'll create a function called hello and in order to
define a function we'll
1:54:17
type def and then a unique function name so let's call this function hello
1:54:23
and with functions they always end with a set of parentheses and lastly to finish
defining this
1:54:29
function we'll add a colon at the end so if you go down to the next line take notice
that my cursor is now
1:54:36
indented any code underneath a function that is indented belongs to that specific
function and
1:54:42
will only execute whenever that function is called so for now we need to type in
something
1:54:48
if you don't know what you want your function to do you can always type pass for
now but let's print something print the word
1:54:56
hello and in order to call this function all we need to do within our program is
1:55:02
type the name of the function which is hello and then add a set of parentheses
because functions always end with a set
1:55:09
of parentheses so what this will do is execute my function once and perform this
block of code
1:55:15
whenever it is called if i were to remove this well our program currently isn't going to
do
1:55:21
anything because functions only execute their block of code only when it is called so
this time
1:55:27
let's print the word hello three times so i will call this function three separate times
hello hello hello
1:55:35
so when i run this our hello function will be called three separate times now with
functions
1:55:41
they are not limited to just one line this is an entire block of code so maybe we can
do something else too
1:55:48
let's print have a nice day now if i were to run this my hello
1:55:55
function is going to execute this entire block of code three separate times hello
1:56:00
have a nice day now an important feature of functions is that we can send our
function some
1:56:06
information and our function can do something with that information that it receives
let's say this time i would like to
1:56:11
print hello plus somebody's name currently name is an unresolved reference what
we can do
1:56:17
when we call this function is send our function some information this can be a value
a variable a
1:56:23
collection all sorts of different things so if you need to send your function some
information within the parentheses when you call
1:56:30
that function just list the data that you want to send your function so let's say i would
like to send my
1:56:36
function a string value of bro whatever your first name is when you send information
1:56:41
to a function these are called arguments they are the information that you're
1:56:46
sending to a function and when you define that function you need a matching
1:56:52
set of what is known as parameters so our hello function is going to
1:56:57
receive one argument a string value we need a matching number of parameters
1:57:03
currently this isn't going to work you can see here that hello takes zero positional
arguments but one
1:57:09
was given our function needs a matching number of parameters so think of it this
way when we receive
1:57:17
this value we're going to give a nickname to this value so when we receive the string
value what
1:57:24
do we want to call it well let's call it name so in order to add parameters
1:57:29
to your function just list the parameters within the parentheses of that function and
1:57:35
well this should work now we have a matching set of arguments and parameters
when we call the hello
1:57:42
function this time we are sending one argument over and when our hello function
1:57:47
receives this argument we're going to give it a temporary nickname of name or you
can call this whatever you
1:57:53
want to and then we can use this value for whatever we want within our function
1:57:58
now if we were to run this this will print hello bro have a nice day now i can send
maybe a
1:58:04
different value let's say hello and then i will send a string value of dude
1:58:11
hello dude have a nice day so this isn't limited to just values we can send variables
to
1:58:18
let's say we have a variable called name equals
1:58:23
bro and this time i'm going to send this variable over hello and then send
1:58:30
name so these don't need to be the exact same let's say this is
1:58:37
my name and i'm going to send my name over and i will temporarily give
1:58:45
this value a nickname of just name and then i can use this variable for
1:58:50
something hello bro have a nice day now with arguments you can send more than
one value over let's say this
1:58:58
time i would like to send two values over so let's send pro maybe for a first name and
code for a
1:59:05
second name but you can use your first name and last name so this isn't going to
work because
1:59:10
we do not have a matching set of arguments and parameters we're sending
1:59:16
two arguments over but our function is only designed to accept one argument
because we have
1:59:23
one parameter so let's rename name as first name and we need
1:59:30
two parameters this time so what are we going to call this value when we receive it
let's call
1:59:35
this last name so this time we are going to receive two
1:59:40
values they will be nicknamed first name and last name so this time let's print hello
first name
1:59:48
maybe i'll add a space plus last name and this is now valid
1:59:56
hello bro code have a nice day with arguments you can mix and match the data
types
2:00:02
that you're sending as arguments so currently we're sending two string values as
arguments this time
2:00:08
let's send an integer value let's say we would like to send an edge over to our hello
function
2:00:15
so i'll just send maybe the value of 21. so currently this isn't going to work because
the hello function takes two
2:00:22
positional arguments but three were given when we call this hello function we're
sending
2:00:27
this argument over but our function doesn't know what to do with this argument so
we need a matching parameter for this
2:00:34
value that it receives so let's call this value maybe edge as a temporary nickname
2:00:40
and then we can do something with this value when we receive it so let's print an
additional message
2:00:47
perhaps u r plus age plus years
2:00:54
old so if you need to display an integer or number along with the string
2:00:59
you do need to convert it to a string so let's just cast our edge as a string when we
display this
2:01:06
so this should work now we now have a matching number of arguments and
parameters so this will
2:01:13
display hello bro code you are 21 years old have a nice day so ladies and gentlemen
in conclusion a
2:01:20
function is a block of code which is executed only when it is called if i were to remove
this function call
2:01:27
well then our program is just going to completely ignore this function and this block of
code would not be executed
2:01:34
when we call this function we can pass information known as arguments but we
need a
2:01:40
matching number of parameters set up to receive these arguments there are a few
advanced
2:01:46
features of parameters but i'll hopefully get to that in future videos we'll just stick with
the basics
2:01:51
for now of functions so that's the basics of functions if you would like a copy of this
code
2:01:57
i will post this in the comments down below but yeah that is how functions work in
python hey what's going on
22.return statement
2:02:04
everybody it's you bro hope you're doing well and in this video i'm going to explain
the return statement in python so
2:02:10
sit back relax and enjoy the show
2:02:15
all right people let's begin the return statement is used within functions to send
python values or objects back to
2:02:23
the caller these values or objects are known as the function's return value so do you
know how with
2:02:29
functions we can pass arguments to a function and then our function can do
something with those arguments
2:02:34
well not only that but our function can pass some value or object back to the caller so
what we'll
2:02:41
do for this example is create a function that will simply multiply two numbers together
2:02:46
and return the result back to the caller so let's define a function called multiply and
we'll set
2:02:52
up two parameters for this function let's say number one and number two
2:02:58
and what we'll do is say result equals number one times number two
2:03:05
and then at the end we can return something what do we want to return let's return
whatever the result is now
2:03:11
when we call this function we will pass in two values as arguments let's say we
would like to multiply
2:03:17
six times eight so when we run this well we can't actually see anything
2:03:22
when we return the result after multiplying numbers one and two it's going to return
the
2:03:27
result back to the caller in order to see the value that is returned back to the caller
2:03:32
one way in which we can do this is to print the value that is returned within a
2:03:38
print statement so what we'll do is print the value that is returned after calling the
multiply
2:03:44
function so 6 times 8 is really great 6 times 8 is 48 now one other thing that we can
do is
2:03:51
store the returned value within a variable so let's say x equals multiply
2:03:58
6 times 8 and then we will print whatever x is so the result is still the same of 48
2:04:05
now with the return value there is another way in which we could write this that uses
less lines of code so we'll delete this
2:04:12
first line and instead of returning a variable called result what we'll do is just return
2:04:18
number one times number two and this would work the same as it does before but
it's written in less lines of code
2:04:25
so that's another way in which you could write this all right everybody so that is the
return statement functions can send
2:04:31
python values or objects back to the caller these values or objects are known as the
2:04:36
functions return value you can either print this value directly to the console window
2:04:42
or you can store it within a variable or some other location and well ladies and
gentlemen that is
2:04:48
how the return statement works in python what's going on everybody it's bro hope
23.keyword arguments
2:04:53
you're doing well and in this video i'm going to explain keyword arguments in python
so sit back relax and enjoy the show
2:05:02
all right everybody let's discuss keyword arguments these are arguments that are
preceded by
2:05:09
an identifier when we pass them to a function the order of the arguments doesn't
2:05:14
matter unlike positional arguments which do matter and we've been working with
positional arguments already
2:05:20
and python knows the names of the arguments that our function receives when we
use keyword arguments here's an
2:05:26
example of us using positional arguments and then i'll demonstrate the benefits of
keyword arguments let's say we have a
2:05:32
function called hello that accepts three arguments a first name a middle name and a
last name
2:05:38
and the output will be hello first name middle name last name so if i were to run this
this will
2:05:43
display hello bro dude code so we're using positional arguments
2:05:48
the order of the arguments does matter what do you think's gonna happen if i were to
replace some of these values
2:05:54
let's say i will swap the first argument with the third argument so let's try it again
2:06:01
hello code dude bro so obviously the order of the arguments matters and now if we
were to use keyword
2:06:07
arguments then the order of the arguments doesn't matter but with each argument
we need to precede each argument with
2:06:15
a unique identifier and that identifier is the name of the parameter we want to
2:06:20
associate each argument with so for my intended last name i will use an identifier of
2:06:26
last to match this parameter so dude will be middle middle equals dude
2:06:32
and bro will be first even though they're not in the correct order so let's try this again
2:06:37
and my output is hello bro dude code so that's all about keyword arguments
2:06:43
they are arguments preceded with an identifier when we pass them to a function the
order of the arguments doesn't
2:06:49
matter unlike positional arguments which do matter and then python knows the
names of the arguments
2:06:54
that our function receives when we use keyword arguments so that's it for keyword
arguments if
2:07:00
you would like a copy of this code i will post this in the comment section down below
but yeah that's how keyword
2:07:06
arguments work in python hey what's going on everybody it's you bro hope you're
doing
24.nested function calls 🖇️
2:07:12
well and in this video i'm going to explain nested function calls in python so sit back
2:07:17
relax and enjoy the show all right people nested function calls
2:07:24
these are function calls inside of other function calls and this is possible because
2:07:29
certain functions will return a value and we can immediately use that value as an
argument for the next function
2:07:35
so here's an example let's say that we have the small program where we will ask a
user to type in a
2:07:41
whole positive number and let's say i don't let's say i type in negative 3.14
2:07:47
and then i'm storing that value within a variable named num what i'm going to do is
2:07:52
convert that number to a floating point number because currently it's a string when
you accept user input
2:07:58
i will find the absolute value of num and then round it to the nearest whole number
2:08:03
and then print the value so we can actually do this using less lines of code with
nested function calls
2:08:09
so let's start with the first function which will be accepting some user input the input
function so this will be in
2:08:16
the very center of our nested function calls and then the next function we would like
to apply
2:08:21
to the variable that is returned the value that is returned we will pass in as an
argument to our
2:08:27
next function of casting this data type to a float so we'll surround our function with
another function and
2:08:34
use the float function and then we'll just keep on adding layers to this so we have the
absolute value function
2:08:42
next so we'll add another layer and we have the round function
2:08:47
so we'll add a layer to that and lastly we're going to print and that'll
2:08:52
be the outermost layer so this program will do the exact same thing
2:08:58
let's say negative 3.14 except we've written this with one line of code so basically we
start
2:09:05
with the innermost function and resolve that first whatever value is returned we use
as an
2:09:10
argument to the next outermost function and in this case it would be float then we
resolve that then move on to absolute
2:09:18
value then round and then print so it's just another way of writing code that takes up
less
2:09:23
lines but this is more of an extreme example you probably won't see like four or five
nested functions well
2:09:29
everybody those are nested function calls if you would like a copy of this code i will
post all of this to the comment
2:09:35
section down below but yeah those are nested function calls in python
25.variable scope
2:09:40
hey what's going on people it's your bro hope you're doing well and in this video i'm
going to show you how variable scope
2:09:46
works in python so sit back relax and enjoy the show
2:09:52
all right let's do a thing people let's discuss the scope of a variable now the scope of
a variable is the
2:09:58
region that a variable is recognized a variable is only available from inside the region
2:10:05
that it is created let's say that we have a function and i just gave this function a name
of display name
2:10:10
i created a variable inside of this function named name and you can assign this a
value of
2:10:16
whatever your name is preferably a last name for this example and what i'll do is just
print
2:10:21
whatever my name is so this variable is known to have a local scope because it's
declared
2:10:28
inside of a function and a variable is only recognized inside of the region
2:10:33
that it is created this variable of name since it's created inside of this function it is
known as a local
2:10:40
variable that has a local scope it's only available inside of this function that it's
2:10:46
created now let's attempt to access this variable from outside of our function so i'm
going to
2:10:52
print my name variable but we'll have an issue though so name name is not defined
so
2:10:59
local variables are declared inside of a function and they have a local scope they are
2:11:05
only available inside of this function in which they're created on the other hand a
global variable is a
2:11:12
variable that is declared outside of any function but within your module that you're
working with
2:11:17
a global variable is known to have a global scope they are available inside and
outside of
2:11:23
any functions now if i was to print my name variable this is going to print
2:11:29
bro or whatever your first name is and it is entirely possible to have both a global and
local version of the same
2:11:35
named variable so we have both a global version of name as well as a local version
of name
2:11:42
if i was to print my name variable from outside of this function it's going to print my
global version
2:11:48
however if i print my version of name inside of that function in which it's created
2:11:54
we're going to first use the local version before the global version of name and let's
try
2:11:59
that so i'm going to call my display name function
2:12:04
and this will print my last name code and then my first name of bro so it
2:12:11
is possible to have a global and local version of the same named variable
2:12:16
now if i was to remove this line where we declare a local version of name well then
the next best
2:12:22
available name to us would be the global version of name so if you use a variable
inside of a function
2:12:28
you'll first use a local version of that variable if it's available if not you'll use a
2:12:34
global version instead and python will follow this rule known as the legb
2:12:40
rule you use any local variables first then enclosed variables then global and then
2:12:45
any built-in variables in that order so that's the main difference between local scope
and
2:12:51
global scope the scope of a variable is the region in which it's available and it's
limited to the region in which
2:12:58
it's created a local scope is available only inside of the function in which that variable
is created
2:13:04
a global scoped variable is a variable that is available inside and outside of any
functions but
2:13:10
within your module that you're working with so that's variable scope in python if you
would like a copy of this code i
2:13:17
will post this code to the comment section down below and well yeah that's how
variable scope
2:13:22
works in python hey what's going on everybody it's bro hope you're doing well
26.args
2:13:27
and in this video i'm going to explain the arcs parameter in python so sit back relax
and enjoy the show
2:13:36
all right guys and gals let's talk about args now args is a parameter that will pack
2:13:42
all arguments into a tuple it's useful so that a function can accept a varying amount
of arguments
2:13:48
here's an example of why it would be useful let's say we have a function called add
that will accept two numbers
2:13:54
as arguments and add them together and return the sum so this works as intended
right as long
2:13:59
as we pass in only two numbers but what if we need to pass in three numbers as
arguments
2:14:04
well we can no longer use this add function because we have two parameters set up
but we're passing in three arguments you
2:14:10
can see here within my console window that we have a type error add takes two
positional arguments but
2:14:16
three were given and one way in which we can solve this issue is to use this args
parameter so what we're going to do
2:14:22
is replace all of our parameters with asterisk args and actually the args portion isn't
as important as
2:14:30
the asterisk args is short for arguments and you can really name this whatever you
want just be sure that you have this asterisk
2:14:36
because with this asterisk we're doing a form of packing what we're doing is passing
all of these
2:14:41
arguments and packing them into a tuple so let's rewrite this program so let's say
maybe sum
2:14:47
equals zero and then we will need to iterate through all of the items within
2:14:53
our tuple because tuples are iterable so let's say for i in args what we'll do is say sum
2:15:00
plus equals whatever i is and at the end when we escape the for loop we will return
2:15:06
our sum so let's try it again and see if it works well would you look at that it works
2:15:11
let's pass in a few other arguments for good measure let's pass in six arguments this
time
2:15:16
all right look at that this does in fact work so you can actually name this whatever
you want the important thing is to have this asterisk
2:15:23
so let's rename this as i don't know stuff and let's try this again yep
2:15:29
it still works with tuples they are ordered and unchangeable let's attempt to edit
2:15:34
one of these values so let's say stuff at index zero is equal to
2:15:42
zero and let's try and change it so you can see that tuple object does not support
2:15:48
item assignment if you need to change one of the values within your tuple after you
pack all of
2:15:54
these arguments you might need to convert this to a different collection one way in
which we can do that is to cast it
2:16:00
let's say stuff equals and we'll cast our tuple as a list because a list
2:16:06
can be changeable it's mutable so let's attempt this again we'll cast our tuple as a list
and then edit one of
2:16:13
the values and then add all the values together so to change one of the values say
the name your collection stuff in
2:16:20
this case let's say at index zero this now equals zero so now if we were to add all
these items together
2:16:26
they should equal 20. there we go so yeah that's basically the args parameter
2:16:32
the name isn't as important as the asterisk because you can really name it whatever
you want a common convention is to just name this
2:16:39
args it's a parameter that will pack all arguments into a tuple it's useful
2:16:44
so that a function can accept a varying amount of arguments well that is all about
args
2:16:50
if you would like a copy of this code i will post this in the comments section down
below but yeah that is how the args parameter
2:16:56
works in python hey what's going on everybody it's bro hope you're doing well and in
this video
27.kwargs
2:17:02
i'm going to be explaining quarks in python so sit back relax and enjoy the show
2:17:10
all right everybody let's talk about quarkx now this is a parameter that will pack all
arguments into a dictionary it's
2:17:17
useful so that a function can accept a varying amount of keyword arguments it's
2:17:22
identical to args except with args args will accept a varying amount of positional
2:17:28
arguments and pack them into a tuple with quarks this will accept a varying amount
of keyword arguments
2:17:34
and pack them into a dictionary so here's an example of why this would be useful
let's say we have a function called
2:17:40
hello that accepts two keyword arguments a first name and a last name all this will
do is
2:17:46
output hello your first name and last name but what if somebody has more than two
names
2:17:51
let's say somebody has a middle name and for this example i'll just say my middle
name is dude
2:17:56
so if i were to run this well this hello function is no longer going to work as you can
see in the console window we
2:18:03
have a type error the hello function got an unexpected keyword argument middle
2:18:08
one way in which we can solve this we can use this quark's parameter so we can
accept a varying
2:18:14
amount of keyword arguments so what we'll do is replace our parameters with two
asterisks then
2:18:21
quarks now if i would like the same output as i had before i'm going to need to
change a few things around
2:18:26
in order to access a value within a dictionary you type the name of the dictionary
square brackets and then within quotes
2:18:34
list your key so i would like to access the value at this key of first
2:18:40
and let's repeat the process for last name so the key is going to be last we'll
2:18:46
access this value at this key and this should work the same as it did before
2:18:52
now let's take this a step further let's say we would like to display somebody's full
name based on the amount
2:18:58
of keyword arguments that they pass into this function well there's a few different
ways in which we could do this one easy way is
2:19:05
that we could write our program like this let's print the word hello
2:19:11
and we will need to iterate once through each key value pair within this dictionary
2:19:17
and we can use a for loop to do that so to iterate once through each key value pair in
your
2:19:22
dictionary we'll type for key value in the name of our dictionary quarks in
2:19:28
this case dot items and then we will print each value
2:19:37
so there's one more thing that we need to do this kind of works as intended but it's
going to print each value including our word
2:19:44
hello on a new line so with a standard print statement this will print a new line
character
2:19:50
after each print statement as if we're hitting enter we can actually change that so
after
2:19:56
your string add and equals and we can replace that new line character with
something else
2:20:01
maybe a space and let's do the same thing for our for loop so value
2:20:06
comma end and then just add space now this will print all of your names on the
same line hello
2:20:12
bro dude code and now we can pass in a varying amount of keyword arguments
based on how long our name is
2:20:19
perhaps we have like a title title equals mr and we can print that as well
2:20:26
hello mr bro dude code here's a fun fact of the day you don't necessarily need to
name this parameter as quarks
2:20:34
the important thing is to have these double asterisks that precede it quarks is just a
2:20:39
common naming convention it's short for keyword arguments you could name this as
something else
2:20:44
perhaps something more descriptive like names since we're passing in a bunch of
names so the name of our dictionary
2:20:49
is now names and this would work the same as it did before the important thing is to
have these
2:20:55
double asterisks that precede the name of your dictionary but quarks is the common
naming
2:21:00
convention well everybody that's all about quarkx it's a parameter that will pack a
varying amount of keyword arguments
2:21:07
into a dictionary if you would like a copy of all this code i will post this in the
comments section down below
2:21:13
but yeah that is how quarkx works in python hey what's going on everybody
28.string format
2:21:19
it's your bro hope you're doing well and in this video i'm going to teach you guys all
about the format method in python so sit back relax and enjoy
2:21:27
the show okay ladies and gentlemen let's begin
2:21:32
the format method is a method available to strings it's optional and it gives users
2:21:37
more control when displaying output to best demonstrate this i'm going to use a line
2:21:42
from a popular nursery rhyme titled hey diddle diddle because well you know i'm a
mature adult like that
2:21:49
i'll be printing this line to my console window the cow jumped over the moon let's do
so using a standard
2:21:56
print statement and then i'll show you the benefits of using the format method later
for this example i have two variables a
2:22:04
variable called animal and i'm going to store a string of cow and item and i'll be
2:22:09
storing an item name which is moon when we compile and run this as you would
expect the output is the cow jumped over the
2:22:16
moon there's a more elegant way of writing this line of code and that is by using the
format method
2:22:22
available to strings i'm going to turn this line into a comment and we're instead going
to produce the same output but use
2:22:29
the format method instead so we need a standard print statement like usual and then
a string that we would like to
2:22:36
format so we're going to write the same thing but in place of the variables we're
going to add a placeholder
2:22:42
the instead of the variable name or value name i'm going to add a set of curly braces
so that functions
2:22:50
as a placeholder for a value or a variable the
2:22:55
this should be cow jumped over the and i'm going to insert another
2:23:01
placeholder now this is a string we can format the string by following this string with
dot format parenthesis
2:23:10
and within the parentheses of the format method we can insert some values or
variables let's begin
2:23:16
with some values i would like to insert cow as well as moon so this will produce
2:23:23
the same output but it's written a little more elegantly we can also replace these with
variables
2:23:30
because they store these values so i'm going to replace cow with animal and moon
with item
2:23:37
and this will create the same output as it did before these curly braces are what's
known as
2:23:43
format fields they function as a placeholder for a value or a variable and they work in
order the
2:23:50
first format field will insert the first value at this location if you have another set
2:23:56
or more sets of this format field you'll insert the next value that's listed within the
2:24:02
format method so if we were to switch these around we'll switch the position of
animal with
2:24:08
item well then the first format field is going to insert our item and the second format
field
2:24:15
will insert our animal this time the moon jumped over the cow an additional way of
inserting values at
2:24:22
a given place holder would be to use what's referred to as a positional argument
2:24:29
within these format fields we will list the index of the value that we would like to insert
at this location
2:24:36
if you need to insert the first value well that has an index of zero because computers
always start
2:24:41
with zero and following that pattern the next index would be one if you had
2:24:47
let's say three values that you need to insert if you had another placeholder that
would be two then so you would just
2:24:53
follow that pattern so there's going to be no visible change but what we could do is
reverse the positional arguments of
2:25:01
these placeholders so what do you think would happen if i switched one with zero at
these locations
2:25:07
well this format field is going to insert the value found at index one this first value is
index zero and
2:25:14
the second one is index one so now the roles are reversed this time the moon
2:25:19
jumped over the cow our last way of inserting some values at a given format
2:25:25
field would be to use what's referred to as a keyword argument within
2:25:31
the format method we're going to list some keyword argument pairs we need a
keyword name followed by a
2:25:37
value so let's say animal is going to be the keyword we're going to say equals some
value animal equals cow and
2:25:45
item equals moon and you may have noticed that the color of your text might have
changed
2:25:51
so we no longer need these variables actually um but i'm going to comment out
these two print statements because we
2:25:57
will get an error then so these are in keyword argument pairs and within our format
field instead of
2:26:04
keeping these empty or using an index we can use the keyword name at my first
format field i'm going
2:26:10
to insert my keyword of animal and at my second format field i would like to insert
2:26:17
the item keyword so the result is going to be the cow jumped over the moon so let's
2:26:22
reverse these now we'll place our item keyword here and then at the second format
field we will
2:26:28
insert our animal and this time the moon jumped over the cow here's an important
2:26:33
concept that i'd like to bring to your attention with these values found within the
format method you can actually reuse
2:26:39
some of these more than once so let's say we would like to state the animal jumped
over the animal we're
2:26:46
going to use this animal keyword twice and our output is going to be the cow jumped
over the cow if we're
2:26:52
doing the same thing with positional arguments we can reuse the same index this
time
2:26:58
let's say that the moon jumped over the moon so our item has an index of one so
within our first format
2:27:06
field we'll state the index will be one and our second format field will also be one
2:27:12
so our output's going to be the moon jumped over the moon all right let's take things
up a level
2:27:17
there's an even more elegant way in which we could write all of this what we'll do is
store our string that
2:27:23
we would like to format within a variable let's turn this line into a comment and let's
create a
2:27:29
variable called text text equals the string that we would like to format which is the
format field
2:27:37
jumped over the format field and if we need to format this variable
2:27:44
we can just call the format method on it so within a print statement let's say
2:27:50
text dot and then use the format method and pass in the values that we would like to
2:27:56
insert at these placeholders so we're going to insert animal as well
2:28:02
as item so our result as you would imagine is the cow jumped over the moon this
2:28:08
next section i'm going to explain how we can add some padding to a string when we
display it using the
2:28:13
format method let's say we have a name variable and assign this a value of whatever
your
2:28:18
name is so i would like to display my name along with the custom message let's say
hello my name
2:28:25
is and then i'll use a format field here so let's follow this string by using the format
method
2:28:31
and i would like to insert my name at this location so currently this will display hello
my
2:28:37
name is bro or whatever else your name is so we can add some padding either
before our name after or we can
2:28:44
add some padding to the left and the right hand side of our value that we have so
what we'll do to begin
2:28:51
is let's add some padding to the right hand side of our name that we have so within
our
2:28:56
format field if we need to add some padding within the format field add a colon and
then the amount of space you would like
2:29:03
to allocate to displaying your value so let's say 10 so i'm going to allocate 10 spaces
worth
2:29:10
of room to display my name you can't really see it right now but let's take a look so i
can't move my cursor past this line
2:29:18
at the end of my name but with my second line i have all of this room to work with so
if i were to change my string that i
2:29:25
have let's say nice to meet you well then
2:29:31
all of this padding that i have is going to be more evident so we can add a number
preceding with a
2:29:38
colon to add some padding to a value and then you can left align this right align it or
center align it
2:29:45
so what i'll do is copy this line that i have if you need to left align it you use the
2:29:51
less than sign but there's going to be no visible change because that's already the
default
2:29:56
if you need to write a line precede your number with a greater than sign
2:30:01
so your value is going to be right aligned if you need to center this value then
2:30:07
use the carrot so this will center align your value within the padding that you
2:30:14
have allotted one question that you might have for me and i'll try and answer that
now because it's relevant what if we need to add a
2:30:20
positional argument or a keyword argument to our format field if there's already some
text within here
2:30:26
so what you would do before the colon you would just precede the colon with either
your positional argument or
2:30:32
your keyword argument followed by colon and then whatever you want to add
afterwards to format
2:30:38
your value that you have alright ladies and gentlemen we are near the end and i
promise that this is the last section
2:30:44
how can we format some numbers let's say we have a number variable and this will
equal the
2:30:50
first few digits of pi 3.14159 let's pretend that we would like
2:30:55
to display only the first two digits after the decimal so we can do so using the
2:31:02
format method let's print the number pi is and then i'll use a format field
2:31:09
here and we will call the format method and insert our number that we have so if i
need to
2:31:17
display only the first two digits after the decimal within the format field i will add
2:31:24
colon dot to f f is for floating point numbers that's
2:31:30
anything with a decimal portion so the result is that this will display
2:31:35
only the first two digits after my decimal because we placed two within
2:31:41
the format field so if this was three point three f this would display three
2:31:47
digits after the decimal portion oh and apparently this will round your number so
that's something that you should keep
2:31:53
in mind so this time let's change our number to something larger like 1000 and i will
copy this line and make
2:31:59
a few changes let's say the number is and i would like to add a comma at the
2:32:06
thousands place so after the colon within the format field i will add a comma so this
will automatically add a comma
2:32:13
to all 1000s places another cool thing that you can do is that you can display your
number as
2:32:19
binary so within the format field add colon b this will display a binary
2:32:25
representation of your number you can do the same thing with octal by
2:32:30
adding colon o so our number is now being displayed as an octal number
2:32:35
and you can do the same thing with hexadecimal lowercase x for lowercase or
uppercase x for all
2:32:42
uppercase so my number 1000 is now in hexadecimal and lastly we can display this
number
2:32:49
in scientific notation using either lowercase e for lowercase or capital e
2:32:55
for uppercase and mynumber 1000 is now in scientific notation
2:33:01
all right everybody so that's a few things that you can do with the format method it's
an optional method that
2:33:07
gives users more control when displaying output if you would like a copy of all the
code that we've written here today i will
2:33:13
post everything in the comments down below don't be afraid to smash that like
button drop a comment down below and
2:33:20
subscribe if you'd like to become a fellow bro hey what's going on everybody it's bro
hope you're doing well and in this video
29.random numbers
2:33:26
i'm going to explain a few useful methods of the random module so sit back relax
and enjoy the show
2:33:34
okay people in this video i'm going to explain how we can generate some random
numbers as well as a few other useful methods of
2:33:41
the random module now we're not going to be creating true random numbers but
something called
2:33:47
pseudorandom numbers which are fairly darn close so the first step is to import the
random module at the
2:33:53
top import random we now have access to everything that the random module has to
offer
2:33:59
let's begin by generating a random number between one and six like we're rolling a
dice so what i'm
2:34:04
going to do is say x equals random dot rand
2:34:10
and we can generate a random integer between a certain range if i want one through
six i will state
2:34:16
one comma six and then we can generate a random number between one and six
like we're rolling a dice
2:34:22
so if i were to print the value of x then we will get a random number between
2:34:27
one and six for example i just got a one a two a three and a six all right
2:34:33
we can also generate a random floating point number two let's say y equals random
dot
2:34:40
random so this will give us a random number between 0 and 1. this time i'm going to
print
2:34:47
y so we have 0.49 and some change 0.145 and 0.858
2:34:56
we can also generate a random choice from a list or other collection let's say we're
playing a game of rock
2:35:02
paper scissors so let's create a list i'll just call this my list equals
2:35:08
square brackets and we will have three choices rock paper and
2:35:14
scissors so let's say z
2:35:21
will be the random string that we generate z equals random dot
2:35:28
choice and we will pass in my list and let's print z so this will generate
2:35:36
a random choice from my list called my list so we got scissors paper and
2:35:43
rock we can also use the shuffle method of the random module to shuffle a list or
other collection let's
2:35:50
say we're working with a deck of cards cards equals let's say one
2:35:56
through nine and we have a jack a queen
2:36:04
a king and an ace this won't be a full deck of cards it'll just be one suit
2:36:10
so we can actually shuffle this list by using the shuffle method random dot shuffle
what do we want to
2:36:18
shuffle we want to shuffle our cards and i will display my cards with a print statement
so the
2:36:26
shuffle method will shuffle a list or other collection for you well everybody that's a few
useful
2:36:31
methods of the random module if you would like a copy of all this code i will post this
in the comments down below
2:36:37
don't be afraid to smash that like button drop a comment down below and subscribe
if you'd like to become a
2:36:42
fellow bro hey what's going on everybody it's bro hope you're doing well and in this
30.exception handling ⚠️
2:36:47
video i'm going to explain exception handling in python so sit back relax and enjoy
the show
2:36:55
well everyone let's talk about exceptions an exception is an event detected during
execution that interrupts the
2:37:02
normal flow of a program here's a program that i've written and we're intentionally
going to cause an
2:37:07
exception so this program that i've written we will accept a numerator and a
denominator from the user
2:37:13
we'll divide the numerator by the denominator we'll get a result and print the result so
what if i were
2:37:19
to divide a number such as 5 divided by 0 which we cannot mathematically do
2:37:24
well this will cause an exception an event detected during execution that interrupts
the
2:37:30
flow of a program so our program was interrupted and within our console window we
have this
2:37:35
traceback message which states what went wrong and a name for this exception that
occurred
2:37:41
this specific exception that occurred is a zero division error so this video is all about
handling
2:37:47
these exceptions so they do not interrupt the normal flow of a program now a very
basic form of exception
2:37:53
handling is to surround any code that is considered dangerous as in it might cause
an exception
2:37:58
within a try block anytime you accept user input would be a good indicator too so this
code is considered dangerous
2:38:05
because we don't know what the user is going to type in so we will try all of this code
if an
2:38:10
exception occurs we can catch it and handle it so to do that we'll add an accept block
accept and
2:38:16
then we will catch exception normally having this block by itself
2:38:21
isn't considered good practice but i'll explain why later so this accept exception block
will catch
2:38:27
all sorts of different exceptions and it will prevent our program from being interrupted
and we can do
2:38:32
something else entirely such as print a message something went wrong so let's try
this
2:38:39
again we will divide five by zero which we cannot do we no longer have that
traceback message
2:38:45
within our console window we caught the exception and we did something else we
printed something went wrong like i said before
2:38:53
it's not considered good practice to have a single accept block that will handle all
exceptions it's much better to first
2:39:00
handle specific exceptions when they occur and we can do so by writing additional
accept blocks
2:39:06
so let's create an additional accept block that will catch any zero division error
exceptions
2:39:13
that occurs when somebody attempts to divide by zero so if this exception occurs
how do we want to
2:39:18
handle it let's just print the message let's print you can't divide
2:39:24
by zero idiot and let's try this again so five divided
2:39:30
by zero you can't divide by zero idiot here's another one i'm going to turn
2:39:35
this accept block into a comment so we can take a look at something what if i divide
by something that's not
2:39:41
a number like we take 5 and divide it by the word pizza well pizza is not a number
2:39:46
and we ran into a value error exception so we should probably handle this too so
2:39:52
let's create another accept block that will handle that accept value error
2:39:59
and how do we want to handle this let's print something enter only numbers
2:40:06
please okay let's try this again and see if this exception is caught five divided by
2:40:13
the word pizza enter only numbers please not only that but you can also add the
2:40:20
accept exception block at the very end just in case there's something you won't
anticipate but it's always considered good practice
2:40:27
to first catch any specific exceptions and let the user know exactly what went wrong
an additional way in which we can
2:40:34
handle these exceptions is that we could display the exception that occurs although
it is completely
2:40:39
optional so to do that what we'll do is that after each of these accept block definitions
2:40:45
let's add as e so we're calling the exception as e that's just standard practice
although
2:40:50
it's not necessary so let's do that for each of these except blocks and then we can
print whatever e is so
2:40:58
let's add that to each of these blocks so it should now look something like this
2:41:04
so when we encounter an exception we can display what exception occurred too as
well
2:41:10
so let's divide five by pizza invalid literal for int with base 10 pizza
2:41:16
enter only numbers please so like i said this is an additional way in which you could
handle an exception although it's
2:41:23
entirely optional another thing that you can do is that you can add an else statement
to the end of your accept blocks so
2:41:30
what's happening here is that we're going to try all of this code if an exception
happens
2:41:36
then catch it and handle it if not then do something else so why don't we print our
result only if
2:41:42
there are no exceptions that occur so if there are no exceptions we will execute
2:41:48
this else statement if not then we won't so let's divide five by an actual number
2:41:53
this time like two so there are no exceptions that occurred so we executed this else
statement whatever
2:42:00
code is within our else block and this printed the result of 2.5 but if an exception were
to occur like
2:42:07
we divide 5 by 0 then we will catch our exception and handle it
2:42:12
and we do not execute whatever block of code is within our else statement there is
one other
2:42:18
clause that you should be aware of that is the final clause this is always at the end
how this works
2:42:23
is that whether or not we catch an exception we will always execute any code that is
within the block of code for our
2:42:30
final clause so this is a good opportunity if you open files to close them within the
finally block
2:42:38
but we haven't dealt with opening files yet so we won't do that all we'll do is just print
something
2:42:43
print this will always execute so
2:42:48
whether or not we encounter an exception this finally block will always execute at the
end so let's try this again so this time
2:42:55
let's cause an exception let's divide five by zero you can't divide by zero this will
always execute and this
2:43:02
time let's take this seriously and divide five by another number such as three so
2:43:07
this will give us a result and like i said the finally clause the code within the finally
clause will
2:43:13
always execute but we won't really be working with this until we get to the section on
file
2:43:18
handling well everybody those are exceptions in python they are events detected
2:43:24
during execution that interrupt the flow of a program and we can handle these
exceptions by
2:43:29
using try and accept blocks so if you would like a copy of all this code i will post all of
this in the
2:43:34
comments down below but yeah that is how exceptions work in python
31.file detection
2:43:40
hey what's going on everybody it's bro hope you're doing well and in this video we're
going to be covering some basic file detection using
2:43:47
python so sit back relax and enjoy the show
2:43:53
welcome ladies and gentlemen to the beginning of a mini series involving doing stuff
with files using python and
2:43:59
in this video we're going to be doing some basic file detection and in order to do so i
recommend
2:44:04
importing the os module it's already included with the standard python library all you
need to do is import it all
2:44:11
we'll be doing is checking to see if a file exists someplace on our computer so we'll
probably need a file to work with
2:44:17
what i'm going to do is go to my desktop for convenience and just create a new text
document i
2:44:23
will call this test.txt now i probably need the file location so i'm going to copy that
2:44:30
and then we can use this so let's create a variable called path that will include the
path to the file
2:44:36
that i would like to detect so that was test.txt now if you have
2:44:41
backslashes in your file path you'll probably need double backslashes because that's
the
2:44:47
escape sequence for a backslash within a string so we now have this variable path
that
2:44:53
we can work with now i would like to check to see if this location exists on my
computer so if os
2:45:00
dot path dot exists and you can see that we can pass in our path as an argument so i
will pass
2:45:07
in my path and if this returns true if that location exists i will print
2:45:14
that location exists so this will not tell me if this is a
2:45:21
file or not we'll be covering that later else let's print that location
2:45:29
doesn't exist all right so if this location exists
2:45:35
this will return true if not this will execute our else statement that location exists so
what if i were
2:45:43
to delete this file and we run this program again that location doesn't exist
2:45:48
now this won't tell you if the location you're dealing with is a file or not there is a
separate
2:45:55
function for that it is if os dot path dot is file
2:46:05
what we'll do is pass in our path and print that is a file
2:46:12
all right so let's create another file because i deleted the previous one
2:46:18
test.txt it's in the same location let's run this again that location exists that is a file
now
2:46:25
what if this was a folder so i'm going to create a new folder i will call this folder
2:46:30
i will delete my test document and let's change the location to folder
2:46:39
that location exists but it is not a file you can check to see if a location is a directory
2:46:44
so let's write an else if statement os dot path dot is directory and we will
2:46:52
pass in our path and let's print that
2:46:57
is a directory all right so we have our folder we're
2:47:03
going to check to see if this location exists and if it's a file or
2:47:09
a directory that location exists that is a directory well everyone that is the basics of
file
2:47:15
detection we'll be building upon this concept in future videos so if you would like a
copy of this code
2:47:21
i will post this in the comment section down below but yeah that is the basics of
simple file detection using python
32.read a file
2:47:28
hey what's going on everybody it's bro hope you're doing well and in this video we're
going to be reading a file in
2:47:34
python so sit back relax and enjoy the show
2:47:40
welcome back guys and gals in this video i'm going to explain how we can read the
contents of a file using python
2:47:46
so we'll probably need some sort of file to work with i created a plain text file called
2:47:51
test.txt it's a plain text file and all this says is omg you can read this have a nice day
2:47:58
like comment and subscribe so what i would like to do is to read the contents of that
file line by line
2:48:04
and print it to the console window and here's how and honestly we only need two
lines of code to do this
2:48:10
so what we'll do is type with open then within the open function list the
2:48:15
name of your file or the file path this file of mine is within my project folder
2:48:21
so i only need to list the file name if this was someplace else you'll probably need
2:48:27
the file path so let's say this was on my desktop well i'll probably need to list the file
2:48:33
path of where that file is located and i'll need to use double backslashes because
that's the escape sequence for a
2:48:40
backslash but since this file is within my project folder i only need to list the file name
2:48:45
so with open then the name your file or the file path as file then what we're going to
do
2:48:54
is print file dot read and that is it so let's test it
2:49:02
and it works omg you can read this have a nice day like comment and subscribe so
this will
2:49:09
actually close files automatically after opening them so that's kind of convenient and
let's
2:49:14
just test that theory so i'm going to print file dot closed if my file is in fact closed this
2:49:22
will print true if it's still open this will print false so normally when you read a file it's
2:49:28
normally open and you need to close it manually but if you write your program this
way with open this will close the file
2:49:35
automatically for you so using with open will close any files automatically for you
2:49:41
after they've been opened however this does not catch and handle any exceptions
that might
2:49:46
occur like if we cannot locate this file so let's say that i mistyped the extension for this
file
2:49:53
let's say that i type in test.tx that's kind of funny because tx is the
2:49:58
abbreviation for texas it's a texas file so this will cause a file not found error and this
will
2:50:05
interrupt the flow of our program and we would probably like to prevent that so what
we could do is write a try and
2:50:12
an accept block so we will try all of this code if we cannot find this file we can handle
this exception
2:50:19
file not found error so we'll place all of this code within the try block then if an
exception happens we will catch
2:50:25
that exception except we would like to catch any file not found air
2:50:30
exceptions and let's handle this by printing that file was not found
2:50:40
so if i were to run this again we will catch and handle this exception and this will not
2:50:45
interrupt the normal flow of our program well everybody that's how to open and read
2:50:50
a file in python if you'd like a copy of all this code i will post this in the comment
section down below
2:50:56
but yeah that's how to open and read a file using python hey what's going on
everybody it's bro
33.write a file
2:51:03
hope you're doing well and in this video we're going to be writing files in python so sit
back relax and enjoy the show
2:51:11
so to begin it's kind of similar to the last video on reading files with open within the
open function list
2:51:18
the file name or the file path so what do you want to call this file i will call this file test
dot txt
2:51:26
now normally with the open function there is a mode and you can actually change
that there's
2:51:31
a second argument that you can pass in by default this is r for read but if you
2:51:36
want to write a file this would be w so now we're going to write a file called
2:51:42
test.txt so to finish with open as file what are we going to do file
2:51:49
dot write and within the write function we can write some text so let's create a text
variable and we
2:51:57
will add a string so let's say text equals
2:52:03
yo and if you need to go down to a new line be sure to use the new line character so
2:52:09
after this new line character the text that comes after is going to be on the next line
as if we're hitting
2:52:15
enter so yo this is some text have
2:52:22
a good one all right so after i run this program we
2:52:29
should have a file a text file within our project folder that appears
2:52:34
and it is right here and it says yo this is some text have a good one now this is in
write
2:52:41
mode if this text was something else it will actually overwrite your current file so if i
were
2:52:47
to change this to uh oh this text has
2:52:53
been over written well then this will overwrite the text that we
2:52:59
have uh oh this text has been overwritten so you can actually append a file by
2:53:04
changing the mode to a for append so let me change this back we will rewrite this
this will save over
2:53:12
our current file now we can append some text so let's add maybe a new line
2:53:17
uh have a nice day see ya so i would like to append
2:53:25
some text to this file i'm going to change the mode to a for append and we can now
append some
2:53:31
text onto the end of this file well everybody that's how to write to a file and or append
a file if you would
2:53:37
like a copy of this code i will post this in the comment section down below but yeah
that's how to
2:53:42
write to and or append a file in python hey what's going on everybody it's bro
34.copy a file 🖨️
2:53:48
hope you're doing well and in this video we're going to be copying files in python so
sit back relax and enjoy the show
2:53:57
welcome back to another video regarding doing stuff with files using python and in
this video we're going to be
2:54:02
copying some files and in order to do so i recommend importing the shuttle module
there's
2:54:08
other ways of doing this too but i tend to like using the shuttle module now with this
module there are three basic
2:54:15
functions to copy a file starting at the top they are copy file copy and copy to so copy
file is what
2:54:24
we'll be working with this will copy the contents of a file copy will do everything that
copy file does
2:54:31
plus copy the permissions mode and the destination can be a directory
2:54:36
and lastly copy 2 this will do everything that copy does plus it copies the
2:54:41
metadata of that file including the files creation and modification times so depending
on the project or the
2:54:48
program you're working with you may need to use copy or copy too but in this video
we're going to be using
2:54:54
copy file just to copy the contents of a file now after you import the shuttle module
2:55:00
this is all you need to do to copy a file shuttle dot then use the appropriate
2:55:05
function that you need to copy your file we're going to be using copy file copy
2:55:10
file and within this function there are two arguments a source and a destination so
within my
2:55:17
project folder i have a plain text file called test test.txt
2:55:22
all this says is yo this is some text see ya what i would like to do is copy
2:55:27
this file because it's cool now since this file is within my project folder i only have to
list the file name
2:55:34
otherwise if this is someplace else i would list the file path here so this file is test.txt
2:55:42
this is the source file and now i need a destination so the second argument will be
separated
2:55:48
with the comma i'm going to list a destination so you can actually rename this to
2:55:53
i would like to name this as copy.txt so the destination is my project folder
2:56:00
so when i create a copy of this file test.txt this copy will appear in my project
2:56:06
folder and say the same thing so after running this we now have a file named
copy.txt that says the same
2:56:13
thing as my test file and remember what i said you can copy this file to a different
location on
2:56:19
your computer so you could list the file path so this file path would go to my desktop
2:56:25
and then when i run this this will copy my original file my source and then copy it to
2:56:30
wherever my destination is oh and i almost forgot so if you need to use copy or copy
2
2:56:35
the arguments are exactly the same so if you needed to use copy this would be a
shuttle dot copy instead
2:56:42
of copy file then copy 2 is shuttle dot copy 2. the arguments are exactly the same but
2:56:48
each of these functions will copy different things depending on what you need for
your own program or project
2:56:54
all right everybody so that's the basics of copying files and python i will post this in
the comment
2:57:00
section down below but yeah that is how to copy files using python
35.move a file 🗃️
2:57:05
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to explain how we can move
2:57:11
files using python so sit back relax and enjoy the show
2:57:18
welcome back everybody to another video regarding that thing that we do called
programming and in this video we're
2:57:23
going to be moving files using python and in order to do so i recommend importing
the os module
2:57:30
this is included with the standard python library all you need to do is import it and
now let's create two variables a
2:57:37
variable called source that will hold the location of where our source file is located
2:57:42
as well as a variable called destination to function as well the destination now for this
video i'm
2:57:49
going to move a file within my project folder to my desktop so to do so i'm going to
create a new
2:57:56
file i will name this test.txt but take the liberty to name this whatever you want
2:58:02
so i'm going to list the location of where my test file is located since this is
2:58:08
within my project folder i only need the file name if this was someplace else on your
computer you would probably need
2:58:14
the file path and now where would i like to move this file to i would like to move this
file
2:58:19
to my desktop so i just need the file path so i'm going to go to my desktop
2:58:24
right click go to properties i'm going to copy this location and paste it within my
2:58:30
destination and i will add slash desktop slash test.txt you can also rename this
2:58:38
too if you want i will just keep it the same now if you have single backslashes you'll
likely need to change these
2:58:45
to double backslashes because that is the escape sequence to print a backslash
within a string so
2:58:52
we have a source location as well as a destination location now we can begin i would
recommend
2:58:58
writing our code within a try and accept block to handle any exceptions so i would
like to
2:59:03
handle any file not found error exceptions just in case our program cannot locate our
source
2:59:10
file so let's print a message if that is the case perhaps source plus was not
2:59:17
found now within our try block let's check to see if there is already a file at this
destination
2:59:23
because i would not like to save over it if there's already a file there so let's do some
basic file detection if
2:59:30
os dot path dot exists and i will pass in my destination as an
2:59:37
argument to the exists function so if there is already a file here i should probably let
the user know
2:59:44
print there is already a file there else
2:59:51
we can replace our files honestly if you do not care about saving over any files you
can omit this part so else os
2:59:59
dot replace and we will pass in our source and our destination
3:00:06
as arguments and let's print a message to the console window to let the user
3:00:11
know that source plus was moved all right let's try this oh i
3:00:19
accidentally deleted my file test.txt all right
3:00:24
now this should disappear in three two one and it's gone
3:00:31
test.txt was moved and it should now be on my desktop which it is now you can also
use this to
3:00:37
move a directory as well i'm going to create a new directory i will name this folder
3:00:44
and i will change source to folder and the destination from test.txt to
3:00:50
folder and now this should move my folder there is already a file there
3:00:59
oh let's delete that okay folder was moved all right everybody so
3:01:05
that is how to move a file and or directory using python if you would like a copy of
this code i
3:01:11
will post this in the comment section down below but yeah that is how to move a file
and
3:01:17
or directory using python hey what's going on everybody it's bro hope you're doing
well and in
36.delete a file 🗑️
3:01:23
this video i'm going to teach you guys how we can delete files using python so sit
back relax and enjoy the show
3:01:32
all right everybody so in this video we're going to be deleting files using python and
in order to do so i recommend
3:01:39
importing the os module this is already included with the standard python library all
you need to
3:01:44
do is import it and now we need a file to work with so let's right click on our projects
folder and go to new
3:01:51
file i will name this test.txt and we now have a file that we can work with but you
know what i would like to
3:01:58
delete this file because it's lame so let's do that in order to delete this file let's type os
3:02:03
dot remove and we're going to list the file path here since this file is within my project
3:02:09
folder i only need to list the file path test.txt if this was
3:02:14
someplace else on my computer i would probably need the file path like that but
since this file is within
3:02:19
my project folder i only need the file name another thing that you can do too at least
what i like to do
3:02:25
is that i like to pass in a variable called path i will make this a variable and assign it a
3:02:31
string value of whatever the path to my file is so i will assign path the name to my
3:02:37
file test.txt now when i run this this will delete my file test.txt and you can see that it's
no
3:02:44
longer there now what if we attempt to delete a file that doesn't exist well we'll
encounter an exception so if
3:02:51
you want this is optional we can do some exception handling so let's write the
remove function within a try block
3:02:58
and we will catch this exception file not found error and let's print something
3:03:04
print that file was not found now if you attempt to delete a file that
3:03:10
doesn't exist your program will not be interrupted and instead you'll print something
that file was not found now this
3:03:18
function does not remove empty folders this is how to do so and actually let's test
that so let's
3:03:23
create a new directory i'll call this empty underscore folder
3:03:29
now i will attempt to delete this empty folder empty folder
3:03:36
we encountered a permission error access is denied so let's create an accept block
for that
3:03:41
accept permission error print i don't know you do
3:03:48
not have permission to delete that right to delete
3:03:55
an empty folder there is a different function os dot rmdir
3:04:02
short for remove directory we're going to list the file path and pass that in as an
argument and
3:04:07
let's create an else block as well because we can else if there are no exceptions let's
print
3:04:14
path plus was deleted all right so rmdir
3:04:20
remove directory will delete an empty directory empty folder was deleted now this
3:04:27
function will not delete a folder that contains files and let's try that so let's create a
new directory i'll
3:04:34
just call this folder and within this folder i will create a new file
3:04:40
test.txt all right let's attempt to delete this folder i will probably need to change
3:04:46
the file name let's change that to folder and we encountered an
3:04:52
os error that directory is not empty so let's handle this exception we don't need to but
i like to
3:04:58
accept os air print you cannot delete that
3:05:06
using that function okay to delete a folder that contains
3:05:13
files we'll need the shuttle module import shuttle and the function that we're
3:05:19
looking for is shuttle dot rm tree short for remove tree and we will pass
3:05:26
in our path now be careful with this function it is considered dangerous because it
will delete a directory and
3:05:32
all files contained within so now we can delete a folder that contains files
3:05:38
folder was deleted in conclusion these are three basic functions to delete a file
3:05:44
or directory they are remove which deletes a file rmdir
3:05:49
which is an abbreviation for remove directory which will delete an empty directory
and
3:05:55
lastly rmtree which is an abbreviation for remove tree and this is of the shuttle
module and
3:06:01
this will delete a directory containing files so if you would like a copy of all this
3:06:06
code i will post this in the comments section down below but yeah that is how to
delete files and
3:06:12
or directories using python yo what's going on everybody it's you bro hope you're
doing
37.modules
3:06:18
well and in today's video i'm going to be discussing modules in python so sit back
relax and enjoy the show
3:06:27
all right well welcome back we're talking about modules today a module is a file
containing python
3:06:33
code it may contain functions classes etc it's used with modular programming which
3:06:39
is this concept of separating a program into useful different parts we're currently
working within our main module
3:06:46
and mine is named hello youtube this is how to create a separate module right click
on your project folder go to
3:06:52
new python file i think i'll create a module filled with maybe message functions
3:06:58
so i'll call this messages and click python file all right we have two tabs one for our
3:07:03
main module mine is called hello youtube and a separate module for messages and i
can write whatever i
3:07:09
want within here since we're using modular programming i'll place a bunch of useful
functions within this module
3:07:15
and i'll import them to the main module that we're currently working with let's define a
function called hello all
3:07:21
this will do is print hello have a nice
3:07:27
day and i'll create a second function for good measure let's call this pie
3:07:35
let's say bye have a wonderful time
3:07:42
now if we would like access to this module we need to import it i will head back to my
main module and
3:07:47
at the top write this import then the name of the module and for this example we
used the name of
3:07:54
messages for our separate module and to use a function from that module type the
name of the module dot and then
3:08:01
the name of the function so messages.hello will call the hello function found within
the messages module and i
3:08:08
can also call the buy function as well messages dot bye and that will print bye have a
wonderful
3:08:16
time no it can be somewhat tedious to have to write the name the module followed
by the function name what we could do to
3:08:22
shorten things up is to give our messages module an alias also known as a
nickname
3:08:27
so at the top write import messages as and what sort of nickname should we
3:08:32
give messages let's say msg for short so we'll replace messages with
3:08:38
msg msg hello and msg buy and this would work the same and it involves less typing
there's also an
3:08:45
additional way of importing a module and it goes a little something like this from the
name of the module import and then
3:08:52
list all of the functions or classes you would like to import i would like to import hello
comma buy
3:08:58
and we no longer need the module name before calling one of these functions i can
simply just call hello and vi
3:09:04
directly another way in which this could be written is from messages import asterisk
that means
3:09:09
to import all and i actually would not recommend using this if you're working on a
large program or something that
3:09:15
contains many modules because you can run into a naming conflict because some
of these modules may have
3:09:21
the same named function names or variables so if you're working on a small program
i really don't think it's a big deal but
3:09:27
if you have multiple modules i would actually refrain from using import alt it's much
safer to write the name of the
3:09:33
module followed by the name of the function that you want to use now another cool
thing is that python has a bunch of pre-written modules that
3:09:40
you have access to if you would like a comprehensive list of them type help and pass
in the word modules and within
3:09:46
my console window this will populate a listing of all the modules available to us here
they are let's see if math is in
3:09:53
here yep there it is so yeah feel free to take a look through some of these modules
3:09:58
because there are some fairly useful tools within some of these modules i suppose
you can also go to python's
3:10:03
official documentation and search for the python module index and here's a listing of
all the different modules too
3:10:09
they have access to well everyone those are modules they are files containing
python code and they
3:10:15
may contain functions classes etc if you would like a copy of all this code i will post
all of this to the
3:10:20
comments section down below but yeah those are modules in python i guess there's
a module called pickle
38.rock, paper, scissors game
3:10:26
hey what's going on everybody it's bro hope you're doing well and in this video we're
going to be creating a basic game of rock paper
3:10:33
scissors in python so sit back relax and enjoy the show
3:10:39
let's begin by importing the random module because we're going to need the
computer to pick a random choice and we'll create
3:10:46
a list of possible choices so we have three strings within here rock paper and
scissors
3:10:57
i think i spelled scissors wrong there we go okay and we will have our computer pick
3:11:04
a random dot choice from our list of choices and let's test it by printing
3:11:12
whatever the computer picks so the computer picked paper paper
3:11:19
scissors and scissors okay what about the player choice let's say player equals
3:11:27
input rock paper or
3:11:34
scissors and let's print what the player has
3:11:40
print player and let's print the computer too
3:11:47
maybe i'll add a string before these so let's say computer call in space
3:11:54
whatever the computer chooses and same thing with the player player all right
3:12:02
let's see if this works rock paper or scissors let's say rock the computer picks paper
and i pick rock
3:12:10
so i would technically lose this round now what if the player doesn't pick rock paper
or scissors what if they type in
3:12:16
gun so we would like some way to prevent that so let's put our input for our player
3:12:22
within a while loop while player
3:12:27
not in choices so we're going to need to initialize
3:12:34
player because right now it's an unresolved reference so let's say player equals none
3:12:42
for the time being okay now let's test our choices our player choices rock paper or
3:12:48
scissors i pick the gun and it's going to keep on asking me until i pick something from
my list of choices so
3:12:56
if i type in that still won't accept it now let's type in rock again there we go now what if
3:13:02
somebody types in one of these choices either all caps or capitalized well that
technically
3:13:07
wouldn't be in choices because these strings are case sensitive so why don't we take
our user input and make it
3:13:14
lowercase and i'll just add on dot lower and use the lower method so therefore we
can now type in any
3:13:22
input that is either all caps or uppercase and then that will still be accepted because
we're going to take our player
3:13:28
input and then make it lower case so that it matches one of these choices okay let's
work on the win conditions
3:13:34
next so first let's check to see if we encountered a tie if player is equal to computer
that
3:13:42
means that we both chose the same thing so let's print whatever the computer
picked
3:13:48
what we picked and we will print tie now let's write an else
3:13:56
if statement e l i f player equals
3:14:03
rock so if we pick rock and then we will create a nested if statement if computer
3:14:11
equals paper that means we lose so let's print
3:14:18
everything we have here except we're going to change tie to you
3:14:25
lose now if the computer picks let's say scissors then we win
3:14:33
so scissors and you win okay then let's just repeat this
3:14:40
process for scissors and paper
3:14:45
so this will be another else if statement if we pick
3:14:53
scissors and the computer picks rock then we lose if the computer picks
3:15:00
paper then you win and lastly we have paper as the player
3:15:07
choice
3:15:12
so else if player equals paper and the computer picks scissors then we
3:15:20
lose if the computer picks rock well then paper covers rock and we win now let's test
it
3:15:29
rock paper or scissors again i'll try and pick the gun which i can't let's take this
seriously
3:15:35
i will pick rock the computer picked rock so it's a tie i'm feeling like paper today oh it's
3:15:43
another tie i guess and how about scissors okay so i win this round i picked uh
scissors
3:15:50
and the computer picked paper therefore i win let's try and lose this time
3:15:55
just to verify it so i won again let's try rock again okay so i lost that round so we know
3:16:02
that the win conditions are working now how about we add a feature where we will
ask the player if they would like to
3:16:08
play again play another round i think the best way to do that would be to write all of
this code within a while loop so
3:16:14
at the top let's write while true and we'll need to indent everything so that it's within
3:16:20
the while loop so let's just indent everything here
3:16:39
now at the bottom we will ask the user if they would like to play again and i will store
this within a variable
3:16:45
named play again play again equals input and the prompt will be play again
3:16:54
yes slash no and depending on their input let's just make it all lower case
3:17:02
if play again does not equal yes
3:17:09
that means they would like to quit so let's use a break statement to break out of our
while loop that we're in
3:17:17
and at the end let's print by and pay attention to the indentation
3:17:23
this input and this if statement should be within the while loop so they should have at
least one indentation and our print statement
3:17:30
here we'll just say bye and you can see that there is no indentation so that is outside
of the while loop
3:17:36
okay let's test this one last time rock paper scissors i'm going to pick
3:17:42
the cannon okay that's not valid input how about rock
3:17:47
okay the computer picks rock i pick rock so it's a tie do i want to play again i'm going
to select actually yes all
3:17:54
caps that should still be acceptable elbow paper this time
3:17:59
it's a tie again man the computer is really good at this game i'd like to play again i'll
pick scissors
3:18:06
okay so i lose i would like to play again and i want to win this time
3:18:11
okay computer pick scissors i pick rock you win do you want to play again nah and it
will print
3:18:19
bye well everybody that's a simple game of rock paper scissors i'll post all of
3:18:24
this code to the comment section down below and well yeah that's a basic game of
rock paper scissors in python
39.quiz game
3:18:32
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to show you guys how we can build a basic quiz game
3:18:39
in python so sit back relax and enjoy the show
3:18:45
to begin this project i think it would be wise for us to create a skeletal structure for
how this program is going
3:18:50
to operate and then we'll fill in the gaps later as we go along so to begin let's define
all of the functions that
3:18:56
we'll need we'll need four we'll create a function that is named new game whenever
we call this function it will
3:19:02
create a new quiz game for us and for the time being let's write pass just as a
placeholder
3:19:08
we'll have a total of four functions new game a function to check our answer
3:19:16
a function to display our score
3:19:21
and lastly a function to play again
3:19:30
all right now i'm just going to add some comments here just to separate each of
these functions
3:19:36
that we have just so that it's a little bit easier to read although this part is not really
necessary so i just want to separate
3:19:42
each of these functions for some readability okay those are all the functions that
3:19:49
we'll need what we'll need now is some sort of collection to hold all of the questions
and answers that we
3:19:55
have and i think a dictionary would be perfect for this so what i did is that i created a
dictionary named questions
3:20:02
a dictionary has key value pairs each key is a question that i would like
3:20:07
to ask and each question has an associated value we'll have the user guess
between
3:20:12
answers of a b c or d so these would be all of the correct answers all of the
3:20:17
values within this dictionary so here are some of the questions that i want to ask feel
free to come up with your own questions
3:20:23
if you would like you can copy the questions that i have they should be posted in the
comment section down below so the questions that i'm
3:20:30
going to ask are who created python what year was python created python is
attributed to which comedy
3:20:36
group and lastly is the earth round i couldn't think of a fourth question so i just threw
in a random stupid question
3:20:43
and the correct answers to all of these questions in order would be a b c and a but
feel free to come up
3:20:50
with your own well we have our questions but we'll need some sort of collection to
hold all of the different possible answers
3:20:57
to each of these questions and i think a 2d list would work perfect for this
3:21:02
so i have a list of lists a list of tuples could work too i suppose so here are all of the
answers for the
3:21:09
first question and the correct answer is a i believe his name is guido van rossum hey
if you're listening
3:21:16
man i'm sorry if i'm mispronouncing your name i'm really trying here the second list
corresponds to the
3:21:21
second question what year was python created it was created in 1991 at least
according to wikipedia python
3:21:29
is attributed to which comedy group the answer is c monty python like monty
3:21:34
python and the holy grail good movie by the way and lastly is the earth round this is
a highly debated topic but the
3:21:41
answer is hey the earth is in fact round so i have a list of lists
3:21:47
each list corresponds to a key value pair within my dictionary of questions
3:21:52
and this is a lot to type if you want you can just copy the code that i posted in the
comments down below
3:21:57
just copy and paste and you'll have all of this or if you want you can pause the video
and type it i don't care so now that you have your
3:22:04
questions and your options we're ready to begin so the first thing that we'll do within
our program is to call the
3:22:09
new game function to begin a new game so after we have our questions and options
created let's create a
3:22:16
new game by calling the new game function now when we run this program we'll
3:22:22
generate our dictionary of questions and our 2d list of different options for each
question
3:22:28
and then we will call the new game function to begin a new game so let's head to the
new game function
3:22:33
and fill in everything within our new game function so at the top let's declare a few
things
3:22:39
let's say we have a list named guesses and guesses will be an empty list for now
3:22:45
i will declare a variable called correct guesses set it equal to zero because we
3:22:51
haven't guessed anything yet and we will set a current question number
3:22:56
and set the sequel to one to represent the first question okay now we need to display
all of the
3:23:03
questions within our dictionary of questions and we can use a for loop for that so
3:23:09
for key in questions
3:23:14
i'm going to print my key
3:23:20
and let's just test this okay yeah here's all my questions i think what i'll do is actually
print
3:23:28
something to separate each question let's say one of these lines i think it
3:23:34
would look better with them okay i'll print this line and then we'll move on to a
question
3:23:40
all right now after re-running this we have each question and i added just a line break
between each of these to make
3:23:46
it look kind of fancy now i need to display all of the different options for each question
3:23:51
and we can do that with a nested for loop so if i were to write i in
3:23:59
options print i let me show you what happens now with
3:24:05
the way this for loop is written now it's going to display all of the different options
available to us for the entire quiz game
3:24:12
what i would like instead is to display only the first list for the first question and then
the
3:24:18
second list for the second question and follow that pattern so we're going to change
our for
3:24:23
loop around 4i in options and we'll set an index for options the index is going to be
our question
3:24:30
number minus one now remember what i said in previous videos where different
3:24:36
collections including lists tuples etc the first element in a collection
3:24:41
has an index of zero because computers always start with zero then the next
element would therefore
3:24:46
have an index of one then two three then you follow that pattern we're using this
question number
3:24:52
variable as some sort of counter and since we initially set this to one i'm just going to
subtract one so that
3:24:59
we effectively receive zero as the index and now we just need to increment our
question
3:25:04
number after each iteration so let's do that i will add that to the end here after we
3:25:11
finish displaying all of the options let's increment question number by one
3:25:16
by typing question num plus equals one and if i were to run this currently
3:25:23
we'll display all of the different options for each question who created python here's
all of the
3:25:28
associated options what year is python created python is attributed to which comedy
group
3:25:34
and lastly is the earth round now it's time for some user input so i'm going to create a
variable called
3:25:41
guess and make sure you don't put it within your inner for loop it should be within the
outer for loop
3:25:48
guess equals input and i'll create a prompt
3:25:53
enter a b c or d
3:26:00
here's something to consider we would like the user to type in either capital a b c or d
with strings they're case
3:26:07
sensitive what if the user typed in one of these letters but lowercase if they're correct
we would still like to
3:26:13
give them their point why don't we take our guess and make it uppercase so guess
equals guess and we can make
3:26:20
this uppercase using the upper method of strings at the end of this game i'm going to
compare
3:26:26
our guesses to the correct answers so we have an empty list named guesses and i'm
going to
3:26:33
append our current guest that we're working on to our list of guesses so guesses
3:26:39
dot append and we will append our guess this round now that we have our
3:26:45
guess and we've appended our guess to our list of guesses let's check to see if it's
the correct
3:26:51
answer or not so we're just about to fill in this check answer function next
3:26:57
so we are going to call this function and pass in a few items as arguments so let's
use the check answer function
3:27:07
and we'll pass in the key for the current question that we're on so the key is the
correct answer so that
3:27:14
would be our questions dot get key
3:27:23
this would be the answer and we'll also pass in our guess and we are going to fill in
3:27:30
the check answer function but we need to set up the parameters we're receiving
effectively our answer
3:27:37
as well as our guess so i'm going to name these as answer and guess and we are
going to
3:27:45
check to see if our answer is equal to our guess if answer is equal
3:27:53
to guess let's print something print correct
3:28:03
and i think we should give the user a point we'll have our check answer function
return
3:28:08
one for one point return one else if this is not the correct answer
3:28:15
let's print wrong and we will return
3:28:20
zero they do not get a point and since this is returning a value we
3:28:26
should assign that so let's assign the point we may or may not receive to our variable
of correct guesses which
3:28:33
is initially set to zero so we'll type correct guesses plus
3:28:38
equals check answer and check answer will return one if we scored a point or zero if
we
3:28:44
did not score a point and make sure you have plus equals because if you just set this
to equals
3:28:50
then we cannot score more than one point it will either be zero or one so we are
effectively
3:28:56
incrementing our correct guesses by one for each point that we score and now we're
going to work on the
3:29:03
display score function and we'll call that at the end after we finish all of our questions
3:29:08
that we have so make sure that you do not write this within the for loop it should be
outside of it
3:29:13
because once we finish iterating through all of our questions we're going to display a
final score
3:29:19
so let's call the display score function and we'll need to pass in some
3:29:25
arguments our correct guesses as well as our list of guesses
3:29:33
okay let's head to the display score function so we have as parameters
3:29:40
correct guesses as well as
3:29:45
guesses i need to remove this pass i'm going to add one of these fancy lines in just
to
3:29:51
separate the questions from the results and i will print
3:29:56
results and maybe another one of these lines
3:30:02
okay so i need to print all of the answers
3:30:08
answers and i do not want to end on a new line
3:30:14
so i'm going to set end equal to nothing and i need to display all of the values within
3:30:22
our dictionary all of the answers i will do that using a for loop for i
3:30:29
in questions print questions
3:30:37
dot get i and i do not want to end on a new line
3:30:44
so i'm going to set end equal to nothing effectively and then i'll print a new line okay
3:30:50
let's work on the guesses i think i'm just going to copy all this and make some
changes i'll replace
3:30:56
answers with guesses for i in guesses
3:31:04
print i okay let's just be sure that
3:31:09
everything's working we're not calculating a score quite yet so i'm just going to
answer a
3:31:16
then b c and d on second thought after printing each of
3:31:22
these answers and guesses i'm going to add a space after each of these so within
each of these four loops at the
3:31:28
end i will add a space let me try that again a b
3:31:33
c d okay not too bad now let's calculate the final score and
3:31:39
we're still within our display score function let's set score equal to
3:31:46
correct guesses divided by
3:31:53
the length of our questions
3:31:58
and let me add a set of parentheses around here then i'm going to multiply our score
by
3:32:06
100 and if you don't want a decimal portion
3:32:12
because we'll display a percentage we can cast this as an end okay that all looks
good and we'll print
3:32:19
the final score print your score
3:32:25
is plus we need to cast our score to a string
3:32:31
because we're using string concatenation plus i'll add a percent sign
3:32:38
all right let's try it i'm intentionally going to get the last question wrong so we should
have a 75 if we have four
3:32:45
questions so that would be a b c is the earth round what's earth i'm
3:32:52
gonna guess d answers a b c a your guess is a b c
3:32:57
d your score is 75 okay let's test it but getting all wrong answers d d d
3:33:04
d your score is zero percent congratulations and we'll test it one more time but get
3:33:10
all the right answers we just want to be sure that we have 100 that's a
3:33:15
b c a your score is 100 now the last thing that you can add is
3:33:22
that we can play again if we want and i have a separate function for that if you would
like to add this option
3:33:27
here's how to do so let's remove that pass let's set a variable named response
3:33:34
equal to some input do you
3:33:40
want to play again
3:33:45
let's ask for yes or no actually let me remove that okay
3:33:55
and depending on the user's response it might be a lowercase so let's make it
uppercase response equals response
3:34:02
and use the upper method if response is equal to
3:34:10
yes all caps if response is equal to yes then return true
3:34:18
else we will return false and that is it for this function
3:34:25
the last thing we need to do is to create a while loop that will continue to ask the
player if they want
3:34:30
to play again this will be after we call the new game function while play again
3:34:38
and remember this will return either true or false depending on the user's response
3:34:44
while play again new game and call the new game function to create
3:34:50
a new game for us if we escape the while loop that means the user doesn't want to
play again
3:34:56
so let's print the message such as by e
3:35:02
all right let's test it i'm going to get all of the correct answers this time a b
3:35:10
c a all right your score is 100 do you want to play again i'm going to
3:35:16
type yes all right then we can play again
3:35:22
d d d d your score is zero percent do you want to play again
3:35:27
no i think i mastered this game bye all right everybody so that's how
3:35:33
you can use python to create a simple quiz game i will post all of this code to the
3:35:38
comment section down below but well yeah that's a basic quiz game in python for
you all
40.Object Oriented Programming (OOP)
3:35:46
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to explain
3:35:51
object-oriented programming in python so sit back relax and enjoy the show
3:35:58
welcome to today's topic on python object oriented programming also known as pop
and in today's video
3:36:06
we're going to be creating objects an object is an instance of a class by using
programming we can create
3:36:12
representations of real-life objects so look around you wherever you're sitting or
standing right now you are surrounded
3:36:19
by objects next to me i have a phone a television some snacks and i'm talking
3:36:24
into a microphone point being we can use programming to mimic real world objects
by assigning a
3:36:30
combination of attributes what an object is or has and methods
3:36:35
what an object can do now in order to create an object we'll need to create a class a
class can function as a blueprint that
3:36:41
will describe what attributes and methods that our distinct type of object will have you
can either create your class
3:36:48
within your main module or you could create a separate file dedicated solely for your
class
3:36:53
now to create a class we would type class and then the name of the object that we
would like to create so today i
3:36:58
would like to create some car objects because i like cars so we would type car and a
common naming
3:37:04
convention with class names is that they should be capital so car is going to have a
capital c
3:37:10
and for the time being we need to type in something i'll just type in pass as a
placeholder so if you have a small program it may be
3:37:16
better to write your class within your main module but if your class is fairly large
3:37:22
you may want to consider placing your class within a separate module so if you were
to take that route we
3:37:28
would go to file new python file and we would name this car click python file and we
would declare
3:37:35
our class within the separate module so class car and for now i'll type in pass
3:37:40
then we just need to import this class so within our main module we would type from
the name of the module import the
3:37:47
name of the class from car import car now objects can have some combination of
attributes and
3:37:53
methods attributes describe what an object is or has so what are a few attributes that
cars
3:37:58
might have they might have a make so let's create a few variables for the time being
i'm just going to use
3:38:04
none as a placeholder cars can have a model a year
3:38:10
and a color now objects can also have methods what
3:38:16
kinds of methods could cars perform perhaps a drive method and a stop method so
let's define those
3:38:22
def let's say drive and we'll have one argument self self refers to the object that is
3:38:28
using this method now what do we want to do when we call this method let's say this
car
3:38:36
is driving let's create a stop method as well def stop
3:38:43
and we will print this car is stopped we now have all of the
3:38:49
different attributes and methods we would like our car objects to have but there is
one more thing that we need
3:38:54
it is a special method called the init method that will construct objects for us in other
programming
3:39:00
languages this is known as the constructor so we need a special method that will
create objects for us
3:39:06
so we need to define this method def and the syntax on this is somewhat strange it is
3:39:11
two underscores init it's short for initialize 200 scores again and then we need at
3:39:16
least self as an argument and then we can actually assign our car objects unique
variables so take all of
3:39:24
these attributes that we have and we're going to place them within this init method
now we can receive
3:39:30
arguments when we create car objects but we need to pass them in as arguments to
our init method so we
3:39:36
need to set up some parameters let's say in order to create a car object we need a
make
3:39:42
a model a year and a color then when we receive these arguments we can actually
assign them to
3:39:49
each car's specific attributes but we need to precede each of these with self self is
referring to the object that
3:39:56
we're currently working on or creating so self.make self.model
3:40:01
self.ear and self.color and then when we assign these we're
3:40:07
going to say self.make equals whatever make that we receive when it's passed in as
an argument
3:40:12
self.model equals model and continue that pattern for year and color all right that is
all we need
3:40:20
for our class car we have a constructor we're assigning arguments that we receive to
the
3:40:26
attributes of our car object and we also have two methods one for drive and one for
stop
3:40:32
now we can start creating some car objects so going back to my main file here to
create an object we need a unique
3:40:38
name for it let's call this first car just car one equals the name of the class car then a
3:40:44
set of parentheses now in order to construct a car object we need to pass in a
matching
3:40:50
set of arguments you can see here that we need to pass in a make a model a year
and a color so what kind of car should we make let's
3:40:57
say that the make is going to be chevy we need a model
3:41:03
perhaps corvette a year 2021 is good and a color let's say blue
3:41:10
alright now let's actually access some of these cars attributes and i should probably
put this one in a
3:41:16
print statement so print car ones make and this should print chevy
3:41:23
then we can do the same thing for model year and color so let's change some of
these around
3:41:29
model year and color
3:41:35
yep this object is a chevy corvette 2021 and the color is blue and we should have
two methods as well
3:41:41
so car one i would like this car to use its drive method this car is driving and let's
have this
3:41:48
car use it stop method car one dot stop
3:41:55
this car is stopped there's one thing that i should mention real quick because i forgot
to mention it earlier
3:42:00
so within our init method you can see that we need five arguments in order to
construct a car
3:42:05
object self make model year in color but when we pass in our arguments we're not
passing in
3:42:12
anything for self we only have four arguments here that's because with python we do
not need to pass itself
3:42:18
that's done automatically for us we're referring to the object that we're dealing with as
you can see with our drive and stop
3:42:25
method we need to pass in self in order to execute our method but when we call this
method we do not
3:42:31
need to do so so just remember with python with self we do not need to pass in
anything for this argument that is
3:42:38
all now the nice thing about this is that we can reuse this class as a blueprint to
create more car
3:42:43
objects we just call that init method that constructor so this time let's create a second
car called
3:42:49
car 2 and this will be a different kind of car what kinds of arguments should we pass
3:42:55
it let's say this is a ford mustang the year will be 2022 and the
3:43:01
color will be red all right let's check card to use attributes
3:43:08
and let's have car 2 use its drive and stop method this is a ford mustang the year is
2022
3:43:15
the color is red this car is driving and this car is stopped one other thing that we can
do too is that within our methods here let's
3:43:22
replace car with the name of the model that we're working with so this
3:43:27
plus self dot model plus is driving now this self keyword
3:43:34
think of it as you're replacing self with the name of the object that we're working on if
car one
3:43:41
is using its drive method replace itself with car one if this is car two that called this
3:43:47
method then replace itself with car 2. think of it that way and let's do the same thing
for stop
3:43:54
this plus self dot model plus is stopped all right
3:44:01
probably don't need these anymore so let's have car one use its drive and stop
method this
3:44:07
corvette is driving this corvette is stopped let's try this with car two this mustang
3:44:13
is driving this mustang is stopped now you can have them do things independently
too let's have car 1 use its drive method and car
3:44:20
2 uses stop method this corvette is driving and this mustang is stopped in conclusion
3:44:26
a class can function as a blueprint to create objects we can assign attributes what
describe
3:44:32
an object is or has and methods what each object can do and then within our class
we have
3:44:38
a special method called the init method we can pass in some arguments and assign
3:44:44
these arguments to each object's attributes and then we can reuse this class as if it
was a
3:44:49
blueprint so we can create more objects out of it so that is the basics of object
oriented
3:44:56
programming in python if you would like a copy of all this code i will post this in the
comment section down below
3:45:01
but yeah that's the basics of object oriented programming in python hey what's going
on everybody
41.class variables
3:45:08
it's bro hope you're doing well and in this video i'm going to explain the basics of
class variables in python
3:45:14
so sit back relax and enjoy the show
3:45:19
here's a super quick video on the differences between class and instance variables
for this example let's say we have two
3:45:26
car objects named car one and car two and i'm just passing in a bunch of values to
each of
3:45:31
these car constructors now within my class car i have my init method also known as
the constructor and what
3:45:38
i'm doing is assigning all of these values to the variables found within my car
constructor
3:45:44
these variables declared inside the constructor are known as instance variables and
each
3:45:50
object can have their own unique values assigned to each of these variables now
where a class
3:45:56
variable is different is that a class variable is declared inside the class but outside of
the
3:46:02
constructor and what we can do is set some default values for some variables let's
say we would like to add an
3:46:08
additional variable let's say wheels wheels equals and we can set a default value
3:46:14
for all instances of this class for each car object that we create so we set wheels to
equal four this is
3:46:20
an example of a class variable whenever we create a car object we can pass in a
unique
3:46:26
make model year in color but by default they will all have four wheels now if i was to
print the
3:46:33
amount of wheels that car 1 and car 2 have they will both have four wheels it's as if
we're setting a
3:46:40
default value if i change this to two wheels well then these cars have two wheels
maybe their motorcycles or something now
3:46:47
you can actually change these values each object will have their own copy of this
variable but you can set it
3:46:53
to a default value so let's say car one is a motorcycle car one dot wheels
3:47:01
equals two so if i were to print car one's wheels along with card two car one will have
two wheels but car two
3:47:09
is still using that default amount of wheels which is four now there's another way in
which you can access a class variable
3:47:16
you don't necessarily need to create an object to do so you can just use the name of
the class so what i'm going to do is turn all of
3:47:23
these lines into comments and i will print the amount of wheels
3:47:28
that our class has so type in the name of the class car with a capital c
3:47:33
it should be the same name as your class name followed by the name of the class
variable car dot wheels but make sure you spell
3:47:41
it right all right so this will display four wheels now what if we decide to change the
3:47:47
class variable through our class well that will affect all instances of our class so let's
say car
3:47:55
dot wheels equals two it's as if we change our mind and we have decided to
3:48:01
create motorcycles instead of cars well if we were to change the wheels
3:48:06
class variable through the use of our car class well then it's going to affect it for all
instances of this class
3:48:13
you can see that both car 1 and car 2 now have two wheels when we just changed
car 1's wheels it
3:48:19
did not affect car 2 at all so that's the main difference between a class variable and
an instance variable
3:48:25
an instance variable is declared inside of constructor and they can be given unique
values with
3:48:31
class variables they are declared within a class but outside of the constructor and
you can set a default value for all
3:48:38
instances of this class for all unique objects that are created and then you can
change those values
3:48:43
later too so if you would like a copy of all this code i will post all of this in the
comment section down below
3:48:49
but yeah that is the basics of class variables in python hey what's going on
everybody
42.inheritance
3:48:56
it's you bro hope you're doing well and in this video i'm going to explain inheritance in
python so sit back relax and enjoy the show
3:49:06
let's talk about irritants the term inherit means to receive derive or be left with and we
can apply this
3:49:11
concept to programming classes can inherit something usually attributes and
methods from another class these classes can form
3:49:18
parent-child relationships where a child will receive everything that the parent class
has much like you
3:49:24
inherit jeans from your parents and classes can have children and give whatever
they own to their children
3:49:30
and in today's video we'll be creating a parent class called animal and children of the
animal class will inherit the common attributes and
3:49:37
methods that all animals might have to kick off this video i think what we'll do is keep
all of our classes within the
3:49:42
same file just so that it's easier for us to read and understand let's begin by creating
a class called animal so at the top
3:49:49
type class animal and what are some attributes and methods that all animals should
have let's say we have a class variable
3:49:55
called alive and we'll set this to true if you're an animal you begin by being alive and
what are some methods
3:50:02
how about an eat and sleep method so let's define those def eat what are we gonna
do when we
3:50:08
call this method let's print something let's print this animal is eating let's also create a
sleep
3:50:15
method def sleep what are we gonna do when we call this method let's print
something as well
3:50:20
this animal is sleeping and we now have our animal class now let's create separate
classes for
3:50:27
specific types of animals let's say a class for rabbit a fish and a hawk so
3:50:32
we can make objects of those classes so let's begin with a rabbit class class rabbit
now to use
3:50:40
inheritance with the class that you intend to be the child class after the class name
add a
3:50:45
set of parentheses and then pass in the name of the parent class in this case it's
going to be animal so we will add that to the parentheses
3:50:53
so now rabbit is the child class and animal is the parent class so the child class is
going to inherit
3:51:00
everything that the animal class has so for the time being i'm going to type pass so
this rabbit class
3:51:05
will have access to a class variable called alive and an eat and sleep method so let's
3:51:11
define a fish and hawk class and i'm just going to copy what i have here and just
change
3:51:16
rabbit to fish and then do so again and change rabbit to hawk this is our family tree
animal
3:51:24
is the parent class and it has three children a rabbit class a fish class and a hot class
and for the
3:51:29
time being we're just going to write pass as a placeholder so i bet we can create
objects from
3:51:35
these classes so let's do so let's say we have an object called rabbit rabbit equals
rabbit let's do the same
3:51:41
thing for fish and hawk
3:51:47
all right we now have three objects rabbit fish and hawk and let's take a look at
3:51:53
their class variables i bet they have an alive variable let's check that so i would like to
print
3:51:59
rabbit dot oh would you look at that this rabbit class has a class variable called alive
even
3:52:05
though there's nothing within this class well that's because we used inheritance so
each of these three children classes inherit
3:52:12
everything from their parents all of these attributes and methods i bet they have
eaten sleep functions as
3:52:18
well so let's check that with fish let's have fish use its eat method
3:52:24
and hawk will use its sleep method hawk dot sleep
3:52:29
so the rabbit is alive the fish is eating and the hawk is sleeping now even though we
3:52:34
have nothing written within each of these specific types of animal classes all of these
animals have access to these
3:52:41
attributes and methods because of inheritance and another benefit of inheritance is
3:52:46
that we don't need to keep on copying and pasting this code for example let's say we
were not using
3:52:51
inheritance so we would not need an animal class we could write our code this way
where
3:52:56
we would just copy and paste everything underneath each of our classes so what if
we need to make a change to one of
3:53:03
these methods as in your boss says you know what let's change sleep to slumber
okay well we would have to go to
3:53:10
each of these classes and make the change manually it's not too bad if you have like
three classes but what if you have hundreds
3:53:16
that's going to take a lot of effort so wouldn't it be nice if we just list everything that
each of these
3:53:21
classes has in common and then have all of these classes inherit from one common
class and then if you need to
3:53:28
make any changes you can do so just within this class and this change will apply to
all of the different child classes so
3:53:35
that's another benefit of inheritance and not only does it make any changes easier
but each class
3:53:40
can have their own unique attributes and methods as well along with the attributes
and methods that they inherit from their parents
3:53:47
so for rabbit fish and hawk let's have each of these classes have their own unique
method as well so for
3:53:53
rabbit let's define a run method if you're a rabbit then you can run so let's print this
rabbit
3:54:01
is running and i'm just going to fix some of the spacing here all right if you're a fish
then you can
3:54:08
swim def swim let's print
3:54:13
this fish is swimming and lastly we have hawk if you're a hawk
3:54:21
then you can fly def fly let's print this hawk
3:54:28
is flying like i said before not only do these specific types of animals have
3:54:35
access to all of the attributes and methods that they inherit from their parents but
they can have their own attributes
3:54:40
and methods as well so let's test some of these methods so we have our rabbit fish
and hawk
3:54:46
objects let's have rabbit use its run method fish we'll use its swim method and hawk
3:54:54
will use its fly method and here we go this rabbit is running
3:55:02
this fish is swimming and this hawk is flying so in conclusion classes can have
children the children
3:55:09
classes will inherit everything that their parent class has all of the parents attributes
and
3:55:14
methods but not only that the children classes can implement their own unique
attributes and methods
3:55:19
as well so that is inheritance if you would like a copy of this code i will post all of this
in the comment
3:55:25
section down below but yeah that's the basics of inheritance using python
43.multilevel inheritance
3:55:31
yo what's going on everybody it's you bro hope you're doing well and in this video i'm
going to explain
3:55:36
how multi-level inheritance works in python so sit back relax and enjoy the show
3:55:43
all right welcome back everybody let's talk about multi-level inheritance this is a
concept where a derived class
3:55:50
also known as a child class inherits from another derived class here's an example
let's say
3:55:56
we have a hierarchy of classes there will be three levels to this we'll create a family
tree of living
3:56:02
organisms so at the very top we're going to have a class called organism any class
that inherits from
3:56:09
the organism parent class will receive one attribute one variable named alive
3:56:14
and this will be set to true so if you are an organism you begin by being alive now
let's say we have a child class that
3:56:21
will inherit from the organism parent class let's say animal class animal
3:56:27
inherits from organism so any animal that we create will have this alive attribute set
the
3:56:33
true and all animals eat so let's define a function that does that
3:56:39
at least i believe all animals eat let's pretend that they do so this animal is eating
3:56:46
and now why stop there let's create a class for a specific type of animal i like dogs so
let's create a dog class
3:56:53
class dog inherits from the animal class and all dogs should be able to bark so
3:56:59
let's create a function to do that and we'll print this dog
3:57:07
is barking okay so what we just did here is multi-level inheritance we had a derived
3:57:15
child class inherit from another child class so it's kind of like this dog class is
3:57:20
the child animal class is the parent and then the parent of the parent class would be
like
3:57:25
a grandparent right and then if you added another layer on top of that to this
hierarchy well then the parent of the grandparent
3:57:31
would be like a great grandparent that's how i think of it at least now let's create a
dog object just to verify
3:57:37
that everything's working so let's call our dog dog and our dog object should have
3:57:42
access to an attribute named alive and our dog object receives that attribute from the
organism class
3:57:49
and this will print true if i were to print whatever this attribute contains and our dog
should be able to eat
3:57:55
because it receives this method from the animal parent class and lastly our dog
should be able to
3:58:02
bark and that method is defined within its own class
3:58:07
so that's how multi-level inheritance works it's a concept where a child class will
inherit from another child class
3:58:14
i like to think of it like a family tree where a child class will have a parent
3:58:20
and the parent of that parent would be like a grandparent basically so if you would
like a copy of this code
3:58:26
i will post all of this to the comment section down below but yeah that is how multi-
level inheritance works in python
44.multiple inheritance 👨👩👧
3:58:33
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to show you all how multiple inheritance works in python so
3:58:40
sit back relax and enjoy the show
3:58:45
all right people multiple inheritance multiple inheritance is the concept where a child
class
3:58:52
is derived from more than one parent class here's an example let's say that all of
3:58:57
our children classes are going to be certain types of animals and we would like our
animal classes
3:59:03
to inherit from either the prepared class the predator parrot class or both
3:59:08
depending on what type of animal it is because some animals in the animal kingdom
can be both prey
3:59:14
and predators and one example that comes to mind would be fish fish will eat
smaller fish but also can
3:59:20
get eaten by larger fish so here we have two classes prey and predator each has a
dedicated method if a class
3:59:27
inherits from the prey parent class they will have access to a flea method which will
print this animal please if a
3:59:35
class inherits from the predator parent class that class will have access to a hunt
method that will print
3:59:41
this animal is hunting and it's possible that some animals can do both they can both
flee and hunt but they'll
3:59:47
need to inherit both of these classes both prey and predator and that's where
multiple inheritance comes in
3:59:53
now let's create some classes based on certain types of animals let's say we have
class rabbit and
3:59:59
rabbit we'll inherit from the prey class because rabbits are typically prey and not
predators but i don't know there could
4:00:05
be some killer rabbits out there haven't run into any yet let's have class hawk inherit
from the
4:00:12
predator class and lastly fish and fish can be both prey and predators
4:00:18
so they will inherit from both classes and we just separate each class with a comma
so prey
4:00:24
comma predator and it's as simple as that now let's create an object from each of
4:00:29
these classes we have rabbit equals rabbit hawk
4:00:36
equals hawk and fish equals fish now let's just verify
4:00:42
all of the different methods that each of these objects inherited so our rabbit should
have access to a
4:00:48
flea method but no hunt method and let's just test that yep this animal flees and my
hawk object
4:00:57
should have access to a hunt method but no flea method this animal is hunting and
lastly
4:01:04
my fish object has access to both a flea and a hunt method and if you take a look
4:01:10
on the right hand side of this pop-up window it shows the class that it inherited this
method from
4:01:15
so fleet comes from the parent class of prey and hunt comes from the apparent class
of predator
4:01:21
so my fish can both flee and hunt because it's typically considered both
4:01:26
prey and predators this animal flees this animal is hunting
4:01:31
so that's basically what multiple inheritance is it's a concept where a child class is
4:01:37
derived from more than one parent class so that's basically multiple inheritance i'll
post all of this code to the
4:01:43
comment section down below and well yeah that's how multiple inheritance works in
python hey what's going on everybody
45.method overriding
4:01:51
it's bro hope you're doing well and in this video i'm going to teach you guys about
method overriding in python
4:01:56
so sit back relax and enjoy the show
4:02:01
here's a super quick video on method overriding i have two classes a class called
animal and a class called rabbit
4:02:08
rabbit inherits from the animal class therefore rabbit is the child class an animal is
the parent class within the
4:02:13
animal class there is one method a method called eat and when you call this method
all this
4:02:18
will do is print this animal is eating the rabbit class inherits from the animal class
therefore
4:02:24
rabbit has access to this eat method and if i were to create a rabbit object and this
rabbit used its eat method well
4:02:31
then this is going to print this animal is eating now method of writing is the ability of
an object
4:02:37
oriented programming language to allow a subclass also known as a child class
4:02:42
to provide a specific implementation of a method that is already provided by one of
its
4:02:48
parents in this case we're going to override the eat method and what we can do is
provide a more specific
4:02:54
implementation for the rabbit class and now to override a method what we'll do is
that within
4:02:59
the child class we need to define a method with the same matching method
signature that is the
4:03:05
combination of a method's name plus its parameters both of these together are
known as a
4:03:10
method signature within the child class we will define and eat method with the same
parameters
4:03:16
def eat and in this case the only parameter is self just to keep this simple
4:03:21
and what we'll do is that will provide a more specific implementation of the eat
method
4:03:26
specifically for rabbits so let's print something more closely associated with rabbits
such as
4:03:32
this rabbit is eating a carrot now if i were to run this program
4:03:37
again we will instead use this implementation of the method instead of the one that it
4:03:42
inherits from its parent class of animal basically speaking an object will use a method
that is more closely
4:03:49
associated with itself first before relying on a method that it may inherit from a parent
class
4:03:55
in this example we're using this version of the eat method specifically for rabbits
instead of the
4:04:00
version that we inherit from the parent class of animal that is the basics of method
overriding i will post this code in the
4:04:07
comment section down below and well yeah that's how to override a method using
python
46.method chaining ⛓️
4:04:14
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to teach you guys all about method chaining in python
4:04:21
so sit back relax and enjoy the show
4:04:26
all right everybody let's talk about method training method chaining is used to call
multiple methods sequentially and each
4:04:33
call performs an action on the same object and return self here's an example of how
method chaining would be useful
4:04:40
let's say we have a class named car car has four methods turn on drive break and
4:04:46
turn off they will all just print a simple message beginning with turn on turn on what
print you start the engine
4:04:53
drive will print you drive the car brake will print you step on the brakes and turn off
will print
4:04:58
you turn off the engine let's say we have a car object car equals car and i would like
my car
4:05:04
object to use its turn on method immediately followed by the drive method with how
we've been
4:05:10
coding things previously we may write something such as the name of the object in
this case car
4:05:15
dot and then the method to use i would like my card to use the turn on method
followed by
4:05:21
car dot drive so currently this will take two lines of code but this will print you start the
engine
4:05:27
and you drive the car now with method training we can call multiple methods
4:05:32
sequentially now the format on this is going to be a little bit different and if we were to
write the same code
4:05:38
using method chaining it might look a little something like this after the first method
call we would add
4:05:43
dot and then an additional method that we would like to call so for example after the
turn on method we'll
4:05:50
immediately call the drive method but there's one more thing that we need to add
when we're using python to do method training
4:05:56
so normally this by itself will not work when we call a method in python if there is
nothing that is returned python will
4:06:03
return none so as you can see here attribute error none type object has no attribute
of drive so we
4:06:10
need to return itself under each method that we're using method training with so let's
add return self to each of
4:06:16
these methods so let's do that for drive break and the turn off method and this
4:06:24
should work now you start the engine you drive the car so think of it like this after we
finish
4:06:30
calling this method python is going to return self so it's going to return car so then
4:06:35
we'll immediately use car.drive now here's another example let's say i would like to
4:06:41
call the break method followed by the turn off method so that would be car dot break
4:06:47
parentheses followed by dot turn off so this will print you step on the
4:06:55
brakes you turn off the engine now here's an extreme example let's say we would
like to call all four methods in order
4:07:01
so that would be car dot turn on followed by dot drive
4:07:08
dot break dot turn off so this will call
4:07:15
all four methods in order starting with the leftmost method you start the engine you
drive the car
4:07:21
you step on the brakes you turn off the engine now if you have one long method
chain like this
4:07:27
it might become difficult to read after a while so what i would recommend if you're
doing a lot of method chaining
4:07:32
is that after each method call just hit enter to move each method call down to a new
line
4:07:38
so it'll look something like that and this backslash might be inserted this is a line
4:07:43
continuation character so this is a little more readable but you know it still does the
same thing
4:07:48
basically all right everybody so that is method training it's used to call multiple
methods sequentially and each
4:07:55
call performs an action on the same object and returns self if you would like a copy
of this code i will post all of this in the
4:08:02
comments section down below but yeah that is how to do a method chaining in
python
47.super function
4:08:08
hey what's going on everybody it's bro hope you're doing well and in this video i'm
going to explain
4:08:13
the super function in python so sit back relax and enjoy the show
4:08:20
well then since you clicked on this video i should probably discuss the super function
now the super
4:08:25
function is used to give access to the methods of a parent class it returns a
temporary object
4:08:32
of a parent class when used here's an example of why the super function would be
useful
4:08:37
let's take this program that i've written there's three classes a class called rectangle
which is the
4:08:43
parent class to two children classes square and cube and all we're doing is creating
a square
4:08:49
object and a cube object but we need to pass in a length and a width for squares
4:08:54
and a length width and height for cubes now with programming we don't like to
4:08:59
repeat code we like to reuse code and if you take a look within my knit methods for
both square and cube we're repeating
4:09:06
self.length equals length and self.width equals width twice for our init methods
4:09:12
so one thing that we can do any similarities between the square and cube class we
can place
4:09:18
within the rectangle class and then reuse this code so what i'm going to do
4:09:23
is copy this indent method within our square class and paste it within our rectangle
parent
4:09:29
class and then in order to access this init method we can use the super function so
we no
4:09:36
longer need these two lines of code what i'm going to do is ask the rectangle class to
use its init
4:09:42
method so we're going to type super parentheses dot and then the name
4:09:48
or the function of the parent class that we would like to use and i would like to use
the init method
4:09:53
but i need to send a length and a width because it requires that so let's send these
so we're going to
4:09:58
pass in our length and our width and now we can do the same thing for our cube init
method
4:10:03
so i will copy this line of code we no longer need these two lines of code because we
will be reusing our init
4:10:10
method within the parent class of rectangle but we'll still need to keep this line in to
assign the height
4:10:16
because that's a difference between our square and cube class and now we can just
reuse this init
4:10:23
method within the rectangle parent class now i'm thinking that we should probably
test to see if
4:10:28
length and width are assigned to these attributes and one way that i think would be
fun would be to
4:10:33
create an area method for squares and a volume method for cubes so let's define
4:10:38
an area method within the square class def area and i would like to return self
4:10:46
dot length times self dot width and we can create a volume
4:10:52
method within cubes so let's do that def let's change this to volume
4:10:58
we'll multiply it length times width times height but we need to type self dot pipe
4:11:05
and we can call these methods to get the area of a square or the volume of a cube
4:11:11
let's print square dot area
4:11:17
and then cube dot volume so if our init method
4:11:25
is actually being used we should have a length and the width assigned to these
attributes and it
4:11:31
looks like we do in conclusion the super function is used to give access to the
methods of a
4:11:37
parent class it returns a temporary object of a parent class when used in order to
4:11:43
access the methods of the parent class also known as a super class and in this
example within
4:11:49
the init methods of both square and cube we immediately called the init method of
4:11:55
the parent class to pass in some arguments that both of these classes have in
common so if you would like a
4:12:01
copy of this code i will post all of this in the comment section down below but yeah
that is how the super function
4:12:07
works in python hey what's going on everybody it's bro hope you're doing well
48.abstract classes
4:12:12
and in this video i'm going to explain how abstract classes work in python so sit back
relax and enjoy the show
4:12:21
hey what's good everybody let's talk about abstract classes now abstract classes
prevent a user from
4:12:27
creating an object of that class think of an abstract class more as a template
4:12:33
it's an idea it's not real it's like a ghost class basically plus an abstract class compels
a user to
4:12:40
override any abstract methods within a child class so it's also a form of checks and
4:12:45
balances as well which we'll discuss later now here's a small program that i've
written let's say we have
4:12:51
three classes class vehicle car and motorcycle car and motorcycle are the children
4:12:57
classes of the vehicle class now within this vehicle class we have a go method
4:13:02
but i'm currently only defining it not actually implementing it so i'm just writing pass
now within car and motorcycle we're
4:13:09
overriding this method and creating our own implementation of the skull method that
we inherit the go method for the car will print you
4:13:16
drive the car and the go method for the motorcycle class will print you ride the
motorcycle now currently i'm creating an object of
4:13:22
each of these classes i have a generic vehicle object car object and motorcycle
object now after running
4:13:29
this program currently the go method within our vehicle class is not being
implemented so this won't print anything but the go
4:13:36
method within our car class will print you drive the car and the go method within our
motorcycle class will print
4:13:43
you ride the motorcycle with that being said let's pretend that we're coding the next
need for speed game and we would like a
4:13:49
user to create an object from a specific kind of vehicle whether it's a car from
4:13:54
the car class or a motorcycle from the motorcycle class we would like to prevent a
user from creating an object of the vehicle class
4:14:02
because the vehicle class is too generic we do not have all of the implementations
set up for a vehicle it would be like
4:14:09
somebody unlocked the ghost car or the invisible car which doesn't actually exist and
is missing a lot of features we need a user
4:14:16
to create an object from a child class because these are fully fleshed out and one
way in which
4:14:21
we can prevent a user from creating an object of this class as well to turn this class
into an
4:14:27
abstract class which is what this video is all about go figure now the first step to
creating an
4:14:32
abstract class is that we'll need some imports so at the top of your program write this
from
4:14:37
abc abc is an acronym for abstract based class abc from abc
4:14:44
import abc all capital comma abstract method all right
4:14:51
now with our vehicle class the class that you intend to be the abstract class add us
out of parentheses and our
4:14:58
vehicle class will inherit from the abc class and now with any methods within your
4:15:04
vehicle class you're going to add this decorator at the top at abstract
4:15:09
method all right we should now be prevented from creating a vehicle object our
vehicle class is now
4:15:16
an abstract class and we cannot give it a physical form a physical manifestation and
if we attempt to you can see here
4:15:22
that we have a type error can't instantiate abstract class from vehicle with abstract
methods
4:15:28
go so an abstract class is a class which contains one or more abstract methods and
an
4:15:35
abstract method is a method that has a declaration but does not have an
implementation
4:15:40
now by definition an abstract class contains one or more abstract methods if i
remove
4:15:47
this one abstract method within our vehicle class well we could still create a vehicle
4:15:52
object and i'm just going to turn this vehicle.go line into a comment because we don't
have a go method anymore
4:15:58
as you can see we can still create a vehicle object which we would like to prevent a
user from doing
4:16:04
so we need at least one abstract method within our vehicle class so i'm going to go
ahead and add this go
4:16:10
method back in an additional feature of abstract classes is that they compel a user to
override any abstract methods
4:16:17
within a child class a method that is abstract has a declaration but does not have an
implementation so let's say that we
4:16:25
create our vehicle class our abstract vehicle class and now we're creating the
children classes of car and
4:16:31
motorcycle and let's say i'm not paying attention and i forget to add a go method
4:16:36
so if i just write pass within my motorcycle class i'm currently missing a go method
within my motorcycle class
4:16:43
well python is going to prevent me from running this because we have a type air
can't instantiate abstract class vehicle
4:16:50
with abstract methods of go so by inheriting from an abstract class it's a good form of
checks and balances
4:16:57
to be sure that your children classes are not missing any implementations of any
methods that
4:17:02
they inherit here's an additional way of thinking of this our vehicle class is telling its
children if you're going
4:17:08
to inherit from me then you need to override this abstract method of mine and if you
don't well i'm not going to let you be
4:17:14
instantiated so in order for us to create a cart and motorcycle class we need to
override the go method that
4:17:22
they inherit from its parent class of vehicle and provide its own implementation now
let's say we create an additional method
4:17:29
let's say we have a stop method so let's define that def stop for the time being i'll
write pass and
4:17:35
to make this an abstract method add this decorator at abstract
4:17:40
method now car and motorcycle both need to implement that method of stop it's
4:17:46
currently missing it as you can see here class car must implement all abstract
methods and the same thing applies for a
4:17:52
motorcycle as well so we need to override this method and provide its own
implementation
4:17:58
so let's remove that decorator and let's print something let's print this car is stopped
4:18:06
and i'll do the same thing for motorcycle so def stop let's say
4:18:14
this motorcycle is stopped and we'll call the stop
4:18:21
method for each of these types of vehicles car dot stop and motorcycle dot stop
4:18:29
all right and we can now create a car and motorcycle object because we are
overriding both of these
4:18:36
abstract methods found within the parent class of vehicle all right everybody so that's
the basics
4:18:41
of abstract classes it's a class which contains one or more abstract methods
4:18:46
and an abstract method is a method that has a declaration but does not have an
implementation and
4:18:52
the benefits of using an abstract class is that they prevent a user from creating an
object of that class
4:18:58
plus it compels a user to override any abstract methods found within a child class
4:19:03
so if you would like a copy of this code i will post all of this in the comments section
down below but yeah that is how abstract classes
4:19:10
work in python hey what's going on everybody it's you bro hope you're doing well
49.objects as arguments 🏍️
4:19:15
and in this video i'm going to explain how we can pass objects as arguments so sit
back relax and enjoy
4:19:22
the show ladies and gentlemen in today's video i'm going to be demonstrating how
we can
4:19:28
pass objects as arguments for this example let's say we have a class car and there
is one class variable
4:19:35
called color color equals none what we'll be doing is calling a function that accepts
an
4:19:40
object as an argument as well as a color so let's create a function named change
4:19:46
color now make sure when you define this function it's not within the cart class then
technically this would be a method
4:19:52
of the car class what we would like is a separate function outside of the car class
now we need to set up some parameters we
4:19:59
would like to accept a car object as well as a color so for the first parameter let's say
a
4:20:05
car and make sure this is in uppercase or capital because python doesn't like that
argument names should
4:20:10
be lowercase so when we pass in a car we will give it a nickname of car and we
would also like to pass in a
4:20:17
color so let's set up a parameter called color now when we pass in a car we will
assign
4:20:22
that car's color with whatever color that we receive so car dot color equals color
4:20:29
and now let's create some car objects so let's say car one equals car car two equals
car
4:20:36
and car three equals cart currently if i were to print their colors this is what we'll see
4:20:43
print car one dot color and repeat the steps for two and three
4:20:48
and we're not calling this function yet so all of these colors should be set to none
they are basically cars with no color
4:20:56
they're i don't know iron or something whatever cars are made out of aluminum
plastic stuff like that okay so now
4:21:02
let's call this function change color perhaps right here so in order to call this function
we need to
4:21:08
pass in a car object as well as a color so let's pass in car one and a color perhaps
red and
4:21:15
we'll repeat the steps for two and three so let's say car two will be white
4:21:21
and car 3 will be blue now after calling this function we have
4:21:27
a car object and a color and we're assigning the color of our car to whatever color
that we receive
4:21:33
now these cars in order car 1 2 and 3 are red white and blue now one thing
4:21:39
that you should know is that the name of this parameter that accepts our object
doesn't necessarily need the same name
4:21:45
as the class name that created the object that we're passing in we could name this
something else entirely perhaps
4:21:51
vehicle you'll just want to be sure that this is at least descriptive of the kind of object
that you would like to pass in and we're not limited to
4:21:58
just passing in car objects we can pass in all sorts of objects but we need to pass in
an object as well
4:22:04
as a color so this time let's create a class called motorcycle class motorcycle and this
class will
4:22:12
also have a color set to none so what we're doing is actually reusing this function for
all sorts of different
4:22:18
kinds of vehicles now let's create a vehicle i'll just name this bike one for short bike
one equals motorcycle now let's
4:22:26
call the change color function and we can pass in an object as well as color this time
we
4:22:33
will pass in our bike object as well as a color i think black counts as a color and let's
print
4:22:42
bike once color and this should work we have red white
4:22:48
blue and black in conclusion we can pass objects as arguments to a function
4:22:54
much like what we've been doing with variables however the type of objects that we
pass in may be limited based on
4:23:01
the required attributes and methods that that given class or object might have and
we'll get more into this on the
4:23:08
next video on duct typing so if you would like a copy of all this code i will post all of
this
4:23:13
in the comment section down below but yeah that is how to pass objects as
arguments
4:23:18
using python hey what's going on everybody it's bro hope you're doing well and in
50.duck typing
4:23:23
this video i'm going to explain duck typing in python so sit back relax and enjoy the
show
4:23:31
i hope you all like ducks because that's the topic of this video duct typing is the
concept where the
4:23:37
class of an object is less important than the methods and or attributes that that class
might have
4:23:43
the class type is not checked if the minimum methods and or attributes are present
it's based off of a popular
4:23:50
phrase if it walks like a duck and it quacks like a duck then it must be a duck here's
an example i have two classes
4:23:57
class duck and class chicken both duck and chicken have walk and talk methods
4:24:02
but they print something different with the duck class the walk method will print this
duck is walking and the duck talk
4:24:09
method will print this duck is quacking the chicken class has the same named
methods walk and talk
4:24:15
but they print something slightly different a slightly different variation the print method
will print
4:24:20
this chicken is walking and the talk method will print this chicken is clucking now let's
say
4:24:25
we have a third class a class called person
4:24:32
now there is one method within our person class let's say we're attempting to catch a
4:24:37
duck we'll pass in self and we need to pass in a
4:24:42
duck object as an argument so let's type duck now within this method
4:24:48
we'll have our duck use its walk method as well as its talk method duck.walk and
4:24:56
duck.talk and then maybe we'll print something
4:25:02
such as you caught the critter
4:25:07
now let's create an object from each of these classes let's create a duck object duck
equals duck
4:25:14
a chicken object chicken equals chicken and lastly person person equals person
4:25:21
now if we would like our person to use the catch method we need to pass in a duck
as an argument
4:25:27
so with our person let's type person dot catch and we need to send a duck object
because that's a required
4:25:34
parameter here so we're passing in our duck object that we created and as soon as
we catch the
4:25:39
duck it's going to use its walk and talk method and then we'll print you caught the
critter so as you would
4:25:45
expect this will print this duck is walking the stuck is quacking you caught the critter
4:25:50
now with duct typing we can pass in a different type of object as long as it
4:25:56
has the same methods and or attributes as our duck we could pass in a chicken
because a
4:26:02
chicken can also walk and talk so this time let's pass in our chicken as an argument
and this will still work
4:26:09
even though our parameter is set up to take a duck object this chicken is walking this
chicken is
4:26:15
clucking you caught the critter in layman's terms think of it like this python is
examining this chicken object
4:26:21
it's using its walk method it's using its talk method which are required and python is
saying well that's close
4:26:27
enough remember if it walks like a duck and it quacks like a duck then it must be a
duck so duct typing is
4:26:33
the concept where the class of an object is less important than the methods and or
attributes that it might have
4:26:39
the class type is not checked if the minimum methods and or attributes are present
now let's say with our
4:26:46
chicken object our chicken can no longer walk well we cannot pass in our chicken
4:26:51
object anymore we have an attribute error chicken object has no attribute walk
4:26:57
so python took a look at this chicken and noticed that it does not have a walk method
well it's not walking like a duck but
4:27:03
it's talking like a duck so it doesn't count as a duck because it doesn't have its walk
method
4:27:08
in conclusion the class type of an object is not as important as the methods and or
attributes that that
4:27:14
class might have when using duct typing the class type will not be checked if the
minimum methods and or attributes are present
4:27:22
since chickens can both walk and talk like ducks then they can be a substitute for
ducks when
4:27:27
we pass in our duck object to this method so if you would like a copy of this code i
will post all of this in the comment
4:27:33
section down below but yeah that is the basics of duct typing in python
51.walrus operator
4:27:38
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to discuss
4:27:44
the walrus operator in python so sit back relax and enjoy the show
4:27:51
all right people i hope you like walruses because that's the topic of this video we'll be
discussing
4:27:56
the walrus operator also known as an assignment expression it's a colon followed by
an equal sign
4:28:02
and it kind of looks like a walrus on its side now this is a new feature for python 3.8
4:28:08
what it does is that it assigns values to variables as part of a larger expression here's
an
4:28:14
example of why the walrus operator would be useful let's say we have a variable
named happy
4:28:19
happy equals true if you're not happy or you're feeling so so you can set this to false
if you want
4:28:24
so let's print the value contained within happy so this will print true to the console
4:28:30
window now wouldn't it be cool if we could combine both of these statements
together so we would want to
4:28:37
say something such as happy equals true and then print it to the console window
using one line of
4:28:44
code well normally we can't do this using the standard assignment operator now one
4:28:49
thing that we can do is use the walrus operator this assigns a value to a variable
4:28:54
as part of a larger expression so this would technically be allowed if this assignment
operator
4:29:00
was instead an assignment expression the walrus operator happy walrus true and
when they're all within
4:29:07
a print statement we can assign a value to a variable and use it as a part of a larger
expression
4:29:14
so as you can see this new variable of happy was assigned and used in one
expression
4:29:19
now here's a more practical example of why the walrus operator would be useful let's
say we have the smell program what
4:29:26
this will do is consistently ask you what food you like and put it into a list called foods
4:29:32
foods equals list while true food equals input what food do you like if food
4:29:38
equals quit break then append our list of foods with this new food item that we have
4:29:44
okay let's run this then so this while loop will continue as long as i don't type in
4:29:49
quit and it will consistently ask me what food do i like and place that food item into a
list
4:29:55
named foods so let's begin what food do i like well i do like pizza i like sushi and i like
ice cream
4:30:04
and that should be enough for me i'm going to type in quit to exit this program now
let's write the same program but
4:30:10
instead use a walrus operator so that we can write this program using less lines of
code now if i were to use
4:30:17
the walrus operator this is how i would write the same program we would still need
our list foods
4:30:22
equals list now for my while loop it's going to look something like this
4:30:28
while food walrus operator input what
4:30:36
food do you like all right this is the assignment portion
4:30:42
now we can use this assignment portion within a larger expression so let's also
check to see if our input
4:30:50
does not equal quit alright so then after each iteration of this
4:30:56
while loop we will take our list foods dot append food and
4:31:04
that is it so we have written the same program in about half the lines of code that we
did previously so
4:31:10
let's test it what food do you like pizza sushi ice cream
4:31:17
and quit well everybody that is the basics of the walrus operator also known
4:31:22
as an assignment expression it's a new feature for python 3.8 and beyond so if this
doesn't work for
4:31:28
you i would check to see what version of python you're using first it assigns values to
variables as part
4:31:34
of a larger expression if you would like a copy of this code i will post all of this in the
comment section down below
4:31:40
but yeah that is how the walrus operator works in python hello what's going on
everybody it's bro
52.functions to variables
4:31:47
hope you're doing well and in this video i'm going to show you guys how we can
assign a function to a variable in python
4:31:52
so sit back relax and enjoy the show hey all in today's video i will show you
4:31:59
all how we can assign a function to a variable this video will be a prerequisite for a
few videos i have coming up regarding
4:32:06
functions now let's say we have a function named hello and all we'll do is print the
word hello
4:32:11
and as you know to call this function you would type the name of the function
followed by a set of parentheses and this will print the word
4:32:17
hello the set of parenthesis that comes after a function's name is the portion that will
call the
4:32:23
function if you were to remove that set of parenthesis afterwards we would not in fact
call that function
4:32:29
now with python python will pretty much treat everything as objects including
functions so there's something
4:32:34
i want to show you guys if i was to print the name of my function hello what will be
displayed is the memory
4:32:41
address of this function this is the memory address of where this function is located
within my computer's
4:32:47
memory and it's in hexadecimal think of it like a street address such as one two three
fake street this is the
4:32:53
address of where this function is within my computer's memory and each time that i
run this program this number can change as you can see
4:33:00
here now one thing that we could do we could assign this address to a variable let's
say hi equals hello and be sure
4:33:06
that you're not adding that set of parentheses afterwards because then you would be
calling the hello function and returning the result to
4:33:12
high so high equals hello no parentheses and if i was to print hi well
4:33:20
the address of hello and hi will be at the same memory address both of these
numbers are the same now
4:33:26
what do you imagine would happen if i was to call the high function after we
assigned the memory address of hello
4:33:32
to hi well then what we'll end up doing is calling the hello function even though we're
listing that we would
4:33:38
like to call the high function even though it doesn't exist so it's as if this hello function
4:33:43
has two names you can either use hello or you can use hi and let me demonstrate
that
4:33:49
so you can call either hello hi or both for this example that's because we're assigning
the
4:33:55
memory address of hello to this variable of high so we could treat high as a function
it's kind of like
4:34:01
an alias where this function has two names now here's another demonstration let's
say that we would like to assign
4:34:07
our built-in print function to a variable so let's say maybe say
4:34:13
say equals print and when i say print i'm referring to the print function and
4:34:18
be sure you're not adding that set of parentheses afterwards now if i need to print
something to the console window i can either use
4:34:24
print like what we've previously been doing or i could use say because say has the
same memory
4:34:30
address let's say perhaps whoa i can't
4:34:35
believe this works surprise face all right so
4:34:42
we can call the print function by saying the word say say all of this text and this will
print
4:34:48
all of this text well say all of this text to the console window under normal
circumstances i'm not sure
4:34:54
why or when you would need to assign the print function to a variable but you can
and well that's kind of cool so
4:35:00
i thought i'd show you guys that so everybody that's how to assign a function to a
variable
4:35:06
like i said we'll be building upon this topic so you want to be sure that you understand
this all right then well if you would like a
4:35:12
copy of this code i will post all of this to the comments section down below but yeah
that's how to assign a function
4:35:18
to a variable in python hey uh what's going on everybody it's bro hope you're doing
well and in
53.higher order functions
4:35:24
today's video i'm going to explain higher order functions in python so sit back relax
and enjoy the show
4:35:33
all right ladies and gentlemen let's talk about higher order functions these are
functions that do one of two
4:35:38
things one they either accept a function as an argument or two they return a function
as output
4:35:44
and in python this is totally allowed because functions are also treated as objects i'll
give you guys an example of both of
4:35:50
these but let's begin with number one here's an example of number one let's say that
i have two functions
4:35:55
named loud and quiet both of these functions will accept a string as an argument
loud will return that string all
4:36:02
uppercase as if we're shouting something quiet we'll return that text i'll lowercase as
if we're whispering
4:36:08
something now i'm going to create a third function called hello and this will be the
higher order
4:36:14
function it accepts a function as an argument i'm not sure if we're going to be
passing in loud or quiet
4:36:21
whatever it is i'm going to give it a name of funk short for function each of these
4:36:26
functions will return some text either all uppercase or lowercase i'm going to assign
that to
4:36:32
text text equals the name of our function we're not sure what it's going to be and i will
pass in some text in order to
4:36:39
call one of these functions i will pass in just the word hello that's capitalized and at
the end i will print whatever
4:36:46
text that we have in order to call this function of hello i need to pass in a function as
an argument
4:36:51
do i want to pass in loud or quiet do i want the loud variety of hello where all of the
4:36:57
text will be uppercase or the quiet variety of flow where all the text will be lowercase
4:37:02
i would like to pass in loud because i want to make my text all uppercase
4:37:08
so i'm typing the name of the higher order function and passing in a function as an
argument and the result
4:37:14
is that the word hello will be printed to the console window all uppercase now here's
a rundown of
4:37:19
what just happened we're calling the hello function and we're passing in loud as an
argument we're naming loud as
4:37:26
funk while we're within this hello function text equals loud and we're sending a
4:37:33
string of text that says hello we're returning that text all uppercase
4:37:38
assigning it to a variable called text and printing that text to the console window now
if i would
4:37:44
like to use the quiet variety of hello then i'm going to pass in quiet as an argument so
quiet will make
4:37:52
all of this text lowercase alright people so that's example one of a higher order
function a higher
4:37:58
order function is a function that accepts a function as an argument this hello function
is an example of a
4:38:05
higher order function because we're accepting either loud or quiet as arguments
4:38:10
all right people let's move on to the second part of this definition for higher order
functions a higher order function is a function
4:38:17
that returns a function i'm going to give a different example this time let's say we
have a pair of
4:38:22
nested functions the outer function will be named divisor and we will accept a
number as an
4:38:28
argument that we will call x a divisor is a number that is used to divide another
number when using division and
4:38:35
inside this function we have an inner function named dividend dividend is the number
4:38:41
that's going to be divided and we will call the argument that is passed in y all we'll do
is return y divided by x
4:38:49
we're dividing the dividend by the divisor y divided by x now within the
4:38:55
outer function but not within the inner function we're going to return our dividend
function so a higher order
4:39:02
function is a function that returns a function divisor is a higher order function because
we're returning
4:39:09
dividend now if i would like to access this nested dividend function i first need to call
4:39:14
the outside divisor function and pass in a number as an argument to serve as the
divisor
4:39:20
the divisor function is going to return my dividend function which we can then assign
to a variable
4:39:26
so the variable i'm going to name divide divide equals and i need to call divisor
4:39:32
and set a divisor let's say that i would like to divide all numbers by two so i will set x
to be two
4:39:40
and now for the dividend i will print call the
4:39:47
divide variable and pass in a number as the dividend the number that's going to be
divided
4:39:52
and let's say i would like to divide 10 by two and this will work as you can see we
4:39:58
have five in the console window so let me explain what just happened here so our
program
4:40:03
begins here divisor and we're passing in two x will be two and it will stay that way
4:40:09
until we finish this program or until we reassign x now x equals two we're skipping
this
4:40:16
function because we did not call it yet we're returning dividend and assigning it to a
variable
4:40:22
and we can call a variable if it has the memory address of a function which is what
we're doing in this line
4:40:28
now we're calling dividend and passing in 10 so y equals 10
4:40:33
and x still equals to we're returning 10 divided by 2 and printing it to the
4:40:38
console window well everyone in conclusion a higher order function is a function that
either one accepts a
4:40:46
function as an argument or two they return a function the format is a little bit strange
and we're not
4:40:52
quite used to it yet but in future videos we'll have more practice with high order
functions if you would like a copy of all this
4:40:58
code i will post all of this to the comment section down below but yeah that is how
higher order
4:41:03
functions work in python yo what's going on everybody it's bro hope you're doing well
in today's video
54.lambda λ
4:41:10
i'm going to explain lambda functions in python so sit back relax and enjoy the show
all right
4:41:19
ladies and gentlemen let's talk about lambda functions these are functions that are
written in one line and use
4:41:24
this lambda keyword they accept any number of arguments but they only have one
expression
4:41:30
think of it as a shortcut they're useful if you need a function for only one use or a
short period of time
4:41:35
and then you're just planning on throwing away that function afterwards here's the
syntax for a lambda function
4:41:40
you type lambda you add the parameters followed by a colon and then your
expression let's say we have this function named
4:41:47
double double accepts one argument that we will name x we're returning x times two
4:41:52
effectively doubling it so if i need to call this function i would type double and then
pass in a number as an argument
4:41:59
so the result if i pass in 5 would be 10. now let's write the same function but instead
write it as a
4:42:04
lambda function so let me turn these lines into comments now the first step is to type
lambda
4:42:12
then our parameters we only have one parameter and that is x colon and then our
expression we would
4:42:18
like to return x times two now this lambda function is going to return a function object
and we
4:42:23
can assign that much like what we do with variables let's say double equals lambda x
colon
4:42:29
x times two and in order to call this lambda function we're going to type double then
a set of
4:42:35
parentheses to call this function and then pass in our arguments so let's say that x
equals 5 and then
4:42:42
we're going to double it and the result is 10. so that's the syntax for a lambda
function you type
4:42:47
lambda your parameters colon and then your expression this time let's say that we
have two parameters
4:42:53
we have two numbers that we want to multiply by each other so let's say that this
function will be
4:42:59
called multiply multiply equals lambda x comma y because we have two parameters
4:43:07
colon followed by our expression we would like to multiply x times y and that is it
4:43:14
now in order to call multiply we need to pass in two arguments if i were to pass in
just one well we'll
4:43:20
have a type error so let's pass in five and maybe six and the result is thirty now let's
try three parameters
4:43:28
let's say we would like to add three numbers together let's say add lambda x
4:43:35
comma y comma z so we have all of our parameters we have our colon here and
then we need
4:43:41
our expression x plus y plus z and
4:43:46
let's add five six and seven together the result is 18. now let's try
4:43:52
something a little more complex let's pass in some strings this time i'll create a
function named full name
4:43:58
full name equals lambda first name comma
4:44:06
last name the expression is going to be first name plus perhaps a space
4:44:12
plus last name now i need to pass in a first name and a last name
4:44:19
full name and you can use your own name for this example if you want let's say bro
comma code
4:44:29
yep this will print your first name and your last name to the console window now let's
take it a step further let's
4:44:34
say we would like to check somebody's age once using a lambda function we would
like to check to see if they're 18
4:44:40
or older because they're signing up for something perhaps let's say a credit card so
let's create a function object age
4:44:47
check equals lambda we have one parameter somebody's edge colon and then our
4:44:53
expression let's type true if age is greater than
4:44:59
or equal to 18 else false to call this age track function
4:45:07
we need to pass in an age let's say the user is 12 and they're trying to sign up for a
4:45:13
credit card while this will return false let's say they're 18 now and this will
4:45:18
return true so that's a lambda function it's a function that is written in one line using
this
4:45:24
lambda keyword they accept any number of arguments but they only have one
expression think of it like a shortcut and they're
4:45:31
useful if you need to use a function only once and then you're just planning on
throwing it away afterwards so if you would like a copy of all this
4:45:37
code i will post all of this to the comment section down below but yeah those are
lambda functions in
4:45:43
python hey what's going on everybody it's bro hope you're doing well and in this
video i'm going to show you
55.sort 🗄️
4:45:49
all how we can sort iterables in python so sit back relax and enjoy the show
4:45:57
well well well welcome back everybody in this video i'm going to show you all how we
can sort data using python we'll
4:46:02
begin with the sort method which is used with lists and the sort function which is
used with iterables and that would
4:46:07
include lists let's start with something very basic and then we'll crank up the difficulty
one step at a time
4:46:13
i have a list named students and i have a bunch of student names within this list we
have squidward sandy patrick
4:46:19
spongebob mr krabs let's say that all of these students are taking a college course
4:46:25
perhaps on marine biology what i would like to do is to sort this list in alphabetical
order and to do that we have a method that is
4:46:31
built in with lists so type the name of your list students dot sort and it's easy as that
and to
4:46:38
display this list i think i'll use a for loop for i in students
4:46:44
print i and there you go that's how to sort a
4:46:50
list starting with the top we have mr krabs then patrick sandy spongebob and
squidward the sort method of lists can
4:46:57
accept keyword arguments there are two optional keyword arguments that we can
pass in we can pass in key and or reverse we'll cover key a
4:47:05
little bit later if we set reverse to true then our list will be sorted by reverse
alphabetical
4:47:12
order beginning with squidward spongebob sandy patrick then mr krabs
4:47:17
now the sort method does not work with other iterables it's a built-in method for lists
4:47:22
if our list of students was instead let's say a tuple well then the sort method is not
going
4:47:28
to work you can see here that we have an attribute error tuple object has no attribute
of sort
4:47:34
that's where the sort function would come in because that's useful for other iterables
you can also use this for a list 2 i
4:47:40
suppose the sort function will return a sorted list so let's use the sort
4:47:45
function this time and we'll assign the result to a list called sorted students
4:47:53
equals sorted and then we need to pass in an iterable and we have the option of
4:47:59
passing in a key and or reverse the iterable that we'll pass in as an argument is our
tuple
4:48:06
named students so this line will be sorted students equals sorted students sorted
students
4:48:13
is a list the sorted function will return a list but it accepts an iterable as an argument
4:48:18
and to display this we should change 4i and students to 4i in sorted students and
now
4:48:25
this function will accept our iterable our tuple and create a sorted list and all of
4:48:30
these are in alphabetical order and to reverse this we can pass in the keyword
argument of
4:48:36
reverse equals true and now our list is sorted in reverse alphabetical order all right
4:48:42
we're gonna take it up a level welcome to level two sometimes data isn't always as
simple here we have a
4:48:48
list of tuples each tuple has a corresponding student record we have a name a letter
grade for their
4:48:55
college course and the student's age now how can we sort this list of tuples by either
the student's
4:49:02
name their grade or their age well that's where the key keyword argument's going to
come in with
4:49:08
sorting if you take a look at this list of tuples it somewhat resembles a spreadsheet
there's rows and then
4:49:14
there's columns the first column corresponds to student names the second column
are grades and the third column are all
4:49:21
the ages of the students so by default sorting by the first column is actually really
easy that's
4:49:27
the default so if we need to sort alphabetically that would be the same process as
before we would type the name of the
4:49:33
list students dot sort but if we were to print
4:49:38
this iterable for i in students instead of just the individual student
4:49:44
names we're going to get each tuple that we have so now all of these tuples are
arranged in
4:49:50
alphabetical order starting with the first column that we have which are all the
student names
4:49:56
now how can we sort these iterables by their second column for this case it would be
student grades
4:50:01
or even the third column which would be the student ages well that's where the key
keyword
4:50:07
argument's going to come in this is a keyword argument and we set key equal to a
function that's going to
4:50:13
return the index of that specific column that we have so let's say key equals grid
grade is
4:50:20
going to be a function object grade equals and we can easily use a
4:50:26
lambda expression for this lambda let's say grades colon
4:50:32
grades and we will set an index of one now the first index is zero that's the
4:50:39
first column because computers always start with zero column two would have an
index of one
4:50:44
and then column three would have an index of two so grade equals lambda grids
4:50:51
colon grids index one you can also rename these if you want so students dot sort
we're setting the
4:50:58
key equal to grade and grade is a function object via a lambda function and now all
of
4:51:05
these students will be sorted by their grades starting with sandy then spongebob mr
krabs patrick then
4:51:11
squidward if this needs to be in reverse order we can pass in that other keyword
argument
4:51:16
of reverse equals true and for practice if you wanted to sort all of this data by each
4:51:23
student's age then we would change grade to let's say age age equals lambda
4:51:28
we'll change grades to maybe ages colon ages and the index would then be two
4:51:34
because the first column here is zero then one then two and change key to our
function
4:51:41
object of edge now each student is sorted numerically beginning with the
4:51:47
smallest edge well the youngest person starting with spongebob sandy patrick
squidward then mr krabs
4:51:53
and like i said before if you want your data arranged in reverse order you can set that
keyword argument of reverse to true
4:52:01
within the sort method and all of our student data is sorted beginning with the eldest
student which
4:52:07
is mr krabs then squidward patrick sandy and then spongebob and for the last part of
this video
4:52:13
let's say that we're working with some other iterable let's say we have a tuple of
tuples instead
4:52:19
well we can no longer use this sort method because that only belongs to lists you
can also use the sort function with
4:52:25
lists and it will generate a new sorted list without changing the placement of the
original so let's say we have a tuple of tuples
4:52:33
and this will create a new sorted list let's call this list sorted
4:52:38
students equals then we'll use the sorted function we need to pass in an
4:52:43
iterable and we can pass in both a key and or the reverse keyword
4:52:48
argument so the iterable would be students and let's set the key equal to edge
4:52:56
and i will not reverse it we can keep it as it is but let's change for i in students to
sorted students
4:53:04
and that's how to use the sorted function to sort an iterable including a list well all
4:53:10
right everyone that's how to sort iterables in python if you would like a copy of all this
code i will post all of this to the
4:53:16
comments section down below but yeah that's a basic way to sort iterables in python
hey what's going on everybody
56.map 🗺️
4:53:24
it's your bro hope you're doing well in today's video i'm going to show you all how the
map function works in python so
4:53:30
sit back relax and enjoy the show what up everybody let's discuss the map
4:53:36
function the map function will apply a function to each item in an interval that would
include lists tuples things
4:53:43
like that and the map function accepts two arguments are iterable as well as the
function we would like to
4:53:50
apply to each item within our iterable for example let's say that we have an online
store to represent this i have
4:53:56
a list of tuples named store each tuple within this list corresponds to an item for sale
within
4:54:03
our store there's shirts pants jackets and socks and each item has a price in us
dollars let's say now what
4:54:10
i would like to do is to convert all of the prices within my store to euros and currently
the conversion
4:54:16
rate as of the filming of this video is one us dollar to 0.82 euros
4:54:22
so let's begin we can easily convert dollars to euros with a lambda function so let's
say two euros
4:54:29
equals lambda the parameter will be data colon and the next part's a little bit
4:54:36
tricky within a set of parenthesis to represent a tuple we'll take data at index 0 that
4:54:42
corresponds to the first column comma data at index 1.
4:54:49
this represents the index of our prices we're going to take data at index 1 times 0.82
4:54:57
and that's it the first column will be left untouched but the data in column 1 will be
4:55:03
multiplied by 0.82 now our map function is going to create a map object but we can
easily cast that
4:55:10
to a different type of iterable so let's say that store underscore
4:55:16
euros equals map parenthesis and then we can pass in our
4:55:21
function as well as our iterable our function is our two euros function
4:55:27
comma and our iterable is our store and that is it now if you want to
4:55:33
convert this map object to an iterable you can easily surround this with a cast i would
like to convert this map object
4:55:40
to a list and we will now have a new store named store euros which will have all of
the
4:55:47
different prices of our store in euros and to display this i'll use a for loop for i
4:55:52
in store euros print i and
4:56:00
let's try it yep that seems about right all of the prices in our store are now
4:56:05
in euros let's pretend that these prices are already in euros and we would like to
convert these
4:56:10
to american dollars so we will use the same conversion rate but divide by 0.82 so
let's create a separate
4:56:18
lambda function two dollars equals i think i'll just copy this but we are
4:56:25
going to divide the first index of data by 0.82
4:56:30
and let's say store dollars to represent our store in dollars because we're pretending
now
4:56:37
that it's currently in euros and we will display 4i in store dollars
4:56:45
so if all of these prices are in euros we are now converting them to american dollars
4:56:51
but you may want to do some rounding as opposed to the nearest descent well
everyone that is the map function
4:56:56
it applies a function to each item in an iterable those include lists tuples
4:57:01
the like you pass in your iterable and your function as an argument to the map
4:57:07
function so if you would like a copy of all this code i will post all of this to the
comment section down below
4:57:12
but yeah that is how the map function works in python
57.filter
4:57:17
what's going on everybody it's you bro hope you're doing well and in today's video
i'm going to show you all how the
4:57:22
filter function works in python so sit back relax and enjoy the show
4:57:30
all right all right let's discuss the filter function the filter function creates a collection
4:57:35
of elements from an iterable for which a function returns true now what the heck
does that mean let me
4:57:42
explain let's say that we have a list of tuples named friends each tuple has the name
of a
4:57:48
friend as well as their age i just made up some ages for all of these friends what i
would like to do is
4:57:53
to create a separate list for all the friends that are 18 or older so let's say that we're
all going out
4:57:59
drinking and i would like to create a list of my drinking buddies because they're of
drinking age i'm in
4:58:04
the united states and the drinking age is currently 21 but i think worldwide is 18 so
let's go
4:58:09
with 18 for this example since i have a worldwide audience all right now with this
filter function
4:58:15
we have to pass in our iterable as well as a function to get anybody's edge that is
over 18.
4:58:21
so let's begin by writing a lambda function for this expression here our filter function
so let's say age equals lambda
4:58:30
data will be the parameter colon data at index one so the first index
4:58:38
the first column in this chart here would be zero for other names the next index of
one would be all the ages
4:58:45
data at index one is greater than or equal to eighteen and that is it
4:58:51
that is our lambda function and now we're going to filter our iterable by this function
of age so
4:58:57
let's write filter pass in our function which is edge comma and our iterable
4:59:06
friends now this filter function will return a filter object you can easily cast this i would
4:59:11
like to cast my friends list back into a list
4:59:17
and i will assign this to a new list let's say drinking buddies
4:59:24
equals list filter age and friends and i should be able to
4:59:30
print all of the friends that are 18 or older for i in drinking bodies
4:59:38
print i and we have rachel monica chandler and
4:59:44
ross well everyone that is the filter function it creates a collection of elements
4:59:50
from an iterable for which a function returns true i like to think of it as the search
4:59:55
results we're searching for any results that meet this criteria where age is greater
than or equal to 18
5:00:02
and then you can create a separate collection or other list if you want from the
results but yeah that is how the filter function
5:00:08
works in python yo what's going on everybody it's bro hope you're doing well
58.reduce ♻️
5:00:13
and in today's video i'm going to show you how the reduce function works in python
so sit back relax and enjoy the show
5:00:22
well i suppose we're on the reduce function today the reduce function applies a
function of our choosing
5:00:28
to an iterable and reduces that iterable to a single cumulative value the way
5:00:34
this works is that the reduce function performs our function on the first two elements
of our
5:00:39
iterable and repeats the process until only one value remains i kind of
5:00:44
like to think of it like we're recycling elements within an iterable until a single value
remains our finished product so
5:00:51
how is this useful let's say that we're playing a game scramble and we have a bunch
of different letters within
5:00:57
an iterable a list is a type of iterable so i have a list named letters and we have all of
5:01:03
the letters we need to spell the word what i would like to do is to reduce all of these
individual elements into a
5:01:09
single cumulative value until only one value remains so we can do that using
5:01:14
the reduce function you can also do this with the for loop too there's multiple ways of
doing this so using the reduce function we're going
5:01:21
to import func tools and now let's say we have
5:01:28
our cumulative value stored within a variable named word word equals func tools dot
reduce
5:01:36
now the reduce function has two arguments the function we would like to apply to the
first two elements and then our
5:01:43
iterable the interval is easy that is just letters so let's put that in letters and for the
function we can
5:01:50
easily write a lambda function for this lambda and now this function needs two
5:01:55
parameters let's say x and y now what kind of function do we want to perform on the
5:02:02
first two elements of our iterable let's combine them together so let's say x
5:02:07
plus y and now let's print our single cumulative value
5:02:14
of word so this should print the word hello i'm fairly confident that
5:02:21
the word hello is worth like 20 points in scrabble now let me explain what happened
step by step our reduce function applies our
5:02:28
function to the first two elements within our iterable it performs this expression that
we set
5:02:35
x plus y but really we can write anything here so we perform this expression on the
5:02:41
first two elements then we repeat the process again using
5:02:46
the result from the first time we use this expression and the next element so we're
combining x plus y again
5:02:54
and then we're just going to repeat this process until only one value remains it's kind
of like
5:03:00
we're recycling all of these letters and creating a finished product out of all of these
that's how i think of it at least here's
5:03:07
a different example let's say that i would like to find the factorial of 5 so i have the
numbers
5:03:13
5 through 1 within a list named factorial so if i was to find the factorial of 5
5:03:20
via the reduce function i would probably write something like this so let me change
word to let's say result result
5:03:28
equals functools reduce now our lambda function will be x times y because with
factorials we
5:03:36
multiply the next two numbers together then we take that result and then multiply the
next one in this
5:03:42
list so the iterable will be factorial and let's print what our result is
5:03:51
so the factorial of 5 should be 120. now let's walk through this we're taking
5:03:56
the first two elements of this iterable five and four multiplying together in this
5:04:02
case in the last example we concatenated the strings together of x and y in this
example we're multiplying them
5:04:08
together so we take the first two elements 5 times 4 we take the result and apply it to
the
5:04:15
next iteration of x times y again so 20 times 3 is 60. we repeat this process again
5:04:24
60 times 2 is 120 and 120 times 1 is 120 so that's kind of how the reduce
5:04:31
function works it applies a function of our choosing to the first two elements of an
iterable and repeats
5:04:38
that process until only a single cumulative value remains so that is the reduce
function if you
5:04:44
would like a copy of all this code i will post all of this to the comment section down
below but yeah that is how the reduce function
5:04:51
works in python hey what's going on everybody it's your bro hope you're doing well
and in this
59.list comprehensions
5:04:58
video i'm going to show you all how list comprehensions work in python so sit back
relax and enjoy the show
5:05:06
all right everybody so list comprehensions a list comprehension is a way to create a
new list with
5:05:12
less syntax you can also use a list comprehension to mimic certain lambda functions
such as in the
5:05:18
previous videos where we use the map and the filter functions and not only that but a
list comprehension
5:05:24
is easier to read than a lambda function but there is a formula that we're going to
follow
5:05:29
when we create our list within our list we're going to write an expression for item in
iterable now let me give you
5:05:37
an example of where a list comprehension would be useful let's take this program
that will create
5:05:42
a list of all the numbers 1 through 10 squared so if i were to print this we'll get the
numbers 1
5:05:48
through 10 whatever their square is right so we have 1 through 10 and the square of
10 is 100 so we wrote
5:05:55
this program in three lines of code we're creating an empty list we're creating a for
loop and then we're writing an expression for
5:06:01
what we want each loop iteration to do now let's write the same program but used a
list comprehension
5:06:07
and we can write the same program with less syntax so i would write something like
this and
5:06:12
we're going to follow this formula so our list is going to be named
5:06:17
squares equals then within a set of straight brackets we're going to write our
expression
5:06:24
which is this portion i times i for item so that would be 4i
5:06:31
in our iterable that would be range 1 through 11 because remember 11's
5:06:38
exclusive and then let's print our squares print
5:06:43
squares and there you go we made the exact same program using a
5:06:49
list comprehension so basically speaking you just follow this formula you set your list
equal to
5:06:55
your expression and our expression for this example is i times i for item for i in
5:07:03
your interval in range one through eleven so this took three lines of code well if
5:07:09
you exclude us printing our list and this program took one line of code
5:07:14
now we can also use a list comprehension to mimic certain lambda functions i wrote
a program
5:07:20
where we will be filtering a list of student grades let's pretend that all of these
students
5:07:25
took some sort of exam so we have one student that received a 100 90 80 70 60 50
40 30
5:07:32
and then one student got a zero because they didn't show up so what i would like to
do is to filter all of these student grades into a list
5:07:39
that only contains students that passed and the criteria for a passing grade will be
5:07:44
a 60 or above so i would like to filter all passing students and if i was to
5:07:50
write this program using the filter function i would probably write something like this
so i need a function and then my list
5:07:56
that i'm working with lambda x colon x is greater than or equal to 60
5:08:02
will filter all of the results and then convert it to a list named past students so the
result will look something like
5:08:08
this i have five student grades in here with 100 90 80 70 and 60.
5:08:14
now let's write the same program using a list comprehension now there's just one
portion to this formula for list comprehension that
5:08:20
we're going to add at the end we're going to add if conditional we can check some
sort of condition after each
5:08:27
iteration so let's write the same program using a list comprehension following this
new updated formula that we have so
5:08:34
we need our list which will be named past students equals first comes our
5:08:41
expression and it's just i for this example for item i in our iterable
5:08:48
of students and then if we can check our conditional if i is greater than or equal to 60
5:08:56
and this will have the same effect as our previous program but instead it uses a list
comprehension
5:09:03
now if your program has multiple output if you need an else statement within your
conditional
5:09:08
you're actually going to move this portion of the if conditional within our formula to
right after the
5:09:13
expression so let's write an if else statement here so let's say that instead of just
5:09:20
excluding any student that didn't pass we'll replace their grade with the word failed
and we can do that
5:09:26
using an if else statement so let's do this again i'm going to copy what we have here
5:09:31
paste it and we'll move our conditional from the end to just after the expression
portion i
5:09:39
if i is greater than or equal to 60 and you can see here that we need to add an else
statement else will return
5:09:46
the word failed so we're replacing each instance of
5:09:54
a failing grade with the word failed instead so if your condition returns whatever i is
you can just add
5:10:00
that to the end if you need an if else statement you're going to add it right after the
expression
5:10:05
so everybody in conclusion a list comprehension is a way to create a new list with
less syntax you can even mimic
5:10:13
certain lambda functions which we did with the filter function and it has the added
benefit of being easier to read than a
5:10:19
lambda function so you just follow one of these formulas depending on what you're
trying to do so you need at least list equals your
5:10:27
expression for item and your interval if you have a condition that you want to check
you just add that to the end of your
5:10:33
list comprehension if you have an if else statement then you'll add that to right after
the expression so you just follow one of
5:10:40
these formulas depending on how you want to write your program so that is a list
comprehension if you
5:10:45
would like a copy of this code i will post all of this to the comments section down
below and well yeah that's how list comprehensions work
5:10:52
in python yo what's going on everybody it's bro hope you're doing well
60.dictionary comprehensions
5:10:57
and in this video i'm going to explain dictionary comprehensions in python so sit back
relax and enjoy the show
5:11:06
okay people dictionary comprehensions they're very similar to list comprehensions
except
5:11:11
they're with dictionaries that's it video over okay but seriously a dictionary
comprehension is a way to
5:11:17
create dictionaries using an expression and they can replace for loops and certain
lambda functions
5:11:22
and all we have to do is follow this formula dictionary equals key colon our
expression for key value
5:11:29
in iterable so let's go over a few examples our first example we're going to take this
dictionary named cities in f f is
5:11:37
short for fahrenheit i have different city names as keys and relative temperatures as
5:11:42
values in fahrenheit so new york is 32 degrees fahrenheit boston is 75.
5:11:48
los angeles is 100 and chicago is 50. and what we'll be doing is creating a separate
dictionary where all of these
5:11:55
temperatures will be in celsius using a dictionary comprehension so let's follow this
formula let's create a
5:12:02
new name for this dictionary let's say cities in c short for celsius
5:12:07
equals and we will follow this pattern first our key golden then our expression we'll go
back
5:12:13
to this in just a moment for key comma value in our iterable
5:12:21
of cities in f and since we're working with a dictionary we're going to use
5:12:27
the items method now we just need to fill in this expression there is a formula to
convert fahrenheit
5:12:33
to celsius and it should be on the screen right about now so let's follow this formula
so we'll
5:12:38
take our value minus 32 and just for good measure i'm going to put these within
parentheses
5:12:45
and we will multiply all of this by 5 divided by 9 and that is it so we have
5:12:52
our key our expression to convert fahrenheit to celsius
5:12:58
for key value in our iterable of cities in f our previous dictionary and
5:13:04
let's test this by printing our new dictionary cities in c
5:13:12
actually i think i'm going to round these numbers just so it's more readable so let's
round all this there we go so
5:13:19
new york and celsius is zero boston is 24. los angeles is 38 and chicago is 10.
5:13:26
so we created a new dictionary using a dictionary comprehension now with these
dictionary comprehensions
5:13:32
you can add an if conditional to the end of this so let's say that we have a separate
dictionary of weather
5:13:38
like a description of the weather in each city new york is snowing boston is sunny los
angeles is sunny and
5:13:44
chicago is cloudy let's say that we would like to create a separate dictionary with a
dictionary
5:13:50
comprehension that only has cities where the weather is sunny so i would write
something like this
5:13:57
let's say sunny weather will be the name of our dictionary equals and we'll follow this
formula and then we just tack on
5:14:04
our conditional to the end so it's kind of the same as before really so key colon and
we don't really have an
5:14:11
expression here so let's just say value then for
5:14:19
key comma value in our iterable of
5:14:25
weather dot items then our conditional if value
5:14:33
sunny because we are only looking for sunny weather and let's print this print
5:14:40
our dictionary of sunny weather and let me just make this more readable
5:14:45
for you guys and our new dictionary comprehension will create a dictionary of key
values
5:14:51
where the value is sunny using an if conditional at the end of our dictionary
comprehension
5:14:57
here's a third example for you well if you have an if else condition you can add that to
where the expression is
5:15:02
within your dictionary comprehension so i'm going to reuse the previous dictionary
for the first example where
5:15:08
we have cities and their temperatures as values so we're going to replace each
temperature with a description of the
5:15:15
weather is it warm or is it cold so let's do that using a dictionary comprehension that
contains
5:15:21
an if else statement so let's say we have a new dictionary named desk short for
description
5:15:27
cities it's a brief description of each city's temperature so we follow this formula key
5:15:34
then our if else conditional for key value and iterable actually i probably can
5:15:40
copy all this just to save some time okay so our iterable
5:15:45
will be cities dot items and our conditional is going to be
5:15:53
we will return warm if our value is greater than or equal to
5:16:00
let's say 40. else cold and we will print
5:16:07
our new dictionary of desk cities description of cities so new york is
5:16:14
cold because it's 32 that's below 40. boston is warm los angeles is warm and
5:16:19
chicago is warm now if your condition gets somewhat complex you can even call a
separate
5:16:25
function to keep your code more organized so key then we can call a function and
pass in a value
5:16:30
for key value in iterable so i'm going to again reuse our city names and our
5:16:35
temperatures and this time we are going to call a function instead i think i'll reuse this
code from the
5:16:41
previous example where we have a separate dictionary named desk cities and i'm
going to replace
5:16:47
this if else statement with a function so let's say we have a function that is named
5:16:52
check temp and we will pass in our value and we just need to define this function
5:16:58
so let's define that here so let's define check
5:17:03
temp and our parameter is our value that we're currently working on within our
dictionary
5:17:09
value then we'll write if value is greater than or equal to
5:17:16
let's say anything above how about 70 will return the word hot
5:17:24
then else if let's say 69 greater than or equal to
5:17:32
value and then value is greater than or equal to
5:17:39
how about 40 then we will return the word warm
5:17:46
and lastly else return cold and let me just fix some of the
5:17:53
spacing now depending on the temperature that is
5:17:58
stored within each value of our dictionary we will call a function that will return
5:18:04
one of a few different outputs so it's kind of like we're using the map function the map
lambda function
5:18:11
so let's print our description of cities and we should get a short
5:18:16
description of the temperature in each city after we call the check temperature
function and then pass in each value
5:18:23
from our dictionary so new york is cold because it's 32 degrees fahrenheit boston is
hot because it's above 70. los
5:18:31
angeles is hot because it's 100 and then chicago is warm because that is 50 which is
between
5:18:36
69 and 40 within our function so that is a dictionary comprehension
5:18:42
it's a shortcut where you can create dictionaries using an expression and they can
replace for loops and
5:18:48
certain lambda functions so if you would like a copy of all this code i will post all of
this to the comment section down below
5:18:54
and well yeah that's how dictionary comprehensions work in python hey what's going
on everybody
61.zip function
5:19:00
it's bro hope you're doing well and in today's video i'm going to show you how the zip
function works in python so
5:19:06
sit back relax and enjoy the show
5:19:11
all right ladies and gentlemen welcome back we're talking about the zip function
today the zip function will aggregate elements
5:19:18
from two or more iterables iterables are those things like lists tuples
5:19:23
sets etc and the zip function will create a zip object with paired elements from
5:19:29
each iterable stored in a tuple for each element within our zip object here's an
example let's say that we have
5:19:37
two different types of iterables i have a list of usernames and a few usernames within
here are
5:19:43
dude bro and mr and i have a tuple of passwords and i have some very
5:19:48
secure passwords such as password abc123 and guest what i would like to do is to
5:19:55
zip elements from each iterable together so that they're in pairs and each pair is
going to be stored as a tuple within
5:20:03
a zip object and here's how we can do that let's say that we will create a zip object
called users
5:20:09
and we will use the zip function the zip function will take a varying amount of
iterables we're going
5:20:14
to pass in our usernames and passwords and zip them together so let's pass in
usernames
5:20:20
as well as passwords and now our zip object of users is actually iterable zip objects
are
5:20:26
iterable so we can use them within a for loop so let's type for i in users
5:20:32
and print i and what we get is that we have a zip
5:20:39
object of tuples and each tuple is storing each pair of elements from our
5:20:44
two iterables now users is a zip object and if you don't believe me let me prove it i'm
going to print
5:20:51
the type of users and this will print that users is indeed a zip object
5:20:58
but you can easily convert this to a different type of iterable by using a cast
5:21:03
let's say that we would like to convert our zip object into a list so we'll surround the
zip function with
5:21:09
a cast to a list and now the data type of users is now a list what we have is a list of
tuples and
5:21:16
each tuple is storing a pair of elements from our two iterables of usernames and
passwords
5:21:22
now currently since we're passing in only two different iterables we can easily make
this a dictionary so that these are key value
5:21:30
pairs so let's cast our zip object as a dictionary and to display all of the elements
5:21:36
within our dictionary all the key value pairs we're going to change our for loop to this
for key value
5:21:43
in users dot and we will use the items method print key
5:21:50
comma value actually i think i'm going to separate each of these with a colon just to
make it more readable
5:21:58
and now when we zip these two intervals together we end up with a dictionary of
usernames
5:22:04
and passwords and the name of this dictionary is users now you're not limited to just
two
5:22:11
iterables you can add a third iterable or more so this time let's create a
5:22:16
maybe a list a list of last login dates and i'll just call this login date equals and why
not make a
5:22:23
list and let's make up some dates let's say 1 1 dash 20 21
5:22:32
12-20-21 and 1-3-20
5:22:38
21 okay so let's create a zip object of users and we're going to
5:22:46
zip user names passwords
5:22:52
and login date and let's iterate over this for i
5:23:00
in users print i now we have a tuple for each element
5:23:08
and instead of a pair we now have a trio i guess of all of the different elements
5:23:14
from each iterable so in conclusion the zip function will aggregate elements
5:23:19
from two or more iterables and create a zip object with paired or grouped together
elements
5:23:25
stored in a tuple for each element within our zip object so that is the zip function if
you would
5:23:32
like a copy of this code i will post this to the comment section down below and well
yeah that's how the zip
5:23:38
function works in python hey what's going on everybody it's you bro hope you're
doing well
62.if _name_ == '__main__'
5:23:44
and i'm going to explain the purpose of if name equals main in python so sit back
relax
5:23:51
and enjoy the show during your programming journey you may
5:23:56
have encountered this strange statement of if name equals main now what the heck
5:24:01
does that mean so with python files also referred to as modules by including this
statement it
5:24:08
gives our modules some flexibility one a module that has the statement can be run
as a standalone program
5:24:15
or two this module can be imported and used by other modules if there's
5:24:20
some sort of useful functions or other resources within this module to be imported
5:24:25
by including the statement of if name equals main we're checking to see if a user
5:24:30
is running this module as either a standalone program or they're importing it from
another
5:24:36
module behind the scenes the python interpreter will set special variables
5:24:41
one of which is double underscore name it's a variable and python will assign the
name variable
5:24:48
a value of main if it's the initial module being run and we can actually test that by
5:24:54
printing double underscore name so since this is the initial module being run
5:25:01
module one for my example name is going to be assigned a value a string of main
now check this out i
5:25:09
have two modules what if i were to import module 2 and check the name
5:25:14
variable of module 2. let's try it import module
5:25:20
two in order to access a variable from
5:25:26
another module i need to type the name of the module module two dot and i would
like to check
5:25:32
the special name variable of module 2 and print it so when you import a module this
name
5:25:40
variable is going to be assigned the name of the module in this case module 2. this
time what if we change the roles
5:25:46
around i'm going to copy all of this text and paste it within module two we're
5:25:52
going to import from module one we will print that special name variable of
5:25:59
this module module 2 and then print the name variable of module 1 which is being
imported now
5:26:06
when i run this program i'm going to instead run from module 2.
5:26:12
now this time that special name variable of module 2 is main and the special name
5:26:19
variable of module 1 is module 1 the name of the module
5:26:24
so by including a statement such as if name equals main we're checking to
5:26:30
see if this module is being run directly or indirectly so let's test it
5:26:36
within this statement of if name equals main let's print running this
5:26:43
module directly else that must mean that we're running
5:26:49
this module indirectly so let's print
5:26:54
running other module indirectly
5:27:00
okay so i'm going to go to module 1 and run from here running this module
5:27:07
directly now let's do the same thing with module two but we need to
5:27:12
import module one and let's run for module two
5:27:24
running other module indirectly for the time being i'm just going to write pass within
5:27:29
our if name equals main as a placeholder and get rid of our if statement so let's say
within module 1 there's a
5:27:36
useful function or resource we would like to access from module 2. so let's say we
have a
5:27:41
function named hello and all this will do is print the word hello
5:27:47
let's just pretend that it's a useful function not really but let's pretend so in order to
use this
5:27:54
function from module two i need to import module one then type module one dot and
5:28:02
then the name of the function so i'm going to run from module 2 and this will print
5:28:08
hello but what if i run this program from module 1. so right now module 1 cannot be
run
5:28:16
directly as a standalone program so i cannot print the word hello so what
5:28:21
i could do is within if name equals main i can call that hello function directly
5:28:28
so let's run from module one hello one other thing that you might see
5:28:34
too within the if statement if name equals main there might be a call for
5:28:39
a main function and you can write the main body of your program within a main
function so everybody in
5:28:46
conclusion the reason that people may include this statement if name equals main is
that it allows our
5:28:52
modules to have some flexibility they can be run as a standalone program or they
can be imported and used by
5:28:59
other modules and this is because the python interpreter sets that special variable of
name with a
5:29:06
value of main if it's the initial module being run if you would like a copy of my code
and
5:29:12
my notes i will post all of this to the comment section down below but yeah that's the
purpose of if name
5:29:19
equals main in python hey what's going on everybody it's bro hope you're doing well
and in
63.time module
5:29:25
this video i'm going to show you all a few useful functions related to times and dates
using the time module in
5:29:31
python so sit back relax and enjoy the show
5:29:37
alright everybody so the time module let's begin by importing time and the first thing
that i'm going to
5:29:42
explain to you while is how we can find our computer's epic also pronounced as
epoch so this is a
5:29:49
date and time in which your computer thinks time began think of it that way at least
5:29:54
so we use our epic as a reference point so to find your computer's epic it's
5:29:59
going to vary based on your computer and your operating system so to find your
computer's epic type
5:30:04
time dot c time and as an argument we will pass in zero
5:30:10
now what this method will do is that this method will convert a time expressed in
seconds and convert it to a
5:30:16
readable string so if i was to pass in 0 and print it well then this will display the date
and time
5:30:23
which is my epic our reference point so for me my epic is wednesday december 31st
6 p.m
5:30:31
1969 so i've just added a note that the c time method will convert a
5:30:37
time expressed in seconds since epic and convert it to a readable
5:30:42
string a readable date and time so for practice let's pass in perhaps
5:30:47
1 million seconds and see what date and time that we receive so our c time method
will return a date
5:30:54
and time one million seconds past this epic here so that would be for me monday
january
5:31:02
12th about 7 a.m 1970. our next method is the time method of the time module let's
5:31:09
print time dot time method and what this method will do is return the current seconds
that have
5:31:16
passed since our epic using our computer's clock so the number that i end up with is
just
5:31:23
over 1.6 billion and this is in seconds so for me 1.6 billion seconds
5:31:30
has passed since that date which is my epic i believe it was december 31st 1969
5:31:37
and every time that i rerun this program you can see that the amount of seconds that
has passed is actually increasing
5:31:44
so that is the time method it will return the current seconds since you're epic using
your computer's
5:31:51
clock and you know what just for fun let's change the date and time under clock and
see what happens
5:31:57
so let's change the year to how about the year 2000 change
5:32:04
and close so let's see what number we get with the time method now
5:32:09
so we get just under 1 billion 948 million seconds have passed
5:32:16
and now if you need to retrieve the current date and time well there's one of a few
ways in which you could retrieve that but one way is
5:32:23
that we can combine both of these methods of the see time method and the time
method of the time
5:32:29
module so let's print time dot c time and we're going to pass in an
5:32:35
amount of seconds into the c time method as an argument so within the c time
method we're going to call
5:32:42
the time method so the time method will return an amount of seconds since our epic
and the c time method
5:32:49
will convert that amount of seconds to a readable date and time so the current date
and time is saturday
5:32:57
january 23rd about 3 p.m 20 21 now there is more than one way to get
5:33:02
the current date and time another way is to use the local time method and the local
time method will
5:33:08
create a time object based on the current time so what i'm going to do is
5:33:14
create a variable called time object and i just need to explain a few things
5:33:19
about time objects so to best explain this i'm going to print our time object now a time
object is also
5:33:26
referred to as a struct time object it is made up of different keyword arguments
5:33:32
there's a year a month day hour minutes seconds day of the week
5:33:39
day of the year and this keyword argument here has something to do with daylight
savings time
5:33:44
so there's quite a few uses with time objects and one way is that we can format them
5:33:50
however we want because right now this time object is not in a readable format
5:33:55
so to convert this time object into a readable string we'll need the help of a separate
function
5:34:01
and that is the strf time function str is short for string f4 format and
5:34:08
time well for time i guess so this function needs two arguments a format and a time
5:34:15
object so our strf time function will accept a format and a time object as an
5:34:22
argument so our format is really just a string of different directives and to best
5:34:28
explain these i'm going to head to python's official documentation on the subject so
here i am on python's website
5:34:35
regarding the time module and underneath this section on the strf time function
5:34:41
there are different directives that we can embed within our format string that we pass
in as an argument
5:34:47
depending on the directive that we add we can display a certain format of our date
and time
5:34:54
so for example if i was to pass in let's say percent lowercase a then we will display
5:35:01
the time object's weekday name and you're not limited to just one directive you can
add any combination of
5:35:07
directives so if i was to add percent m well we would display the month of our date
time object as a
5:35:14
number 1 through 12. so there's a bunch of directives here and i'm going to be using
some of these
5:35:20
so within a string for the format argument i'm going to pass in percent capital b for
the name of the
5:35:27
month percent d for the day percent y for the year
5:35:35
percent h for the hour and to format this i'm going to add a colon to separate hours
and minutes percent
5:35:43
m for minutes colon percent s for seconds and then we are going to
5:35:51
assign all of this to a variable let's say local time and local time will be a string
5:35:58
so let's print our local time and the current time is january
5:36:06
23rd 2021 about 3 p.m oh and i almost forgot you can also get
5:36:12
the utc time that is the coordinated universal time if you know how that works
5:36:18
so if you need that you would just use the gm time method for the utc time
coordinated
5:36:25
universal time okay next up we have the strp time function and this function will
5:36:32
parse a string representation of a time and or date and return a time object so we
need to
5:36:39
pass in a string representing the date and or time as well as a format string so let's
create a time string and
5:36:47
this variable is going to be a string representation of a date let's say 20th of april
2020
5:36:54
and what we can do is take this string representation of a time and or date and parse
it to a time object so we're
5:37:01
going to pass in our time string variable as well as a format string
5:37:06
so let's say i would like to parse the day so that would be percent d for
5:37:12
day then percent b for name of the month
5:37:17
and then comma percent y for year this function will create a time object
5:37:23
so let's assign that to a variable time object equals time dot strp time
5:37:29
we're passing in our string representation of a time or date as well as a format string
5:37:35
and we can print our time object using a print statement however this is going to be
in a form
5:37:41
that is somewhat difficult to read but you can see at least we have a time object with
all of these keywords filled in
5:37:48
with anything that we passed in via these format directives that we have now the
next function
5:37:53
is the asc time function and this function accepts a time object
5:37:59
or a tuple representation of a relative time so this time let's create a
5:38:04
time tuple and we're going to follow this order we can pass in up to nine values the
first value is a
5:38:12
year so let's pass in perhaps 2020 a month let's say four a day how about 20 four
hours
5:38:20
let's say four minutes 20 and seconds maybe zero let me just fix some of the
5:38:26
spacing here okay you can also pass in a numbered day of the week um i'm just
going to say zero i don't
5:38:33
think it's really that important a day of the year zero and negative one or zero for
daylight
5:38:39
savings time so we created a time tuple and we can pass in a time object or a tuple
representation of a
5:38:47
time following this formula so let's pass in our time tuple and this will create a time
string a
5:38:55
string representation of the time that we create and let's print our time string
5:39:02
and we should have april 20th about four in the morning the year 2020.
5:39:08
so that is asc time it will convert a tuple representation of a time and date
5:39:15
or a time object and convert it to a readable string now another option is to use m
5:39:21
k time and mk time will take a tuple representation of a time or a time
5:39:28
object and convert it to seconds since epic so april 20th of the year 2020
5:39:35
is about 1.5 billion seconds since our epic date and for me that was
5:39:40
december 31st the year 1969 so that is a few useful functions of the
5:39:47
time module if you would like a copy of this code i will post all this to the comment
section down below
5:39:53
and well yeah that's the time module in python yo what's going on everybody it's bro
64.threading
5:40:00
hope you're doing well and in this video i'm going to explain multi-threading in python
so sit back relax and enjoy the show
5:40:09
think of a thread as a flow of execution like a river and each thread can carry out its
own
5:40:15
separate order of instructions if we use this process of multi-threading we can have
our program
5:40:21
run different parts of its program at different times they all run concurrently but not
truly
5:40:27
in parallel that is a concept for later referred to as multi-processing so with reds they
each take a turn
5:40:34
running to achieve concurrency this is due to a notorious feature known as the gil
5:40:40
the global interpreter lock only one thread can be running at one time but they can all
take turns when one
5:40:47
thread is idle so this allows one thread to hold control of the python interpreter
5:40:53
at any one time so they run concurrently but not truly in parallel which is what we do
with
5:40:59
multi-processing now programs and tasks can be divided into two different
categories they can be cpu
5:41:05
bound that is a program or a task that spends most of its time waiting for
5:41:10
internal events such as a task that is cpu intensive it is better to use multi-processing
for
5:41:17
tasks that are cpu bound now tasks that are io bound means that
5:41:22
the program will spend most of its time waiting for external events such as waiting for
user input or if
5:41:29
you're doing activities like web scraping you do a lot of sitting around so with i o
bound tasks it's better to use
5:41:35
multi-threading because we can have multiple threads running concurrently but not
truly in parallel like what we
5:41:42
do with multi-processing after importing the threading module we can count the
number of threads that are
5:41:48
currently running in the background whenever we run a program we have one thread
that is running that is in charge of
5:41:54
executing our program and we can print the active count of threads running in our
program
5:41:59
using the active count function of the threading module so this will print one we have
one
5:42:05
thread that is running and we can print a list of all the threads that are running
5:42:11
by using the enumerate function so the one thread that is in charge of
5:42:16
running our program is referred to as the main thread by using this concept of multi-
threading we
5:42:22
can have more than one thread running concurrently not truly in parallel all the
threads will take turns while
5:42:28
one of them is idle so we can have more than one thread running more than just the
main
5:42:34
thread which is in charge of running the main body of our program so while our main
thread is in charge of
5:42:39
running the main body of our program we can have another thread that's in charge of
a separate part of it
5:42:45
maybe like a countdown timer or something so one good example take that quiz
game
5:42:50
that we made some number of videos ago while we were waiting for user input which
is a
5:42:56
i o bound task we could have had a countdown timer going like you only have so
many seconds to
5:43:01
answer this question we could have had one thread in charge of waiting for user
input and another thread
5:43:06
in charge of the countdown timer so that's an example of multi-threading we had two
threads running concurrently
5:43:14
and what we'll be doing in this video is creating a program that involves multi-
threading we can have
5:43:19
different threads in charge of different parts of our program and they can all run
concurrently they'll all take turns
5:43:25
while one of them is idle so let's say that we're running late for school or work in the
morning and we have three
5:43:32
different tasks that we need to complete before we can leave for school or work so
think of three
5:43:38
different things you do in the morning such as maybe eat breakfast some people
drink coffee or
5:43:44
maybe a beverage of your choice so drink coffee and some people like me they like
5:43:51
to do their homework last minute so i'll say study before i leave for work or school in
the
5:43:57
morning so what we'll do in each of these functions each of these functions should
take some amount of time to
5:44:03
complete so we can have our main thread sleep for a given number of seconds
using the sleep function
5:44:10
time dot sleep and let's say that in order for me to eat breakfast this task will take me
three seconds
5:44:16
let's just pretend that instead of minutes this will be in seconds so drink coffee will
take me
5:44:23
four seconds and study will take me five seconds then when we finish
5:44:30
sleeping let's print a confirmation message let's say
5:44:35
you eat breakfast as in you finish eating breakfast with
5:44:41
drink coffee you drink coffee
5:44:48
and with study you finish studying
5:44:56
now each of these tasks are i o bound they're going to be spending a lot of time just
waiting around
5:45:01
for external events they're waiting for the sleep function to expire before they can
finish their task
5:45:07
so we're going to have all of these three functions run on our main thread and we'll
see how long it takes for us
5:45:13
to complete our morning ritual these three tasks so let's call these three functions
within
5:45:19
our main thread so let's call the eat breakfast function first followed by
5:45:26
drink coffee and then study in that order so this program is going
5:45:33
to take approximately let's see 12 seconds to complete so there's going to be a
pause for a second
5:45:40
you eat breakfast then followed by you drank coffee and then study
5:45:50
you finished studying so this program took about 12 seconds overall so if this were
5:45:56
realistic what we would have done is we would sit down and eat breakfast for three
minutes well three seconds in this
5:46:03
case and then once we finish eating breakfast only then are we allowed to drink our
coffee
5:46:09
and once we finish our coffee only then can we study so we completed these tasks
sequentially
5:46:16
and not concurrently for us to move down to the next function we need to complete
the
5:46:21
previous functions because we're doing this in order but realistically us human
beings we
5:46:27
would probably eat breakfast drink coffee and study altogether because we can
multitask and we can complete
5:46:34
these three functions in less time and that's kind of the same process as multi-
threading we can have
5:46:40
these three separate functions running concurrently as if we're multitasking we're
eating
5:46:45
breakfast drinking coffee and studying all at once now currently we have one thread
that is in charge of these
5:46:52
three separate functions what we could do is that we can create three additional
threads
5:46:57
each thread will be in charge of each task and then we'll have our main thread
running in the background that will
5:47:03
complete the rest of the program so this is how to create an additional thread let's
say that
5:47:08
x equals threading dot thread we need to pass in a target
5:47:15
target equals and then the name of the function so let's say
5:47:21
thread x will be in charge of eating breakfast and then you can pass in arguments
too
5:47:26
if your function has parameters by typing args and then you pass in a tuple so let's
say you have one argument to pass in
5:47:33
you'll type your argument followed by a comma but we don't have any arguments in
this example but
5:47:38
you'll need to be sure to enter them in if you do have any okay then to begin this
thread you type
5:47:44
the name of the thread in this case x dot start so we now have an additional thread
and
5:47:50
this thread is in charge of eating breakfast now let's create another thread to drink
5:47:55
coffee and we'll call this y
5:48:01
and the target will be drink coffee and lastly we have a thread in charge of
5:48:08
studying and this will be z
5:48:14
and the target is our study function okay now let's see how long it takes for
5:48:19
us to complete our program oh and be sure to comment out these function calls
within the main thread
5:48:25
because we don't want the main thread in charge of those anymore okay now we
can run the program so we
5:48:30
have four threads running this time you eat breakfast you drink coffee you finish
studying
5:48:35
so this program took approximately five seconds to finish and the reason that this
program took
5:48:41
five seconds instead of 12 is because before our main thread was in charge of
running these tasks sequentially in order but
5:48:49
now since we have a thread dedicated to each task we can run them all concurrently
instead of sequentially so
5:48:56
that this program now took about five seconds to complete and you may have
noticed too that the activecount function as well as
5:49:03
the enumerate function were called before threads 1 2 and 3 finished their respective
tasks
5:49:09
that's because the main thread is not going to wait around for these three threads to
complete it has
5:49:14
its own set of instructions to do so it is no longer in charge of these
5:49:19
three functions the program is going to handle those three functions to our three
threads
5:49:25
and our main thread is going to continue its own set of instructions its job is to print
the active count as
5:49:31
well as call the enumerate function which it did and it finished its tasks before
threads one two and three now one
5:49:39
trick that you can do too is that you can use the time modules performance counter
function and this function will
5:49:46
return how long it takes our calling thread as in our main thread to finish its set of
instructions so our
5:49:53
main thread is not in charge of executing these three functions our main thread is
5:49:59
in charge of creating three additional threads and then calling the activecount
function
5:50:04
and the enumerate function as well as the performance counter function so our main
thread will take
5:50:11
approximately 0.075 seconds to complete and our three threads are still running
5:50:17
in the background our main threads job its order of instructions is to create
5:50:22
three additional threads and then immediately print whatever is returned via the
5:50:28
activecount function enumerate and then your performance counter so our main
thread says that it finished
5:50:35
its tasks in about .07 seconds but threads one through three were still trying to catch
5:50:42
up that's why the main thread finished before are three additional threads and once
5:50:47
all active threads have completed their tasks then your program will finish and exit
5:50:52
there's also this concept called thread synchronization we can have a calling thread
in this case our main
5:50:58
thread wait around for another thread to finish before it can move on with its own
instruction set so let's
5:51:05
say we would like our main thread to wait around for thread 1 also known as
5:51:10
x so we're going to use the join function of thread x and now our main thread
5:51:17
has to wait around for thread x also known as thread 1 to finish before it can move
on
5:51:22
with its instruction set so let's do the same with y and z so now our main thread
before it can
5:51:30
move on with the rest of the program has to wait for all of these threads to
synchronize and join and then and
5:51:36
only then can it move on with the rest of its own instruction set so this time our
program is going to
5:51:42
look a little something like this you eat breakfast you drink coffee you finish studying
5:51:47
so by the time we reach our active count these threads are already joined and
synchronized
5:51:52
they're no longer active when we enumerate over our active threads we only have
our main thread and our main thread
5:51:59
completed in about 5.1 seconds this time because it did a lot of waiting around
5:52:04
it was waiting for threads x y and z to join and finish in conclusion a thread
5:52:10
is a flow of execution like a separate order of instructions that a program can
5:52:16
follow and when we run a program we always have at least one thread running
initially and that is
5:52:22
referred to as the main thread however if you have multiple threads using the
concept of multi-threading
5:52:28
we can have multiple threads running concurrently but not truly in parallel which is
what we'll cover
5:52:34
during the video on multi-processing this is due to a feature known as the gil
5:52:40
the global interpreter lock which allows only one thread to hold control of the python
5:52:46
interpreter at any one time and tasks can be either cpu bound or io bound a cpu
bound task
5:52:53
is a program or task that spends most of its time waiting for internal events
5:52:59
such as a task that is cpu intensive and o bound tasks spend most of their
5:53:05
time waiting for external events such as waiting around for user input
5:53:10
or web scraping with i o bound tasks it's better to use multi-threading
5:53:15
with cpu bound tasks it's better to use multi-processing well everybody that is a quick
overview
5:53:21
of multi-threading i will post all of this code to the comment section down below and
well yeah that's how to achieve
5:53:28
multi-threading using python hey what's going on everybody
65.daemon threads
5:53:33
it's you bro hope you're doing well and in this video i'm going to explain demon
threads in python so
5:53:38
sit back relax and enjoy the show okay so demon threads also pronounced as
5:53:46
daemon threads a demon thread is a thread that runs in the background and they
are normally not important for your
5:53:52
program in order to run your program will not wait for demon threads to complete
before exiting whereas non-demon threads they
5:54:00
cannot normally be killed they will stay alive until their task is complete so a few
common uses of demon threads would
5:54:08
be background tasks garbage collection waiting for input or other long-running
processes so
5:54:15
here's an example of why demon threads would be useful let's say that we'll have
two threads
5:54:20
our main thread will be in charge of waiting around for some user input we'll ask do
you wish to exit and in the
5:54:26
background we'll have a timer going that will display how long somebody is logged in
so let's
5:54:32
create a function named timer and what we'll do is just display how
5:54:38
long somebody's logged in so let's say we have a count variable while true we'll use
5:54:46
the sleep function of the time module to sleep for approximately one second
5:54:51
then we'll increment our count variable by one and we'll print a message so let's say
logged in
5:54:59
for count
5:55:06
seconds and let's create a thread that will be in charge of this timer and run
5:55:11
it in the background so let's say x equals threading dot thread the target will be our
5:55:19
function of timer and we need to start this thread x dot start so our thread
5:55:26
x will be in charge of our timer and run it concurrently while we're waiting for some
user input
5:55:32
our main thread will be in charge of waiting for this user input do you wish to exit now
here's the issue
5:55:38
of us using a non-demon thread for a background task such as a countdown timer
5:55:43
so our main thread is in charge of waiting around for some user input and thread x
our additional thread is in
5:55:49
charge of running this countdown timer so if i wish to exit this program i'm just going
to type
5:55:55
okay and hit enter well my background timer is still going but my main thread
5:56:00
is complete so i would like to exit this program but my program will not exit as long
as
5:56:06
there are non-demon threads that are still alive we cannot normally close this
program unless we do like a brute force
5:56:13
you know close and exit of the program so we can turn this thread of x into a demon
thread
5:56:20
so that when we wish to exit the program when there are no more non-demon
threads alive and to change a
5:56:26
thread to a demon thread it's actually fairly easy when you create your thread you
can set this flag of demon equal to true and
5:56:33
that's it so when all non-demon threads have finished their tasks your program will
complete and exit but
5:56:41
if there are demon tasks running in the background they will be killed automatically
so let's try this program again
5:56:47
so do you wish to exit my program is going to continue running my demon thread is
in charge of the timer and it's going to continue running
5:56:53
until all non-daemon threads have completed their tasks so once i type ok my
program will
5:57:02
finish running and my countdown timer will stop because all demons are killed when
your program
5:57:07
is finished running there's also two additional methods that you might be interested in
you can use the set daemon method
5:57:14
of a thread and you can set it to false or true if you would like to change a thread to a
non-demon or a demon
5:57:21
however if your thread is currently running well you cannot actually change it from a
non-demon thread to a demon thread
5:57:28
while it's currently running so you would have to do that before you actually use the
start function
5:57:34
and you can also check to see if a thread is a demon or not by using the is demon
method this will
5:57:40
return true or false so everybody in conclusion a demon thread is a thread that runs
in
5:57:46
the background and they're not important for your program in order to continue
running your program will not wait for demon
5:57:53
threads to complete before exiting the program whereas non-demon threads cannot
5:57:58
normally be killed they will stay alive until their task is complete and demon threads
are commonly
5:58:04
used for background tasks garbage collection waiting around for input or other long
running processes so if
5:58:11
you would like a copy of this code i will post all of this to the comment section down
below but yeah those are demon threads in
5:58:18
python yo what's going on everybody it's bro hope you're doing well
66.multiprocessing
5:58:23
and in this video i'm going to explain multi-processing in python so sit back relax and
enjoy the show
5:58:31
what's up let's talk about multi-processing multi-processing is the act of running tasks
in parallel
5:58:38
on a different cpu cores it's different from multi-threading although it sounds similar
because with
5:58:44
multi-threading we're limited to running one thread at a time because of the gil that
lock which is
5:58:51
used for threading we can run threads concurrently but not in parallel however with
5:58:57
multi-processing we can create processes and we can run each process
5:59:02
in parallel on a different cpu course so with multi-processing it's better for tasks that
are cpu bound
5:59:09
where you need heavy cpu usage whereas multi-threading that is better for i o
5:59:15
bound tasks where you do a lot of waiting around so before we begin i recommend
these two imports
5:59:21
multi-processing and time let's begin quick note if you're running the windows
operating system you'll probably need to
5:59:28
add this line if name is equal to main so when we run a program we have a
5:59:34
main process that is running and if we create a child process from that process it's
going to copy the
5:59:41
module that we're currently working with and that child process will create its own
children processes and it's going to
5:59:48
be a problem so we're going to add this line if name is equal to main so when we
create a child process it
5:59:54
will copy our module but it's not going to execute it so let's create a main function
and a
6:00:00
majority of our code is going to be within our main function if you're running a
different operating system you probably don't have to do
6:00:07
this but if you're running windows you probably will now with multi-processing multi-
processing is
6:00:12
better for tasks or functions that are cpu bound where they require heavy cpu usage
6:00:19
let's say that we have a function named counter and we'll pass in a number to count
up
6:00:24
to but the number we're going to pass in is a ridiculously large number like a billion
so let's create a function that
6:00:30
will count from zero so count equals zero and while count is less than
6:00:38
our number that we pass in we will increment our counter by one so that is the
function that
6:00:45
we'll call with our processes that we create now within our main function we'll create
a process and to do that
6:00:52
you'll need the multi-processing module so import process and cpu account and
6:00:57
we'll save this for a little bit later to create a process let's say we have process a a
equals process this step is very
6:01:06
similar to creating a thread we have a target our target will be our function of counter
6:01:13
and if we have arguments we will pass those in so remember with our arguments we
have
6:01:18
to pass in a tuple since we only have one argument to pass in to differentiate this
from an expression
6:01:25
we have to add a comma at the end so our number let's say is 1 billion
6:01:31
so that's a million 10 million 100 million 1 billion let me just verify that okay
6:01:38
so that's 1 billion we're going to count from zero to a billion and in order to start this
process a
6:01:46
my process use the start function and then i will use some process
6:01:52
synchronization by using the join function my main process is going
6:01:57
to wait around for my child process of a to finish before continuing
6:02:03
and let's print our performance counter so we'll print finished in
6:02:12
let's say time dot performance counter
6:02:19
seconds okay so let's see how long it'll take to count from zero to a billion
6:02:25
using one process i fast forwarded this video but it took my program 56.7 seconds to
count from zero to a
6:02:33
billion but we can speed this up by using multi-processing let's create a second
process named b
6:02:40
and i'm going to divide the amount of work in half so each process will count to 500
million
6:02:47
half of a billion so let's change a to b and i'm gonna group these start
6:02:53
functions together just so it's easier to read and then b
6:02:59
dot join just to synchronize everything okay and then let's change 1 billion to
6:03:06
500 million for each it's the same amount of work but divided among workers
6:03:12
so that is 500 million let's see how well it runs this time so it took my program about
40.3 seconds
6:03:19
to finish counting from zero to a billion but i divided that task among different
processes each of my two
6:03:26
processes counted from zero to 500 million this time so this time let's create
6:03:31
four processes so we have a b c and d
6:03:39
and we'll need to start them and i'm going to join them now this
6:03:48
might not actually speed it up for you and i'll explain why later it depends on the
amount of cpu cores
6:03:54
that you have okay so oh let's change this to so 250
6:03:59
million for each okay see you in a couple seconds
6:04:06
okay welcome back so on my computer when i ran four processes and each was
counting up
6:04:11
to 250 million i could finish my program in 27.3
6:04:17
seconds now for me if i created more processes than this it would
6:04:22
probably take longer and here's why so you can print the cpu count of your computer
6:04:27
using this function of cpu count so if i were to print whatever this function returns i
6:04:34
can get the count of the number of additional processes that i can run for the time
being i'm just going to
6:04:40
comment out this start function because i just want to print whatever this value is so
my cpu count on my computer
6:04:47
is four so i can run four additional processes but let's attempt to run eight processes
6:04:55
so i'm going to copy all these and let's say that we have e f g
6:05:02
and h and we will start all of those two so give me a second just to start them
6:05:10
so a b c d e f g
6:05:18
h and then i'm going to join all of them
6:05:23
e f g and h okay so
6:05:31
let's run this now oh and change the arguments to i keep forgetting to do that so let's
say 125 for each
6:05:40
so 125 million times eight is one billion okay see you in a bit
6:05:47
all right welcome back this time when i had eight processes working it actually took
me longer than when i had four processes
6:05:54
working when i had four processes working it took me about 27 seconds this time it
took 30 seconds when i had
6:06:01
eight processes working that's because i had more processes than my cpu count
6:06:06
and that's partially because whenever you create a process there's significant
overhead with
6:06:11
beginning and destroying a process and if i can only run four processes on my
computer at one time
6:06:18
well then i'm creating additional processes to no extra benefit it's actually hindering
the performance of the
6:06:24
computer because i'm creating all of these additional processes when it really
doesn't help me
6:06:30
in conclusion multi-processing is the concept of running tasks in parallel on
6:06:35
different cpu cores it's similar but different from multi-threading because with multi-
threading
6:06:41
we can run tasks concurrently but they're all taking turns because of the gil
6:06:46
with multi-processing we can run all of these different tasks together in parallel multi-
processing is
6:06:53
better for cpu bound tasks where a task has heavy cpu usage and multi-threading
6:07:00
is better for i o bound tasks tasks that involve a lot of waiting around so that's multi-
processing if you would
6:07:06
like a copy of all this code i will post all of this to the comment section down below
but yeah that is how multi-processing
6:07:12
works in python hey what's going on everybody two bro here hope you're doing well
and in this
67.GUI windows 🖼️
6:07:18
video we're going to be creating our very first graphical user interface in python so sit
back relax and enjoy the show
6:07:27
welcome one and all today we'll be creating our very first graphical user interface in
python also
6:07:32
known as a gui for short we're going to be using the tk inter gui which is a module
that
6:07:38
is included with python our first step in order to use tk enter is to import this module
so that we can
6:07:44
begin using it our first line of code will be from tk enter
6:07:49
import asterisk this will import everything related to the tk inter module
6:07:55
and with this import we can now use all of the gui features that this module has to
offer now there is one important distinction
6:08:02
that we need to make we need to discuss the differences between windows and
widgets here's a few quick
6:08:09
definitions that i definitely did not copy from the internet widgets are gui elements
such as buttons
6:08:15
text boxes labels images you name it and windows they serve as a container
6:08:21
to hold or contain these widgets for this topic we'll be creating and customizing our
own window
6:08:28
and in future topics we'll be exploring different widgets that are available to us so
let's begin by creating a simple
6:08:35
window and we should give our window a unique name such as window that's pretty
creative
6:08:40
and in order to instantiate this window we're going to follow this window name with
equals tk make
6:08:46
sure you get the capitalization right this is a uppercase t and a set of parentheses to
serve as the constructor
6:08:53
what this will do and i'm just going to add a comment is instantiate an instance of a
window for us
6:09:02
and when we compile and run this nothing appears to happen that's because we
only instantiated our
6:09:07
window and we're not actually displaying it in order to display our window we're
going to follow this with
6:09:13
the name of the window dot main loop and this will display our window and
6:09:19
this is our first graphical user interface and we'll be adding widgets to this window so
i'm going to add a
6:09:25
comment that this will place window on computer screen and it will also listen
6:09:34
for events but we'll get to that in future videos now what we'll be doing is customizing
6:09:39
the appearance of this window let's begin by changing the size and we can do that
with the geometry function
6:09:46
so we're going to first begin by typing the name of our window which is window
6:09:51
follow this by using the geometry function and we pass in a width and a height
6:09:56
but make sure this is within quotes and set this to whatever width and height that you
want so if i want 420 by 420 that would be
6:10:03
420 x 420 and the size of our window is now the size that we set with the
6:10:09
geometry function now take a look at the top left of this window we have
6:10:14
this feather icon as well as a title of tk that's kind of lame so let's change that
6:10:21
let's first change the window title and there is a function to do that so we type in the
name of our window
6:10:28
window dot title function and we can pass in a new title that we want to set
6:10:35
and i will set the title to be maybe bro code first gui
6:10:42
program that'll work and we now changed the title of this window and it says
6:10:48
bro code first gui program the next thing that we can do although
6:10:53
not necessary is that we can change the icon of our window bar at the top
6:10:58
i would like to replace this feather icon with an image of my choosing here's my
desktop don't mind all of the
6:11:05
clutter and i would like to use this image of the logo for my channel and set this as
the icon
6:11:10
for my window so what i'm going to do is copy and then go to my project folder and
6:11:17
then paste it click ok and i now have this image
6:11:22
within my project folder but it's in a format that we cannot use currently we need to
convert this to
6:11:28
what's called a photo image that is a format that tk enter can use
6:11:34
so we'll need to convert our image to a photo image so do that anywhere within the
window
6:11:39
between window equals tk and window.main loop right here is good i suppose we're
going to create a photo image
6:11:46
from this image and this is a png file but yours might be something different so let's
give our photo image a unique
6:11:53
name like maybe icon and in order to create a photo image we'll follow this with
equals
6:11:58
photo image and within the constructor of our photo image there is an option for file
we can
6:12:05
either list the file name or the file path if it's somewhere else on your computer since
this image is within my same
6:12:12
project folder i only have to list the file name and this is called logo.png
6:12:18
and this is probably going to be named something else for you though so we now
have this photo image called
6:12:24
icon that we can use and i need to set the icon of my window to this icon and luckily
6:12:31
there is a function to take care of that for us type in the name of the window which is
window
6:12:36
dot and we're going to use the icon photo function there are two arguments the
6:12:43
first we're going to set to true and the second is the photo image that we want to use
6:12:49
and this photo image is called icon so we're going to pass this photo image as an
argument to this
6:12:55
function of icon photo and now when we compile and run this the image that we
wanted to use is now
6:13:02
set for the top left icon of this window bar however it loses a lot of the quality
6:13:07
since the dimensions are so small but you can still tell that it's my logo though so
that's how to replace
6:13:13
the icon of your window bar in the top left corner now let's change the background
color
6:13:18
of this window and one way in which we can do that is to use the config function you
can use the config function
6:13:24
anytime that you want to make any changes to this window so let's change the
background color here and there is an option called
6:13:31
background and we can set this equal to either a color name or a hexadecimal value
6:13:37
let's begin with just some color names let's say that i want the background color to
be black
6:13:42
i'll just type in the word black here and this will change the background color of my
window to black
6:13:48
alternatively you can set a hex value as the color so if you were to look up or google
hex
6:13:55
color picker you can select whatever color you want let's say that i want a sky blue
color like this well this is
6:14:03
the hex value i'm going to copy this value and then paste it within the option for
6:14:09
my background make sure you have this hashtag as well because that represents
that this is a
6:14:14
hex value so now the background color is that sky blue color that i picked
6:14:20
well everybody that's the basics of creating a window in python and in future videos
we're going to be
6:14:25
exploring different widgets that we can add to this window that have some
functionality so if you would like a copy of all this
6:14:32
code i'll post all of this in the comments down below but yeah that's how to create
your very first gui in python
68.labels 🏷️
6:14:38
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys how we can create
6:14:44
labels in python so sit back relax and enjoy the show
6:14:49
what is a label well i'm glad you asked that question it's an area widget that holds
text and
6:14:55
or an image within a window so to begin we'll need to create a window to act as
6:15:00
a container to hold this label we can create a window with window equals tk followed
by
6:15:07
window.mainloop and this will give us a window in which we can add a label to
6:15:12
now let's give our label a unique name such as label and in order to instantiate a
label
6:15:18
we'll follow this label name with equals label with a capital l and a set of parentheses
6:15:24
the parentheses are acting as the constructor for this widget our label and we can
pass in arguments
6:15:30
into these parentheses the first argument that we're going to pass in is a master the
container for this
6:15:37
label we're going to pass in the name of our window because our window is acting as
the container
6:15:43
with widgets in python we can de-eliminate the arguments that we're passing in with
a comma and there are options
6:15:50
available to us options are keyword arguments that we can pass in to the constructor
for this
6:15:56
widget one option available to us is text and we can set the text of this label equal
6:16:02
to a string that we set so let's say that we want this label to say hello world well we're
going to pass in
6:16:09
this option text and it's a keyword argument so if we were to do this we're going to
6:16:16
set the text of the label but this label currently doesn't appear within our window
6:16:21
what gives bro i thought you knew how to code hold up wait a minute there's one
more thing that we need to do
6:16:27
so we're going to actually add this label to the window now we set the master
6:16:33
of the label to be our window but now we actually have to add it as step two one way
which we can do that is to use a
6:16:40
pack function of our label so follow label equals label and pass in all of your
6:16:46
arguments and then follow this with the name of the label dot pack
6:16:51
so with the pack option by default this will place our widget in the top center of our
window another way in
6:16:58
which we can add a label to a container or window is to use the place function
6:17:03
and we can set some coordinates of where we want this label or other widget to
appear within the window if we want this to
6:17:10
appear in the top left corner we can set x to equal zero and y t equals zero and these
are options
6:17:17
available to us too that we can pass in so this place function will place
6:17:22
our label at some position or some set of coordinates within the
6:17:27
window and with x equals 0 and y equals 0 that will place our widget in the top left
corner
6:17:33
of the window and if i were to change these to let's say x equals 100 and y equals
100 well this is going to
6:17:40
be placed down by 100 pixels and to the right by 100 pixels
6:17:46
so you can use pack or place there's a few others like grid but they're a little more
complex so we'll get to that in a future
6:17:53
video for the time being i'm going to use the pack function of the label and turn this
place function into a
6:17:59
comment dot pack all right now
6:18:04
how can we customize this label we can pass in different options when we
instantiate this label let's
6:18:10
change the font and that's another option we can change the font with font equals
6:18:16
and then we can pass in a font let's begin with a font family i will say maybe ariel
6:18:22
a size perhaps 40 and then a style if you so choose
6:18:28
like bold there's also italic and underline uh but this will work just fine then so
6:18:33
this will change the font of the text of our label and it says hello world still
6:18:39
now let's change the foreground color so that is another option the foreground
6:18:46
is the font color and the option is fg for foreground it's short for foreground
6:18:51
foreground equals we can state a color name such as green and our text
6:18:58
should now be green you can select something more specific by passing in a hex
value so a custom color
6:19:05
that i like is hashtag zero zero ff00 this is bright green you can always look
6:19:12
up what different hex values are and we can also change the background color too
6:19:17
that is bg short for background color and we can pass in a color name or a hex value
i'm just going to set
6:19:24
this to black just to demonstrate that we can pass in both either hex values or color
names and
6:19:30
what we have is green text on a black background and this is the limits of our label
6:19:37
within the window because when we expand this window the label is still going to
stay the same
6:19:42
size if we were to change the text within the label our label is only going to take up
the
6:19:49
room that it needs and you can see that our label is now smaller now we have all of
these
6:19:54
arguments within our constructor and it's getting a little bit difficult to actually read so
i'm going to separate each of these
6:20:00
arguments line by line just so that this is more organized but it'll work just the same
as it did before now how do we add a
6:20:07
border around our label one we can set the border style with the relief option and
one border
6:20:14
that i like is raised but right now the border width is one so it's a little difficult for us
6:20:21
to actually see the border let's increase the border width with the option bd
6:20:26
and i'll set this equal to maybe 10. so this border is now more visible
6:20:32
another relief which is the border style is sunken and this will give our label a
6:20:38
sunken appearance but i think i'll change this back to raised we can also add some
padding around the
6:20:45
text between the text and the border we can pad x and pad y pad x will add some
padding some space
6:20:52
between the x-axis of our text between the border so let's set this to maybe 20
6:20:57
and we now have 20 pixels worth of space between the text and the border let's also
pad y
6:21:06
this will add some space above and below our text between the text and the border
6:21:13
now how do we add an image to a label we first need to create what's called a photo
image and we will create this
6:21:21
within the lines of window equals tk and window.main loop and let's give this photo
image a unique
6:21:27
name like photo and in order to create a photo image we're going to follow this with
6:21:33
photo equals photo image and within the constructor of our photo
6:21:38
image there's an option called file and we can list a file path or a file name
6:21:44
right now on my desktop there is a image that i want to use and i need to convert this
to a photo image
6:21:50
here's my desktop and this is an image that i want to use to add to my label i need to
get the file path and i can
6:21:56
get that by going to properties then underneath location i can copy this
6:22:01
location address and paste it within the quotes for file
6:22:07
and then i need to add the image name and this is person.png and with these
backslashes these need to
6:22:14
be double backslashes because that is the escape character for a backslash
6:22:20
but what would be a better option is to add this image to my project folder that
6:22:25
contains the python file that i'm working with so in place of listing the entire file
6:22:31
path i'm going to copy the image that i want to use go to my project folder and then
paste it and i no longer need
6:22:40
this entire file path i can just list the image name that i have and now what we need
to do is add this
6:22:46
photo to the label and there's an option to do so that is the image option and we'll
place
6:22:52
this within the constructor for the label we're going to set image equal to the photo
image that we have
6:22:58
and our photo image is called photo this will add our photo image to our label or
other widget depending
6:23:04
on what we're working with so we have our image but it replaced all of the text
6:23:10
that we had for this label how do we add both text and an image to a window well we
6:23:16
can follow this with the compound option we can set a direction of where we want
this image to be placed relative
6:23:23
to the text that we have so i'm going to set compound equal to bottom so that the
image appears on the bottom
6:23:31
or underneath our text so we can both have text and an image or if i want this to be
in a different
6:23:37
location i could set this to maybe top left or right if i were to set this to top we have
6:23:42
our image on top of our text and one thing to know about labels and many
6:23:48
other widgets in python is that the size of the widget will actually increase to
accommodate
6:23:54
the size of all the components that's contained within if i were to give the text a
longer string of characters such
6:24:01
as bro do you even code then the width of our label is actually going
6:24:07
to expand to accommodate the size of the longer string of text that we have
6:24:13
so that's the basics of labels everyone if you would like a copy of all this code i will
post all of this in the
6:24:18
comments down below but yeah that's the basics of labels in python
69.buttons 🛎️
6:24:24
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys how we can create buttons in python
6:24:31
so sit back relax and enjoy the show buttons you click them and then they do
6:24:38
stuff that's pretty much it so this is how to create a button in python we'll need a
window
6:24:43
window equals tk followed by window.main loop let's call our button just button and in
6:24:49
order to create this we'll follow this with equals button and within the constructor
6:24:55
for this button we're going to list what we want to add this button to what is the
master
6:25:00
the master is the window and in order to display this button we're going to follow this
with button
6:25:07
dot pack and we should have a small rectangular button within our window
6:25:13
but it doesn't display any text nor does it do anything so let's add some text and
there is an
6:25:19
option to do that within the constructor we're going to list the option for text and set
this to whatever text you want
6:25:25
to display let's say click me and then when we run this our button now says the text
that
6:25:32
we set and it says click me however it currently doesn't do anything though we need
to set a command that is another
6:25:39
option command equals and we list a function name this is what's known as a
callback
6:25:46
so let's create a function called click and then outside of the window let's define what
our click
6:25:51
function does def click and let's print a message you
6:25:58
clicked the button now with the command we're going to list
6:26:05
the function name without the parentheses so make sure you're writing it as this and
not that this is what's known
6:26:12
as a callback so when we click on this button it's going to perform our callback
6:26:19
and perform whatever is within our function click just like that let's customize the
6:26:25
appearance of this button i'm going to change the font that is another option font
equals
6:26:31
and let's pick a professional font such as comic sans it's very professional and i'll set
6:26:38
a size of 30. and now you can actually read what's on this button it's large enough
6:26:46
let's set the foreground color that is the font color fg you can place a color name or a
hex
6:26:52
value here i'll pick green that is 0 0 ff00
6:26:57
and our text should now be green which it is let's also change the background color
6:27:04
that is bg i will pick the color name of black and now we have
6:27:09
green text on a black background notice that when i click on this button or hold my
mouse button down the color
6:27:16
scheme has changed that's because there is a different active foreground and active
background
6:27:22
and we can change those too so that this won't flash every time you click on the
button so let's change the active foreground
6:27:29
and active background those are other options active foreground
6:27:34
and i'll set this to the same color as my foreground which is green so the text color is
6:27:41
going to stay the same and i'll also change the active background
6:27:47
and i will also set this to black to match my background and then this should no
6:27:53
longer flash when we click on our button if you need to disable somebody from
6:27:59
clicking on this button there is an option to do so and that option is the state
6:28:04
option and normally this is active but you can set this to disabled and we can no
longer click on
6:28:11
this button and you can see that the color scheme has changed too it's all grayed out
and
6:28:16
whatnot so i'm going to replace this back with active because we still have a few
more things to do
6:28:22
now how do we add an image to a button there is an image option and we set this
equal to a
6:28:28
photo image but first we need to create a photo image i have this photo of a like
button a thumbs up that i want to
6:28:34
use i need to create a photo image from this image file so within the window let's call
this
6:28:40
photo image just photo photo equals
6:28:46
photo image then i'm going to list the file path or the file name
6:28:51
since this image is within my same project folder i only have to list the file name and
6:28:57
this is called like.png but if you use your own image it's probably going to be named
something else
6:29:02
then i need to set the image equal to my photo image which i named photo but the
photo replaces the text on
6:29:11
this button but it still works the same so we would like to display some text as well as
our image
6:29:17
we need to use the compound option and we list a direction we can list
6:29:24
top bottom left right we want this image to be on the perhaps bottom so let's list the
6:29:31
bottom and now when we run this we have our text as well as our image and if we
changed compound to a
6:29:37
different direction like top then the image is going to be displayed on the top relative
to the text
6:29:45
what if we wanted to count the amount of times that we clicked this button we could
create a variable called count
6:29:52
and we'll set this equal to zero but in order for us to access this count variable
6:29:58
within the function of click we're going to list count as global so that this is a global
6:30:06
variable every time we click this button let's increment our variable count by one by
saying count plus equals one
6:30:14
and then we will print whatever the value of count is so within a print statement we'll
just
6:30:20
say count now when we click on this button we have access to our count variable
that's outside of this function
6:30:27
and every time we click on this button it's going to increase our count variable by
6:30:32
one well everybody that's the basics of creating buttons in python if you want a copy
of
6:30:37
all this code i'll post all of this in the comments down below but yeah that's the basics
of buttons in
6:30:43
python hey what's going on everybody it's your bro here hope you're doing well and
in this video i'm going to teach you guys
70.entrybox ⌨️
6:30:49
how we can create a simple entry box in python so sit back relax and enjoy the show
6:30:56
if you find this video helpful please remember to like comment and subscribe your
support will
6:31:02
help keep this channel running alright my fellow bros in this video we're going to be
discussing the entry
6:31:07
widget this is a text box that accepts a single line of user input as always we'll need a
window window
6:31:14
equals tk followed by window.main loop and let's give our entry box a unique name
let's
6:31:19
call this entry for short and then to create this entry widget we're going to follow this
with equals
6:31:25
entry and within the constructor we have to state what we're adding this to we're
going to add this to our window
6:31:32
and we should finish this by packing it or placing it entry dot pack and we should
have an
6:31:39
entry box within our window however the text is somewhat small so let's change that
6:31:45
we can set this as an option within the constructor of our entry box so let's set the
font font equals and
6:31:53
pick whatever font you want i'm going to pick maybe ariel and then a size
6:31:58
it'd be 50. and this should be a little bit larger and we can type in some
6:32:06
text like this cool right now we should add a submit button
6:32:13
because right now this currently doesn't do anything if we type some text in so let's
create a button i'm going to
6:32:18
create this button maybe after the entry box and let's say that this is a submit button
so let's call this
6:32:25
submit button equals button what are we adding this to writing the square window
6:32:32
and we'll set some text text equals submit we'll also need a command for this button
6:32:39
command equals and we'll create a function called submit and we want to pack the
submit button to
6:32:46
add it to the window submit button.pack all right now let's put our entry box on
6:32:53
the left hand side and our submit button on the right hand side when you pack these
widgets you can set
6:32:58
these on a certain side side equals left and our submit button
6:33:04
we want this on the right not necessary but i think it looks better and we also need to
define our submit
6:33:10
function outside of the window let's define what this does def submit
6:33:16
what's this going to do we need to get the current value that's within our
6:33:21
entry box one way in which we can do that is use the entry boxes get
6:33:26
function this will return a string and we can do something with that string such as
store this within a variable so
6:33:33
let's create a variable called maybe username let's pretend we're having a user
submit their username to login to something i
6:33:40
suppose so we'll get the current value of the entry box store that within a variable
and we'll
6:33:46
call this username and then we will maybe print this for fun print
6:33:52
hello plus user name and let's try this
6:33:58
so here's our entry box here and our submit button i'm just going to make up
something
6:34:03
click submit and it displays our message hello ergo gerger gerger
6:34:09
i didn't say that this video was going to be professional let's add a few other buttons
let's say we want a delete button as
6:34:15
well as a backspace button so let's copy what we have for our submit button and
create a delete button
6:34:22
and then a backspace button the delete button is a little bit easier to code so let's
rename our submit button as
6:34:28
delete button make sure to change the name to when you pack this and the text
6:34:34
we will say delete and we'll create a function called delete and let's define what
delete does
6:34:42
so after our submit function let's define our delete function and we need to
6:34:50
delete all of the text within our entry box one way in which we can do that is to
6:34:55
type in the name of our entry box dot delete function
6:35:00
and this takes two positional arguments which portion of our entry box do we
6:35:06
want to delete so we want the very first character that is index 0 followed by the last
character and we
6:35:13
can say end so this will delete all of the characters within our entry box and let's try it
so
6:35:19
i'm just going to type in my name bro i'm going to delete this and all of the text is now
gone
6:35:27
just like that let's create a backspace button we'll copy what we have for our delete
button and change
6:35:32
the word delete to backspace make sure you change that when you pack
6:35:38
this as well and for the text we'll set this to backspace and we'll create a function
6:35:44
called backspace and let's define what backspace does so
6:35:49
after our delete function let's define back space and this is going to do something
6:35:55
very similar to our delete function except we're going to change the first
6:36:02
positional argument we need to place the second to last character within this
positional argument one way
6:36:09
in which we can do that is to first get the entire length of all of the characters within
our entry box
6:36:16
so let's begin by typing entry dot get and we need to determine the length
6:36:21
of this and we can surround this by placing this within the length function so this will
6:36:28
return the current length of all of the characters within our entry box and then we're
going to add -1 so that
6:36:36
this first positional argument is the second to last character and the second
positional argument is
6:36:42
the end the last character and then when we run this let's type in
6:36:47
our name i'm just going to type in draw code this time i'm going to click backspace
and then it's only going to
6:36:52
delete the last character and then if you were to click delete it deletes everything
6:36:58
broy and then if you click submit it's going to submit your name all right let's
customize the appearance
6:37:05
of this entry box so within the constructor for this entry box let's change maybe the
color maybe the
6:37:13
background color and foreground color let's begin with the foreground color you can
pass in a color name or a hex
6:37:18
value i want this to be green so the hex value for that is zero zero f zero ff00 and the
foreground
6:37:25
color of the text when we type it in is now green and we can also change the
background
6:37:31
color that is bg we could pass in a color name or a hex value i'm just going to say
black to
6:37:37
keep it simple and we now have a black background and the text color is now
6:37:42
green here's a few useful features that you might be interested in let's insert some
default text for our
6:37:49
entry box so we can use the insert function entry dot insert
6:37:54
we place a positional argument as well as some text so for the positional argument
let's say
6:37:59
zero so that's the very beginning index zero and let's set the text to maybe
6:38:05
spongebob and then the default text within our entry box is now spongebob another
option available to us
6:38:12
is that we can set the state of this entry box we can set this to active or disabled
6:38:19
let's say that after somebody submits our name we want to disable this entry box so
we can either pass in
6:38:25
this option within our constructor or we can use the config function to make any
changes
6:38:32
to the state of our entry box so let's say after we submit our username we want
6:38:37
to disable this entry box so let's type in the name of our entry box dot config
6:38:44
and for the option for state let's set this to disabled and then once we type in a name
6:38:54
and then click submit our entry box is now disabled there's also an option called
show where
6:39:02
you can show a certain character in place of the normal text on the screen so this
would be useful for a password
6:39:09
let's say that we're typing in a password but we don't want to display the password
on the screen
6:39:14
we can use the show option to show a certain character in place of our text so you
might see bullets or
6:39:22
an asterisk so let's pretend that we're now typing in a password so we're going to set
6:39:27
the option for show and place a character that we want to replace each
6:39:32
character with let's say we want to only display asterisks when we type in some
characters and i'm going to type in
6:39:39
the word password but it's only displaying asterisks now but if i were to click submit
6:39:45
it displays our text and i would say plain text instead of our hidden text
6:39:50
because we're only showing whatever character that we set so that's an option
available to you as well
6:39:55
well everybody that's how to create an entry box in python you can submit
information such as a username for example or even a
6:40:03
password if you set the show option available to you if you want a copy of all this
code i'll post all of
6:40:09
this in the comments down below but yeah that's some of the uses of the entry
widget in python
6:40:14
hey how's it going everybody two bro here hope you're doing well and in this video
i'm going to teach you guys how we can create and customize our own
71.checkbox ✔️
6:40:21
check buttons in python so sit back relax and enjoy the show
6:40:27
all right my fellow bros in this video we're going to be creating and customizing our
own check buttons in
6:40:33
python i sometimes refer to these as check boxes so in case i do that's what i'm
referring to as always we'll need a
6:40:40
window window equals tk followed by window.main loop and let's call this button just
check
6:40:47
button and in order to create this check button we type in the name of the check
button
6:40:53
equals check button and within the constructor
6:40:58
what are we adding this check button to we're adding this to our window and also
within the constructor we can
6:41:04
set the different options for how we want to customize this check button let's begin
with the text
6:41:10
text equals i agree to something and last but not least we
6:41:17
need to add this check button to the window one way in which we can do that is to
use the pack function of this widget
6:41:24
so follow this with check button dot pack when we run this we have our check button
within our
6:41:30
window and we can toggle this on or off right now i'm agreeing to something i'm not
sure what i'm agreeing to but
6:41:37
hopefully it's not anything bad now currently this doesn't do anything so we'll need to
associate a variable
6:41:42
with this check button so we'll add that as an option variable equals and let's create a
variable
6:41:49
called x and we'll need to define this within the window so after we create our
instance of our
6:41:55
window let's say x equals now with check buttons they store a 1 or a 0 by default
within
6:42:03
our variable our variable that we call x so with x we're going to set this equal to a int
6:42:12
var if this were to return a string we would say string bar but normally by default this
6:42:19
returns a one or a zero and we can actually change that using the on value
6:42:25
and off value options so the on value is what's going to be stored within our
6:42:31
variable if it's toggled on so by default this is one and the off value is the opposite
this
6:42:38
is what is stored within our variable if this is toggled off alright now let's have some
fun with
6:42:44
this so let's associate a command with this check button command equals and let's
create a
6:42:51
function called maybe display and then let's define this before the window def display
and we're
6:43:00
going to check the value of x to see if it's one or zero so within our display function
6:43:07
we're going to check to see if x and to get the value of x we're going to use the
6:43:14
get function so if x dot get is equal to one that means that somebody clicked on the
6:43:20
check box or toggled it on so if they check the check box
6:43:26
or check the check button then they agree so print you agree
6:43:34
if it's zero and we can do this with an else statement else print
6:43:41
you don't agree frowny face and let's test this theory so we have
6:43:48
our check button when we click on this it's actually going to call our display function i
agree to
6:43:54
something and it says within our console window you agree and if i were to toggle
this
6:43:59
off it says you don't agree let's change the font of our check button so we'll add that
as an option
6:44:05
font equals and we can pass in a font that we want i'll say maybe ariel and then a
size
6:44:12
of 20 and now this should be readable i agree to something let's also color
6:44:19
this because well i like coloring let's begin with the foreground color so that is fg for
short fg equals
6:44:27
and then we can pass in a color name or a hex value i like hex values so i'm going to
stick
6:44:33
with those i want a green foreground color so that is zero zero f
6:44:39
zero zero but feel free to adjust these values however you want so our text should
now be green that is
6:44:47
a very bright green and it hurts my eyes so i'm also going to change the background
color which is bg and i'm going to set
6:44:54
this to black so we'll have green text on a black background
6:44:59
okay now when you click on this it flashes that's kind of annoying that's the active
foreground and active
6:45:06
background if we click on this or hold this down the color scheme switches to
6:45:11
our active foreground slash background and we can change those too so i'm going to
set those options active
6:45:19
foreground i'm going to set this the same as my foreground color
6:45:25
and the active background will be the same as our normal background so that's
active
6:45:31
background equals black and this should no longer flash
6:45:38
when we click on it not too bad not too bad let's add some
6:45:43
padding pad x i'll set this to 25
6:45:49
and pad y i'll set this to 10. we have a little more padding around
6:45:57
our check button one feature that's available to us is that we can add an image next
to our
6:46:03
check button i downloaded an image of the python logo that i want to add
6:46:08
so we'll use the image option to add a photo image to this check button but first we
need
6:46:15
to create a photo image from our image file so within the window
6:46:21
let's call this python photo but you'll probably use something different for your project
6:46:27
python photo equals photo image and within the constructor for our
6:46:33
photo image we can set the file path or the file name
6:46:39
since this image is within my project folder i only have to list the file name
6:46:44
and this photo of mine is python.png and now we set the image equal to
6:46:52
our photo image that we have image equals python photo but when we run this
6:46:59
it's going to overlap the text we still have our image though and this still works as
intended so in
6:47:06
order to display the image as well as the text we're going to need to use the
compound
6:47:13
option and let's add that at the end compound is where we're adding this photo
6:47:19
relative to the text i want this photo on the left hand side of the text so i'm going to
set this to left and now
6:47:27
when we run this surprise surprise our image is on the left hand side one thing that
you should know is that
6:47:33
with the on value and the off value data type if you were to change this to a non-
integer value let's say we're
6:47:40
going to replace the on value with a boolean value of true and the off value
6:47:45
is false with this variable make sure to change the data type as well to reflect
6:47:52
what you're storing within it so with our int var variable we want to change this to a
6:47:57
boolean var value so replace int with boolean and this will work much the
6:48:04
same now with this display function we'll need to account for that so within the if
statement here we'll
6:48:10
say if x dot get we don't need to say is equal to one this will return true or false and
this
6:48:18
will work just the same as it did before and if you replace this with a string let's say
the on value is
6:48:26
yes all caps and the off value is no this is now a string bar
6:48:34
and we'll change our display function to say if x dot get is equal to yes
6:48:43
and this should work you agree you don't agree you agree you don't agree and i'll
change that
6:48:50
back to one so one and zero is the default for this but you can edit these based on
what you need
6:48:56
all right so that's the basics of check buttons in python if you want a copy of all this
code
6:49:01
i'll post all of this in the comments down below but yeah that's the basics of check
buttons in python hey what's going
72.radio buttons
6:49:08
on everybody it's your bro here hope you're doing well and in this video i'm going to
be teaching you guys how we can create radio buttons in python so sit
6:49:15
back relax and enjoy the show okay everybody let's create some radio
6:49:21
buttons now these are similar to check boxes but you can only select one from any
single grouping
6:49:27
so let's pretend that i'm buying you lunch and you can only select one item from a
menu you can select
6:49:33
either a slice of pizza a hamburger or a hot dog so let's begin by creating a list i will
6:49:39
call this list food food equals
6:49:44
pizza hamburger hot dog and let's create a window
6:49:52
window equals tk followed by window dot main loop
6:49:59
so to create a radio button we'll need a name so i'll call this radio button radio button
equals radio button
6:50:08
and we're going to be adding this to our window and we're going to put this within a
for loop so we're going to iterate
6:50:15
through all of the items within our list so let's create a for loop so we'll say
6:50:20
for index in range
6:50:26
length of food so this is going to iterate once through
6:50:32
all of the elements within our list so when we run this it's going to create
6:50:38
three radio buttons for us because we're instantiating one radio button for each item
in the
6:50:44
list now what i'm going to do is set the text equal to
6:50:51
our list of food at a certain index so when we first run this for loop it's
6:50:57
going to be zero for the index then one and then two and then so on and
6:51:03
so forth for each item within our list and then we just need to pack this so
6:51:09
radio button dot pack and then let's take a look at this
6:51:17
alright so here are all of the radio buttons that we have currently they're all selected
though so
6:51:22
we'll need to link these within a single grouping the next thing we'll add is a variable
6:51:28
and we'll set this equal to x and we'll need to declare this after we create an instance
of our window
6:51:33
so x equals int var so this will hold an integer object
6:51:40
so these are all within the same grouping however if we try to run this again and
6:51:45
select one of these options they all become selected so we're going to need to give
6:51:51
each of these individual radio buttons their own value because right now they're all
6:51:56
sharing the same value so we can add value equals whatever the index is currently
so the
6:52:04
first item within our list of food is going to have an index value of zero then one
6:52:11
then two so let's try that again and you can see by default it's zero so pizza is
already selected then
6:52:18
we can select either hamburger or hot dog but we can only select one so that's
6:52:23
kind of what we want then so do you guys usually know how i call the config function
after each change
6:52:29
that i make for an object well that's because i like to add comments just for teaching
purposes
6:52:34
for what each change does however it might be easier for you instead of calling the
config function a bunch of
6:52:40
times just to list all of the changes within the parentheses when you create an
instance of the
6:52:46
object that you're working with however just for teaching purposes i'm going to put
this all within
6:52:52
separate lines that allows me to add some comments so i think i'm going to start
doing that from now on uh so what this does
6:52:59
is that this adds text to radio buttons and this
6:53:06
groups radio buttons together if they share the same variable
6:53:16
so if you wanted a completely different grouping of radio buttons you can give them a
different variable
6:53:21
like y and what this line does is that this assigns
6:53:28
each radio button a different value
6:53:37
all right so let's make some cosmetic changes to our radio buttons so right now they
are centered let's
6:53:45
anchor these to the west so we'll place this within the pack function so
6:53:50
anchor equals w alternatively you can just say capital w
6:53:56
without the quotes this would also work too so now they're all lined up let's add
6:54:01
some padding so i'm going to add pad x
6:54:06
and i'll set this to 25 and this adds
6:54:13
padding on x axis
6:54:20
let's also change the font so we'll save font
6:54:27
equals maybe i'll pick impact for a font style and set the font size to 50
6:54:36
and now we can actually read these because the font is large enough now let's add
some images so let's
6:54:43
create some photo images you'll want to be sure to create these after you create
your instance of your
6:54:49
window so let's create a pizza image first pizza image equals photo image
6:54:58
file equals the file path or the file name and my file is pizza.png and it kind of
6:55:04
looks like this it's just the pizza emoji so
6:55:10
pizza.png and then i'll do the same thing for hamburger and hotdog
6:55:16
so this will be hamburger image file equals hamburger.png
6:55:25
and then hotdog image
6:55:35
and this is what the other emojis look like
6:55:40
and then let's create a list of the photo images that we have i'll call this food images
6:55:47
equals than the names of these images pizza image hamburger image
6:55:56
and hot dog image so then we can set the image for each
6:56:03
radio button image equals the name of the list food images
6:56:11
followed by the index that we're currently on within our for loop
6:56:16
so this adds image to radio button and
6:56:23
let's test this cool here's our images now if you want some images as well as
6:56:30
text you'll have to use compound so let's add that next so i'm just going
6:56:36
to add a comma followed by compound
6:56:43
equals let's say left so this will add the image to the left of the text
6:56:52
adds image and text i'll just say left side
6:56:59
so this will display both an image and text
6:57:04
so with these radio buttons you can eliminate these circle indicators and there's
actually a
6:57:10
way to do that so what we'll add if you want to get rid of those is indicator
6:57:18
on equals zero so this will i should say eliminate
6:57:27
circle indicators so it's going to change these to these
6:57:34
push buttons however they are going to be of uneven size you can set the width
6:57:40
if you want to so we'll do that so we'll say width equals
6:57:47
what about 75 i'm not sure if that's a good size or not
6:57:52
okay that's way too small let's try 375
6:57:59
so yeah you can have some push buttons if you want to if you don't like the circle
indicators
6:58:04
so this sets width of radio buttons
6:58:11
now let's actually set a function to uh be called when we click one of these
6:58:16
buttons so let's call this function maybe order like we're ordering one of these items
6:58:24
so def we'll call this order like we're ordering something
6:58:29
and what we'll do is just a few if statements because i'm feeling lazy so we'll just say
if x that's
6:58:37
the value that's going to be stored because each of these radio buttons is
6:58:42
grouped together by the variable x if x dot get
6:58:48
is equal to zero that's our first index for pizza what we'll display is
6:58:57
print you oops you ordered
6:59:04
pizza then we'll just add else if x dot get
6:59:12
is equal to one we will print
6:59:17
you ordered a hamburger
6:59:25
and then else if two
6:59:30
you ordered a hot dog
6:59:37
else print huh
6:59:44
so there's probably a more efficient way to write this however i was just feeling lazy
and this is
6:59:49
probably the best way to keep this simple and easy to understand so let's try this
now oh however we need
6:59:55
to set the command for our radio buttons so
7:00:00
command equals then the function name which is order and make
7:00:07
sure you do not add the parentheses so don't do this do that
7:00:13
and this will set command of
7:00:19
radio button to function
7:00:25
all right let's test this so if we click on pizza it says you ordered pizza
7:00:30
you ordered a hamburger and you ordered a hot dog okay everybody so that's the
basics of
7:00:37
radio buttons if you would like a copy of all this code i'll post all of this in the
comments down below but yeah that's the basics of radio
7:00:44
buttons in python hey what's going on everybody it's you bro here hope you're doing
well and in
73.scale 🌡️
7:00:50
this video i'm going to teach you guys how we can create a sliding scale in python so
sit back
7:00:55
relax and enjoy the show let's create a scale so we'll need a
7:01:00
window window equals tk followed by window.mainloop we'll create
7:01:06
a scale called scale scale equals scale we need to add the scale to our window
7:01:12
so we'll put that here we can list a from position and a 2 position
7:01:18
both of these values will be our range of values that we can use for our scale so from
7:01:25
sure to add an underscore after from it won't work without this underscore from
equals let's say 0 and 2 equals 100
7:01:34
so we're going to create a scale for temperature and this will be in celsius so 0 will be
7:01:41
freezing and 100 will be the boiling point uh you know for water so then to display the
scale we'll need
7:01:48
to add scale.pack and let's run this so the front position is zero so that's
7:01:54
at the top here and then if you go down it increases so you can actually flip this so
we'll save from
7:02:01
102 will be zero so now you can scroll up and the number
7:02:06
will increase let's also get the current value so
7:02:12
we'll need a button so button equals button
7:02:18
we'll add this to the window we'll add some text text equals submit and then a
command
7:02:26
command equals submit but we'll have to make this function
7:02:31
so let's do that outside the window def submit and we will simply print the
7:02:38
current temperature so let's display message the temperature
7:02:43
is plus scale dot get
7:02:51
plus degrees celsius we can just say degree c
7:02:59
so let's try this uh but we also have to pack the button so button.pack
7:03:07
so if we were to try and submit this right now scale.get is going to return
7:03:12
an integer so we need to convert this to a string so str scale.get
7:03:20
and you have to do that with python if you're displaying a integer value along with
another string
7:03:26
uh you need to convert it to a string so now this should work so it is 82
7:03:33
degrees celsius so let's do some cosmetic changes for our temperature gauge
7:03:39
because right now it's kind of ugly so the first thing we'll change is
7:03:46
let's say the length so length equals 600 so that should be fairly large
7:03:56
you can also set the orientation so that is orient spelt like that
7:04:04
so this can either be vertical which it currently is or horizontal
7:04:15
but i'll keep this as vertical so i'll let a comment that this is
7:04:22
orientation of scale let's also change the
7:04:27
font so font equals pick whatever font you want
7:04:34
and a size so maybe 20.
7:04:41
now you can add some tick intervals maybe i'll set this to 10. so these will
7:04:50
be numeric indicators on the scale
7:05:00
and show value this hides the current value so if you set this to zero this will
7:05:06
hide current value
7:05:12
so it no longer displays what the current value is next to the slider to actually retrieve
it you'd have to
7:05:19
get the value so right now it's 40 but it helps that you have the tick intervals on the
left-hand side
7:05:26
i should probably add that this adds uh numeric
7:05:34
indicators for value good enough let's continue on
7:05:41
now let's use the set function we can set the current value of the knob on our scale
and by default it is zero
7:05:49
so we can use the scale dot set function and we can place a number in here
7:05:54
a value so let's say we want this to be 100 so this is now at the 100 position
7:06:00
instead of zero if we wanted this in the middle we could set this to 50 and now it's in
the middle however if
7:06:07
you change the range of your skill of from minus 2
7:06:12
and set this to 50 now 50 is all the way down here so it no longer appears in the
middle
7:06:18
so this isn't really necessary but if you want this to be a little more sophisticated
there's actually a formula that you
7:06:23
could use we can use scale straight braces and then place the word from
7:06:29
within here this will give us our max basically uh so this is from without an
underscore
7:06:35
so this not this this scale from minus scale
7:06:43
two and then we're going to divide this by two
7:06:48
and then i'm just going to put this within a extra set of parentheses
7:06:53
plus scale two just in case our two value is greater than zero
7:07:01
we just want to account for that and then i'm just going to put these within another
set of
7:07:06
parentheses so this should always appear in the middle then even if we were to
change this
7:07:14
range yeah it seems like it works uh like i said it's not really necessary but
7:07:20
if you want this to be a little more sophisticated that would work now let's make some
cosmetic changes to
7:07:25
this scale and make it look pretty so first let's change the trough color
7:07:30
this is this gray portion right here so let's set the trough color
7:07:39
equals whatever color you want you could say like blue or you can place a hex value
here and i
7:07:45
actually picked one out already so i'm going to use 6 9 e a f f and that is this icy blue
color
7:07:54
kind of like this is sliding on ice and i'm also going to change the font
7:08:00
color and i'm going to pick this fiery red
7:08:06
color to symbolize heat
7:08:12
and then you can change the background color too so that is bg and i'll just set this
to
7:08:20
like black it's looking kind of cool now right and
7:08:27
for no good reason i'm going to add some pictures so feel free to pause the video if
you'd
7:08:32
like to download some images i'm going to use an image of a flame for the hot side
of our scale and a
7:08:39
snowflake for the cold side so let's begin by adding the hot image uh to the scale
7:08:46
before we actually add the scale to the window so we'll do this after we instantiate
the window
7:08:52
because that's the only place that we can do that so we're going to create a image i'll
just call this hot image
7:08:58
for the fire icon so hot image equals photo image file
7:09:04
equals this is hot.png so hot.png we're going to add the image to
7:09:12
a label i'll call this hot label hot label
7:09:17
label image equals hot image and then we need to pack this
7:09:24
so hot label dot pack and this should appear at the top let's
7:09:31
test it yep there it is let's do the same thing for the cold side
7:09:37
so i'll add this here before the button and i'm going to replace hot with cold
7:09:44
same thing with the file name cold.png
7:09:49
this will be cold label cold image and cold label dot pack
7:09:57
and that should be it let's take a look not too shabby it's looking pretty sweet
7:10:02
it has our submit button at the bottom too uh so right now the temperature is 100
degrees
7:10:07
celsius the temperature is 0 degrees celsius the temperature is 60 degrees celsius
7:10:14
well that's how to create a scale in python if you would like a copy of all this code i'll
post all of this in the
7:10:19
comments down below but yeah that's how to create a scale in python
74.listbox
7:10:24
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys how we can create a list box in python
7:10:31
so sit back relax and enjoy the show all right you wonderful people let's
7:10:38
create a list box a list box is a listing of selectable text items within its own container
7:10:44
so as always we'll need a window window equals tk followed by window dot main
loop let's
7:10:51
create a list box simply called list box big surprise there right so list box equals list
box what are we
7:10:59
adding this list box to we're going to place that here within the parentheses of the
constructor we're adding this to our window and then
7:11:05
we're going to follow this with listbox.pack you can also do listbox.place as well and
place some
7:11:11
coordinates but we've always done pack let's just stick with that for now all right so
this is what our list box looks like
7:11:18
it's a small rectangular box and we're going to add some text items that are
selectable
7:11:23
and eventually we'll be able to submit a choice so i have an idea let's create an
online menu like we're
7:11:30
ordering food from a restaurant let's say it's bros italian restaurant so let's add some
food items to our menu
7:11:36
and somebody can submit an order so we'll need to use the insert function of the list
box to add
7:11:42
some items so list box dot insert we place an index and a text item so let's say
7:11:50
for index one we'll have pizza and this is what we end up with we have
7:11:56
the item pizza it's selectable and well that's really it we'll need to create a submit
button
7:12:02
and populate the rest of this list box so let's add a few other items for our menu so
let's say we'll have five items
7:12:10
and we need an index for each so one two three four and five so we have
7:12:16
pizza for the first item for the second item let's have pasta and third let's say garlic
bread because
7:12:23
garlic bread is awesome and restaurants usually have appetizers so why not soup
and salad
7:12:30
and that should be enough for this example and this is what we have we have our
7:12:36
menu and we can select an item from this menu so let's customize the appearance
of this menu
7:12:42
well because we can so let's do that let's change the background color so we do that
with the option
7:12:48
bg for background and you can set this equal to a color of your choice or a hex value
so i have a hex color
7:12:55
value already picked out it is f7fde and this has the appearance
7:13:01
of i would say paper like it's kind of faded i think that would fit some sort of menu for
a restaurant but pick
7:13:08
whatever background color you want so let's change the font next so we can
7:13:13
do that with the option font and i will pick the font
7:13:22
it's somewhat fancy i think it would fit like a restaurant menu and then a size of 35.
7:13:29
yeah we're getting somewhere we can also change the width and the height
7:13:35
so width equals let's say 12. that's not too bad for a width let's
7:13:41
also change the height so we can do that with a function of our list box so let's get
the current
7:13:49
size of the list box and let's actually do that after we insert all of the items within our
list
7:13:55
box so to change the size of our list box what we do is type in list box
7:14:02
dot config you usually use config if you need to change any uh options and we want
to change the
7:14:09
length of or the height of this list box so we're going to say height equals
7:14:17
list box dot size function so then this will adjust
7:14:24
the size of our list box dynamically even if we were to remove an item
7:14:30
then the size of our list box is going to shrink just like that but let's keep salad in
7:14:37
there for now because we need salad all right so how do we actually submit
7:14:43
one of these options we'll need to create a submit button so let's do that after we
create our
7:14:48
list box so near the bottom we'll call this submit button
7:14:53
submit button equals button we're adding this to our window let's set the text to
submit
7:15:01
and we'll need a command so we'll create a function called submit and then we need
to pack
7:15:08
this submit button submit button.pack now at the top of
7:15:14
this program let's create a submit function so at the top def submit
7:15:24
so to retrieve or get the current selected item of a list box there is a certain
7:15:30
function that you can use so we'll begin by saying list box dot get
7:15:38
what are we getting we want to get anything that is currently selected and there is a
function for that so what we're going to do
7:15:45
within the parentheses of the get function is type in listbox dot current selection
7:15:54
it's shortened to cur selection all right and then we can either store this within a
variable or we can just print
7:16:01
this but i think we'll just print this to keep it simple now when we select an item and
7:16:09
click on the submit button it's going to submit our order actually better yet let's
actually uh
7:16:16
print you have ordered whatever item that
7:16:25
we select so garlic bread you have ordered garlic bread cool what we're going to
work on now is
7:16:32
a way to submit or insert an item to our list box after this program is compiled and
already
7:16:38
running so we'll create an entry box within our window where we can submit a
custom item to
7:16:44
this menu so let's create an entry box and let's do that before the submit button that
7:16:50
would be a good spot for this so let's call this entry box where somebody can type in
something to add to the menu so
7:16:58
entry box equals entry and we're adding this to our window and then entry box
7:17:05
dot pack then we're going to create a button called add and i'm just going to copy
7:17:12
everything from the submit button and we'll change submit to add
7:17:17
we're going to add an item to the menu so we'll change the text to add and command
add
7:17:24
add button dot pack then we're going to create a function called add and we'll do that
7:17:31
at the top def add parentheses colon so to add an item to the list box
7:17:39
first we need to get what is currently in the entry box so we can do that by typing in
7:17:47
list box dot insert then within the parentheses we need to
7:17:52
get an index number as well as a text item so for the number what we
7:17:59
can type here is the listbox dot size so this will give us the current
7:18:05
position of the index that we're currently on for the list box and then we need to get
the
7:18:11
text in the entry box so we type in the name of the entry box entry box dot get
7:18:19
and this will insert a new item within our menu our list box so let's try it
7:18:24
again so we can type in an item let's say soda we want to add soda to the menu
7:18:31
and then we click add all right there's our soda but we have to scroll down to see it
7:18:37
so let's change the size of our list box so there's actually a line of code that
7:18:43
we did that already listbox.config height equals listbox size so let's add that within the
add
7:18:50
function at the very end so then the height of our list box will adjust
7:18:56
so currently there's no soda within our menu let's type in soda or some other item
click add and then
7:19:02
that item will appear and the size of the list box will change to accommodate any
new items
7:19:07
let's say now that we serve sushi we're now an italian and japanese restaurant now
we have
7:19:14
sushi on the menu you know what why stop there let's create a delete button to
delete an item
7:19:19
from this menu so we'll create a delete button i'll just copy everything for add and
7:19:26
change this to delete so delete button text equals delete
7:19:32
command equals delete as well and delete button dot pack and then at the
7:19:38
very top we'll create a delete function so we'll do that here
7:19:44
d e f delete and to delete an item from a list box
7:19:51
there's actually a function to do that so list box dot delete
7:19:56
and then within the parenthesis of the delete function we can pass in list box dot
current selection or
7:20:04
just cur selection for short and then we want to readjust the height of this list box
7:20:10
after we delete an item so we'll just add listbox.config height
7:20:15
equals listbox.size because the size of all the items within the list box is
7:20:20
going to shrink after we delete an item so let's try this so here's our menu
7:20:26
let's say we no longer want salad because salad is healthy well we can delete it
same thing with soup and you know what
7:20:33
let's also delete the pasta and the pizza so we're only left with sweet sweet garlic
bread
7:20:39
well ladies and gentlemen we're going to get into some more advanced stuff with list
boxes what if you want to select more than one
7:20:45
item from this list box we're going to need to change a few things so when we create
our list box
7:20:51
there is a select mode select mode and we are going to set this equal to
7:20:56
multiple so now we can actually select multiple items from this list box like i want to
select
7:21:03
one of everything but what happens when we submit this well we're going to run into
some problems so we're going to need to
7:21:09
change this program around so let's begin with the submit option so what we're
going to do within the submit
7:21:16
option is first get rid of this line let's say we no longer need this anymore uh and then
7:21:23
before we print out the items that we ordered let's create a list called food
7:21:30
and we'll just leave this empty for now we'll fill this later so we'll create a for loop for
index
7:21:37
in list box dot current selection
7:21:44
what we're going to do after each iteration food dot insert
7:21:52
our index number as well as listbox dot get
7:21:59
the item name at this index what this for loop is doing is that it's going to iterate once
for each item that we
7:22:07
select so since we have three items that are currently selected this is going to iterate
three times
7:22:13
get the index number as well as the item at that index number and now we just need
to display the
7:22:18
contents of what we have within our list of food so we can do that with another for
loop so let's add that after
7:22:26
you have ordered then for index in our list
7:22:34
food all we'll do is print index so this will print out everything
7:22:42
that we ordered so let's say that we want one of everything and submit this you have
ordered pizza pasta garlic
7:22:49
bread soup and salad now what happens if we try to delete multiple items
7:22:54
so let's say we want to delete soup and salad and we click delete well we're going to
run into an error
7:23:00
because our function is not set up to accommodate for multiple items so we'll need to
change a
7:23:05
few things around so let's get rid of this line for now list box dot delete list box dot
current
7:23:10
selection and we're going to place a for loop here so for index in list box
7:23:18
dot current selection then after each iteration what we're going to do is use list box
7:23:25
dot delete and we're deleting the item at whatever index we're currently on so you
7:23:32
would think this would work at first glance let's say we want to delete pizza and pasta
7:23:38
we're going to click delete so pizza was deleted but not pasta that's because the
indexes are
7:23:44
changing after we delete an item so when we use this for loop we should actually
reverse this so with
7:23:53
listbox.currentselection we are going to enclose this with reversed
7:23:59
so then we're going to start at the last index and work our way to zero in that order
so now if we want to
7:24:06
delete pizza and pasta i don't know why we would because they're amazing and we
click delete it
7:24:12
deletes pizza and pasta but we still have garlic bread soup and salad then we can
add an
7:24:18
item let's say we want to add sushi against the menu we can do that and then we
can order one of everything
7:24:24
because we're really hungry you have ordered garlic bread soup salad and sushi
7:24:29
so that's the basics of creating a list box in python if you want a copy of all this code
7:24:35
i'll post all of this in the comments down below but yeah that's the basics of list boxes
in python
75.messagebox
7:24:41
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys how we can create
7:24:47
message boxes in python so sit back relax and enjoy the show
7:24:54
okay people let's create some message boxes but first we'll need an import so from
tk enter import
7:25:02
message box and what this will do is import the message box
7:25:09
library so it's not necessary to add this comment uh so we'll need a window
7:25:15
window equals tk followed by window dot main loop let's create a button
7:25:22
so button equals button we're going to add this to the window we're going to set the
command to
7:25:29
execute a click function that will have a message box appear when we click on the
button
7:25:35
and let's set the text so text equals click me and then we will need to pack
7:25:43
this button so button.pack now we'll need to create a click function so let's do that at
the top def
7:25:50
click and what do we want to do so there's a few different uh message
7:25:56
dialog boxes that you can use so let's begin with a basic show info message box
7:26:02
so message box dot and there's a few options let's begin with show info show
7:26:09
info this just displays a simple message so if we were to run this we have a
7:26:15
button we click on the button and it's going to create a message box for us and it just
shows some info but we don't
7:26:21
really have any info to show yet so we can set a title and we can set a message
7:26:26
so let's set the title title equals this is an info
7:26:34
message box although part of the title got cut off
7:26:42
let's also set the message message equals you are a person
7:26:53
click me you are a person thanks i didn't know that so there's a few other
7:26:59
message boxes that we can use too so for now i'm just going to turn this into a
comment and i'll just copy this
7:27:05
just to save some time and the next one that we have is
7:27:12
show warning so this will have a different icon so
7:27:18
the title let's say warning and the message
7:27:24
you have a virus
7:27:31
so now we get this annoying warning message but you can just click ok to get rid of it
7:27:36
so one thing that you might see with tech support scams they'll have a message box
like this
7:27:42
that shows a warning but they'll put this within a while loop that's set to true so this
will just continue on and on
7:27:52
kind of like this so if we were to try and close out of this it's just going to reappear
while
7:28:00
true is true basically so that's just something funny that i wanted to show you
7:28:05
so let's move on i'm going to turn this into a comment then the next one we have is
show error
7:28:17
so show error title set it to whatever you want
7:28:24
error message something went wrong
7:28:36
there's a few more sophisticated ones too we can ask for some user input
7:28:42
so this next one is message box dot ask and there's a few to choose from
7:28:49
let's go with ask okay cancel first we'll set the title title equals
7:28:56
ask okay cancel and a message message
7:29:05
do you want to do the thing
7:29:15
so we can actually put this within an if statement so this returns either true or false
7:29:21
depending on what you click so let's put this within an if statement if this is true
7:29:28
print you did a thing
7:29:34
if it returns false we can use an else statement else what else do we want to do we'll
7:29:40
say else print you
7:29:45
cancelled a thing and let's test this
7:29:56
but i am forgetting a colon there do you want to do the thing okay you did
7:30:04
a thing let's try it again do you want to do the thing cancel you cancelled a thing cool
7:30:11
there's a few others so i'll turn all of these into comments
7:30:17
and the next one we can go over is ask retry cancel and this is fairly similar to what
we
7:30:23
had before i'm going to copy this let's change this to ask
7:30:32
retry cancel ask retry cancel do you want to
7:30:40
retry the thing you retried a thing
7:30:48
or you cancelled a thing so it's very similar to what we had before do you want to
retry the thing retry you
7:30:55
retried a thing and cancel you cancelled a thing
7:31:03
okay the next one we have is ask yes no so we can get some user input
7:31:11
if it's a yes or a no so this returns a boolean value a true
7:31:17
or false value so message box dot ask
7:31:24
yes no and we'll set the title title equals
7:31:31
ask yes or no and a message
7:31:40
do you like cake
7:31:47
so let's put this within an if statement because this returns a true or false answer
print
7:31:57
i like cake too else they answered no then if this is
7:32:05
false print why do you not like cake
7:32:15
and i'm missing a colon there we go so if they click yes this returns true
7:32:22
and we execute this line of code i like cake too if they answer no it's going to
7:32:28
execute this else statement do you like cake no why do you not like cake now what
we have next is ask
7:32:36
question and this doesn't return a boolean value of true or false it actually returns a
string of yes or
7:32:43
no so this is message box dot ask question
7:32:50
and we'll set the title to ask question and a message
7:32:57
message equals do you like pi
7:33:05
so let's just print the result what this returns so print everything within here
7:33:13
so like i said this doesn't return it true or false but yes or no
7:33:20
so what we could do is assign the response to a variable such as answer
7:33:27
so answer equals whatever the answer is whatever they click whatever button they
click on so
7:33:33
if our answer is equal to yes
7:33:38
will print a message print i like pie 2.
7:33:47
then else the answer is no will print why do you
7:33:54
not like pie
7:34:00
so do you like pie yes i like pie too do you like pie no why do you not like pie
7:34:07
so this next one is probably the most difficult it's ask yes no cancel
7:34:15
so it's message box dot ask yes no cancel
7:34:21
it's this last one so let's set the title title equals whatever you want yes
7:34:28
no cancel and a message message
7:34:35
equals do you like to code
7:34:42
so let's just print the results what this returns so print everything just so we can take
7:34:50
a look so the possible answers are true false
7:34:58
or none so we can just run some if statements for this
7:35:04
so if answer is equal to true
7:35:11
what we can do is print you like to code
7:35:20
oh but we need to assign this to an answer so answer equals all of this
7:35:29
else if answer is equal to false
7:35:37
let's print then why are you watching a video
7:35:44
on coding and then else the response is none so we
7:35:51
can just use an else statement so else you have dodged the question
7:36:02
okay let's try this do you like to code yes you like to code
7:36:08
uh but this should really be a smiley face right do you like to code no then why are
you
7:36:15
watching a video on coding do you like to code cancel you have dodged the question
7:36:22
no one other option available to you is that you can change the icon that you're using
so let's say that we want to change this
7:36:29
icon so right now it is currently this question mark so we can set this to
7:36:34
one of a few preset icons so icon equals and let's set this to warning
7:36:42
so instead of that question mark we get this yellow exclamation point and you can
also use info
7:36:54
it's just an eye and error
7:37:02
so do it fits best for you all right well that's the basics on message boxes in
7:37:08
python if you'd like a copy of all this code i'll post all of this in the comments down
below but yeah that's the basics of
7:37:14
message boxes in python hey what's going on everybody it's your bro here hope
you're doing well and in this video i'm
76.colorchooser
7:37:21
going to teach you guys about the color chooser module in python so sit back relax
and enjoy the show
7:37:29
okay everybody so before we begin it we'll need a second import so let's add this at
the top so from tk enter import
7:37:37
color chooser and the reason that we need a second import and it's not included with
this first import
7:37:44
is because this is a sub module it's not necessary to add this comment i
7:37:49
just like to add comments for teaching purposes so make sure you have these two
imports before we begin
7:37:55
now we'll need a window and a button that we can add to the window and when we
click on the button we're
7:38:00
going to select a color and change the background color of the window so let's
create a window
7:38:07
window equals tk followed by window.main loop and then let's set the geometry so
7:38:14
we're going to change the background color of the window after we select a color so
let's select a fairly large size for
7:38:21
a window maybe 420 by 420 so this is the width and the height
7:38:26
and then let's create a button so button equals button let's set some text text equals
7:38:34
click me and we're going to set a command for this button that is
7:38:39
called so we're going to create a function maybe called click and then we just need
to pack this
7:38:45
button so we can add this to the window so button.pack now we're going to need to
create a
7:38:50
function and we'll call it click so when we click on the button this function is going to
be called
7:38:56
so def click and what do we want to do after clicking this button
7:39:01
what we're going to do is take color chooser and we're going to use the ask
7:39:09
color function so currently this is what this does we can select a color after clicking
7:39:15
this button and pick whatever color you want then you hit ok however we need to
store
7:39:21
this color within let's say a variable so we'll create a variable called color color equals
color chooser dot ask color and then we
7:39:28
can do something with this color but for now let's just print this color just to take a
look to see what it
7:39:33
contains so print color
7:39:39
so let's say that i want this green color click ok this is what this prints this
7:39:46
is our color so this is the rgb values that we have in this
7:39:51
first element so this is the amount of red then green and then blue and the second
7:39:58
element is the hexadecimal representation of these values so what we want is this
hexadecimal
7:40:06
value so what we can do is create a second variable to store this so let's just call this
color hex
7:40:14
color hex equals color and we're going to get the first element
7:40:21
well technically it's the second element but it's the element at position one so this
one
7:40:27
this is zero index zero so we're going to basically extract this hexadecimal value
7:40:33
and let's just print this just to be sure that it's working fine so print color
7:40:38
hex and let's try this again but select a different color uh maybe i want this purple
color then
7:40:45
click ok so this is cb42ff
7:40:50
and here we have cb42ff now let's change the background color so
7:40:56
since we have this hex value that's actually fairly simple so we're going to take
window dot config
7:41:05
bg for background color equals color hex
7:41:11
and this will change background color
7:41:18
and let's set this to a sky blue color maybe like this click ok and it changes
7:41:25
the color however it's not really necessary to continue printing these values you can
take these out if you want
7:41:33
and this would work like just the same then now this part isn't necessary but if you
7:41:40
want you can condense these three lines of code that we have for our click function
into less lines of code so what you
7:41:47
could do instead of assigning the value of color at index 1
7:41:53
to a separate variable we can take color at index 1 get rid of this line and assign bg
7:42:00
equals color at index one so now this is done with just two lines of code and why
stop there we
7:42:06
can write this with just one line of code so now we're going to take color chooser dot
ask
7:42:12
color get rid of this line bg equals color chooser dot ask color at index
7:42:20
one and now this is done with just one line of code however it's
7:42:27
a i would say a little more difficult to teach so that's why i kind of like to separate
7:42:32
this line by line so if you want you can condense all of this into just one line of code
7:42:38
so here's some ideas of where the color chooser sub module could be helpful so of
course you can change the
7:42:44
background color too but let's say that you're creating a game and you can
customize your character or avatar
7:42:51
well it would be kind of nice if you could have some way for the user to actually
change maybe the character's
7:42:56
hair color eye color something like this would actually work fairly well so yeah that's
the basics of color
7:43:02
chooser if you'd like a copy of all this code i'll post this in the comments down below
but that's the basics of
7:43:08
color chooser in python hey what's going on everybody it's bro here hope you're
doing well and in this video i'm going
77.text area
7:43:14
to teach you guys how we can create a text area in python so sit back relax and
enjoy the show
7:43:21
all right my fellow bros let's discuss the text widget this functions like a text area
7:43:27
you can enter multiple lines of text i'm not sure why it's called a text widget instead of
a
7:43:33
text area widget text area would make more sense but idk though so let's begin by
7:43:39
creating a window window equals tk followed by window.main loop
7:43:46
so to create a text widget also known as a text area let's call this text text equals text
7:43:54
and we're going to add this text to our window and then we need to pack this text text
dot pack
7:44:03
so we should have a text area and you can enter in
7:44:09
stuff like this all right but what good is a text area
7:44:16
if you can't actually submit some text so let's create a button to do that for us
7:44:21
so button equals button we're going to add this button to the window
7:44:28
and button dot pack so we need a command for this button
7:44:36
and a function as well so let's create a function called submit it's going to be a submit
button
7:44:42
and let's change the text to because we can text equals submit
7:44:50
cool now let's create a function called submit and we'll do this at the top d e f
7:44:56
submit and what is this going to do well it's going to
7:45:02
get the text from the text area and let's assign this to a variable and we
7:45:07
can call this maybe input or whatever you want so to get the text
7:45:12
from the text area let's say text that's the name of this text area dot get but we need
7:45:20
a starting index as well as an ending index so we're going to place 1.0 here
7:45:27
for the beginning index this is the first line we want to get everything so for the
7:45:33
second index we can just put end then we can do anything we want with this input so
let's just print this to
7:45:39
the console window so print input and this should work
7:45:49
hey you drink plenty of water love you
7:45:55
and click submit and then you can do whatever you want with this input well that's
how a text area
7:46:02
functions but now let's customize the appearance of this text area and i have a few
ideas that you might be interested
7:46:08
in so let's change the background color of this text area so bg equals and i will pick
7:46:15
light yellow and you will see why in just a second so now this text area kind of
resembles
7:46:22
maybe a piece of paper a notebook a post-it note however this font is really small so
let's
7:46:30
change the font styling and the font size so
7:46:35
font equals and i will pick the font ink free this somewhat resembles
7:46:42
handwriting so i kind of like this and it kind of fits our notebook theme and then a size
maybe 25. one thing you
7:46:49
should know the text area size corresponds directly with the font size so 25 is a fairly
7:46:57
large font so if we were to compile this and run this the size of our text area is now
massive
7:47:05
because it corresponds directly with the font size so we might want to limit the width
and
7:47:10
the height so the height is the amount of characters that this is tall
7:47:16
so let's say 8 and the width is the amount of characters that this is long and let's say
20.
7:47:24
and now this should be much smaller and it kind of resembles a post-it note which is
kind of cool let's also change
7:47:31
the amount of padding that we have we can do that with pad x and pad y so
7:47:36
pad x let's say 20 and pad y also 20. so the text shouldn't
7:47:43
actually touch any of the borders this is some sample
7:47:50
text cool let's finish this by changing the font color so that is
7:47:56
fg for foreground foreground equals and i'll pick purple because purple is a
7:48:02
pretty color and let's run this one last time
7:48:07
roses are red violets are blue
7:48:14
booty booty booty booty
7:48:19
rockin everywhere
7:48:25
well that's the basics of the text widget in python if you would like a copy of all this
code
7:48:30
i'll post all of this in the comments down below but yeah that's the basics of the text
widget in python
7:48:37
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys how we can use a file dialog
78.open a file (file dialog)
7:48:44
in python to open and read the contents of a file so sit back relax and enjoy the show
7:48:51
okay people so this is how you can use file dialog to open and read the contents of a
file
7:48:57
but first we'll need a second import along with this import at the top so what we're
going to be importing is
7:49:03
from tk enter import file
7:49:08
dialog and then we'll need a window like what we usually do so window equals
7:49:15
tk followed by window dot main loop now we'll need a button that
7:49:21
we're going to add to the window and when we click on this button it's going to
launch our file dialog so
7:49:26
we can select a file so we'll call this button button equals button
7:49:31
let's set some text maybe to just open like we're opening a file and we'll
7:49:36
associate a function to our command for this button so let's create a function called
7:49:43
open file that's fine and then we need to pack this button so we can add it to the
7:49:49
screen so button.pack now we need to create a function called open file and we'll do
this before we
7:49:55
create our window let's type def open file
7:50:00
and when we call this function we're going to take file dialog and use the
7:50:07
ask open file name function so this is going to return a string
7:50:14
and that string is the file path of where your file is located so what we could do is
store this within
7:50:21
a variable so let's store this within a variable called file path and to test this let's just
print this
7:50:29
just to take a look at the contents of this variable so let's just print the file path now i
7:50:35
have a file on my desktop that i want to use called test file so let's click the open
button and this
7:50:42
might not necessarily bring you to your desktop it might bring you someplace else
within your file directory so this just happens to
7:50:49
be bring me to my desktop so i'm going to click the test file that i have and click
7:50:54
open so this prints the file path so it's going to return a string and
7:51:00
it's the file path basically so now we want to open and read the contents of this file
7:51:05
so we don't really need this print line anymore so what we'll do is create a file
7:51:12
variable and we're going to use open we're going to list the file path and
7:51:18
then we're going to use r for read so the default is normally rt for read text otherwise
you can read
7:51:25
binary uh but we can just set this to r r is the same as rt basically uh so then
7:51:30
we're going to open this file and then we're going to read it so let's just print this to
the console window
7:51:37
so print the name of the file which is file where this is located file dot
7:51:45
read function and then it's a good idea to close your files after you're done with them
so file dot
7:51:51
close and let's test this so i'm going to click open find the file that i want
7:52:00
click it and then open and that text file says my name is bro i
7:52:07
like pizza now let's say you want to make this a little more advanced so what i'm
going to do is that i'm going to set the initial
7:52:14
directory of where we begin looking for a file when we launch our file dialog so with
that test file i'm going to move
7:52:21
this to my project folder so i'm just going to paste it so now this is within my project
folder
7:52:28
now i'm going to set the initial directory within the function of ask open file name
7:52:35
so we're going to set the initial d i r for initial directory and then we
7:52:40
can list a file path so one thing you could do i want to get this
7:52:46
path here this file path so if you're using pycharm what you could do is right click this
copy path and then get the absolute path
7:52:53
and then i'm just going to paste this and then uh with these backslashes you'll have
to do double backslashes
7:52:59
because this is the escape character for a backslash alright so this should open my
project
7:53:06
folder which it does and here's that test file
7:53:12
another thing we could do is that we can change the title of our file dialog right now
the default is just open so we
7:53:18
can change that to something else so i'm going to add a comma maybe put this on a
different line
7:53:24
uh so this is still within the function of ask open file name so i'm going to set the title
to
7:53:30
something else i'll say open file
7:53:35
okay so if we were to run this again instead of just saying open it says open
7:53:43
file okay and we can also limit the file types that we accept and look for
7:53:49
so we can use file types
7:53:55
so let's say that we want to initially look for a txt file so what we'll do is type in
7:54:04
like a name for the file that we're looking for so this is what's going to appear to the
user uh text files followed by an extension
7:54:12
so we want asterisk dot txt
7:54:19
and then we can also have an option for all files so i'm going to add a comma for a
second
7:54:24
option and then let's say all
7:54:29
files and then an extension so comma
7:54:34
asterisk dot asterisk this will look for all files uh but i think i am missing a
7:54:40
parenthesis there that should be good so then when we open our file dialog
7:54:47
it's going to initially be looking for plain text files and with this drop down menu it says
7:54:52
text files and all files so that's what we have listed within our file types of what we
want to
7:54:58
search for so you can see that if i switch to all files our python file actually appears
because
7:55:06
we don't have any limits on the files that we're trying to open or search for so if i
change this back to text files
7:55:13
only folders and plain text files are visible so then we can open our test file again
7:55:19
but yeah that's the basics of file dialog in the next video we're going to be saving
7:55:24
a file so if you'd like a copy of all this code i'll post all of this in the comments down
below but yeah that's the basics of opening a
7:55:31
file using python hey what's going on everybody it's your bro here hope you're doing
well and in this video i'm going
79.save a file (file dialog)
7:55:37
to teach you guys how we can use python's file dialog to save a file someplace on
your computer
7:55:43
so sit back relax and enjoy the show okay everybody so this is how we can
7:55:50
save a file we're going to be reusing a file dialog again kind of like what we did with
the
7:55:56
video on opening and reading a file so from tk enter import file dialog we're going
7:56:03
to be creating a window and adding a button as well as a text area to this window so
let's create our window window equals
7:56:10
tk followed by window.main loop let's create a button so button equals
7:56:18
button let's set the text of the button so text equals save it's going to be a save
button and
7:56:25
then a command so we'll need to link a function to this command so let's create a
7:56:30
save file function and then we need to pack this button so button dot pack
7:56:38
now let's create a text area this is just called text so text equals text we're going to
7:56:44
add this to the window and we need to pack this text so text dot pack and now let's
create
7:56:52
a save file function so at the top of our program d e f save file
7:57:02
and then we're going to take file dialog and use the ask save as file function and
we're
7:57:09
going to store the contents within file file equals file dialog dot ask save as file
7:57:18
let's run this just to test it out so we have our window a save button and this
7:57:25
is our text area we can type in multiple
7:57:32
lines of text yay and let's try and save this just to
7:57:39
see what we have so far so this will bring me to my desktop it might bring you
someplace else for
7:57:46
your computer so i'm just going to save this as test file now with this drop down
menu
7:57:53
save as type there's nothing set up so we'll need to do that manually uh if we were to
save this and here's
7:58:00
that test file on my desktop so let's take a look at the properties we did not list a
7:58:06
file extension so the type of file just says file so we'll get to that later on in
7:58:11
this video and if we were to open this with a program like notepad well it's currently
empty that's because
7:58:18
we need to write some text to this file so let's do that next
7:58:23
let's store all of the text from our text area to a variable let's call this variable file text
7:58:32
file text equals and then we need to get all of the text from our text area so
7:58:38
text dot get and we're going to list the starting index as well as the ending
7:58:44
index for this function so we're going to begin at 1.0 that is the beginning of this text
7:58:51
area and the ending we can just put end and then we need to convert all of this into
a string so let's
7:58:57
surround this with str and then we can store all of the text
7:59:03
from our text area within this variable file text and now we just need to write
7:59:09
our text to this file so file dot write and we're going to pass in file
7:59:15
text and then it's a good idea to close your files when you're done using them so file
7:59:21
dot close and let's try this again
7:59:26
hello my name is bro let's save this
7:59:34
i'll call this hello and click save and we still can't save s-type so we'll
7:59:40
work on that next let's go to my desktop here's the test file that we created i'm going
to open
7:59:46
this with notepad and it has all of the text that we added to that text area now
7:59:52
let's list some available file extensions that we can have the user save this file as so
let's begin by
7:59:59
setting the default extension so default extension and let's set this to
8:00:07
a text file that is dot txt and then we can list
8:00:14
all of the available file types so file types
8:00:21
and we're going to place these within a set of straight brackets and i'll just separate
these line by
8:00:26
line so let's begin with a plain text file so this will take two strings
8:00:32
the first is the message that will appear within that save as type drop down menu so
8:00:39
let's place text file and the second string
8:00:44
the file extension and that is dot txt
8:00:51
then let's add another let's say html so i'm going to copy this and paste it
8:00:57
and let's say html next dot html and then lastly you can do
8:01:06
just all files so all files and that is dot
8:01:13
asterisk and let's try this again so hello my
8:01:21
name is bro i like pizza and
8:01:28
ice cream and this time i think i'll save this as an html file
8:01:35
and this is we'll call this pizza and ice cream i guess
8:01:43
and click save now let's go back to my desktop here's that file pizza and ice cream
and
8:01:49
it is an file let's just verify that yep.html for
8:01:56
the type of file so we can open this with notepad
8:02:01
and it has all of the text i could even open this with sublime text as well kind of like
that
8:02:11
all right let's change the initial directory for saving a file let's change it to
8:02:18
my project folder for uh this python file that we have so that is and i'm just going to
8:02:25
organize this a little bit initial d i r
8:02:31
equals and then we can list a file path so i'm just going to copy this
8:02:37
so copy path paste it
8:02:42
then i want to make sure that these are double backslashes
8:02:49
and i need to add a comma at the end okay let's try this again
8:02:54
so testing one two three and this should go to my
8:03:01
project folder which it does testing123
8:03:07
and let's save this as a plain text file click save
8:03:12
and here's that plain text file testing123 another option available to you is that
8:03:18
you don't necessarily need to use a text area to get some text to write to a file
8:03:24
you could use the console window to accept some text so to demonstrate this i'm
going to turn
8:03:30
this line into a comment and we're instead going to use the console window this time
8:03:36
so we'll say file text equals input and then let's just have a prompt
8:03:43
here enter some text i guess and then let's run this again so
8:03:50
we still need to click this save button and select a file location i'll just save over our
file
8:03:57
testing123 i'll click save yeah i'll replace it alright so we need
8:04:02
to enter some text i guess so i will write a message
8:04:08
such as the cake is a lie and hit enter
8:04:16
so then our file testing123 now says the cake is a lie so you don't
8:04:21
necessarily need to use the get function for a text area you can
8:04:26
use the console window as well here's one last thing to consider before you go so
what if we begin the process
8:04:33
of finding a file location to save a file to and then we exit out of this window well
8:04:40
we're going to encounter an exception so one way that we can prevent this is to add
8:04:46
this line if file is none return
8:04:52
so then if we were to try this again begin saving a file and then exit out of
8:04:58
this there is no exception that we encounter so that's just one little extra i
8:05:04
thought i would throw in but yeah that's the basics of saving files in python if you
would like a copy
8:05:09
of all this code i'll post all of this in the comments down below but yeah that's how to
save a
8:05:15
file using python hey what's going on everybody it's your bro here hope you're doing
well and in
80.menubar
8:05:20
this video i'm going to teach you guys how we can create a menu bar in python so sit
back relax and enjoy
8:05:26
the show all right people let's create a menu bar
8:05:31
not too different from the menu bar that you see at the top of many programs there's
usually a tab for file edit view help stuff like that
8:05:39
except we're going to create one that doesn't have as many options or features it's
going to be the generic
8:05:45
brand menu bar like the walmart variety so let's begin by creating a window so
window equals tk followed by
8:05:53
window.mainloop then we're going to create a menu bar and add menus to the menu
bar and each
8:05:59
menu is going to function like a dropdown menu where you hover over one of these
tabs
8:06:04
and there's a drop down so we need a menu bar let's just call this menu bar menu
bar
8:06:10
equals menu and we're adding this to our window so then we're going to take
window dot
8:06:17
config and we can set the menu of this window to equal the menu bar that we just
8:06:24
created with each of these tabs for your menu bar you need to create a separate
menu and add each menu to your menu bar and
8:06:31
the menu bar is added to the window there's different levels to the stuff so let's
create
8:06:37
a file menu because well this one has a file menu why not our program so file
8:06:44
menu equals menu kind of like what we did with our menu
8:06:49
bar except instead of adding this to our window we're going to add this to our menu
bar so menu
8:06:57
bar like that there's one other step two we need to use this function menu
8:07:05
bar dot add underscore cascade this will have a drop
8:07:12
down menu sort of effect so let's create a label just called file to mimic the file tab
8:07:19
that you see at the top of many programs so label file and we're going to set the
8:07:25
menu to equal the file menu that we created so file menu and let's take a look just
8:07:33
to see what we have so here's our menu bar and here's our menu there's a drop
down
8:07:39
effect for this but we need to add actual options to this file menu so let's do that
8:07:46
so let's begin by creating let's see we got maybe open save and exit
8:07:54
that should be good so file menu dot add
8:08:01
underscore command so this is a clickable option and let's create a label called open
8:08:08
and we can open a file and maybe two more so we got open
8:08:14
save and exit open save exit
8:08:21
and let's take a look at this so here's the drop down menu we can open
8:08:27
save exit now you might notice that there is this annoying line this is called a tear off
you can
8:08:34
actually get rid of this if you want so when we create this file menu we're
8:08:39
going to set tear off to equal zero and that should get rid of that
8:08:47
and it's gone so sometimes you might see a line to separate sections well
8:08:54
you can do that with a separator so let's separate the exit command that we have
8:09:01
from open and save and to add that separator we just type in the name of our menu
file menu dot add
8:09:10
separator so this will separate your different commands from each other
8:09:16
within a menu you can see that there is a line between save and exit because we
placed a
8:09:21
separator here now if you want each of these file commands to actually do
something you need to associate
8:09:27
a command with each of these commands it's a command within a command so
command equals for our open file
8:09:35
command we'll create a function called open file
8:09:40
and we'll need to declare that function and then let's create a command called save
file and then another called
8:09:48
exit actually one shortcut if you want to exit something you can just say quit all right
let's create an
8:09:56
open file and a save file function so def open file
8:10:03
let's just print something just to keep this simple i already have another video on
opening
8:10:09
files so print file has been opened and let's do the same thing
8:10:18
for save file
8:10:25
file has been saved and then our last command exit we'll
8:10:30
just quit so let's try this so let's go to file here's the drop down menu we can
8:10:36
open a file file has been opened we can save a file and then exit and exit will close
out of
8:10:43
our window let's create a second menu called edit to mimic the one that we have at
the top
8:10:48
of our ide so let's create a edit menu so it's the same process that we did with our file
8:10:54
menu so let's call this edit menu equals menu
8:10:59
we're adding this to our menu bar and i do not want a tear off so tear off equals zero
then for the
8:11:07
next line we need to add cascade so i'm going to copy this
8:11:12
menu bar dot add cascade the label name will be edit and the file i mean the
8:11:18
menu will be edit menu and we're setting this menu that we're adding to edit menu
8:11:26
and let's try this so we have file and edit but we need to add commands to this edit
8:11:31
menu so it's the same process that we did with our file menu i'm just going to copy
this
8:11:36
change file to edit and let's make a command called cut copy and paste
8:11:44
so the label will be cut we'll create a command called cut we'll copy this
8:11:54
and we'll create a another command called copy and then paste
8:12:08
all right then we just need to create some functions for this so cut copy paste let's do
that at the top
8:12:18
so cut
8:12:24
you cut some text
8:12:29
so cut copy and then paste
8:12:35
you copied some text and paste you
8:12:42
pasted some text all right let's try this so here's our
8:12:47
edit menu here's the drop down we can cut we can copy and we can paste not
actually but it's
8:12:55
going to call a function that can do something like that if you want to set that up
maybe that's a video for a
8:13:01
different day let's customize these menus well because we can so let's begin by
changing the font so
8:13:08
we can just type in within each of these menus font equals pick a font that you want
and then a
8:13:14
size so i'm going to add this font for both of these menus that we have and
8:13:19
then when we hover over and click on these menus you can see that the font has
changed
8:13:25
did you guys know that you can actually add images next to each of these
commands well you can and this is how we can do
8:13:31
that first you'll need some images here's a few that i'm using so add these to your
project folder i plan on using a
8:13:38
floppy disk for save and open folder for open and for some reason a stop sign for exit
because i
8:13:44
couldn't really think of anything at the moment i guess an arrow would have worked
too i guess so let's create some photo images
8:13:50
out of these images so i already did this i'm just going to copy and paste the text that
i had
8:13:57
so create a unique name for each image i have open image save image and exit
image
8:14:03
they're all photo images and here is the file path or the file name for each of these
8:14:09
pictures that i'm using so to add an image to each of these commands when you add
8:14:14
the command we can just say image and then the photo image that you want to use
8:14:19
so with my open command i'm going to use this open image photo image that i have
and
8:14:25
this is the open folder so i'm going to do the same thing for save and exit so we have
save image
8:14:33
and exit image
8:14:38
but when we run and compile this let's take a look yeah so all the text is gone that's
8:14:44
going to be a problem so we'll need to compound views so with compound we can
add
8:14:50
an image on a certain side of the text and keep the text so compound
8:14:56
let's say we want the image on the left so compound left and i'm just going to add
8:15:02
this to each of these commands and now we should have an image as well
8:15:09
as some text well that's the basics of creating a menu bar in python if you want a
copy of
8:15:15
all this code i'll post all of this in the comments down below but yeah that's how you
can create a simple
8:15:20
menu bar in python what's going on everybody it's bro here hope you're
81.frames ⚰️
8:15:25
doing well and in this video i'm going to teach you guys how frames work in python
so sit back relax
8:15:31
and enjoy the show what's going on people we're going to be discussing frames
today
8:15:38
a frame is really just a rectangular container to group and hold widgets together
8:15:43
so we're going to practice using frames today but first we'll need a window we're
going to create a frame and add a
8:15:49
frame to the window so window equals tk followed by window.main loop
8:15:56
well we're going to need some widgets to add to our frame so let's create a bunch of
buttons
8:16:01
because why not so i'm going to show you guys a shortcut too so let's say
8:16:06
button equals button and then for now we're going to add this
8:16:12
to our window but we're going to replace this eventually with frame when we create
this and i'm just
8:16:18
going to set the text to equal let's say w we're going to have four different
8:16:24
buttons wasd like the buttons on the keyboard if you're playing a game and you want
to move forward
8:16:30
left back or right so we're adding this to the window for now let's set the text to w
8:16:38
and what else can we do uh let's change the font so font equals i'll pick the font
consoles
8:16:45
because that tends to be even uh with buttons the size of the button adjusts to uh the
8:16:52
size of the uh letter that you're using or the size of the text uh so let's set this to 25
8:17:00
and then a width so maybe width equals three all right and then
8:17:07
button.pack and i just want to take a look at this
8:17:14
so here's our button w so i'd like to show you guys a shortcut if you don't plan on
using this
8:17:20
button by name you don't really need to give it a name you can just say button and
then have all your uh options here
8:17:28
and then instead of having button.pack just add dot pack to the end so this will do
the
8:17:34
same thing however you can't adjust this button by its name though because it
doesn't have
8:17:39
a name anymore so let's create a few other buttons one for a s and d so we have w
8:17:47
asd
8:17:52
and by default they're all top and center so with pack we can actually put these
8:17:59
on a certain side so let's say we want uh w on the top
8:18:04
and then the other three will be all left so side equals left and then after i
8:18:11
show this to you you'll see the benefits of using a frame all right so right now they're
all at
8:18:18
least it appears that they're all within a container so what if i were to expand this well
w
8:18:23
is just drifting off it's sailing away by w so what if we could put these within a
8:18:29
frame or container to actually contain all of these widgets well we can
8:18:34
so let's create a frame frame equals frame
8:18:41
and instead of adding these buttons to the window we're going to add these buttons
to our frame
8:18:51
and now we need to add the frame to the window
8:18:56
and this is what this looks like however we need to actually add this by using pack or
place
8:19:03
so frame dot pack so now what this looks like is that all of these buttons all of
8:19:10
these widgets are now contained within a frame a container let's change the
background
8:19:16
color of this frame just so that we can see the width and the height of this frame so
8:19:21
when you create your frame there is an option for background color it's just bg
8:19:27
and let's set this to a color we haven't done pink yet pink is a very masculine color so
now
8:19:33
the background of our frame is pink and that might be a better visual cue to the
balance of
8:19:40
the frame let's also give this frame a border so let's set a width of 5 using bd
8:19:47
and then the type of border that we want and we can specify that with relief so relief
8:19:53
equals let's say raised so this will have a 3d pop effect kind of like that let's try
sunken
8:20:00
there's a few other ones as well but i don't feel like it's necessary to go over all the
border types let's stick
8:20:07
with sunken for now that'll work alright so with this frame we are currently packing
this
8:20:13
we can set this on a certain side within our window so currently it's at the top let's set
8:20:19
this to maybe the bottom so side equals bottom
8:20:25
now this frame is sticking to the bottom of the window and it still contains all of the
buttons
8:20:31
all of the widgets that we added another function available to you is the place
function we can place this
8:20:38
frame at certain coordinates within our window so let's set
8:20:43
x to equal zero and y to equal zero so then when we place this frame within
8:20:48
our window and set some coordinates this entire frame is going to stick at
8:20:54
these coordinates even if we were to resize this window and we can change these
too
8:21:00
let's say x is now 100 and y is also 100
8:21:05
so the frame along with all of the components all of the widgets that it contains
8:21:11
are sticking within the container and they move along with this so that's the basics of
frames it's
8:21:17
really just a rectangular container to group and hold widgets together so if you want
a copy of all this code
8:21:24
i'll post all of this in the comments down below and well yeah that's how frames work
in
8:21:29
python hey what's going on everybody it's your bro here hope you're doing well and
in this video i'm going to teach you guys a
82.new windows
8:21:35
few different ways in which we can create new windows using python so sit back
relax and enjoy the show
8:21:42
let's get started everybody so we're going to create a window and we'll add a button
to this window when we click on this button it'll
8:21:48
create a new separate window for us so window equals tk followed by window.main
loop
8:21:54
we'll need to add a button so let's do that button we're adding this to the window let's
add some text to the button let's
8:22:00
say create new window and we'll set a command
8:22:06
command equals and we'll need to define a function let's create a function called
create
8:22:12
underscore window and let's pack this as well so we will define
8:22:17
this function create window and let's do so at the top def create window and what do
we want
8:22:23
this to do well we're going to create a new window and let's call this next window
new underscore window equals and there's
8:22:31
two popular ways of creating a window one is we can use tk and the other is
8:22:36
that we can use something called top level and let me explain the difference between
the two here's a definition for a top level
8:22:43
window this is a new window on top of other windows and it's usually linked to a
bottom
8:22:51
window our main window is serving as the bottom window and our new window is
serving as the top
8:22:57
window the top level window so let's try this and just experiment here is our main
window
8:23:03
the bottom window and if we were to click on this button it's going to create a new
window for us a new top level window
8:23:11
if we were to close out of the bottom level window any top level windows associated
with
8:23:17
this would also close kind of like what you just saw now however it's not the same
case
8:23:22
the other way around if we have a bottom window and a top level window and we
close out of the top level window
8:23:29
our bottom level window is just fine it didn't close out of that so think of these like
8:23:34
jenga pieces if you were to pull one of the pieces one of the windows out from
underneath
8:23:40
the bottom any pieces or windows on the top of it will close out as well or collapse
kind of like in the game
8:23:46
jenga so that's one way to think of top level windows they're stacked on top of any
8:23:52
bottom level windows if you remove a bottom piece it removes any from the top on
the flip side we have tk this is a
8:23:59
new independent window and we've actually been doing this since day one of our gui
programming tk is a
8:24:06
new independent window and our main window and the new window that we create
are not linked whatsoever they're
8:24:12
completely independent so if we were to create a new window this is our new
window and close out of
8:24:17
our old window well this new window still persists so this could be useful for let's say
a login form or a login screen
8:24:25
after you log in you'll be brought to your main program which could be within the new
window one other thing that you can do too you
8:24:31
can also destroy the old window after creating a new window so we need to type in
the name of the
8:24:37
window that we want to destroy actually let's call the main window just old
underscore window just to clarify
8:24:43
things so let's rename that here here and here as well so we want to destroy our old
window as
8:24:51
if we're closing out of it so we type in the name of the window old underscore window
8:24:56
and we use the destroy function and that sounds pretty sweet so we're going to
destroy our old window
8:25:02
after creating our new window so let me add a comment that this will close out of old
window
8:25:10
and let's test it so we have our old window our main window we'll create a new
8:25:16
window it creates our new window and then closes out of the old window so that's
one of a few ways in which you
8:25:22
can create a new window for your program so if you want to copy of all this code i'll
post all of this in the comments
8:25:28
down below but yeah that's how to create a new window using python hey what's
going on everybody too bro
83.window tabs
8:25:34
here hope you're doing well and in this video i'm going to teach you guys how we
can create separate tabs for our gui applications in python so
8:25:41
sit back relax and enjoy the show okay let's begin so in order to create
8:25:47
tabs for our python gui application we're going to need access to a widget
8:25:52
called notebook and that's found in a different module so this time we're going to
need two imports so at the top from tk enter
8:26:00
import just everything but the notebook widget is found within a separate module
8:26:05
so we'll need a second import from tk enter import ttk so this ttk import
8:26:14
gives us access to several different widgets that are normally not available to us so
once you have these two imports
8:26:20
you're ready to begin so we'll need a window window equals tk followed by
window.main
8:26:26
loop and we need to create a notebook widget so right after you create your window
we're
8:26:31
going to create a notebook we'll call this notebook equals ttk
8:26:37
dot notebook and we want to add this notebook to our window so let me
8:26:43
add a comment here what this widget does so this is a widget that manages
8:26:49
a collection of windows and displays and that's really all there
8:26:55
is to it so in order to create different tabs we're going to create some frames so for
the first tab i'll call this tab
8:27:02
1 equals frame and we want to add this to the notebook
8:27:08
and with our notebook we're adding this notebook to the window all right so this will
be a new frame
8:27:16
for tab one and let's create a second tab and we'll call this tab two and you can
8:27:22
create more tabs if you want but that might be overkill so this is a new frame for tab
two
8:27:28
after you finish these two lines of code we're going to follow this up with notebook.ad
so notebook.ad
8:27:36
what widget are we adding we're adding our tab 1 frame and we can set some text
for
8:27:43
this tab so text equals and i'll type in tab one and we'll repeat the process for
8:27:49
tab two so notebook dot add tab two and we'll change the text to tab two and
8:27:57
then we need to pack this notebook notebook dot pack
8:28:02
and let's take a look to see what we have all right we have tab one and tab two but
we'll probably
8:28:09
want to actually add something to each of these tabs so that the content is visible so
let's create some
8:28:15
labels so after we pack our notebook let's create some labels i don't really feel like
giving these
8:28:21
names because we don't really plan on using them for anything else so we'll just say
label tab
8:28:27
one that's going to be the parent widget and let's set some text text equals
8:28:33
hello this is tab number one then i think i'll give this a
8:28:40
width and a height too just so it's more visible so for the width i'll make this 50
8:28:45
and the height 25 so this will be roughly a square shape
8:28:51
and then we need to pack this so i'm just going to add pack and let's create a second
label for tab
8:28:57
two so label tab two maybe i'll say goodbye this is tab number two
8:29:05
okay we should have some actual content for these labels now for these tabs
8:29:10
so here's the label this is tab one and this is tab two and you can switch between
these and they display
8:29:16
each of these separate labels however with the way that this window is arranged if
we were to expand the size of this
8:29:23
window well our frames and our tabs kind of stay in the top and in the center so i'm
going to add a
8:29:30
few lines within the pack function of our notebook the first thing i'm going to do is use
the expand option and set this to
8:29:38
true so what this does and i'm just going to add a comment that expand
8:29:44
this will expand to fill any space
8:29:50
not otherwise used so what happens now
8:29:57
is that this will actually expand and it's staying in the center of the window but there's
one more thing we're
8:30:04
going to add fill equals both normally this is x or y or you can do
8:30:11
both so i'll add a comment as to what this option does so fill will fill
8:30:19
space on x and y axis so if you combine
8:30:26
both of these together what you get is that these tabs will stay in the top left corner
and if you were
8:30:32
to expand the size of this window the tabs aren't going to move they're going to stick
to the top left
8:30:38
but the size of the window will expand all right so that's the basics of creating tabs in
python if you want a
8:30:44
copy of all this code i'll post all of this in the comments down below but yeah that's
the basics of creating tabs in python
84.grid
8:30:52
hey what's going on everybody it's your bro here hope you're doing well and in this
video i'm going to teach you guys all about the grid geometry manager in
8:30:59
python so sit back relax and enjoy the show all right everybody let's talk about the
8:31:06
grid geometry manager well because we have to this is a useful way to organize
widgets within a
8:31:12
container imagine our windows being arranged into a series of rows and columns
similar to an excel
8:31:18
spreadsheet i tend to use that example a lot the top left most cell is row zero column
zero computers always
8:31:25
begin with zero so that's why our grid doesn't begin with row one column one and we
can list which
8:31:31
row and column we would like our widgets to be placed within and by default we only
have
8:31:36
one row and one column to work with at the start but we can explicitly state which
row and column this widget should
8:31:43
be placed within relative to the container that it's in and python will create new
relative rows
8:31:48
and columns for us if we state where these widgets should be placed so let's create
a small
8:31:53
submission form and then organize all of our widgets using our new grid system that
we have so let's begin by creating a window
8:32:00
window equals tk followed by window.mainloop now that we have our window we'll
need to create
8:32:05
some widgets to add to this window so let's say that we want an entry box and a
label for a first name last name
8:32:12
and email address as well as a submit button so let's create a label and an entry box
8:32:18
for a first name just to demonstrate this so let's call this label first name
8:32:24
label equals label we're adding this to the window and let's set the text to first
8:32:31
name for now i'm just going to use that pack to demonstrate the differences between
8:32:36
pack and grid and let's create an entry box let's call this first name
8:32:45
entry equals entry we are adding this to the window and for
8:32:51
now we're just going to pack this and let's take a look
8:32:57
so here's our label on the top it says first name and then it has our entry box right
underneath so what
8:33:03
if we want to have this label on the left hand side and our entry box on the right hand
side of the
8:33:09
label we're better off using the grid geometry manager so we're going to replace
pack with grid
8:33:21
and we have to set which row and column that we want each of these widgets to be
placed within normally if you use the pack geometry
8:33:29
manager as you create new widgets they're just going to be placed directly
underneath in one long column so
8:33:36
let's say that for our name label we want to place this where the row is zero and the
column is zero
8:33:43
that's the top left corner so within the grid geometry manager when we call it
8:33:49
we're going to set row to equal zero as well as column to equal zero and then for the
first
8:33:56
name entry we want this to be on the right hand side of our label so we'll place this
within the same row
8:34:03
row equals zero but in a different column column equals one so column one is
directly to
8:34:10
the right of column zero and then when we compile and run this we now have our
entry box
8:34:16
to the right of our label that we have so let's repeat the process for a last name an
email address and
8:34:22
then a submit button so i'm just going to copy what we have here and change first
name to
8:34:27
last name so last name label and we'll change the text to last name
8:34:35
last name entry and we'll keep that the same now we want this last name label to be
8:34:42
underneath the first name label because right now they're going to overlap because
we have them in the same
8:34:48
row and columns so you can see that we can no longer see the first name label or
the first name
8:34:53
entry so let's place this directly underneath we're going to put these underneath by
one row
8:34:59
so we'll keep the columns the same but we're going to put this underneath by one
row so row
8:35:05
one and row one for the last name entry and what you get is that
8:35:10
these two labels and entry boxes are arranged neatly into rows and columns and
let's do the same
8:35:16
thing for an email so i'm going to copy what we have and we're going to change
8:35:22
last name to email so email label and email entry and then
8:35:28
change for the text last name to just email and we're going to place these where row
8:35:34
equals 2 and we'll keep the column the same and for email entry
8:35:39
row 2 column one so doesn't that look a lot better now
8:35:45
let's create a submit button so let's call this submit button
8:35:52
equals button and then we're adding this to the window and we should set some text
text equals
8:35:59
submit i guess dot grid and we want to place this underneath
8:36:06
currently kind of just shoves it underneath for now let's say that we want this
between these two columns
8:36:13
so we'll want to set the row and the column to begin with row is going to equal three
8:36:20
and column we'll set this equal to zero for now there isn't going to be any apparent
change it's still going to be in the
8:36:26
same place but one thing that we can do is use an option called column span we can
have a widget take up more
8:36:32
than one column and it's going to place this widget in between both of these columns
8:36:38
in between the combined width of both of them so we'll add column span
8:36:44
equals two so this widget is going to take up the next two available columns
including
8:36:50
the one that it's currently in and you can see that our submit button is now between
these two columns that we
8:36:56
have one thing that you should know is that the column width is dependent on the
width
8:37:01
of the largest widget that is contained within that particular column let's say that we
increase the width of
8:37:07
our first name label well our column is going to expand to actually fit
8:37:12
that widget that we have so for our first name label let's set the width to maybe 20
8:37:19
and this is going to increase the width of column 0 and to even better demonstrate
this
8:37:25
let's color each label a certain color just for a visual for our first name label let's say
this
8:37:31
is red and then for our last name label let's set this to green
8:37:38
and then for email maybe blue
8:37:46
now all of these are color coordinated and you can see that the background color of
our first name
8:37:51
label is taking up the entire column space now what if we changed the size of our
8:37:58
email label to something even larger like 30 so width equals 30.
8:38:06
and now the column 0 has expanded to accommodate the size of our largest
8:38:11
widget which is our email which has a width of 30 and before we finish this video let's
add a
8:38:17
title to the submission form right on top of the first name label and the entry label so
let's call this title
8:38:25
label equals label writing this to the window let's set some text let's say enter
8:38:32
your info and let's pick font font equals
8:38:40
what am i feeling like today maybe just arial and then a size maybe 25 we're going to
8:38:46
use the grid geometry manager and we want this at the top so we want
8:38:53
row zero at least i want this to take up two columns so we'll say column
8:38:59
equals zero and then set a column span column span equals two
8:39:05
now a few things are going to overlap here so we're going to move everything down
8:39:11
by one row besides our title label so we want
8:39:16
this first name label to be directly underneath our title label so let's move everything
down by one row
8:39:32
and our title is going to be directly on top of our first name label as well as the first
name
8:39:38
entry box that we have all right so that's the very basics of the grid geometry
manager
8:39:43
if you want a copy of all this code i'll post all of this in the comments down below but
yeah that's the basics of the grid
8:39:49
geometry manager in python hey what's going on everybody it's your bro here hope
you're doing
85.progress bar
8:39:55
well and in this video i'm going to teach you guys how we can create a progress bar
in python so sit back relax and enjoy the show
8:40:03
okay let's create a progress bar but we'll need a few imports to begin the progress
bar widget is found within
8:40:09
that ttk module so we'll need a separate import from that from tk enter dot ttk
8:40:16
import everything and we'll import everything from time as well let's
8:40:22
create a window window equals tk followed by window.mainloop we're going to add a
progress bar and a
8:40:28
button to this window when we click on the button it's going to fill our progress bar
let's begin by creating a button to
8:40:35
begin with let's call this button equals button we're adding this to the
8:40:40
window let's set some text text equals maybe download
8:40:45
and then a command maybe i'll call this start finish by adding this button to the
8:40:52
window and we'll just use pack to do that let's define what our start function does
8:40:58
def start we'll fill this in later i'm just going to print nothing just as a placeholder and
8:41:05
we should just have a button now let's add a progress bar before this button
8:41:11
and we'll call this progress bar just bar for short bar equals progress bar
8:41:18
we're adding this to the window we can set an orientation
8:41:25
this can be horizontal or vertical later on in this video i'll show you the differences
between the two
8:41:30
but for now let's set this to horizontal and then i'm going to pack this bar bar
8:41:37
dot pack and add some padding pad y equals 10 and let's see what we have
8:41:46
okay this is our progress bar at the top and this is the button when we click on this
button it's going
8:41:51
to fill our progress bar but i think i'm going to increase the length of this progress bar
8:41:57
let's set this to maybe 300 i believe 100 is the default okay that's not too bad of a
size now
8:42:04
let's define what our start function does we can actually increase the value of
8:42:10
our progress bar by typing in the name of this progress bar here value plus equals
some amount let's say
8:42:18
10. so what's going to happen right now is that every time we click on this
8:42:24
button it's going to fill our progress bar by 10 out of the total which is 100
8:42:32
let's pretend that we're waiting for a certain amount of tasks to complete so let's
define a few
8:42:38
variables let's say we have a variable called tasks and let's set this to 10 just to keep
it simple
8:42:45
and x will represent the current task that we're on and we'll say 0 to begin with let's
8:42:51
create a while loop while x is less than the amount of
8:42:57
tasks that we have we will increment the value of our bar by 10.
8:43:03
10 10 for each test that we have which is 10 for a total of 100
8:43:09
and we'll want to increment the task that we're on as well which is represented by x
so let's
8:43:15
increment x by 1 after each iteration of this while loop now when we run this
8:43:21
and click download this spills instantly let's add a delay just to simulate
8:43:29
each task waiting to be completed so let's add that here let's add a delay by
8:43:35
using time dot sleep and let's set this to one second we're going to wait one second
and then
8:43:41
complete a task what ends up happening now is that
8:43:46
nothing appears to be updating well what gives bro you lie to me well that's because
the window is waiting to have this
8:43:53
progress bar complete before it's actually going to display it being filled
8:43:58
so we would want to refresh this window after each iteration of the while loop
8:44:04
so at the end of this while loop let's add one thing window dot update underscore idle
8:44:11
tasks after each iteration of this while loop it's going to update the window that we
have so then when we
8:44:18
download something it's going to update our progress bar after each iteration of this
while loop
8:44:24
until it's completed right about now let's take this a step further by adding a label that
displays
8:44:31
the current percent of the progress bar that is complete let's add this label after the
progress
8:44:36
bar but before the button sandwiched right between these two widgets so let's add
that here
8:44:42
we'll call this label percent label equals label we'll add this to the
8:44:47
window and we'll need a text variable text variable equals let's call this percent
8:44:54
so the reason that we use a text variable is so that we can update this label with
some text
8:45:00
after each iteration of our while loop and we'll need to pack this okay within the
window
8:45:07
near the top let's define percent percent equals string var this allows us to
8:45:14
update percent with some new text and set the percent label with the new text that
we have
8:45:20
within the while loop but before we update idle tasks let's set what percent is
8:45:28
percent dot set and let's say x divided by tasks and then we'll
8:45:35
multiply this by 100 i'm just going to put this within its own set of parentheses times
100
8:45:43
and then we'll convert all of this to a string so we can display this along with some
8:45:48
text there are a lot of parentheses here so it might be difficult to keep track of all this
8:45:54
all right and then we're just going to tack on percent and that's it let's try it
8:46:03
so we got 10 20 30 40 50. i'll shut up now one change that
8:46:10
i'm gonna make is that i'm going to cast all of this as an integer before we convert it
to a string
8:46:18
because i do not like that decimal at the end of our percentage so yeah it says 10
percent 20 so on and
8:46:25
so forth let's add a label that displays the current task that we're on so let's copy
what we have for our
8:46:31
percent label and let's rename this as maybe task label
8:46:38
task label text variable will equal maybe text
8:46:43
and let's define what text is so text is also a string var and within our
8:46:50
while loop let's update the value of our text text dot set
8:46:57
will display x plus maybe a forward slash
8:47:05
plus our tasks and since we're displaying integers
8:47:10
along with some strings we need to convert these to strings and we can use the str
function to do that
8:47:16
for us so surround x and tasks with str to
8:47:22
convert these to strings and then we'll add just plus tasks
8:47:28
completed so the value of x will update after each iteration of this while loop
8:47:35
let's try it and after clicking download this will display the percentage as well as the
8:47:41
amount of tasks that have been completed all the way until it reaches the end
8:47:49
let's change this into a more practical example we'll pretend that we're downloading
a video game so let's replace or rename
8:47:56
tasks as maybe gb4 gigabytes and this game will be 100 gigabytes
8:48:03
we'll rename x as well so let's refactor rename and change x to maybe download to
8:48:09
represent the amount of gigabytes that are downloaded and let's create a variable
called speed
8:48:17
and let's set this to one alright so while our download
8:48:22
is less than the amount of gigabytes for this game we'll sleep for maybe a portion of
a
8:48:28
second .05 should be good we'll increase the value of our bar by
8:48:35
this amount speed divided by gigabytes and then multiply this by 100
8:48:44
for the download we're going to increment this by whatever our speed is and right
now it's one so
8:48:49
that's the same we can keep percent the same for our text let's change tasks to
gigabytes
8:48:57
and that's all the changes that we have to make and now when we download
something we're
8:49:02
kind of pretending that we're downloading a large file or a game you can see that
once it reaches 100 it says 100
8:49:10
100 out of 100 gigabytes completed now we can change this to a different value let's
say
8:49:15
maybe 50 and the speed will be maybe two
8:49:21
so this will adjust to accommodate whatever the new file size is as well as the speed
8:49:26
and before i forget you can change the orientation to a vertical bar if you so choose
so we have
8:49:33
this vertical download bar now which could be good for something all right so that's
the basics
8:49:39
of progress bars in python if you want to copy of all this code i'll post all of this in the
comments
8:49:44
down below but yeah that's the basics of progress bars in python what's going on
everybody it's
86.canvas 🖍️
8:49:50
your bro here hope you're doing well and in this video i'm going to teach you guys
how we can use the canvas widget to draw some simple shapes so
8:49:56
sit back relax and enjoy the show welcome back everyone well in this video
8:50:03
we're going to be creating a canvas a canvas is a widget that is used to draw simple
graphs plots and images
8:50:09
within a window we're going to be creating some simple shapes in this video not
anything too sophisticated i might get more into the
8:50:16
canvas widget in a separate video and at the end of this lesson we're going to
practice by creating a pokeball
8:50:22
so to begin we'll need a window window equals tk followed by window.main loop to
create a
8:50:28
canvas we can just simply call this canvas canvas equals canvas and we're adding
this canvas
8:50:34
to our window and then we need to follow this with canvas.pack or place but i'm just
going to use pack
8:50:40
because it's simple this is what our canvas looks like it's a rectangular area which we
can draw
8:50:45
graphics on we can also set the width and the height of this canvas so when we
create this canvas
8:50:51
we're going to set the options for a height and a width so let's begin with the height
height equals let's say 500
8:50:58
and the width will be the same 500 and now we have a square canvas that we can
draw
8:51:04
graphics on let's begin by creating some simple shapes maybe a line i think a line is
a shape maybe a
8:51:10
one-dimensional one i don't know i don't geometry so let's create a simple line
8:51:16
so we type the name of the canvas dot create and there's a bunch of different
shapes that we can create
8:51:21
let's create a line now i'm just going to turn this into a comment for a moment we
need to set some
8:51:28
coordinates for this line this top left corner of our canvas is 0 0 and the bottom right
corner of
8:51:35
this canvas is 500 by 500 depends on what you set the dimensions of the canvas to
8:51:41
so when we create a line we need a starting point and an ending point so if we want
this to start in the top
8:51:47
left corner we're going to pass in 0 0 that is the starting position and we
8:51:52
can place the coordinates for the ending position if we want this line to end in the
bottom right corner that would be
8:51:58
where x is 500 and y is 500 and this is what this looks like we just have a simple line
going across
8:52:05
the screen but that's not all we can also change the color of this line and the
thickness let's begin by
8:52:11
changing the color so we can do that with fill fill equals let's pick color blue
8:52:20
and now we have a blue line going across the screen but we can also change the
width of this
8:52:25
as well so we can do that using the width option and let's set this to five
8:52:30
now we get an extra thick line across the screen now let's make a red line
8:52:37
so i'm just going to copy this canvas.createline and let's change some coordinates
for this uh let's say
8:52:45
we want this to start in the bottom left corner and go up to the top right
8:52:50
so the starting x position would be zero y would actually be 500 x
8:52:56
for the ending position would be 500 and y would be zero let's set this to red and
we'll keep the
8:53:03
width the same okay so here's our red line and notice
8:53:08
that this red line is overlapping the blue line so when you create shapes on a canvas
if there's any that overlap
8:53:16
the one that you most recently created will appear on the top one option available to
you is that you can
8:53:22
actually give these graphics a unique name with our blue line we could call this blue
line blue line equals all of this
8:53:30
and then we can call our red line something different like red line this would be useful
if you
8:53:35
want to call or use a graphic by its name if you want to like move a graphic on the
screen or have
8:53:41
something appear you can use this by its name but we don't really need it for this
lesson so i'll just revert this back to what we
8:53:48
had previously i'm just letting you know that you can do that alright what do we have
up next let's create a rectangle so canvas dot
8:53:55
create rectangle we can set some starting coordinates and ending coordinates
8:54:01
the starting coordinates are for the top left of the rectangle and the ending
coordinates are for the bottom right so for the starting
8:54:07
coordinates let's say we're going to place this where x is 50 y is 50. for the ending
coordinates we want this
8:54:14
where x is 250 and y is 250 i'm just making up numbers
8:54:19
put it whatever you want really and this is what this looks like
8:54:24
but i think i'm going to comment out these lines for now
8:54:29
and here is our rectangle well i guess it's also a square we can actually give this a fill
color
8:54:35
as well so we do that with the fill option fill equals purple
8:54:41
i guess and now we have a purple rectangle next on our list
8:54:47
is create polygon we can create many sorts of shapes using create polygon but it
really depends on how many
8:54:53
coordinates that we enter let's create a simple triangle so we need at least three
coordinates for this
8:54:59
to work so canvas dot create polygon and i have some coordinates picked out
8:55:04
already so let's place the first set of coordinates where x is 250 y is zero for the
second set
8:55:12
where x is 500 and y is 500 in the last set the third set we'll
8:55:17
place this where x is zero and y is 500 and what this looks like
8:55:22
is that we have this black triangle that's taking up most of the canvas we can also
change the fill color as
8:55:29
well so we do that with the option fill and we can set this to whatever color you want
8:55:35
let's say yellow and we now have a yellow triangle on our screen and it
8:55:40
kind of resembles the triforce from the legend of zelda series one of my personal
favorites let's also add a border this is actually
8:55:48
done with outline outline equals and then pick a color for the outline let's say black
and we have a black
8:55:54
outline around this graphic and you can also change the thickness of this too with
width and let's set the width to
8:56:01
five and now we have an extra thick line around our triangle
8:56:06
with these shapes you can pass in a list of points too so i'm going to get rid of all
these and
8:56:12
we're going to pass in a list so let's create a list called points points equals all the
points that we
8:56:20
just entered in previously and then instead of passing in all these coordinates
individually i'm just going
8:56:25
to pass in our list of points and this will work exactly the same okay
8:56:31
let's create an arc now so canvas dot create arc and arc is really just a
8:56:37
curved line between at two points however when we list some coordinates we're not
listing the coordinates of the
8:56:44
starting position and ending position for this arc it's more or less for the amount of
space that we want to allocate
8:56:50
to actually draw this arc so let's say that we want this arc to take up the entire canvas
we can pass in
8:56:56
the dimensions for this canvas so the top left corner is zero zero and the height and
the width are five
8:57:02
hundred so this is our arc it resembles a pie
8:57:09
slice it's not starting in the top left corner and ending down here that's the amount of
space that we're
8:57:15
giving this crate arc function to actually draw an arc it's really an entire circle but
8:57:21
only a portion of it is visible let's also set a fill color for this so fill equals
8:57:26
what color did we not pick yet let's say green
8:57:32
and now we have a green pie slice there's different styles you can do too
8:57:38
so we can change that with style equals the default is pi slice that's what we had
previously let's
8:57:44
change this to chord this will draw a line between these two points and this kind of
resembles like a bow
8:57:51
like a ball with arrows and there's also just arc where it's simply just a curved line
8:57:59
let's change this back to pie slice well because i like pie we can change the starting
position of
8:58:06
where this arc begins with the option start so start equals 0 by default so there's
8:58:12
going to be no apparent change let's adjust this by 90 degrees so this
8:58:17
is in degrees so this shifted what is this direction counterclockwise
8:58:24
by 90 degrees so 180 would theoretically flip this
8:58:31
and now this is on the other side of what we had previously and then 270.
8:58:40
all right we can also set an extent so the default is normally 90 degrees
8:58:46
but let's change this to 180 and now we have a what is that
8:58:51
hemisphere half a circle all right people let's practice by creating a pokeball there's
still one
8:58:57
more function that we have to cover it's called create oval but we'll do that as we're
making this so let's begin by creating the top red
8:59:04
hemisphere of this pokeball so that's canvas dot create arc and we're going to set
8:59:11
the extent of this to 0 0 500 500
8:59:16
so that this is going to take up the entire canvas and then let's also set a fill color to
8:59:22
red so fill equals red we need to change the extent of how far
8:59:28
this extends so we'll do that with the extent function so extent equals 180 so that this
is a half circle
8:59:36
and let's also change the thickness of this outline as well using the width option so
width equals
8:59:44
10 would be good and we have a red hemisphere for the top of our
8:59:49
pokeball let's work on the bottom half so that is a white hemisphere
8:59:54
so canvas dot create arc these are all the same we'll change the fill to white the
extent will be the
9:00:01
same but we need a different starting position so start equals 180. so this is going to
begin
9:00:08
at the 180 degree mark so when you combine these both together we get two
different colored hemispheres that
9:00:14
form a perfect circle now let's create an oval within the center of this pokeball
9:00:21
so we're going to do that with canvas dot create oval we're going to set the amount
of
9:00:29
space that we want to give the circle to be drawn so we'll place this where x is 190
where y is 190
9:00:37
where x 2 or the second x position is 310 and y2 is 310
9:00:46
let's set the fill color next so we do that with fill fill equals white
9:00:53
and lastly let's just give this a thicker border so that is width
9:01:00
equals 10 to match the width for the two arcs that we have and there you have
9:01:06
it we have a simple pokeball all right well that's the basics of the canvas widget if
you'd
9:01:11
like a copy of all this code i'll post all of this in the comments down below but yeah
that's the basics of the canvas
9:01:17
widget in python what's going on everybody it's your bro here hope you're doing well
and in this
87.keyboard events ⌨️
9:01:22
video i'm going to teach you guys how we can create key events in python so sit
back relax and enjoy the show
9:01:30
welcome back everyone in this video we're going to be discussing at key events we
can bind a key event and a function
9:01:38
to a widget or a window so that when we press a certain key or do something we can
trigger a
9:01:45
function to be called that will perform some sort of task for us so we can actually do
this by using the
9:01:51
bind function so widgets and windows have access to a bind function
9:01:57
so when our window or our widget is selected and then you press a certain key or do
9:02:02
something then it's going to call a certain function to perform a task for us so there's
two arguments within the bind
9:02:08
function this takes an event as well as a function name let's say
9:02:13
that when we press the enter key we want to call this function to do something so
9:02:18
for this event what we type for that is a set of quotes angle brackets and the name of
the
9:02:25
button so for example if you want to press w to move forward like you're playing a
game this would be w
9:02:32
a s d there's also up down and then let's say we want to press
9:02:38
enter to do something that is actually a return and then we will call this function so
let's create a function
9:02:44
called do something and let's define this at the top def
9:02:50
do something now pay attention to this part we need to set up one parameter for this
do something
9:02:56
function this needs an event so be sure to include that and it's easy to forget
9:03:02
so what do we want to do after we press enter let's just print a message you
9:03:09
did a thing and this should work now so let's try it
9:03:17
so while this window has focus ability we can actually uh have this respond to
9:03:22
events so right now i'm pressing all sorts of keys you can probably hear me right now
going crazy on my keyboard but now i'm
9:03:28
going to hit enter and it says you did a thing and you can bind all sorts of keys to
9:03:33
this window let's say that i want to press q to quit so we'll just replace this with
9:03:39
lowercase q so i'm typing all sorts of keys but now i'm going to press q
9:03:45
and it says you did a thing kind of like we're quitting the game you can actually have
this respond to
9:03:51
all keys well almost all of them so to do that you just type in key with the capital k
9:03:58
so i can pretty much press on any key then it's going to trigger or call our function
9:04:05
kind of like that so one thing that we could do we can display the key event that was
9:04:11
pressed so i'm just going to add that here maybe i'll change this message u
9:04:18
pressed plus and to display what key was pressed that is event
9:04:24
dot key sim i believe that's for key symbol so this will actually display
9:04:31
what button you pressed so w-a-s-d
9:04:36
here's a unique idea for practice let's say that we want to add a label to our window
and we're going to
9:04:42
change the text on the label to reflect whatever key that we press so let's create a
label
9:04:48
label equals label we're adding this to our window and let's change the font as well to
9:04:54
something massive like helvetica and a size of 100 that should be good
9:05:02
then we need to pack this label so label dot pack then within
9:05:07
this function let's turn this into a comment and we're going to use label dot config
9:05:14
and we're going to update our text and the text is going to be whatever key
9:05:19
that we press so we can do that with event dot key sim so now
9:05:25
this label is going to display whatever key that we press so for example w a s
9:05:32
d backspace escape up up down down left right left right b
9:05:38
a start all right so that's the basics of key events if you could do me a favor and in the
comments down below
9:05:44
just press f to pay respects so if you want a copy of this code i'll post this in the
comments down below but yeah
9:05:50
that's the basics of key events in python hey what's going on everybody it's your bro
here hope you're doing well and in
88.mouse events 🖱️
9:05:57
this video i'm going to teach you guys all about mouse events in python so sit back
relax and enjoy the show
9:06:05
hey everyone we're going to be discussing mouse events in python so before we
begin we'll need a window so
9:06:10
window equals tk followed by window.main loop and in the last video we learned that
we
9:06:16
can bind a widget or a window so that when a certain event occurs we can call a
9:06:21
function so we're going to do the same thing for this video but we have various
mouse events that we can cover
9:06:28
so we're going to use the window.bind function and this takes two arguments an
event as
9:06:34
well as a function name so let's define a function let's call it the do something
function so do something
9:06:42
and we'll need to define this so at the top def do something and remember that we
need
9:06:49
to set up one parameter for this function the parameter is an event and then we'll just
print
9:06:55
something print you did a thing
9:07:01
okay so there are various mouse events for a left button click within quotes and then
9:07:08
within angle brackets a left button click is button dash one
9:07:13
so then if we were to click within this window it's going to call our function you did
9:07:19
a thing but if i were to right click it doesn't do anything so there are other mouse
events too so
9:07:26
button two is that scroll wheel not if you scroll up or down but if you press in on it so
right now i'm clicking left
9:07:34
i'm clicking right now i'm going to press in on the mouse wheel and it says you did a
thing
9:07:40
button three is a right mouse click
9:07:46
so i'm clicking left nothing but i'm gonna click right and that seems to work all right so
9:07:53
button one i'm just going to add a note left mouse click button two
9:08:00
is the scroll wheel that's if you press in on it and then
9:08:07
button three is a right mouse click so did you guys know we can actually get
9:08:13
the x and y coordinates of where this event occurred so to do that we type in the
name of the
9:08:19
event dot x however since we're displaying this along with some text
9:08:24
we'll need to convert this to a string so string event dot x then i'll add the
9:08:31
y coordinates as well event.y i think i'll just add a comma
9:08:38
between these to separate them and we'll change our text
9:08:44
so we'll say mouse coordinates all right let's try this again so this
9:08:51
is going to give us the coordinates of where we click and right now i'm just left
clicking
9:08:58
so the top left corner is zero zero well at least close to it not there
9:09:04
exactly alright so here's a few other events that we can cover
9:09:11
i'm going to copy this we have button release if we were to release a button it's
9:09:18
going to trigger our event so right now i'm holding in on the left mouse button but i'm
going to let go
9:09:25
then it triggers our event if i were to right click and hold it it doesn't do anything but
9:09:31
as soon as i let go then our event is triggered we also have enter
9:09:36
not to be confused with the enter button just going to get rid of that so that
9:09:42
is enter if we were to enter our binded window or widget
9:09:49
so if i were to enter it's going to give the coordinates of where i entered this
9:09:56
there is also leave so i'm going to say that enter
9:10:03
the window and leave is leave the window
9:10:14
so if i enter nothing happens but as soon as i leave it gives the coordinates
9:10:22
and the last one is motion as long as the cursor is in motion
9:10:28
so this one might be good for a game so that is motion
9:10:34
where the mouse
9:10:40
moved it's consistently giving me the coordinates if i'm moving the mouse but if i
were
9:10:45
stationary or stop moving it stops all right so that's the basics of mouse
9:10:52
events in python if you want to copy of all this code i'll post all of this in the
comments down below but yeah that's the basics of
9:10:58
mouse events in python hey what's going on everybody it's bro here hope you're
doing well and in this video i'm going
89.drag & drop
9:11:05
to teach you guys how we can drag and drop widgets in python so sit back relax and
enjoy the show
9:11:12
all right people let's just dive into this so we'll need a window and then we're going to
be adding widgets to the
9:11:18
window so window equals tk followed by window.main loop now you can drag and
drop all sorts of
9:11:25
widgets but let's just stick with the label because creating a label is simple so let's
define a label label equals
9:11:32
label we're going to be adding this label to the window and let's set a background
color so we
9:11:38
can actually see this label pick i don't know your favorite color i'm going to pick red
and then i'm going to set a width and a
9:11:45
height because normally if you have a label without any text it's really small so let's
set a width of maybe 10
9:11:53
and then a height of 5. that should be good and we want to place
9:11:59
this label someplace within the window so label dot place and we can pass in some x
9:12:05
coordinates as well as y coordinates so x equals zero and y equals zero
9:12:11
so that this is going to appear in the top left corner of our window so this is what we
have
9:12:17
this is our label and we want to be able to drag and drop this label someplace within
the window
9:12:23
what we're going to need to do is actually bind this label twice but let's begin with just
9:12:28
the first binding so in order to bind a widget you type in the name of the widget
followed by calling the bind function
9:12:36
so this bind function can take two arguments it can take an event as well as a
9:12:42
function name so for the function name let's call this drag underscore start but you
don't
9:12:48
really need to name it the same that i do and for the event that occurs this is going to
be a left button click
9:12:55
with the mouse so that event is within quotes and within angle brackets
9:13:01
button dash one so if we were to click someplace within the label using the
9:13:06
left mouse button it's going to call our drag start function but we need to define this
9:13:12
so at the top of our program let's define this function def drag start and this takes one
9:13:19
parameter the parameter we have is our event so what we're going to do is actually
get the coordinates of where
9:13:26
we click within this label and we're going to assign these to a new variable or
attribute of our
9:13:33
label so label dot let's create an attribute called start x equals our event
9:13:41
dot x so this is where we click within the label and not necessarily the
9:13:47
window and let's repeat the process for y so label dot start y equals event dot
9:13:54
y so we're going to have some coordinates of where we click within this label and we
can actually
9:14:01
use that for our next function so we're going to bind this label a second time so i'm
going to copy this line and paste
9:14:08
it the second binding let's say is drag underscore motion for the function
9:14:15
name and this event is going to occur if we were to hold down the left mouse button
9:14:21
and then drag so that is b one dash motion
9:14:28
and then we need to define this function so at the top d e f drag underscore motion
and this
9:14:35
takes our event as a parameter so this part is going to be a little funky we're going to
9:14:41
get or create new x and y coordinates so let's begin with x x
9:14:47
equals the name of our label or widget label dot w info
9:14:54
underscore x this is a function so add some parentheses this will get the top left x
coordinate
9:15:01
of our label relative to the window that we're in minus label x well labeled.startx
9:15:11
plus event dot x all right so we're going to do the same
9:15:17
thing for y well we're going to replace x with y for the second line
9:15:25
all right so this is the top left corner the x coordinate of our
9:15:32
widget relative to the window this is the place where we click within
9:15:40
the label itself and this is where we begin dragging our widget to it's a little
9:15:47
difficult to explain but if you can find combine combine all of these together you'll get
the new coordinates
9:15:52
of where you want to drag this component this widget to so we need to replace this
widget so
9:16:00
label dot place x equals x and y equals y so then when you combine
9:16:07
all of these together we have our widget and we can drag it and drop it someplace
else within the
9:16:13
window now wait a second what if we have more than one widget
9:16:18
well with the way that our functions are written now they wouldn't be compatible with
more than one widget
9:16:24
just this label itself so what if we were to create a second label so i'm going to call
the
9:16:30
second label just label two so i'm just going to rename them and let's change the
background color to
9:16:37
maybe blue and i'm just going to place this somewhere else within our window so
maybe where x
9:16:42
is 100 and y is 100 and we'll need to bind label two as well so
9:16:49
make sure to bind label two so we have label one binded it's just called label
9:16:55
and the same thing with label two all right let's see what happens i'm going to
attempt to drag
9:17:01
label 2. the label 1 is moving that's because with our functions we stated we're
9:17:07
getting the coordinates and we're storing them within the attributes of label 1 and not
necessarily label two so in
9:17:14
order to make these functions compatible with all widgets we can actually add one
line of code
9:17:22
widget equals event dot widget so this is going to get the widget of
9:17:28
the event that we're dealing with and we're going to temporarily rename this as
widget
9:17:34
so now we're going to replace all instances of where we use label and replace it with
widget
9:17:42
be sure to do the same thing too with drag motion and then we will need to add this
line
9:17:48
at the top of drag motion so now these functions are now compatible with
9:17:55
any widget that we create and we should be able to drag and drop
9:18:02
both widgets all right well that's the basics of dragging and dropping widgets in
9:18:08
python if you want a copy of all this code i'll post all of this in the comments down
below but yeah that's the basics of dragging
9:18:14
and dropping widgets in python hey what's going on everybody it's your
90.move images w/ keys 🏎️
9:18:20
bro here hope you're doing well and in this video i'm going to teach you guys how we
can move an image on both a window and a canvas in python
9:18:27
so sit back relax and enjoy the show all right my fellow bros i'm going to
9:18:33
divide this video into two different sections in the first section we're going to move a
widget
9:18:39
within a window and in the next section we're going to move an image on a canvas
so let's begin by moving a
9:18:46
widget within a window so we'll need a window window equals tk followed by
window.mainloop and we'll
9:18:52
also need to set some geometry for this window window dot geometry let's make this
9:18:58
500x500 500 x 500 and let's take a look
9:19:06
okay that should be a decent size let's add a label to this window
9:19:11
so i actually have a race car image that i want to use if you would like an image feel
free to pause the video and
9:19:16
download one what i would like to do is create a photo image from this png file that i
have
9:19:22
i'm just going to call this my image equals photo
9:19:28
image file equals since this is within my project folder i can just list the
9:19:34
file name otherwise you might have to list the file path and for me this is called
9:19:40
racecar.png i want to add this image to my label not necessary but
9:19:46
it'll be helpful for this example i'm going to call my label just a label label equals label
i'm adding this to
9:19:52
the window and i'm going to set the image to equal my image that i created it's a
photo
9:19:57
image if you do not have an image that you want to use you can simply just change
the background color you
9:20:03
could say bg i don't know red oh then we need to place this so be sure
9:20:10
to do that label dot place and i'm going to place this in the top left corner that is
where x
9:20:15
equals zero and y equals zero and then we should have a race car or whatever
image you used and a red
9:20:22
background you can get rid of the background color if you prefer that's just if you
don't have an image
9:20:27
to use you can set the background color just for a visual to show you where the label
currently is
9:20:33
let's set some key bindings for this window and let's add them maybe here before we
create the label and our image
9:20:39
so let's find the w key for up s for down a for left and d for right so we do that
9:20:46
with window dot bind and we pass in an event as well as a function name so we
want to
9:20:54
tie the w key to a function and we'll call this move underscore up and then we will
need
9:21:02
to define this so outside of the window let's define
9:21:08
move underscore up and this takes an event so we'll place that as a
9:21:14
parameter and to move our label up we're going to take label dot place
9:21:22
and we're going to place this at some new coordinates x equals the label's current
position
9:21:28
relative to the window that it's in so that is label dot w info
9:21:35
underscore x all right so then for y it's similar
9:21:41
y equals label dot w info underscore y
9:21:48
but we're going to subtract maybe one all right so this is all we need
9:21:56
although this is only going to move up so i'm going to press w and our label our race
car or whatever
9:22:03
image you used is going to move slowly up i'm going to increase this to 10 so the
change is more drastic
9:22:11
so now our race car is drifting off into space let's find a few other keys s a
9:22:18
and d sad all right so s will be down move underscore down
9:22:27
a for left move left and d for right move
9:22:35
right we'll just copy what we have here and make a few changes let's begin with
move down
9:22:44
the only thing we're going to change within here is in place of subtracting 10 from the
y coordinate we're going to add
9:22:50
10 and with our race car oh i guess we have to define these so let's do that real
9:22:56
quick uh so we have left move left we're going to subtract
9:23:03
10 from the x coordinate and keep y the same
9:23:09
and then the same thing for move right move underscore right and this time we are
9:23:15
going to add 10 to x and y will stay the same
9:23:21
okay now with our image we can move up down right and then left
9:23:29
and y stop there we combine these to our arrow keys
9:23:36
so up is just up with a capital u down is down these are all capital by
9:23:42
the way left is left and then right is right
9:23:49
and now i can use my arrow keys to move this image
9:23:58
now for this part of the video i'm going to teach you guys how we can move an image
on a canvas this time and not just
9:24:04
within a window but we'll need a window to begin with window equals tk followed by
window.mainloop
9:24:10
and we're going to create a canvas canvas equals canvas we're adding this canvas
to the window
9:24:17
and let's set a width and height for the width let's make this 500 and the height 500
as well
9:24:22
so this is an even square and we'll need to pack or place this canvas canvas.pack
9:24:28
should take care of that and let's run this and this is our canvas at this step we need
to add an image to
9:24:36
our canvas and i have an image of a racecar that i want to add so i need to create a
photo image from
9:24:42
this file so we can do that let's give this photo image a unique name i'm just going to
9:24:47
call this photo image to keep it simple photo image equals photo image and i can
9:24:53
list the file path or the file name since this image is within my project folder i
9:24:59
only have to list the file name and my file is called racecar.png
9:25:04
this next step we need to give our image a unique name the image that's being
added to the canvas i'll call this
9:25:11
my image equals and we need to turn this photo image
9:25:18
into an image added to the canvas so we type in the name of the canvas canvas
9:25:23
dot create underscore image we first need to list the coordinates of
9:25:28
where we want to place this photo image so let's place this in the top left corner that
is where
9:25:34
x is 0 and y 0 and we need to set the image of the image that we're creating
9:25:40
so we can do that with saying image equals the name of our photo image
9:25:46
and let's see what we have so far so this image is somewhat cut off in the
9:25:51
top left corner we can easily fix that by anchoring our image when we create it so
let's at
9:25:56
the end add anchor equals northwest so that should fix that problem and we
9:26:03
now have our image that we want to use and to move our image we can set some
key bindings
9:26:10
so we type in the name of the window dot bind and we pass in two arguments an
event as
9:26:16
well as a function name let's tie pressing the w key to moving up
9:26:21
so the first argument is going to be w and the second is going to be a function name
so let's say move
9:26:28
underscore up and let's find some other keys w for up s for down
9:26:39
a for left and d for right
9:26:45
we'll need to define these now so at the top outside of the window let's begin by
defining move up
9:26:53
def move up this has an event as a parameter
9:26:59
and let's create the other definitions here
9:27:05
so i'm just going to rename these so we have move up move down move left
9:27:12
and move right okay now this next step
9:27:18
we can use the canvases move function to move an image a certain amount of
pixels on the x-axis
9:27:24
and y-axis so let's begin with move up we type in the name of the canvas
9:27:29
dot and use the move function and this has three arguments the image that we want
9:27:34
to use and this image is called my image the one with the race car
9:27:41
the next argument is the amount of pixels we're moving this on the x-axis with up we
can keep this the same
9:27:50
but on the y-axis let's say we want to move this up 10 pixels so that would be -10
9:27:57
and let's repeat the process for move down move left and move right for move down
we're going to change
9:28:04
y to 10 for move left we'll keep y the same at zero but we're
9:28:11
going to subtract 10 and then move right
9:28:16
we are going to add 10 to our x's and that should be it so we can move up
9:28:24
down right left and if you want to bind these to some
9:28:30
arrow keys all we have to do is just change the event so we can bind these to our
wsad keys
9:28:38
as well as up down left
9:28:46
and right and this time i'm going to use my arrow keys up down right and left
9:28:56
all right so that's a few of the basics of moving either widgets or images on a canvas
or window if you
9:29:02
would like a copy of all this code for both examples i'll post both of them in the
comments down below
9:29:08
but yeah that's the basics of moving widgets and images in python hey what's going
on everybody
91.animations
9:29:14
it's your bro here hope you're doing well and in this video i'm going to teach you guys
how we can create some simple 2d animations in python so sit
9:29:22
back relax and enjoy the show alright my fellow bros in this video
9:29:28
we're going to be animating an image on a canvas we're only going to be moving this
image
9:29:33
up down left and right feel free to download an image that you want to use i'm just
going to use this
9:29:39
ufo emoji that i found so we'll need two imports at the top from tk enter import
9:29:45
everything that's what we usually have we'll also need to import time as well
9:29:50
we'll need a window window equals tk followed by window.main loop we'll need a
canvas i'm going to call
9:29:57
the canvas canvas canvas equals canvas we're adding the canvas to the window i'm
going to create
9:30:03
something that's called a constant we're going to create a constant for the width and
the height
9:30:09
of our canvas so a constant is a variable or value that you don't plan on
9:30:14
changing later at all and a common naming convention for a constant is that the
name of the
9:30:20
constant is all uppercase so i want the width and the height of this canvas to be
constants so for
9:30:27
the width this is going to be all uppercase it's not necessary to make it all uppercase
but that's a common naming convention
9:30:34
for the width i'm going to set this to 500 and the height set to 500 as well
9:30:39
and for the width of this canvas i'm going to set the width to equal my constant which
is 500
9:30:46
and the height to my constant height so the reason that i'm making constants
9:30:52
is that we'll be reusing these a lot later in this program so we might as well just
declare them now and get it
9:30:58
over with so for the canvas we're adding this to the window we're setting the width to
9:31:03
our constant of width and the height to our constant of height and we need to follow
this by packing
9:31:08
this or place canvas dot pack pack to make it simple so we should have a canvas on
our window
9:31:17
now if you have an image that you want to use we're going to animate it i need to
create a photo image from this
9:31:22
file it's a png file it's called ufo but yours is probably going to be named something
different so
9:31:28
don't copy me exactly i need to create a photo image from this i'm going to call this
photo underscore
9:31:35
image but name it whatever you want this equals photo image and i need to list the
file
9:31:42
path or the file name since this file is within my project folder i only have to list the file
name
9:31:48
and for me this is called ufo.png our next step is that we're going to
9:31:54
take our photo image and add this photo image to the canvas and we'll also give this
image a unique
9:32:00
name i'm going to call this my image equals we're going to use the
9:32:06
canvases create image function so canvas dot create image there's a few arguments
9:32:13
that we need to pass in where we want this image to appear we need a set of
coordinates if we want this to appear in the top
9:32:20
left corner that is where x is zero and y is zero for the image
9:32:25
we're going to set this equal to our photo image whatever you named this
9:32:30
and lastly we should anchor this i'm going to anchor this in the northwest corner to
keep it simple
9:32:37
and we should have our image on our canvas in the top left corner and here is my
ufo that's being piloted
9:32:44
by a green alien that's waving to me aliens are usually not that friendly unless they're
abducting you our next
9:32:49
step is to create a while loop so we'll say wow true this will continue forever until we
close out
9:32:55
of the program if you're looking at the code for a game this might say instead while
running running will be a boolean
9:33:03
variable that contains either true or false so if this is true while the game is running
9:33:08
they're going to continue moving all of the characters on the screen and then if
somebody pauses the game or
9:33:13
closes out of the game then they'll flip this running variable to false but to keep things
simple we'll
9:33:19
just say wow true but that's something that you might see what we'll want to do is get
the
9:33:24
coordinates of our image within the canvas and this will return a list of coordinates
9:33:29
so let's create a list called coordinates so coordinates equals canvas
9:33:37
and we can use the chords function to get the coordinates i guess that's short for
coordinates and
9:33:44
we're getting the coordinates of my image so we'll pass this into this function
9:33:49
and then let's print this just to see what this returns we'll print coordinates
9:33:57
okay now to update the window after each cycle of this while loop we can use the
update
9:34:02
function of our window so window.update will also have this thread sleep for let's say
0.01 seconds
9:34:10
so time dot sleep 0.01 one so here's a quick rundown of what we
9:34:17
have going on so far within this while loop we're going to first get the coordinates of
where our image
9:34:23
is located we'll print the coordinates to the console window this part isn't required but
it's going
9:34:29
to help us for learning purposes we'll update this window for any changes and then
our thread
9:34:36
which is in charge of running the program is going to sleep for i believe that's a
hundredth of a
9:34:41
second so when we run this our image is staying in place
9:34:47
and we know that our while loop is running because it's continuously printing the
coordinates the first number is the x position
9:34:55
the second number is the y position and now we're going to give our image a speed
9:35:00
or velocity i like to call it velocity and in fact i'm going to give this image two velocities
9:35:06
one for how fast this image is moving on the x-axis and another for how fast it's
moving on
9:35:11
the y axis so i'm going to create two variables one called x velocity
9:35:17
and i'm going to set this equal to one and y velocity
9:35:22
and i'll set this to one as well okay so what we need to do now is update
9:35:29
the position of our image so let's do that before we update our window so i'm going
to type in the
9:35:35
name of the canvas and use the move function this move function takes three
arguments
9:35:41
what you want to move i want to move my image my ufo and how far i want to move
this image on
9:35:49
each axis each coordinate so what i want to do is update the
9:35:54
x velocity the x position of my image so i'm going to pass in my x velocity
9:36:02
variable and then y we'll just keep the same for now so we'll just say zero so after
each iteration of this
9:36:08
while loop my ufo is going to move one pixel to the right and it's just going to go off
into the
9:36:13
void forever and now if we replaced x velocity with zero
9:36:19
and for y we set this to our y velocity this image is now going to travel down
9:36:26
and it will continue into the void forever and then if we combine both of these
together
9:36:33
we're going to move diagonally down and to the right so what if we want this image
to bounce
9:36:40
off the walls we're going to add some if statements we're going to begin by just
having this image bounce off the
9:36:45
left and right walls for now let's set y velocity to zero so that this image is only
moving
9:36:51
left and right so before we move this image we'll add an if statement we're
9:36:56
going to check to see if the x position of our image is either greater than the width of
the
9:37:03
canvas or if it's less than zero so we take our coordinates and we're going to get
9:37:09
the first element from this list so that is zero that is x the x value
9:37:15
if coordinates at element zero is greater than or equal to the width of this window and
this is a
9:37:22
constant so we can just place that here if coordinates
9:37:27
well x within our coordinates is greater than or equal to width or coordinates at index
9:37:36
0 is less than 0 then we want to reverse the direction or flip the velocity
9:37:43
so we want to change our x velocity and make this a negative number one easy way
to do that is that we can
9:37:49
take x velocity equals x velocity times negative one
9:37:55
or another way of writing this is just to say x velocity minus negative velocity now
what ends up
9:38:02
happening is that when this image reaches the right border it's going to bounce back
9:38:08
however it only bounces back after it reaches the top left corner of the image we
need
9:38:13
to factor in the width of the image as well so we're going to create two new variables
and let's do this
9:38:19
right before the while loop we'll want to determine what the height and the width are
for our photo image
9:38:25
that we have so this is what we're going to write let's call these two values image
underscore width and
9:38:33
image underscore height so we need to determine what the width
9:38:39
and the height is for our photo image and there is a function to do that type in the
name of the photo image dot
9:38:47
and there is a width function and the same thing for height
9:38:53
photo image dot height and then we can reuse these so i'm just
9:38:59
going to add one line to the sift statement if the x position of our image is greater
9:39:06
uh greater than or equal to the width of our window minus the width of our image and
i'm
9:39:13
just going to put these within parentheses just to keep these more organized and
what ends up happening now is that
9:39:20
when the right portion of our image hits the right wall it's going to bounce off
9:39:27
and let's do the same thing for our y velocity we're going to replace coordinates at
9:39:35
index 0 with 1 and change that here as well replace with with height
9:39:43
and this is image underscore height and we're going to change the y velocity
9:39:49
this time
9:39:54
and when we move this image let's also add the y velocity
9:40:00
and now our image is just going to bounce off the walls forever but it's kind of
predictable since we
9:40:06
set x and y to equal one now let's change the speeds to kind of spice things up
9:40:12
let's say for the x velocity this is three and y velocity this is now two this is going to
go in a completely
9:40:18
different direction so you can mess with the speeds however you want so
9:40:24
guys how about we add a background image to this well because we can i'm going
to reuse this image of space
9:40:30
from my java video on this same topic i need to create a photo image from this
9:40:35
and i'm going to copy what we did for our normal image that we were moving and
animating
9:40:41
so this is called space.png well that's the name of my file and i'm going to give this
image a
9:40:47
different name maybe i'll call this background underscore photo
9:40:53
and then i'm going to add this photo to the canvas and i'm going to call this a
different
9:40:59
name i'll just call this background all right
9:41:04
so our window should have a background now and then our image moves on top of
the
9:41:10
background so make sure you add and create the background before you add any
other images
9:41:15
otherwise the background might overlap your image alright so that's the basics of
some
9:41:21
simple animations in python if you want to copy all this code i'll post all of this in the
comments down below
9:41:26
but yeah that's the basics of some animations in python hey what's going on
everybody it's your
92.multiple animations 🎞️
9:41:33
bro here hope you're doing well and in this video i'm going to teach you guys how we
can animate multiple objects in
9:41:38
python so sit back relax and enjoy the show
9:41:43
welcome back my fellow bros in this video we're going to be animating multiple
objects within a window
9:41:49
more specifically we'll animate some circles and each of these circles will have their
own characteristics
9:41:54
speeds and directions so we'll need two imports to begin from ck enter import
everything we'll
9:42:01
also be importing everything from the time module as well we'll need a window
window equals tk
9:42:06
followed by window.main loop i'm going to create two constants one for the width
and one for the height
9:42:12
let's set the width equal to 500 and the height equal to 500 as well we'll create a
canvas and
9:42:17
we'll be animating the circles that we're going to create on the canvas so let's create
a canvas
9:42:23
canvas equals canvas we're going to be adding the canvas to the window and we'll
set the width of
9:42:30
our canvas equal to our constant width which is 500 and our height equal to our
9:42:36
constant of height which is also 500 and we will add this canvas by using the pack
function
9:42:42
canvas.pack here's our canvas and we're going to be creating multiple circles
9:42:47
let's pretend that they're all different sports balls like a tennis ball baseball volleyball
bowling ball
9:42:53
they'll all be bouncing off of the borders and once each of them encounters a border
they will change direction
9:42:59
since we're going to be creating multiple objects i think it's best if we do this with
another class
9:43:05
so let's create another class i'm going to go to file new python file and let's call this
9:43:12
class ball and click python file and we need to define
9:43:17
this class class ball and we also need a constructor for this
9:43:24
ball class we can create one with def two underscores i-n-i-t two underscores again
9:43:32
and we can set up the parameters of what we want to receive if we were to create a
ball object so let's pretend that
9:43:40
we're going to be creating a volleyball to begin with so let's call this ball volley
9:43:47
underscore ball equals ball and we'll pass in some arguments so
9:43:53
let's set up the parameters first what do we want to receive one we want to receive
our canvas so that we can actually draw
9:44:00
this oval we can draw this ball we should also have an x and y position
9:44:07
a diameter x velocity y velocity
9:44:15
and a color all right so when we create a ball like a volleyball
9:44:21
we're going to pass in whatever the parameters require so we need a canvas
9:44:26
x y a diameter an x velocity y velocity and a color so within the
9:44:32
constructor for the ball we're passing in our canvas it's kind of like what we do with
widgets
9:44:38
we're specifying what we're going to be adding this widget to we're going to be
adding this ball to
9:44:44
our canvas we also need x y and a diameter let's make this simple by setting x and y
to both zero
9:44:51
for the diameter let's set this to maybe 100 and we also need an x velocity and y
9:44:58
velocity let's set this both to one and a color so let's pass in a string
9:45:04
like white let's head back to our ball class and finish assigning all of these arguments
that we're going
9:45:10
to receive the first step is that we'll say self dot canvas equals
9:45:16
the canvas that we receive when it's passed to us as an argument
9:45:21
our next step is to draw this oval draw this ball and we'll say self dot image
9:45:29
equals canvas dot create underscore oval and with creating ovals
9:45:36
we can pass in x y and with circles the diameter is equal
9:45:42
for both the width and the height so we'll pass in our diameter twice
9:45:48
and lastly a fill color and we'll say this is equal to the color
9:45:54
that we receive which is white we're going to set the x velocity and
9:46:00
the y velocity this is related to the direction that it's initially going to head in
9:46:06
so self dot x velocity equals the x velocity that we receive
9:46:15
and we'll do the same thing for y velocity self.y velocity equals y velocity
9:46:23
now since this is in another class we need to import this class
9:46:29
from ball import everything and you can see that this red
9:46:34
underline went away so let's see what happens now we have our volleyball but it's
not
9:46:42
currently moving so that's the next step so let's create a function within the ball class
called move that
9:46:48
we can call upon so we'll do this after the constructor cef
9:46:53
move and this will take self as an argument
9:46:58
the first thing that we should do is get the coordinates so we'll say coordinates and
this is a
9:47:04
list coordinates equals self dot canvas dot
9:47:09
c o o rds and we'll pass in self dot image
9:47:18
and we will print these coordinates just to see what's here
9:47:26
all right now we'll create a while loop within our main class so let's add that near the
end and we'll
9:47:33
just say while true so while this is running
9:47:39
let's move our volleyball so volleyball dot move so this will call the move
9:47:45
function of our volleyball and then we will update the window
9:47:51
window dot update so that this refreshes and we will sleep for maybe a hundredth
9:47:57
of a second time dot sleep 0.01 but you can put
9:48:02
whatever you want here so this is going to at first just print the coordinates of this ball
9:48:09
so it's not currently moving and this time with our list we have four coordinates zero
zero that's the
9:48:15
top left corner and 100 100 that's the bottom right corner so this time we have four
9:48:21
coordinates that we have to deal with the top left corner as well as the bottom right
corner our next step is that we should move
9:48:28
this image self dot canvas dot move
9:48:34
what object are we going to move we're going to move self dot image and in what
directions
9:48:41
self.x velocity as well as self dot y velocity now
9:48:48
our ball is going to move across the window and it right now it's displaying all of the
coordinates we don't have any bounds set up so it's
9:48:55
just going to continue into the void forever so we should set up some if statements to
check to see if one of the
9:49:01
borders touches one of the walls of the window if so it's going to change direction so
let's check to see
9:49:09
if the left or right side of our ball touches one of the left or right borders so let's write
an if statement if
9:49:17
and then let's take a look at these coordinates if coordinates at index 2
9:49:24
that is this one 0 1 2. so this is the bottom right corner
9:49:31
with these two sets of coordinates if coordinates at index 2 is greater than or equal to
9:49:38
i'm going to put these within parentheses self dot canvas and we need to get the
width
9:49:44
of the canvas one way which we can do that is to use the w info underscore with
function
9:49:53
so this is going to get the width of the canvas and then let's add one more thing
9:50:00
or coordinates at index zero is less than zero
9:50:07
so if this ball touches the left or right border we want to change direction so we can
9:50:14
flip the x velocity self.x velocity equals negative self x
9:50:22
velocity and let's do the same thing for the top and bottom borders so let's copy what
we have
9:50:31
and we'll replace coordinates at index two with three and we'll also replace
9:50:38
w info underscore width with height and coordinates at index one
9:50:46
change self dot x velocity with y velocity same thing here as well alright and
9:50:54
that should be everything let's try it so let's see if this ball actually
9:50:59
bounces off the borders which it does pretty cool so that's everything we need to do
within our ball
9:51:06
class now if we need to create multiple balls we can just construct them so let's say
this time we would like a
9:51:12
tennis ball i'm going to name this volleyball as tennis ball and
9:51:18
let's make the diameter smaller like five but we'll make this a lot faster like
9:51:23
for the velocity let's say four and three and the color let's make this yellow and we
also need
9:51:30
to call the move function of this tennis ball so within the while loop tennis ball dot
move and then we should
9:51:38
also have a tennis ball moving across the screen in a different direction now
technically we don't need to print the coordinates
9:51:44
but i tend to do that just for teaching purposes to better demonstrate all of this
9:51:49
now our ball class is acting as a blueprint for how balls should be created and the
9:51:55
behavior that they exhibit and if we need to create a ball we can just come up with a
name for the ball
9:52:01
and call the ball constructor to take care of that for us kind of like what we've been
doing with widgets so all we need to do to create
9:52:09
another ball is come up with a unique name for the ball and instantiate it using our
ball class that we created so this
9:52:15
time let's create a basketball i'll rename this as basketball
9:52:20
and we'll pass in different arguments based on how we want to customize this
unique ball so for the diameter
9:52:28
let's say this is now 125 and the x and y velocity let's set this to something very fast
like
9:52:35
eight and seven and with the color let's change this to orange and make sure you
also call the move
9:52:42
function so basketball dot move and we should
9:52:47
have a basketball moving across the screen all right everybody so that's the basics
9:52:53
of animating multiple objects in python if you want a copy of all this code i'll post all of
this in the comments down below
9:52:59
but yeah that's how to animate multiple objects in python
93.clock program
9:53:04
hey what's going on everybody it's ibro here hope you're doing well and in this video
i'm going to teach you guys how we can create a clock program in python so
9:53:12
sit back relax and enjoy the show all right my fellow bros in this video
9:53:18
we're going to be creating a clock gui program so we'll need a few imports from tk
enter import everything
9:53:27
and from time import everything as well we'll need a window window equals tk
9:53:33
followed by window.main loop and in this program we're going to be creating three
separate labels
9:53:39
one for the time another for the day of the week and another for the date so let's
begin with just displaying the
9:53:45
time and work on the day of the week and the date later so let's create a label for the
time
9:53:51
called time label equals label we're adding this to the window
9:53:56
and let's set the font because we can i'm going to choose maybe ariel and then a
size of 50 so
9:54:03
that this is somewhat large i'm going to change the foreground color that is the font
color
9:54:09
and i will pick a hex value of green so that is 0 0 f 0 0 but feel free to pick whatever
9:54:16
color you want and i'm also going to set the background color and you can pick a hex
value or a
9:54:21
color name i'm just going to say black and then i'm going to pack this label
9:54:27
time label dot pack now we're going to call a function and
9:54:35
we'll call this update and we'll define what this does after every second has passed
9:54:40
we will use a recursive function to keep on updating the time every one second or so
so let's
9:54:47
define what this function does def update
9:54:52
there is a function within the time module called strf time which we can use to return
the current
9:54:59
time and format it however we want so we're going to actually place this within a
string variable and we'll call
9:55:05
this time string and this function is strf time i'm actually going to go to
9:55:12
python's website just to give you an example of how this works here we are on
python's website and this
9:55:18
is the strf time function and this converts a tuple representing a time as returned by
9:55:26
gm time or local time to a string as specified by the format arguments
9:55:32
so when we call this function we can pass in directives as arguments based on what
we
9:55:39
want to display if we want to display the current time there is a few directives that we
can
9:55:44
pass in so let's say that we want a 12 hour clock as represented by a decimal
number
9:55:50
between 1 and 12 well we would pass in percent i as an argument if we want the
minutes
9:55:57
that would be right here percent m and if we want the seconds that is percent s so
we're going to pass in
9:56:05
these directives as if they are arguments so within this function we'll pass in those
format
9:56:13
specifiers or directives so i want percent i followed by
9:56:20
percent m and percent s alright uh but these also have to be
9:56:27
within quotes so don't forget that because i just did okay this will display the current
time
9:56:35
oh then we also have to update our time label so time label dot config
9:56:44
text equals our time string because remember that this returns a string of text
9:56:52
text equals time string so this will display the current time and right now it is six in the
morning
9:56:59
or so however we just get one long jumbled number so it would be a good idea if we
9:57:05
separated some of these numbers so within our strf time function i'm just going to
add some colons to
9:57:11
separate these values and this will display the current time
9:57:17
it is currently 6 46 in the morning let's add am or pm to the end of this
9:57:23
so the directive for that is percent lowercase p
9:57:29
6 46 am now how do we update this label every one second to reflect the current
9:57:35
time well that's a good question and i'm glad you asked it so we're going to use the
time labels after function
9:57:42
we pass in a delay and then a function that we want to call after that delay so let's
say we want to
9:57:49
call this function update every 1000 milliseconds or one second so we'll pass in the
delay
9:57:56
as an argument 1000 milliseconds and the function that we want to call and we're
going to perform what is known
9:58:02
as a recursive function we're going to call a function within itself so after 1000
seconds
9:58:10
we're going to call update again and it's going to repeat the process it's going to
return the time
9:58:15
and format that time and update our label with the new time so what ends
9:58:21
up happening now is that our clock is going to update every 1000 milliseconds
9:58:28
now let's add a few other labels let's say the day of the week i'm going to copy what
we have here and
9:58:34
let's rename this as day label
9:58:40
but i probably don't want this to be the same size for the font let's say ink free
because i like that font and a
9:58:48
size of maybe 25 and i'll get rid of the coloring
9:58:53
it's the same process as before really so let's replace
9:59:00
time string with day string
9:59:05
the directive for strf time to display the day of the week
9:59:11
is percent a and we're going to replace time label
9:59:17
with day label text equals day string
9:59:23
and this will display the day of the week and it is currently wednesday and if we want
to display
9:59:31
the current date we can create a label and we'll call this date label
9:59:40
and i'll make the font slightly bigger maybe 30. we'll copy what we have for our day
9:59:46
string and rename this as date string
9:59:54
so we have a few options for the date i want to display the name of the month the
day and then
10:00:00
the year so that would be percent capital b
10:00:06
percent lowercase d i'm going to add a comma to separate the day and the year
10:00:12
and percent capital y and then i want to set the text of my
10:00:17
date label to my date string so that this will now display the current date there's just
10:00:24
one quick change that i think we should make right now we have three different
labels a time label a day label
10:00:30
and a date label and right now we're using the after function of the time label
10:00:36
so since we have all of these different labels i think we should replace time label with
our window because our window has
10:00:42
an after function as well so that after 1000 milliseconds has passed we can update
the time the day of the
10:00:50
week and the date all right so that's the basics of creating a simple clock
10:00:55
program in python if you want a copy of all this code i'll post all of this in the
comments down below but yeah that's the basics of
10:01:01
creating a clock in python hey what's going on everybody it's you bro hope you're
doing well
94.send an email
10:01:07
and in this video i'm going to show you all how we can send an email using python
so sit back relax and enjoy the show
10:01:16
oh a quick note for you all you're going to need a gmail account to sign into as well
as your username and password so
10:01:22
once you have those set you're ready to begin all right let's begin by importing the
simple mail
10:01:28
transfer protocol library we'll need a couple variables to hold strings let's say we
have a sender a receiver
10:01:37
a password a subject for your email as well as
10:01:44
a body for the email okay sender is going to be you sending the email so enter your
email in
10:01:49
here i'm just going to make up one you at gmail.com actually sender would be better
i would
10:01:56
say let me change that sender at gmail.com let's say we have a receiver this is who
10:02:01
you're going to send the email to receiver gmail.com
10:02:07
don't actually enter in these emails use your owner the person that you intend to
send the email to for your test
10:02:12
because these will probably not work i'm assuming then enter the password for your
gmail
10:02:17
account so let's say password123 and a subject
10:02:23
uh let's say python email test and a body what do you want to say
10:02:30
within your email i wrote an email
10:02:36
okay so that should be good so remember that you're going to want to change these
to your actual
10:02:41
gmail account and you'll want to send it to a valid email address too and make sure
that you use your password
10:02:47
for your gmail account okay now we're going to create a header for our email and the
syntax on this is
10:02:53
a little strange we're going to say message f and then triple quotes so what this
10:02:59
triple quotes is is that a triple quote string can span multiple lines of text so the first
10:03:06
thing we'll enter is from within our header from and then since we're using an f-string
we can
10:03:12
insert a variable at a given location when we insert a pair of these curly braces so
from
10:03:20
will be sender the next line to colon
10:03:27
receiver then the next line subject colon
10:03:34
subject this will be the title of the email then i'm going to add a new line and then
insert the body
10:03:43
and if you want you can give yourself a name when you send this email let's say
maybe snoop dogg and if
10:03:50
there's somebody you want to send the email to and give them a name you can say
like nicholas
10:03:56
cage but these aren't necessary okay so we have our header completed let
10:04:02
me just add a note that this is our header okay we're going to create a server
10:04:07
object server equals smtplib.smtp
10:04:17
there are two arguments the first is smtp
10:04:25
the other argument is the port number and that is going to be 587 that is the default
mail submission
10:04:32
port 587 now we're going to take our server object and start tls transport layer
security
10:04:42
okay now we need to log in so server dot login we will pass in
10:04:50
our sender email that's us as well as our password
10:04:56
password and let's print
10:05:02
logged in we'll eventually place all this within a
10:05:08
try and accept block later all right and then to send an email type
10:05:13
server dot send mail and we pass in our self sender
10:05:20
receiver and our message sender comma receiver
10:05:27
comma message and let's print a confirmation print email has
10:05:36
been sent now if you were to run this program you may encounter this error smtp
10:05:43
authentication error that either means your username password combination was not
correct or
10:05:49
you need to turn on the less secure app access on your gmail account which you
can find here however i would
10:05:56
recommend turning that back off when you're done with this program just because
your account will be less secure and i'm
10:06:02
not liable if you get hacked hey you know what let's place this code within a try block
we will try
10:06:07
all of this code and then if we encounter this exception we can catch and handle
10:06:13
it properly so we will try these four lines of code and we will handle
10:06:18
our exception of smt lib dot smtp authentication error
10:06:25
that means we could not sign in so let's print unable to sign in
10:06:32
so i'm going to run this one more time and instead of getting that ugly error we
should get a simple message that says
10:06:38
unable to sign in once you have the proper username password combination for your
gmail account
10:06:43
and if you need to set less secure app access to on but make sure that you turn it
back off
10:06:49
when you're done with this you should be able to send an email let's try it logged in
email has been
10:06:54
sent here is a snapshot of the email that i received i blurred out my actual email
because i
10:07:00
don't want it public because some of you guys are weird so we have our sender our
receiver and i gave myself a
10:07:07
nickname of snoop dogg that's why it says snoop to nicholas the receiver but you
don't
10:07:12
actually need a name for these as long as you have the sender and receiver fields
filled in you should be good
10:07:18
we have our subject python email test as well as our body i wrote an email
10:07:24
well everybody that's how to write and send a simple email using python if you would
like a copy of this code i will
10:07:30
post this to the comment section down below but yeah that's how to send a simple
email using python
95.run with command prompt 👨
10:07:37
hey what's going on everybody it's you bro hope you're doing well and in this video
i'm going to show you all how we can run a python file using
10:07:44
command prompt so sit back relax and enjoy the show
10:07:50
hey y'all in today's video i'm going to show you all how we can run a python file with
command prompt
10:07:55
so first we'll need a script to work with let's just make a very simple program let's say
print hello world and then
10:08:03
maybe we will ask for some user input name equals input
10:08:08
what's your name and then at the end we will print the word hello
10:08:16
plus the user's name okay so let's save this to a place that will
10:08:22
remember perhaps my desktop now to save this file to a separate location go to
10:08:27
file save as you can pick a name for this python file then to directory click these three
dots
10:08:34
and pick a location you want to save this to to make this easy i'm going to save this
python file to my desktop
10:08:40
and then click ok so the name of this python file of mine is hello underscore world dot
py then
10:08:48
click ok now our second step is to open command prompt to open command prompt
just
10:08:53
search for cmd if you're using mac i believe it's terminal step number three is that we
need to
10:09:00
navigate to the directory that contains our python file i saved this python file to my
desktop
10:09:06
so if you right click on your file and go to location you can just copy this address
10:09:11
now within command prompt type cd to change directory space and then paste that
directory
10:09:18
that contains your python file hit enter and for the last step we need to invoke
10:09:23
the python interpreter by typing python space and then
10:09:29
the name of the script and the name of my script is hello underscore world dot p y hit
enter to
10:09:36
run the script hello world what is your name bro hello bro
10:09:41
so that is how to run a python file with command prompt i will post all of these notes
to the comments section down below
10:09:47
and well yeah that's how to run a python file with command prompt it's you bro hope
you're all doing well
96.pip 🏗️
10:09:54
and in this video i'm going to show you all how we can use pip for python to
download packages and modules so sit
10:10:00
back relax and enjoy the show alright people pip for python pip is a
10:10:08
package manager for packages and modules from the python package index if
you're
10:10:13
using python version 3.4 or above pip should already be installed
10:10:18
if you're using a python version below 3.4 one option is that you can always update
10:10:24
your version of python just click download open when done
10:10:30
go to customize installation make sure that pip is checked go to next and add
10:10:36
python to environment variables then install now to use pip open up command
prompt
10:10:42
and type pip for a help menu all sorts of different general options and commands will
appear
10:10:49
if you need to check your version of pip type this command and here's a cheat sheet
that i wrote
10:10:55
so to check the current version of pip type pip dash dash version enter
10:11:02
and currently the version that i'm using is 21 if you need to upgrade pip use this
10:11:08
command pip install dash dash up
10:11:14
grade pip mine should be the latest version yeah requirement already satisfied and
10:11:21
if you need a list of installed packages type pip list
10:11:28
so here's all of the packages that i currently have installed some of these you might
recognize like
10:11:34
numpy pie game pie installer those are just a few to check to see if any of your
10:11:40
packages are outdated type pip list
10:11:45
dash dash outdated and this might take a second here are
10:11:51
four packages that are outdated on my computer babel pi game pi tz and setup tools
10:11:58
if you need to upgrade one of your packages type pip install the name of the
package let's
10:12:04
say that i would like to update pygame then dash dash upgrade
10:12:10
i just realized that in my notes i'm missing updated package so i just added that here
now you can see that pie game has
10:12:17
successfully updated and let's just check to see if it has by using pip list outdated pip
10:12:25
list dash dash outdated and pi game is no longer in here so it
10:12:32
is up to date and if you would like to install a package type hip install and the name
of the package you
10:12:38
would like to download let's say i would like to download pandas pip install the name
of the package in this
10:12:44
case pandas two valley boring minutes later and it's
10:12:50
done so i should now have pandas installed and let me check pip list
10:12:57
and here it is i have successfully downloaded the package of pandas so those are a
few commands for pip and
10:13:05
if you're interested in downloading more packages head to python's package index
on second thought i should probably show
10:13:11
you all where that is python's package index is located at pipi.org
10:13:17
and you can do a search for all sorts of different projects and packages so
everybody that is pip4 python i will
10:13:24
post my notes in the comments section down below and well yeah that's how to use
pip for
10:13:29
python hey what's going on everybody it's bro hope you're doing well and in this
video i'm going to show you
97.py to exe
10:13:34
all how we can convert a python file to an executable so sit back relax and enjoy the
show
10:13:43
here's a quick disclaimer before we begin windows defender and other anti-virus
programs may prevent your
10:13:49
executable from running but if you head to the settings section of your anti-virus you
can disable that so that
10:13:54
your python file can run also please be sure that pip and pi installer are both installed
and up to
10:14:00
date i have a separate video on that if you're interested now the program that i'm
going to be converting to an
10:14:06
executable has a gui portion to it i created a clock program in a previous video
10:14:11
so i'm going to turn this clock program into an executable now i recommend creating
a new folder on
10:14:17
your desktop just because this process is normally somewhat messy so we'll create
a folder to contain it all
10:14:23
now copy any relevant python files and or images and paste them to within this
folder if you have an image that
10:14:29
you would like to set as an icon for your executable i recommend moving that to the
same folder as well our next step is to open
10:14:36
command prompt and we need to change the directory of command prompt so that
it's pointing to
10:14:42
that folder that contains our python file so on your python file right click on it go to
properties and copy
10:14:48
this location and in command prompt to change directory type
10:14:53
cd space and then paste that location now here's the cheat code to convert a python
file to an executable
10:15:00
make sure that pi installer and pip are installed and both up to date so type pi
installer
10:15:07
dash capital f to make all of this one file if you need the terminal window in your
10:15:14
program then omit this next part since i'm running a gui application i do not need that
terminal window
10:15:20
so to remove that window type dash w if you have an icon that you want to set
10:15:26
type dash i and the name of your icon however it should be an ico file
10:15:32
so if you have some other image that you would like to convert to an ico file here's
how if you need to convert an
10:15:39
image to an ico file you can just google convert to ico but one website that i
commonly use
10:15:45
is icoconvert.com just pick a file to upload so choose file i'm going to convert this
10:15:52
png file of an alarm clock upload it there is a bunch of different settings you can use
10:15:58
convert ico and then download my icon and once your ico file is downloaded i
10:16:04
will drag and drop it to my folder and i will rename this let's rename this as
10:16:09
icon dot ico and here's that cheat code one more time pi installer dash f
10:16:16
dash w if you do not need the terminal window dash i if you would like to add an icon
10:16:22
the name of your icon it should be an ico file followed by the name of the python file
10:16:27
and mine is named clock.p.y then hit enter
10:16:36
alright looks like building our executable has completed successfully so open that
folder back up
10:16:43
your executable is located within this dist folder i would recommend just dragging
and dropping it to your desktop
10:16:50
and we can get rid of this old folder but close out a command prompt and by
10:16:56
clicking on this executable this executable will now run my python program well
everyone that is how to convert a
10:17:03
python file to an exe file i will post my notes to the comment section down below
10:17:08
and well yeah that's how to convert a python file to an executable what's going on
everybody it's you bro
98.calculator program
10:17:15
hope you're doing well and in this video i'm going to show you all how we can create
a basic calculator using python so
10:17:21
sit back relax and enjoy the show let's create a calculator after
10:17:27
importing tkinter we'll need to define three separate functions our first function will be
named
10:17:33
button press and there is one parameter which we will call num we're not going
10:17:39
to fill this in quite yet we'll get back to it later we'll use pass as a placeholder we'll
also need an
10:17:45
equals function to calculate our expression and a function to clear our calculator
10:17:55
okay that's it for our three functions let's create a window window equals tk and at
the end of our
10:18:02
program we'll write window.mainloop let's set a title window
10:18:09
dot title calculator programs good but make sure you spell
10:18:17
title right and let's set a geometry of
10:18:25
500 500 should be decent now we're going to create a string named
10:18:31
equation text equals a set of quotes
10:18:36
this will effectively be empty for the time being and we'll need to create a string var
10:18:42
and i will call this equation label equals string
10:18:48
var let's create a label label equals label we're adding our
10:18:54
label to our window i will set our text variable equal to our equation label
10:19:04
you can pick a font if you wish
10:19:11
i'll set the background color equal to white
10:19:17
a width of 24 and a height of two
10:19:24
and we need to finish this by using label dot pack and we should have a blank label
within
10:19:30
our calculator this will display our numbers that we're entering in or our expression
10:19:36
okay this is the tedious part we have to create all of the buttons and i think we should
place these all within a frame
10:19:42
frame equals frame we're adding our frame to our window frame dot pack
10:19:50
now let's create button one we'll do some copying and pasting later so it's not as
tedious so button one
10:19:57
equals button we're adding our button to our frame i'll set the text equal to 1
10:20:04
the height equal to 4 and the width equal to 9 and a font size
10:20:11
of 35 okay we'll need a command command
10:20:18
equals i'll write a lambda function here lambda we will call button press
10:20:28
i think i'm going to put this on the next line just so it's easier to read button press and
we will pass in the
10:20:33
number one and we need to add our button to our frame
10:20:38
button one dot grid and i'm going to set a row equal to zero
10:20:45
and a column equal to zero and let's just test it
10:20:50
okay let's add the other buttons i'm going to copy and paste everything that we have
for button one
10:20:56
and change button one to button two set the text equal to two pass into
10:21:03
to button press and for the row and column i'm going to set the column equal to one
10:21:09
but keep row the same okay button three
10:21:15
it's gonna be the same process really so text equals three button press three row
zero column two
10:21:27
and button four text equals four button press
10:21:34
four column actually let's set this to row one
10:21:40
column zero and i need to change that too
10:21:47
okay let's just double check this
10:21:52
okay button five so button five text equals five
10:22:01
button press five and let's set column equal to one
10:22:10
okay number six i told you all this is going to be tedious
10:22:21
okay so row one column two
10:22:29
button seven
10:22:38
okay so row two column two
10:22:44
actually let's change that to column zero much better okay
10:22:51
button eight eight eight eight eight
10:22:59
row two column one number nine
10:23:14
row two column two
10:23:20
and lastly zero
10:23:26
so button zero text equals zero button press pass in
10:23:33
zero and this will be row three column
10:23:38
zero okay not too bad now let's fill in some of
10:23:43
the symbols so we need plus
10:23:49
i'm just going to rename this as plus
10:23:54
the text will equal the plus sign put that within quotes
10:24:00
and button press will be plus oh and we should probably put this in a
10:24:07
row and column so let's say row 0 column three
10:24:12
so it should be on the right hand side here okay we need minus
10:24:25
let's change plus to minus
10:24:30
and place this at row one column three
10:24:36
we need multiply
10:24:45
that is an asterisk
10:24:51
row two column three and divide
10:25:04
that is a forward slash
10:25:12
row three column three
10:25:18
i promise we're near the end of creating these buttons we'll need an equal sign
10:25:23
let's say equal equal text
10:25:31
is the equal sign and our command is going to be
10:25:37
equals the function and this will be row 3 column 2.
10:25:50
we need a decimal
10:26:02
let's change the text to a decimal
10:26:08
row three column one
10:26:13
okay and lastly we just need a clear button let's put this at the bottom
10:26:24
so clear clear let's set the text equal to the word
10:26:31
clear and the command will be our clear
10:26:36
function and i think i'm going to pack this at the end
10:26:42
but let's change frame to window okay let's check out all these buttons
10:26:50
okay not too bad i think i'm going to change the width on our clear button so it's a
little bit wider let's change the width to 12.
10:26:58
that's a little bit better okay let's fill in some of these functions let's start with button
press we're
10:27:06
going to state global equation text then our equation text
10:27:16
is equal to equation text plus we're converting whatever number or
10:27:22
symbol that we receive to a string and our equation
10:27:29
label which is a string var will be set to our equation text
10:27:36
and let's test this so we can type in any combination of
10:27:42
numbers or symbols okay let's work on our equals
10:27:50
function next so let's say global equation
10:27:57
text total equals
10:28:04
eval now eval will parse the expression that we pass in
10:28:10
equation text
10:28:17
and our equation label will be set to whatever our total is
10:28:26
and if we want to reuse our total let's set our equation text
10:28:33
equal to total 2 plus 2 equals four minus three equals one
10:28:41
times five equals five divided by two equals two point five now what if we
10:28:47
divide by zero well we'll run into that ugly zero division error
10:28:53
so let's write our code within a try and accept block so i'm going to indent some of
this
10:29:01
and let's write try all of this code and we will catch a zero division error
10:29:06
exception so accept zero division error
10:29:15
and i will set our equation label set
10:29:22
let's say arithmetic error equation text
10:29:30
equals a set of quotes okay so we should be able to catch this exception now
10:29:37
five divided by zero equals arithmetic error now what if we have a syntax here
10:29:45
like we type in just a bunch of symbols and hit equals we'll probably want to catch
the syntax
10:29:51
error as well so let's write an additional accept block before zero division error or
after i guess it
10:29:59
doesn't matter accept syntax
10:30:04
error let's change arithmetic to syntax and we can keep equation text the
10:30:11
same so now our program won't crash if we type in a bunch of symbols
10:30:17
syntax error now let's work on the clear button
10:30:22
so that is within the clear function
10:30:28
so we will set global equation text
10:30:36
equation label dot set to a set of quotes to
10:30:43
effectively clear it and our equation
10:30:49
text equal to a set of quotes as well and that should be it for this program
10:30:55
let's run this one last time let's say 3.14 times
10:31:01
2.1 equals 6.594 divided by 2.1
10:31:08
equals 3.14 minus one i'm just making up numbers here plus ten equals twelve point
one four
10:31:16
and let's clear this and start again one plus two plus three plus four equals ten
10:31:23
i think that's good enough all right everybody so that's how to create a basic
calculator in python i will post all of
10:31:29
my code in the comments section down below if you would like a copy for yourself
but yeah that's how to create a basic
10:31:36
calculator using python hey what's going on everybody it's your bro hope you're
doing well and in this
99.text editor program ✏️
10:31:42
video i'm going to explain how we can build a basic text editor program in python so
sit back relax and enjoy the show
10:31:51
okay people let's create a text editor here's all the different imports that you'll need
and we'll begin by defining
10:31:57
all of the different functions that we'll need so let's say we have a change color
function to change the color
10:32:03
of our font and for all of these functions i'm going to write pass as a placeholder
10:32:08
so we have change font and this function will accept a varying
10:32:14
amount of arguments so we'll use that args keyword
10:32:19
we will need to open a file new file
10:32:27
open file
10:32:34
save file
10:32:40
cut copy
10:32:49
paste yeah there's a lot of functions here
10:32:54
about this is for an about section
10:33:01
and lastly quit so how many do we have one two three
10:33:09
four five six seven eight nine ten ten functions let's create the
10:33:15
main window that we're gonna work with window equals tk and at the very end
window.main
10:33:24
loop okay so let's create a title for this window window
10:33:30
title notepad program i guess or maybe text editor
10:33:36
program would be more appropriate text editor program
10:33:44
okay and we will need a file to work with and i'll declare that now file equals none
10:33:51
we'll need a window width and height window with equals 500 is good and a
10:34:00
height okay so let's attempt to center this
10:34:05
window because right now it just appears to the side so our next step is to get the
screen
10:34:12
width equals window dot w
10:34:20
info screen width and i'll copy this for screen height
10:34:35
okay so we need to figure out how much we're going to move our window on the x
and y axis so let's say x
10:34:42
equals screen width divided by 2
10:34:49
minus window width divided by
10:34:56
two and we're going to cast all of this to an end so add an inch cast around this
10:35:04
let's copy this do the same thing for y except this will be screen height
10:35:11
and window height
10:35:17
okay let's set the geometry for the window window geometry i'm going to use a
10:35:24
string format method so let's say
10:35:32
these will be placeholders for our values
10:35:37
so we have window width for the first value
10:35:42
window height x and y so this should be near the
10:35:50
middle cool
10:35:55
we're going to create a string var variable to hold the font name font name
10:36:02
equals string var and pass in your window and you can set
10:36:08
this to what do you want your default font to be so let's say font name dot set and
then pick a font
10:36:16
i'll use ariel i think let's create a font size string bar as
10:36:22
well
10:36:28
we're going to add this to the window and we can set a default size
10:36:37
perhaps 25. okay let's create a text area
10:36:43
text area equals text we're adding this to the window i'm
10:36:51
going to set a font equal to font name dot get the name of the font that
10:36:59
we want to use will be the font that we set within our font name string var so now
we'll use a size
10:37:07
of font size dot get font size dot get
10:37:16
okay now we need a scroll bar scroll bar equals
10:37:23
scroll bar writing this to our text area so pass
10:37:29
that in and i'm going to configure my window
10:37:34
using grid row configure this will allow our text area to expand
10:37:42
but we're going to set a weight equal to 1 so that it doesn't back expand and then we
need column configure
10:37:55
okay and text area dot grid
10:38:03
i'm going to use the sticky keyword and this will stick north plus east
10:38:10
plus south plus west so my text area should take most of the
10:38:16
window this is some sample text
10:38:24
hooray so it goes down to the next line once it runs out of room on one line unless
you resize the window which is
10:38:31
exactly what we wanted let's create a scroll bar while we
10:38:37
already created it we just have to add a few details to it
10:38:42
so that will be near the bottom and why don't we say scroll
10:38:49
bar dot pack side equals right
10:38:56
fill equals y and then we just have to configure our
10:39:03
text area with the scroll bar so text area dot config
10:39:12
y scroll command equals scroll bar
10:39:20
dot set so that should work but let's test it to be sure okay i'm just
10:39:27
going to type a bunch of f's okay look at that our scroll bar works
10:39:34
cool okay let's head to this portion and we'll create a bunch of buttons well
10:39:42
a button an option menu and a spin box for the font color the size and the font style
10:39:49
so i'm going to place all of these different widgets within a frame frame equals frame
add it to the window
10:39:58
and then i'm going to use frame dot grid okay let's create a color button to
10:40:05
change the fonts color color button equals button
10:40:10
i'll add my button to my frame i'll set the text of the button equal to
10:40:16
color and i will associate a command of change color which we have already
10:40:23
defined at the top where is it at the top here change color
10:40:32
and then actually that's it for this portion so then we need to add this button color
10:40:40
button dot grid i'll set the row equal to zero and the column equal to
10:40:47
zero so let's be sure that this appears make sure you spell grid right
10:40:53
okay and we have a color button but this button doesn't do anything so let's head to
the change color
10:41:00
function within the change color function we're going to say color
10:41:05
equals color chooser dot ask color
10:41:14
and you can set a title to pick a color or
10:41:21
else you don't technically need a title and then text area
10:41:28
dot config oh i should probably print this color so let me do that um i'm just gonna
10:41:35
explain something here so this color is going to be i believe a tuple so let's print it so
let's say that i
10:41:43
would like white okay so we have a tuple we just
10:41:49
need one of these values not both of them so i'm going to say
10:41:56
text area dot config foreground equals our color but we do
10:42:03
not want to assign a tuple to our color we just need one of these values so let's say i
would like this
10:42:09
hex value so i'm going to say at index 1
10:42:15
and let's try it color perhaps i would like my text pink for
10:42:22
some reason so we're going with pink this is some text i suppose
10:42:28
and we can change this back too by using this color button now i would like blue and
we have blue text okay so that
10:42:36
is the change color function i'm going to minimize this because we do not need it
anymore
10:42:41
okay we're going to change the font next so head to the portion of our code right
10:42:48
after the color button and we'll create an option menu to list all of the different fonts
10:42:53
available to us so let's say font box equals option
10:42:59
menu we're adding this to our frame we need to set a variable a string var
10:43:08
so that will be our font name that we declared up here now here's a trick to add all of
the
10:43:14
fonts as options to our option menu we can use the unpacking operator which
10:43:20
is an asterisk followed by font dot families
10:43:25
and then call this function so this will return all of the different fonts available to
10:43:31
us and we just need to set a command and when we use our option menu we'll
10:43:37
call change font and we need to add our font box to our grid
10:43:46
row equals zero and column equals one so this should be on the
10:43:51
right hand side of our color button yeah it's going to look something like that and you
can see the default is
10:43:58
arial that we set and we can change it to something else like
10:44:03
comic sans or whatever uh however it's not going to change it quite yet so that'll be
the change font
10:44:10
function before we fill in the change font function there's another widget that's
10:44:15
going to share the same command of change font so we should create that next so
our next widget is a spin box which i
10:44:22
will name sizebox this will be in charge of increasing or decreasing the font
10:44:28
so size box equals spin box we're adding the spin box to our frame
10:44:35
and we'll set a range so from underscore you have to add that underscore it's kind of
weird
10:44:41
this is the range of our spin box for the font size from 1 to let's say 100
10:44:49
the text variable will be font size
10:44:57
and our command equals change font so when
10:45:04
we use our spin box and call this command of change font it's going to
10:45:10
pass in our font size as an argument whereas with option menu it doesn't pass any
arguments that's why
10:45:17
with our change font function we're accepting a varying amount of arguments
because one's going to pass in an
10:45:23
argument and the other isn't and we just need to add our spin box
10:45:28
so that would be size box dot grid row equals zero
10:45:36
and column equals two so we should have a spin box right on
10:45:42
the bottom and it's set to 25 that was our default for our font size string var which we
set as
10:45:50
the text variable okay so we can fill in this function of change font now
10:45:55
which will apply both to our option menu and our spin box so that is all the way at the
top within
10:46:03
the change font function we're going to take our text area dot config set
10:46:11
the font equal to font name dot get
10:46:18
and the size will be size box dot get and that's it for this function
10:46:25
i'll minimize it for now and let's test it this is some sample text
10:46:34
hey how you doing good looking okay so let's change
10:46:41
the font uh let's pick comic sans or whatever you want
10:46:46
and we can increase or decrease the size and we can change the color how about
10:46:54
red this time all right nice now let's add a menu bar
10:46:59
at the top so we can save this file or open a file or create a new file and
10:47:04
some other stuff on second thought i'm going to move these two last lines of code
10:47:09
to this section here where we create our scroll bar just so that it's more organized
10:47:16
okay so at the bottom let's create various drop-down menus so we'll need a
10:47:21
menu bar to begin with so let's say menu bar
10:47:27
equals menu we're adding this to our window make sure that's capital by the way
10:47:35
and window dot config menu equals menu bar
10:47:44
so we'll create a file menu edit menu and a help menu so let's begin with the file
10:47:50
menu to add to the menu bar file menu equals menu we're adding this to our
10:47:57
menu bar and set a tear off equal to zero
10:48:06
we'll need a cascade for it to actually be a drop down menu so that will be menu bar
10:48:14
dot add cascade
10:48:21
label equals file comma menu equals
10:48:28
file menu let's see if anything appears yet yep so
10:48:35
we have a file menu but there's nothing to drop down yet
10:48:40
so we need some options well commands technically file menu
10:48:47
dot add command
10:48:54
label equals new
10:49:00
and the command will equal our new file function
10:49:06
actually i'll put these together okay so let's copy this and paste it we have open
10:49:16
the command will be open file
10:49:22
we have save the command will be save
10:49:28
file and i'll add a separator
10:49:33
file menu dot add separator and lastly
10:49:40
we will have exit command quit
10:49:47
okay let me show you what this looks like so here's our drop down menu
10:49:54
that separator is this bold line going across so if i was to remove this then we
10:50:00
wouldn't have that bolt line you can keep it in or get rid of it do whatever you want
10:50:05
and this is the tear off so i'm going to remove the tear off
10:50:11
and you get this like tear off line at the top i guess it's the default i usually don't like it
so i set tear
10:50:17
off equal to zero so that is our file menu let's work on a edit menu
10:50:24
edit menu equals menu menu
10:50:32
bar make sure that's capital two
10:50:38
tare off equals zero we'll need a cascade menu bar
10:50:46
dot add cascade
10:50:55
label equals
10:51:00
edit menu equals edit
10:51:06
menu edit menu dot add
10:51:14
command label equals cut to cut some text
10:51:25
command equals copy actually that's cut my bad
10:51:33
okay next is copy so let's copy this and paste it
10:51:40
cut copy we have paste
10:51:47
command paste all right let's take a look
10:51:55
oh make sure you spell label right okay there we go okay so we can cut
10:52:01
copy and paste and lastly we should have a help menu
10:52:08
help menu equals menu menu
10:52:15
bar tear off equals zero
10:52:22
i think i'll copy this for my cascade so menu bar dot add cascade
10:52:30
help menu equals help menu
10:52:38
what we'll be doing for a command is that we'll be creating an about section that will
bring up a
10:52:44
pop-up window if you need to display any like help information or anything
10:52:49
uh so let's say about and the command will be about okay let's take a look
10:52:58
so we have help and we'll have an about section that will tell you more about this
program
10:53:03
but we need to work on some of these functions let's head to the quit function
10:53:09
this one's fairly easy this is found within the file menu window dot destroy that will
close out
10:53:16
of the window so go to file then exit to exit the
10:53:22
program we don't need it anymore so let's minimize this okay let's go to the about
function
10:53:31
let's bring up a message box show info this first argument is the title so
10:53:38
let's say about this program and the text will be whatever you want
10:53:45
to tell the user about this program this is a program written
10:53:53
by you okay so we should have a pop-up window
10:53:59
when you go to help then about about this program this is a program written by you
the next function is the
10:54:07
paste function so we can paste some text text area dot event
10:54:14
generate and the event is going to be within quotes and double angle brackets
10:54:22
paste let's copy this and change paste to copy
10:54:31
and then cut
10:54:37
okay so we should be able to cut copy and paste
10:54:44
hey how's it hanging okay
10:54:50
let's highlight this edit copy let's move down to the next line
10:54:58
and paste and we should be able to cut as well edit cut nice
10:55:06
head to the new file function and we'll set the title of our window to
10:55:12
maybe untitled until we save something so untitled
10:55:20
and i need to delete any text within our text area text area dot delete and we need to
set
10:55:28
a range so this has to be a floating point number the first character until the end
10:55:34
let's try it so i'll write some sample
10:55:40
text and we are going to go to file new that changed the title and it deleted
10:55:48
our text so that is the new file function let's work on open file we are now
10:55:55
within the open file function and we'll say file equals ask open
10:56:03
file name so we have a default extension if you would like one
10:56:11
default i think i spelled that wrong extension
10:56:18
equals let's say a txt file
10:56:23
comma what are the accepted file types
10:56:28
i'll set this equal to all files
10:56:35
comma and then we need asterisk dot asterisk for all file types
10:56:43
as well as text documents so these are what is accepted when we
10:56:49
look for a file so we have either all files or all text documents text
10:56:57
documents comma
10:57:02
asterisk dot txt
10:57:08
okay so then let's try some code
10:57:14
we'll set our window title equal to os dot path
10:57:21
dot base name of our file so that we will change the title of the
10:57:26
window to match whatever the file is that we opened
10:57:32
then let's take our text area delete what's currently here
10:57:38
from one until the end take our file use the open function
10:57:47
open our file and read it so pass in our
10:57:52
text area dot insert
10:57:58
beginning at index of 1.0
10:58:04
file dot read that you can catch any exceptions to
10:58:11
i'll just write accept exception but it's better to name specific exceptions i'll just print
10:58:20
couldn't read file and we should close our file at the
10:58:28
end within a finally block finally file dot close
10:58:35
we'll need a sample file to read so i'm going to create a new file on my desktop
10:58:40
i'll call this test.txt and let's write something whoa
10:58:47
you can actually read this
10:58:53
awesome okay let's save close out of it add run our program
10:59:01
file open test.txt open whoa you can actually read this
10:59:08
awesome nice so we know that the open function is working if we need to create a
new file just go
10:59:15
to file new and that will delete our old text so lastly we have save file
10:59:25
underneath the save file function file equals file dialog
10:59:31
dot ask save as file name so this will be the default
10:59:38
name for the file but you can change that initial file equals
10:59:45
let's say untitled dot
10:59:53
you txt set a default extension default extension equals let's say txt
11:00:05
file types equals
11:00:14
all files comma then asterisk dot asterisk
11:00:22
for all file types
11:00:30
and any text documents text documents
11:00:38
asterisk dot txt
11:00:46
okay if file is none that's if they close out of our
11:00:52
file dialog we'll just return
11:00:59
else so we'll set the window title first
11:01:07
window title os dot path dot base name
11:01:16
file file equals open open
11:01:22
our file and write
11:01:27
file dot right take our text area and get
11:01:34
the text from index one through the end
11:01:42
catch any exceptions it's better to name them instead of just doing a general accept
exception
11:01:49
but i'm just going to print couldn't save file
11:01:57
and finally close our file file dot close all right so
11:02:03
let's test it so i'm going to change the font i wonder if impact is in here
11:02:11
yes it is i'll change the font size maybe 30 and i'll pick a color
11:02:18
how about this swamp green color okay so write something perhaps some lyrics to a
11:02:24
song you like
11:02:40
i should probably test copying and pasting edit copy
11:02:46
edit paste nice
11:02:55
all right let's actually try and save this now i'll go to file save the default file name is
11:03:02
untitled because we set it to that i'll say all star
11:03:09
lyrics and save now i should be able to open
11:03:17
this so i'm going to go to my desktop here's that text file i'm going to open it and here
are the
11:03:23
lyrics that i wrote within my text editor and let's try and read this so let's go
11:03:29
to file open find your text document open and would you look at that my
11:03:35
lyrics are still here however this program doesn't save the font so i thought of a fun
last minute
11:03:42
project that we can do we can actually use our text editor to write a python file much
like what we do with pycharm
11:03:49
so let's write a program using our text editor program it'll be just a simple program
let's
11:03:56
print hello world and ask for some user input
11:04:02
let's say name equals input
11:04:09
what's your name
11:04:14
and we will print hello plus our name
11:04:21
variable and you can change the font and everything too if you want
11:04:31
okay so let's save this file save i'll save this to my desktop but make
11:04:36
sure you save it as a py file a python file i'll name this as
11:04:42
hello dot py and save
11:04:47
now to run a file using command prompt we need to find the directory that contains
our python
11:04:52
file mine is hello.py and it's located on my desktop so i need the location right click
go to
11:05:00
properties you can copy the location then within command prompt or terminal
11:05:05
you can change directory by typing cd space and then paste your new directory the
11:05:11
new location enter then to run a python program type python space and then the
name of
11:05:19
the program the python file mine is hello dot py hit enter hello world what is your
11:05:26
name bro hello bro i just thought that would be a fun last minute project
11:05:32
that we can do you don't even technically need an ide to write a python program
screw you pycharm well everyone that is
11:05:39
a basic text editor program i will post the code for this program in the comment
section down below and
11:05:46
well yeah that's a basic text editor program using python all right
100.tic tac toe game
11:05:52
ladies and gentlemen let's create a game of tic-tac-toe to begin we'll need to import
tkinter as well as random and
11:05:59
let's begin by defining all of the different functions that we'll need let's define a
function named
11:06:04
next turn for the time being we'll just write pass we'll fill this in later on
11:06:10
we'll need a function named check winner
11:06:16
a function named empty spaces to check if there are any empty spaces left and
11:06:24
lastly a function named new game that will launch a new game for us okay we have
our four functions now
11:06:31
let's create a window let's create a window window equals tk and at the end of our
program we need
11:06:39
to use window dot main loop and when we run this we should have just a small
11:06:45
basic window let's set the title window dot title let's set this to
11:06:53
tick tack toe we'll need a list of players players
11:07:00
equals x comma o
11:07:05
and with the way that we're writing this program we can swap these symbols with a
different character
11:07:10
for example we could say dollar sign is playing against the at symbol too
11:07:16
but i'll demonstrate that later let's keep it as x and o for now
11:07:21
now we need to select a random player to begin player equals random
11:07:28
dot choice and pass in our list of players now we'll need nine buttons i'm going to
11:07:35
create a 2d list of buttons named buttons and this will be the first row this will
11:07:42
be the second row for the time being i'm just going to initialize these with 0 for
everything and put these all
11:07:50
within a list so we have a 2d list named buttons
11:07:56
and to better visualize this i'm going to place each row on a new line so that's what
our board is going to
11:08:01
look like we have a 2d list named buttons we'll need a label to
11:08:06
display whose turn it is label equals label i'll set the text equal to
11:08:15
player plus the word turn and i'll set the font to
11:08:22
font equals pick whatever font that you want
11:08:30
and i am going to pack this label label dot pack and set the side
11:08:37
equal to top and let's run this just to test it
11:08:42
okay looks like it's x's turn let's try it again oh stern okay that's fine for now
11:08:49
let's create a reset button reset underscore button
11:08:55
equals button i'll set the text equal to
11:09:02
restart it's going to be a restart button or reset button pick a font
11:09:14
maybe i'll set the size to 20 and we'll need a command command
11:09:21
equals new game when we click on this button it's going
11:09:26
to call this new game function for us and we need to
11:09:31
pack this button reset button dot pack
11:09:37
and i will set the side equal to top okay let's see if that appears alright so we
11:09:43
have our turn order a label that will display whose turn it is as well as our reset
11:09:49
button now we'll need to create all of the buttons to add to our 2d list named buttons
but i'm going to
11:09:55
place these all within a frame frame equals frame we're adding our frame to our
window and
11:10:03
i'm going to pack this frame frame dot pack
11:10:08
now we'll take our 2d list of buttons and add a button to each spot and i think the
best way to do this
11:10:14
would be to use nested for loops we'll have an outer for loop in charge of the rows
11:10:20
four row in range three remember we only have three rows and
11:10:26
three columns the inner for loop will be in charge of the columns four column and
range three
11:10:34
and inside the inner for loop we're going to create a new button
11:10:39
we'll say buttons at index row and column we have two indexes
11:10:46
because we have a 2d list so buttons at row whatever column whatever
11:10:53
depending on what iteration we are within our for loops we'll create a new button
11:11:00
and we'll set the text oh first add this to the frame almost forgot about that we're
adding
11:11:06
our buttons to our frame and adding the frame to the window now we'll set the text
equal to a blank
11:11:12
set of quotes because we don't want any text we'll set the font i'll just copy what
11:11:19
we have here for label
11:11:26
for a width let's say five and a height let's say two i'm going to hit enter
11:11:32
just to move down to the next line because we still have a few more things to fill in
okay we'll need a command command
11:11:39
equals and i'm going to set this equal to a lambda function so we need some
arguments actually i
11:11:45
think i'll pass in some keyword arguments row equals row and column equals column
then our expression is
11:11:54
next turn and we will pass in row and column now we also need to add
11:12:00
our buttons to our frame buttons at index of row and column
11:12:09
we'll use the grid function for this grid row equals and
11:12:16
column equals column and it's always a good idea to test your
11:12:22
program after making any major changes so we should have our label our reset
11:12:27
button and our grid of buttons now that the main body of our program is complete
11:12:33
let's head to the next turn function and remember that we're passing in row
11:12:38
and column as arguments so we need to set up those parameters within the next
turn
11:12:44
function row and column and the first line within here we would like access to our
player
11:12:52
so let's say global player so that we have access to it and we're first going to check
to see if
11:12:58
the button that we click on is empty so buttons at index of row and index of column
11:13:07
to access the text of a button just add a third index operator and type in text
11:13:14
if the text of our button that we click is equal to a set of quotes that means
11:13:20
it's empty and when we call check winner
11:13:25
winner winner chicken dinner and it's false there is no winner then we will fill in
11:13:32
that button with our player our player's character whatever it is x or o or whatever we
decide
11:13:40
so within here let's check to see if player is equal to players
11:13:47
at index zero that is our first player and remember that we created a list of players
and we're planning on
11:13:55
swapping these later we could say like if player is equal to x but that would make our
program less
11:14:01
flexible if we want to pick like a different character instead of an x so if player is equal
to player at index
11:14:08
zero whatever symbol is there then we are going to take buttons
11:14:14
at index of row index of column take our text and set it equal to
11:14:23
our player and we should check to see if there's a
11:14:29
winner after filling in this button if check
11:14:35
winner and we still need to fill in this function returns false
11:14:42
then we will switch players player equals players at index of one and
11:14:50
label dot config text equals
11:14:59
players at index 1 plus the word turn
11:15:06
so what we did here is that we're checking to see if after placing our text of our
player
11:15:13
on that button that we click if there is no winner then we're going to swap players
player
11:15:20
equals our next player and we're configuring our label so that it displays the next
player's turn
11:15:26
players at index 1 turn now what if there is a winner let's say
11:15:32
else if check winner is true
11:15:40
then we will take our label dot config and set the text
11:15:48
equal to players at index 0
11:15:54
plus the word wins now let's add another else if statement
11:16:02
else if there's a tie let's say check winner is equal to
11:16:09
the word tie because we are already using false and true then let's change our
11:16:16
label and i'm just going to copy this text equals the word
11:16:23
tie okay so let's create an else block
11:16:29
so that goes right here so if it's not player one's turn player
11:16:35
at index zero then it's our other player's turn player at index
11:16:40
of one so oh then so we just need to mostly just copy this text and change a
11:16:46
few things around so i'm going to copy all this
11:16:52
and within here let's set player equals players at index
11:16:59
zero then players at index zero's turn players
11:17:06
at index one wins and that is it now our next turn
11:17:11
function is now complete let's head down to the check winner function and we need
to check all of the different win
11:17:18
conditions and return it true if somebody won false if there is yet no winner and the
word
11:17:24
tie if it's a tie so let's check all of the horizontal win conditions
11:17:29
so we can do this using a for loop for row in range three
11:17:37
we need to check the text of each button in each row if buttons at index of row
11:17:44
and column index of 0 we'll use the index operator to check the text
11:17:50
is equal to the next button in our row so i will copy this and change zero to
11:17:57
one check to see if that is equal to buttons at index of row index of two
11:18:06
and i'm going to check to see if this is all not equal let me move this a little bit
11:18:13
to a space so if that is the case that means
11:18:19
somebody won so if all of these buttons are the same and they are not equal to
11:18:25
an empty space that means they're all the same so let's return true that means that
somebody won
11:18:32
no we need to check the vertical win conditions if anybody has the same character
all
11:18:38
the way down a single column this next for loop is going to be four column in range
three
11:18:44
for the first button this will be button at index of zero and column then
11:18:52
one and column let me just paste that and two and column
11:19:01
now we need to check the diagonal win conditions so if buttons at
11:19:07
index 0 0 that's the top left corner if the text of that button is equal to
11:19:14
buttons at indexes of one one is equal to the indexes of
11:19:23
button two two and if all of this does not equal an empty space
11:19:30
then return true so there is a winner and we have one last win condition to
11:19:37
check that is the other diagonal win condition so we just need to change some of
these indexes around
11:19:44
so we have zero two one one and two zero
11:19:50
i'm going to change this statement to an else if statement no we need to check to
see if there's
11:19:56
any spaces remaining else if we will call the empty spaces
11:20:01
function which we have yet to fill in if this returns false then we will
11:20:09
return the word tie
11:20:15
and lastly else else there is no winner and no tie so we will return false
11:20:24
and that is it for the check winner function let's test this
11:20:31
so this doesn't account for a tie quite yet but we can fill in some of these spaces
looks like x wins
11:20:37
and we can no longer fill in buttons and we cannot start a new game quite yet either
11:20:43
within the empty spaces function let's create a local variable named spaces and set
this equal
11:20:49
to nine whenever we call this function four row in range
11:20:56
three and we'll create a nested for loop for column in range
11:21:04
three we will check the text of each button if buttons at indexes of
11:21:11
row and column and we will check the text to see
11:21:21
if it's not equal to an empty space if that is the case spaces
11:21:29
minus equals one then we'll write an if statement if
11:21:36
spaces as in spaces remaining is equal to zero
11:21:41
that means we will return false and there are no spaces left else
11:21:49
we will return true okay let's test it
11:21:56
so this time i'm trying to get a tie
11:22:06
yep looks like it's a tie this next part's optional but i would like to change
11:22:12
the color of each button for the winning combination so within our check winner
function
11:22:17
underneath the first win condition i'm going to take buttons at index
11:22:23
of row and index of zero
11:22:29
use the config method and set the background color equal to
11:22:36
let's say green and i'm going to repeat this process for
11:22:41
the other buttons within this win condition so that is row
11:22:47
zero row one and row two i'll copy all of this paste it
11:22:55
and switch some of these around so these should all be matching
11:23:00
so zero and column one and column and two in column
11:23:08
okay so we have zero zero one one and two two
11:23:14
and one more zero two one one and
11:23:20
two zero now when we have a winning combination
11:23:26
the color of the buttons involved in that combination are going to change to green
now if there is a tie let's change all
11:23:33
of the buttons to let's say yellow so i'm going to write nested for loops for this
11:23:39
four row in range three for column
11:23:47
in range three take our buttons at row
11:23:54
and column and change the background color to yellow or some
11:24:00
other color of your choosing so if there's a tie they should all be
11:24:06
yellow i'm trying not to win here it's actually more complex than what i thought
11:24:16
yep it looks like it's all a tie and the last thing that we need to do is to fill in the new
game function so that we can
11:24:22
begin a new game so let's say global player we would like access to our player
11:24:27
from inside this function and set player equal to a new random choice random
11:24:35
dot choice and pass in our list of players and we will change our label
11:24:41
label dot config and set the text equal to player
11:24:48
plus the word turn and we also need to reset all of our buttons
11:24:56
for row in range three for
11:25:03
column in range three buttons
11:25:10
at index of row and index of column
11:25:16
dot config text equals a set of quotes so it's
11:25:22
empty and we'll change the background color factors previously
11:25:27
so the default color for buttons is actually hexadecimal f 0 f 0 f 0
11:25:35
so this function will begin a new game for us and let's test it to be sure i'm going
11:25:41
to start a new game reset reset i'll actually try and win this time and
11:25:49
reset okay so it looks like the new game function is working now another thing that
you can do too is
11:25:56
that you can change the players around let's say we would like to play as
11:26:01
dollar sign and at sign so this program is flexible enough so that you can change the
icons of the
11:26:08
players all right everybody so that's a basic game of tic-tac-toe for
11:26:14
python if you would like a copy of this code i will post all of this to the comments
section down below
11:26:19
but yeah that's how to code a basic game of tic-tac-toe for python hey what's going
on everybody
101.snake game
11:26:26
it's your bro hope you're doing well and in this video we're going to create a game of
snake using python so
11:26:32
sit back relax and enjoy the show first thing we should do is import from
11:26:39
tkinter as well as the random module and let's define all of the different classes and
functions that we'll need
11:26:45
so let's create a class for our snake object as well as our food object so for the
11:26:52
time being i'm just going to write pass for my classes we'll fill these in later so we
have class snake and
11:26:58
class food and let's define all of the different functions that we'll need so let's say
11:27:04
we have a function named next turn
11:27:10
change direction
11:27:18
check collisions and lastly game over
11:27:29
okay for change direction i'm going to have one parameter a new direction
11:27:37
now when i create a game i like to place a bunch of constants at the top of my game
constants are variables that
11:27:44
you do not want to change later they're kind of like the settings however in python
there are no constants compared to other
11:27:51
programming languages so we're just going to create a bunch of variables that will
behave like them
11:27:56
so constants are values we do not want to change and they're kind of like the game
settings and i placed them at the top for
11:28:02
convenience and the naming convention for a constant is that all the letters are
uppercase so these
11:28:08
will be settings like the game width the speed etc so let's say the width of our game
will
11:28:14
be 700 but feel free to take the liberty to pick whatever size you want so we have
game width game height
11:28:22
i'll set this to 700 as well so it's a square let's say we have a speed the speed of
11:28:28
the snake how often will our canvas update so let's say 50 but the lower the number
11:28:33
the faster the game and a space size how large are the items in our game like
11:28:39
the food and body parts of the snake so i'm going to pick 50 but you can
11:28:44
change this and body parts how many body parts does
11:28:50
our snake have when we begin a game let's say three how about a snake
11:28:55
color you can pick a color name you can use rgb values or you can use a
11:29:01
hexadecimal value so i'm going to pick green 0 0 ff00 but
11:29:06
you can pick any color you want how about food color
11:29:13
equals red and that is ff000
11:29:20
and what about a background color for the canvas background color i'll pick black so
that is
11:29:28
six zeros feel free to mess with some of these colors too so we have a green snake
11:29:35
our food is going to be red and our background is going to be black so that is all of
the constants for our
11:29:41
game but you can feel free to adjust them if you like let's head down to the bottom
and make our window so we have
11:29:48
window equals tk and at the very end we should have window dot main loop
11:29:57
let's set a title for this window window dot title
11:30:04
snack game okay fine i'll spell it right snake game and if you do not want your
11:30:12
window to be resizable you can use resizable
11:30:18
and then you have to pass in false twice it's kind of strange but it's how it is
11:30:23
okay we should have a small window and we cannot resize this even if we tried
11:30:30
okay let's create a score label but we probably need a score first so
11:30:35
let's say score equals zero and an initial direction direction
11:30:41
equals let's say down now let's create a score label so let's
11:30:47
say label equals label we're adding our label to our window let's set the text
11:30:56
equal to score colon then i'm going to use the format
11:31:02
method and we will pass in our score whatever
11:31:08
it is and i'll set a font pick whatever font you prefer
11:31:17
and 40 is a decent size and then i'm going to pack this label
11:31:25
let's test it okay we're getting somewhere we'll need to create a canvas canvas
11:31:33
equals canvas we're adding it to our window i'm going to set the background
11:31:39
color equal to our background constant
11:31:45
i'm going to set our height equal to the game height and the width equal
11:31:52
to the game width that we set and we need to pack this canvas dot pack
11:32:02
and we should have a game board i'm going to try and center this window
11:32:08
when it appears so we can do that using a few lines of code the first thing we'll do is
11:32:13
update our window so that it renders and then we need to find some dimensions
11:32:18
so let's say the window width equals window dot w
11:32:26
info width and window height
11:32:35
equals window dot w info height
11:32:43
we'll need our screen width
11:32:48
equals window
11:32:53
dot screen width and screen height
11:33:00
equals window dot w info screen height
11:33:07
where is it there it is
11:33:13
okay then we need to see how much we're going to adjust the position of our window
11:33:19
so let's say x equals
11:33:24
screen width divided by 2 minus window
11:33:32
width divided by two and do the same thing for y
11:33:40
except this will be screen height and window height
11:33:48
and that will be why okay then we need to set the geometry
11:33:55
window dot geometry
11:34:00
we're going to use an f string so let's set the window
11:34:07
width times window height
11:34:17
and then add plus x plus y
11:34:24
so when we pass in x and y these cannot be floats they have to be
11:34:30
whole integers so let's add a cast around x and y
11:34:38
okay now this should be fairly close to the center yeah that's not too bad now when
we
11:34:44
begin a new game we should create a snake object as well as a food object snake
equals snake and called the snake
11:34:51
constructor and food equals food okay let's fill in our food class first
11:34:58
because i think that'll be easier than our snake class so let's head to the food class
11:35:04
now let's create an init method so def init this will construct a food object
11:35:11
for us and we need to place our food object randomly so for the x coordinate let's say
x
11:35:18
equals random dot rand int and we need a range the range is going to be zero
comma now
11:35:27
with our game board i view it like a chess board there's a given amount of spaces so
11:35:33
700 divided by our space size is 14 possible spots on the x-axis
11:35:41
and then 14 possible spots on the y-axis so i need to pick one of these spots
11:35:47
randomly so let's say game
11:35:53
width divided by our space size so we'll get a random number between 0 and 14
11:36:00
however this should be exclusive so let's say -1 and for good measure i'm just going
to
11:36:07
add a set of parentheses around this okay and then we'll convert this to pixels
11:36:12
so let's multiply all of this by our space size the size of each item in the game
11:36:19
and we'll do the same thing for y but change game width to height
11:36:30
okay we should be good
11:36:35
all right now let's set the coordinates let's say self dot coordinates
11:36:41
equals a list of x and y and that's it but make sure you spell
11:36:47
coordinates right now we need to draw our food object on our canvas
11:36:54
canvas dot create oval you can pick a square two if you
11:37:00
like we need a starting corner that will be where x and y is and an ending coordinator
11:37:08
so that will be x and y plus our space size the size of an object in
11:37:14
that game so x plus space
11:37:19
size y plus space size
11:37:26
and you can set a fill color too fill will be our food color that we
11:37:32
declared and i recommend adding a tag as well
11:37:37
that'll make it easy to delete this object so tag will equal the string food
11:37:44
now let's just test this so we should have a random circle or a square if you picked a
square
11:37:50
appear someplace at some spot on my game board cool seems like it's
11:37:55
working let's work on the snake class next our snake class we'll need a constructor
11:38:06
and we will set a body size equal to our body parts
11:38:16
we'll need a list of coordinates
11:38:24
fill this in later and a list of square graphics
11:38:31
so that will be a list okay so we need to create a list of
11:38:37
coordinates we can use a for loop for that so let's say for i in range zero
11:38:45
through body parts
11:38:50
we will take self dot coordinates and append a new list
11:38:59
and the coordinates for each body part at the start of the game will be 0 0 so that our
snake will appear in the
11:39:06
top left corner now we'll need to create some squares
11:39:11
okay so for x y in self dot coordinates
11:39:18
and remember that we have a list of lists so that's why we're using x y in self
coordinates
11:39:27
we'll create a square equals canvas dot create
11:39:35
rectangle so we need a starting corner that will
11:39:40
be x and y then x plus our
11:39:48
space size the size of each object in the game
11:39:53
let me use the constant though space size y plus
11:40:00
space size
11:40:06
and let's set a fill color equal to our snake color
11:40:14
all right and let's set a tag for convenience tag equals
11:40:22
a string of snake okay so we have a list of squares and we can append each
11:40:28
square into our list self dot squares
11:40:36
dot append and pass in whatever square that you create
11:40:42
so we have a snake that has a body size a list of coordinates and a list of
11:40:49
square graphics now after testing this we should have the head of our snake in the
top left
11:40:54
corner and now we just need the snake to move in a given direction every turn so the
initial direction is down but
11:41:02
we'll be able to change that later so let's head to the next turn function and there's
actually two things we'll
11:41:08
need as parameters that i forgot to fill in snake as well as food and we will call
11:41:14
this function when we begin our game so let's unpack the head of the snake so
11:41:19
that's x comma y equals snake dot coordinates
11:41:26
at index of zero so that's the head of the snake the coordinates will be stored in x
and
11:41:34
y so let's check to see if our direction
11:41:39
our initial direction equals up and we'll need some else if
11:41:46
statements we'll fill this in just a moment
11:41:52
else if our direction equals down
11:42:00
then left
11:42:06
and then right
11:42:11
okay if our direction is up then let's take our y coordinate for the
11:42:17
head of our snake minus equals our space size so that we move one space up
11:42:26
and then down is plus equals space size left
11:42:33
is x minus equals space size and lastly
11:42:39
right is x plus equals space size
11:42:45
okay then we need to call the next turn function again for the next turn so we can use
window
11:42:52
dot after we need the time so let's say our game speed
11:42:59
we're going to call the next turn function and we need to pass in our arguments of
snake and food
11:43:08
snake food oh and make sure you're not actually
11:43:13
calling the next turn function inside of the after method you just have to write the
function name of next turn without
11:43:19
the parentheses okay so let's update the coordinates for the height for the snake and
write that before we move on to the
11:43:26
next turn so snake dot coordinates
11:43:34
and we will insert a new set of coordinates after updating one of them
11:43:41
so zero will be the index the head of the snake and we will insert x and y coordinates
11:43:48
at this new location now we're going to create a new graphic for the head of the
11:43:54
snake square equals canvas dot create rectangle pass in x
11:44:02
and y for the starting corner of our rectangle and the ending corner will be x plus
11:44:09
our space size y plus our space size
11:44:17
then i will add a fill color of snake color and then we need to update
11:44:26
our snakes list of squares so that will be snake dot squares
11:44:35
and insert at index 0 a new square that we create
11:44:44
okay let's test this one last thing that we'll need to get this program to run is that after
you create your snake and
11:44:50
food object we should call it the next turn function and pass in our snake and food
object so we should be able to
11:44:57
test this so our snake is going to move but we need to delete the last body part in our
snake within the
11:45:05
next turn function but before we update to the next turn let's delete the last body part
of our
11:45:12
snake so delete snake dot coordinates
11:45:18
at negative index of one that is the last set of coordinates
11:45:24
we will update our canvas canvas dot delete
11:45:31
snake dot squares an index of negative one
11:45:40
and lastly delete snake
11:45:46
the list of squares at index of negative one
11:45:52
so it should appear that our snake is moving cool we need some controls for our
snake so
11:45:59
at the bottom of our program let's bind some keys let's do that here
11:46:06
so window dot bind let's find the left arrow key
11:46:16
and we will use a lambda the argument is event and we will call
11:46:23
the change direction function and pass in the word left
11:46:31
okay then we have to do the same thing for the other directions
11:46:40
so we have right pass in right
11:46:48
up pass in up down pass in down
11:46:59
let's head to the change direction function we need to access our direction global
11:47:07
direction this is the old direction
11:47:12
if our new direction that is passed in is equal to left
11:47:22
and if our old direction this direction does not equal right because we do not
11:47:28
want to go backwards and do a 180 degree turn
11:47:34
then we will set our direction equal to our new
11:47:40
direction and we just need to repeat this for the other directions for new direction
11:47:48
and i'm going to change this to else if else if new direction is right
11:47:56
and our direction is not already left
11:48:05
then we have up if our direction is not down
11:48:16
and down if our direction is not up okay so we should be
11:48:23
able to change the direction of our snake sweet okay we need to eat that pesky
11:48:31
apple next so let's work on that there's nothing else that we need to change within
the change direction
11:48:37
function so i'm going to minimize this function and head to the
11:48:42
next turn function we'll place an if statement here if
11:48:50
x remember that we unpacked the coordinates for the head of the snake if x
11:48:57
the x coordinate for the head of the snake is equal to
11:49:02
our food objects coordinates
11:49:08
at index of zero that's the x-coordinate for our food object and
11:49:16
y is equal to food coordinates at index of one
11:49:23
that means they're overlapping let's take our score
11:49:31
and increment it by one and change our label label.config
11:49:44
the text will equal score
11:49:52
and then i'm going to use the format method and pass in my new score
11:50:01
let's delete our food object and we gave our food object a tag so we
11:50:07
can just use the name of the tag to delete it and create a new food object
11:50:16
then i'm going to write this part of our program within an else statement
11:50:23
we will only delete the last body part of our snake if we did not eat a food object
11:50:32
okay let's test it again so i have three body parts now i have
11:50:38
four five six seven eight nine
11:50:47
ten you can see that the score is going up too okay let's work on collisions next
11:50:53
because i should not be able to go off screen like this there's one thing that we're
going to change so let's add an
11:51:00
if statement that will check collisions this will return true or false if we
11:51:07
detect a collision and pass in our snake object
11:51:13
if there is a collision we'll call the game over function
11:51:20
else we will update to the next turn
11:51:25
okay let's fill in the check collisions function and we no longer need our function of
11:51:31
next turn actually we don't need these classes either so i'll minimize them
11:51:36
okay check collisions so it looks like we will need a parameter of snake so be sure to
set that
11:51:44
let's unpack the head of the snake x y equals snake dot coordinates
11:51:52
at index of zero
11:51:57
and let's check to see if we cross the left or right border of the game if x
11:52:03
is less than zero or x is greater than or equal to
11:52:11
our game width then return true i think for testing
11:52:19
purposes i'm going to print something to the console window
11:52:24
game over let's test it okay i'm going to go over to the right
11:52:30
border game over and our game stopped let's go over the left game over
11:52:37
cool so we know that it's working okay let's do the same thing for y
11:52:43
we can use else if here if y is less than zero or
11:52:50
y is greater than or equal to our game height let's print game over
11:52:59
and return true and we should probably test it okay i'm going up game over and let's
go
11:53:07
down game over so what if our snake touches its tail or
11:53:15
another body part so let's say four i actually i
11:53:20
isn't too descriptive let's say for every body part
11:53:25
in snake dot coordinates
11:53:34
we're going to set this to everything after the head of the snake
11:53:40
we're going to check to see if any of the coordinates are matching
11:53:45
if x is equal to body part at index
11:53:52
0 and y is equal to body
11:53:59
part at index 1 then return true
11:54:07
and i'll print game over to test it print game over
11:54:15
otherwise we can return false there are no collisions
11:54:20
i think i'm going to change the size of the snake to 10. okay let's run into each other
11:54:29
cool game over and the last thing that we need to do is to fill in the game over
function
11:54:36
because it looks like everything else is fine so head to the game over function take
11:54:42
your canvas and delete all
11:54:49
and we'll create some game over text canvas create text
11:54:57
i would like this in the center of my canvas i'll take canvas w info
11:55:04
width divided by two comma canvas w info height
11:55:12
divided by two i'm going to put some of this on the next line for readability i'll set a
font
11:55:19
pick whatever font that you want
11:55:26
we'll need some text text equals game over
11:55:36
pick a color red's decent i'll add a tag too for convenience game
11:55:44
over and that should be it for the game over function i'm going to change the body
parts of
11:55:51
the snake back to what it was originally and we should probably test that game over
screen
11:55:58
all right it appeared okay so let's change some of these settings around you can
create a larger game board let's
11:56:05
say 1000 by 700
11:56:11
that still works you can slow down the speed or speed it up so 100 will be about half
as fast
11:56:19
it's going pretty slow now but what if we set it to 20.
11:56:28
you can change the space size so everything is a lot smaller now
11:56:35
including the food object you can change the body parts what about
11:56:42
20 to begin with that's fairly excessive
11:56:50
i'll change that back to three you can change the snake color let's say we would like
a blue snake
11:56:57
so that is for a hex color four zeros and then two f's
11:57:05
you can change the food color so let's say we would like a yellow food object so that
would be four f's and
11:57:11
then two zeros kind of resembles the python logo that
11:57:16
color scheme you can change the background color too
11:57:23
so let's say all white that would be six apps
11:57:28
i do not like that go back delete delete delete alright well that should be everything
let's run this
11:57:35
game one last time
11:57:46
[Music]
11:58:14
[Music]
11:58:44
well everybody that is a very basic game of snake for beginners i will post all of this
code to the comment section down
11:58:50
below but yeah that's a basic game of snake using python hey you
11:58:57
yeah i'm talking to you if you learned something new then help me help you in three
easy steps by smashing
11:59:03
that like button drop a comment down below and subscribe if you'd like to become a
fellow bro
11:59:11
[Music]
11:59:59
you
English (auto-generated)
coding learning
Private
Vansh Rojivadia
7/8
9:16

How I would learn to code (If I could start over)


Jason Goodison

7:08

What programming languages should you learn for Physics?


Jonathon Riddell

6:03

how NASA writes space-proof code


Low Level Learning

1:18:51

Computation and the Fundamental Theory of Physics - with Stephen Wolfram


The Royal Institution

26:16

C++ Introduction | C ++ Tutorial | Mr. Kishore


Naresh i Technologies

6:00:00

C++ Full Course for free ⚡


Bro Code

12:00:00

Python Full Course for free 🐍


Bro Code

13:40:10
Python for Everybody - Full University Python Course
freeCodeCamp.org
Sorry, you can't donate in this country or region yet.
AllFrom Bro CodePythonRelatedRecently uploadedWatched

9:16
NOW PLAYING

How I would learn to code (If I could start over)


Jason Goodison
3.6M views 1 year ago

1:00:00
NOW PLAYING

Learn HTML in 1 hour 🌎


Bro Code
1M views 2 years ago
Fundraiser

1:14:29
NOW PLAYING

Software Engineering Job Interview – Full Mock Interview


freeCodeCamp.org
718K views 5 months ago

6 videosNOW PLAYING
Full Courses
Bro Code

7:00
NOW PLAYING

How to Write Basic Python Code | Python programming For Beginners


Learn Loner
952 views 1 month ago

NOW PLAYING

Mix - Bro Code


More from this channel for you

9 episodesNOW PLAYING

Web Development Learning Path


James Cross

9:51
NOW PLAYING
How I Learned to Code in 4 Months & Got a Job! (No CS Degree, No
Bootcamp)
Tim Kim
2.1M views 3 months ago

NOW PLAYING

Permutations and Combinations Class 11 Maths | Must Practice Questions


| JEE | CBSE
LearnoHub - Class 11, 12
18 watching
LIVE

6:03
NOW PLAYING

Jack and Tim receive GOLDEN BUZZER from Simon Cowell! | Auditions |
BGT 2018
Britain's Got Talent
19M views 5 years ago

16:28
NOW PLAYING

$1 Vs $100,000,000 Car!
MrBeast
93M views 5 days ago
New

13:47
NOW PLAYING

Day in the Life of a Japanese Game Programmer


Paolo fromTOKYO
15M views 3 years ago
11:35
NOW PLAYING

Les Twins - TSBF 2019


BachataDay TV
1.7M views 3 years ago

6:46
NOW PLAYING

How to Learn to Code - 8 Hard Truths


Fireship
1.7M views 1 year ago

17:13
NOW PLAYING

How does Computer Hardware Work? 💻🛠🔬 [3D Animated Teardown]


Branch Education
1.3M views 5 months ago

7:13
NOW PLAYING

Using My Python Skills To Punish Credit Card Scammers


Engineer Man
4.4M views 2 years ago

2:57:12
NOW PLAYING

300 Days of Project Zomboid - The Movie


MadMavn
1.9M views 3 weeks ago
9:08
NOW PLAYING

Les Twins | FRONTROW | World of Dance 2014 #WODHI


Official World of Dance
87M views 8 years ago

5:01
NOW PLAYING

Meet a 12-year-old hacker and cyber security expert


CBS Mornings
7M views 5 years ago

3:45:18
NOW PLAYING

Ultimate Spider Man - Full Story From Comicstrorian


Comicstorian Full Story
409K views 2 months ago

31:07:30
NOW PLAYING

C++ Programming Course - Beginner to Advanced


freeCodeCamp.org
4.3M views 1 year ago

1:00:06
NOW PLAYING

Python for Beginners - Learn Python in 1 Hour


Programming with Mosh
13M views 3 years ago
12:00:00
NOW PLAYING

Java Full Course for free ☕


Bro Code
7.9M views 2 years ago
Fundraiser

53:06
NOW PLAYING

Python Object Oriented Programming (OOP) - For Beginners


Tech With Tim
2.9M views 3 years ago

4:54
NOW PLAYING

2 YEARS of PYTHON Game Development in 5 Minutes!


Coding With Russ
260K views 4 months ago

5:57
NOW PLAYING

Making a Game in Python with No Experience


Goodgis
958K views 8 months ago

2:12:35
NOW PLAYING

Object Oriented Programming with Python - Full Course for Beginners


freeCodeCamp.org
1.3M views 1 year ago
1:17:12
NOW PLAYING

Python Tutorial for Beginners | Learn Python in 1.5 Hours


Apna College
7.4M views 1 year ago

6:14:07
NOW PLAYING

Python Tutorial - Python Full Course for Beginners


Programming with Mosh
35M views 4 years ago

31:39
NOW PLAYING

Hanif ki Shaadi | Standup Comedy by Munawar Faruqui | 2023


Munawar Faruqui
6.1M views 7 days ago

1:03:21
NOW PLAYING

👩💻 Python for Beginners Tutorial


Kevin Stratvert
2M views 2 years ago

7:40
NOW PLAYING

Coding Learning MISTAKES that Hold Beginners Back


Internet Made Coder
220K views 4 months ago
46:29
NOW PLAYING

Celebrities in Big Boss |Big Boss Parody | Harsh Beniwal


Harsh Beniwal
9.9M views 5 days ago
New

12:19:52
NOW PLAYING

Python for Data Science - Course for Beginners (Learn Python, Pandas,
NumPy, Matplotlib)
freeCodeCamp.org
3.2M views 3 years ago

11:49
NOW PLAYING

FASTEST Way to Learn Coding and ACTUALLY Get a Job


Namanh Kapur
1.4M views 1 year ago

19:00:27
NOW PLAYING

Python API Development - Comprehensive Course for Beginners


freeCodeCamp.org
1.7M views 1 year ago

12:30:50
NOW PLAYING

Data Structures and Algorithms in Python - Full Course for Beginners


freeCodeCamp.org
1.7M views 2 years ago
3:35:07
NOW PLAYING

Python Full Course 🐍


Amigoscode
795K views 2 years ago

Andrew Ng: Opportunities in AI - 2023


Stanford Online
743K views 3 weeks ago

C++ Full Course for free ⚡


Bro Code
1.8M views 1 year ago
Fundraiser

Transcripts have moved to the description.

You might also like