(English-Arabic) CS50P - Lecture 0 - Functions, Variables (DownSub - Com)

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 124

[Music] so [Music] all right this is cs50s introduction to programming with

python my name is david malin and this is our week on functions and variables but
odds are many of you most of you have never actually programmed before so let's
start by doing just that let me go ahead here and open up my computer and on it a
program called visual studio code or vs code which is just a very popular program
nowadays for actually writing code now you don't have to write code using this
particular tool in fact all we need at the end of the day is a so-called text
editor a program for writing text and heck if you really want you could even use
something like google docs or microsoft word you'd have to save it in the right
format but really at the end of the day all you need is a program for writing
text because that's what code is text now within this particular program i'm
going to have the ability to create one or more files via this top portion of the
screen and i'm going to do so by diving right in and doing this at the bottom of
my screen at the bottom of my screen is a so-called terminal window and this is a
command line interface or cli interface to the underlying computer be it your mac
or your pc or even some server in the cloud and what i'm going to do here is
literally write code and then the name of the file that i want to code for
instance hello dot pi as we'll soon see any program that you write in python
generally has a file name that ends in dot pi to indicate to the computer that
it's indeed a program written in python now you'll see here at the top of my
screen i have a blinking cursor a line one which is where the very first line of
my code is going to go and then just a tab that reminds me of the name of this
file hello.pi and without even knowing much python i'm going to write my very
first program here as follows print open parenthesis quote hello comma world
close quote and close parenthesis and you'll see that at my keyboard some of my
thoughts were finished for me i only had to type one parenthesis and the other
one automatically appeared and that's just a feature that we'll see of tools like
this tool here now even if you've never programmed before odds are you can guess
infer what this simple program is going to do and it's only one line print open
parenthesis quote hello world close quote close parenthesis indeed when i run
this program ultimately it's just going to say hello to the world and in fact
this is a very famous perhaps the most canonical program you can write as your
very first program in python or any other language and so that's what i've done
here but on my mac my pc even my phone i'm generally in the habit like you of
running programs by double-clicking an icon or just tapping on the screen but i
see no such icons here and in fact that's because my interface to at least my
current mac or pc or some server in the cloud is again only a cli command line
interface which even though it might feel like it's a step back from the menus
and buttons and icons that you and i take for granted every day you'll find we
think that it's ultimately a much more powerful interface and incredibly popular
to use among programmers in the real world so to run this program i'm going to
have to use a command and i'm going to move my cursor back down to the bottom of
the screen here where previously i already ran one command the command code which
has the effect of opening vs code in my computer and then i passed in the name of
the file that i wanted to code up now i have a chance to type a second command
and you'll see i see a second dollar sign now the dollar sign here doesn't
indicate any kind of currency or money it just is the symbol that's generally
used to indicate your prompt where the command line interface wants you to put
those commands now the command i can run here is going to be this i'm going to
run python of hello dot pi now why is that well it turns out that when i actually
write code in a language like python it's of course stored in that file hello.pi
but i need to interpret the code top to bottom left to right so that the computer
knows what to do indeed at the end of the day even if you don't really know much
about computers you've probably heard that computers only understand zeros in one
the so-called binary system well if that's the case then something that says
print and parenthesis and quote-unquote hello world is not surely zeros and ones
we have to somehow translate it into the zeros and ones that the computer
understands now fortunately so long as you've installed such a program in advance
there's a program as well as a language called python so python is not only a
language in which we're going to write code it's also a program otherwise known
as an interpreter that you install for free on your own mac or pc or some server
in the cloud and you can then run that program that interpreter passing to it as
input the name of your file like mine here hello.pi and then that program that
interpreter will handle the process of reading it top to bottom left to right and
translating it effectively into those zeros and ones that the computer can
understand so let's do just that let me go back to vs code here i already typed
out python of hello.pi but i didn't yet hit enter and that's what's now going to
kick off this command and hopefully if i didn't mess any of this up i should see
my very first program's output to the screen and voila hello world so if you two
have typed exactly that same code and have executed exactly that same command you
will have written your very first program in this case in python well now let's
take a step back and consider what is it that we actually just did and what is it
we're looking here on the screen well first and foremost in most any programming
language you tend to have access to what are called functions a function is like
an action or a verb that lets you do something in the program and generally
speaking any language comes with some predetermined set of functions some very
basic actions or verbs that the computer will already know how to do for you that
the language really will know how to do for you and you the programmer the human
can use those functions at will to get the computer to do those things now the
program in question here hello.pi is using one function and you can perhaps guess
what it is that function is of course going to be this function print and that
print function of course doesn't print some pre-ordained string of text that is
to say it prints whatever it is you want it to print and here too do we have
another piece of terminology in the world of programming namely arguments an
argument is an input to a function that somehow influences its behavior the
people who invented python of course didn't necessarily know what it is you and i
are going to want to print to the screen so they designed this print function
using these parentheses with the ability to take as input some string of text be
it in english or any other human language that is what you want this function
ultimately to print onto the screen and what is it that the program's ultimately
doing on the screen well it's printing of course it's showing us hello world on
the screen and that's generally in programming known as a side effect it can be
visual can be audio in this case it's something that appears on the screen and
functions therefore can indeed have these side effects one of the things they can
do as this verb action is to display on the screen as a side effect something
like those world words that we wanted hello world so that's my first program and
you know i'm feeling pretty good everything worked as planned i didn't make any
mistakes but honestly when you're learning how to program and even once you've
learned how to program years later you're going to make mistakes and those
mistakes of course or refer to a term you might already know which is that of a
bug a bug is a mistake in a program and they can take so many forms and take
comfort perhaps in knowing that over the coming weeks you're going to make so
many mistakes you're going to have so many bugs in your code just like i did and
just as i still do and those bugs themselves are just mistakes that are problems
for you to solve and over the weeks to come we're going to give you a lot of
tools both mental and technical via which you can solve those problems but just
don't get discouraged if when writing your program for the first time it doesn't
even work that first time it will with time with practice and with experience so
let me deliberately now make a mistake that there was a non-zero chance i might
have done accidentally already but i got lucky let me go ahead and just suppose i
forgot to include something like the closing parenthesis at the end of this line
of code you know the code is almost correct it's like 99 of the way there but now
that i've pointed it out it's pretty obvious that it's missing that closed
parenthesis but even little seemingly minor details like that that you and i as
humans wouldn't really care about and if you're sending an email or a text
message whatever it's just a typo it's not that big a deal it is going to be a
big deal to the computer a computer is going to take you literally and if you
don't finish your thought in the way the language expects it's not going to
necessarily run at all so let's do this i'm going to go ahead here and clear my
screen down at the bottom just so i can start fresh and i'm going to go ahead and
run this version of my program after having made that change by deleting the
parenthesis i'm going to
go ahead and type python again of hello.pi and this time when i hit enter i'm
hoping i'm going to see hello world but here we have an error on the screen a so-
called syntax error which refers to my having made a mistake at my keyboard and
it this one fortunately is pretty straightforward it indeed says that this open
parenthesis was never closed and so that's probably pretty intuitive now what i
need to do i need of course to close it unfortunately sometimes the error
messages we'll see in the coming weeks are not going to be nearly that user
friendly but there too again with experience with practice will you get better at
debugging such programs let me now make sure that i indeed fixed it correctly let
me go ahead run now hello.pi and hit enter and voila we're back in business well
let me pause here and see if we have any questions now about python itself
writing or running even the simplest of these programs could i write code inside
a word or for example microsoft excel and what's the barrier to doing that a
really good question and allow me to very explicitly say to the entire internet
that you should not write code with microsoft word i mentioned that only because
it's a tool via which you can write text and code is at the end of the day just
text but it's not the right tool for the job we don't need bold facing
underlining paragraphs and the like we generally want something much simpler than
microsoft word or google docs and so vs code is an example of just a more general
purpose text editor its purpose in life is to allow you the human to edit text
nowadays these text editors come with many more features in fact you'll notice
that even in my code here even though it's just one line there's a bit of color
to it the word print for me is appearing in blue the parentheses are black and
we'll see as we might write more lines of code more and more of the lines will
come to life in various colors now that's just one feature of a text editor will
see too that it has features like this built-in terminal window it's going to
have a built-in tool for debugging or finding problems with code and it's just a
very popular tool nowadays but there are many many others out there you're
welcome to use them for this course and beyond we just happen to use this one in
large part two because you can also use vs code nowadays for free in the cloud
how about one other question here on programming with python or hello world or
syntax more generally that will spend to us if it's not possible to run the
computer using the terminal window i think i heard is it not if it's possible to
run the program without the terminal window are you full okay you froze for me
again but let me infer what the question is so in this environment as i've
configured my computer i can only run these python programs via the terminal
window now that's good for me the programmer or the person who's trying to learn
how to program but it's not very good if you want to ship this software and have
other people use your actual code you can absolutely write programs and then
allow other people to use not a command line interface but a graphical user
interface or gui gui this is just one mechanism and perhaps i think the the best
one with which to start writing code because eventually it's going to give us a
lot more control allow me to forge ahead here but please feel free to continue
asking questions along the way if only via the chat let's consider now how we
might go about improving this program let's go about improving this program to
make it a little more interactive and not just assume that everyone is going to
want to be greeted more generically as hello world let's see if i can't get this
program to say something like hello david or hello jeremiah or hello horatio or
whatever the actual user's name is well to do this i'm going to go back up to
hello.pi and i'm going to add another line of code at the very top that simply
says for instance what's your name quote unquote with an extra space at the end
so i'm printing to the user asking them a question for some input but now i need
another function to actually get input from the user and perfectly enough python
comes with a function named input so here i'm going to go ahead and call a
function input open paren close paren and that's going to prompt the user with
just a blinking cursor waiting for them to type something in now it turns out if
i read the documentation for the input function it actually takes an argument
itself i don't need to use print separately and then prompt the user for input so
i can actually simplify this code before we even use it i'm going to go ahead
here and take that same string from print put it as an argument to the input
function and get rid of the print altogether and in fact that print would have
added a new line anyway so now i've just got a prompt where the user's cursor is
going to end up blinking at the end of the line asking them what's your name in
my terminal window i'm going to run python of hello.pi enter ok we're making
progress it seems that this new function input is indeed prompting me the human
for input so i'm going to type in my name david and hit enter unfortunately it
doesn't really do anything with my name it just outputs it immediately all right
well i could fix this right i could go up to line two and i could change world to
david and then back in my terminal window here i can do python of hello.pi enter
what's your name david enter and there we go all right now i'm up and running now
my program is working as intended of course this isn't really working as intended
here let me go ahead and try pretending to be my colleague carter here well
carter's name is this i'm going to go ahead and hit enter and i'll see of course
hello carter well obviously not because i've hard coded so to speak i've written
literally my name inside of the string so we need some way now of actually
getting back what the user's input is and doing something with it ultimately and
for this we're going to leverage another feature of programming specifically a
feature of some functions which is that they can have return values as well if
you think of input as being again this action this verb you can actually
personify it as maybe a person like a friend of yours that you've asked a
question of and you've asked your friend to go get input from someone else go ask
that person their name and if your friend comes back knowing that person's name
well wouldn't it be nice if they handed that name back to you that's kind of what
we need metaphorically the function to do is get the user's input and then hand
it back to me so that i the programmer can do something with it but if it's going
to be handed back to me i kind of want to put it somewhere so that i can then
print it back on the screen i need to do the equivalent of take out like a piece
of paper or a post-it note write down on this piece of paper what it is the human
has said so that i can then feed it into as input that print function and to do
that we're going to need one more feature of programming namely variables and
odds are most everyone's familiar with variables for math class way back when x
and y and z and the like well programming has that same capability this ability
to create a variable in this case in the computer's memory not just on a piece of
paper and that variable can store a value a number some text even an image or
video or more a variable is just a container for some variable a variable is
just a container for some value inside of a computer or inside of your own
program so how do i go about expressing myself in this way well i think what i'm
going to do is introduce a variable that's a little more interestingly named than
x or y i could just say this x equals input but i'm going to use a better name
than a typical mathematical variable here and i'm going to literally call my
variable name why well in programming because i have a whole keyboard in front of
me i can use more descriptive terms to describe what it is i'm writing and now
though there's an opportunity to consider a specific piece of syntax we've seen
parentheses we've seen quotes all of which are necessary when passing inputs to a
function but this equal sign here that's in between input on the right and name
on the left is actually important and it's technically not an equal sign per se
it doesn't mean equality as much as it means assignment so in python and many
programming languages a single equal sign is the assignment operator and what
that means specifically is that you want to assign from right to left whatever
the user's input is so the equal sign copies from the right to the left whatever
the return value of the function on the right is so again the input function
clearly gets input from the user that's why i was able to type my name or carters
but it also sort of behind the scenes hands that value that return value back to
me the programmer and if i use an equal sign and a variable no matter what i call
it i can store that input in that variable so as to reuse it later so now sitting
in the computer's memory somewhere is a container containing david quote-unquote
or carter quote-unquote or whatever the human has typed in but here it's easy to
make a mistake suppose i decide to try to print that name and so i i kind of on
a hunch type in this hello comma name just kind of plugging in the name of the
variable well let me go ahead here and run python of hello.pi and hit enter
that's going to prompt me for my name and let me type in my name david but i
haven't hit enter yet and perhaps via the
chat what's going to happen here when i now hit enter i'm hoping it says hello
david i'd be okay if it says hello world but i don't want it to say what it's
actually gonna say and yep what we're seeing in the chat is well it's probably
gonna say literally hello comma name so that's not quite right so we need another
way of printing out the value inside of that variable rather than just this word
name well let me try this in a couple of different ways let me try this as
follows let me go ahead and maybe undo this because i've gotten pretty good
already at saying hello so let's let you know let's draw that line in the sand
and just say all right let's at least get hello comma out the door let's now
print name and just on a hunch i'm going to try this i'm going to use print again
because you can use these functions as many times as you need and i'm going to
pass to the name to the print function the variable called name but notice i'm
being a little clever now i'm not putting it in double quotes because we've seen
already that double quotes means literally print out n-a-m-e i'm getting rid of
the quotes this time in hopes that now by passing the variable called name to
the function called print it will in fact go about printing the contents of that
variable that is its so-called value all right let's go ahead and do this here
python of hello.pi enter what's your name david and now crossing my finger still
i see hello comma david all right so it's not the best program i'm kind of
cutting some corners here so to speak i'm saying hello david on two separate
lines so it's not as elegant it's not as pretty it's not as grammatically
appropriate in english as just saying it all in one breath on one line but at
least i've solved the problem just not very well yet but let me take a step back
now and perhaps introduce a couple of other concepts with which we should be
familiar which is as our programs get longer and they're no longer just one line
or two or even three eventually our programs are going to become dozens of lines
maybe even hundreds of lines long let's set the stage for success moving forward
it turns out that python and a lot of programming languages also support
something called comments comments are notes to yourself in your code and you
include comments by way of a special symbol in python it's going to be the hash
symbol typically and that allows you to write the equivalent of a note to
yourself but in a way that's not going to break your code the computer actually
ignores your comments it's just there for you it's just there for your teacher
it's just there for your colleague with whom you're sharing ultimately that code
so if i go back to vs code here and i just want to add some comments to this
program to explain to my teacher to myself to my colleagues what this program is
doing well let's go ahead and do that i'm going to go at the very top of my
program and on line one now i'm going to move that original line of code down a
bit i'm going to add a hash and i'm going to say something like this ask user for
their name now i don't have to use that language i don't have to use that that
text i could use any human language whatsoever it doesn't have to be english but
i'm going to now below that just say something like this say hello to user and
you'll notice that vs code by default is kind of graying out my comments they're
no longer blue there's no red there's no color in them and that's just because
they're notes to myself and the computer ultimately is going to ignore them but
what we have now is two comments ask user for their name and then a second
comment say hello to user and i've just kind of commented each chunk of code like
each line or line's plural of code that are doing something noteworthy why well
tomorrow morning when i wake up having you know uh slept for quite some time
forgotten what it is i did the previous day it's convenient with comments to just
see in english or your own human language what it is this program is doing so
that you don't have to read the code itself and better yet if there's maybe a
mistake down the road you can read what your intention was and then you can look
at the code and figure out if your code's now doing what you intended so this
isn't really necessary for a program this small it's pretty obvious with just one
or two or three lines what the program's doing it's just as fast to read the code
than the comments but getting into this habit is generally a good thing to
comment your code every one or few lines so as to remind yourself and others what
it is your intent and your code is doing what's nice about comments too is this
comments can also serve to be sort of a to-do list for yourself there's this
notion in uh programming of pseudocode pseudocode isn't a formal thing it's not
one specific language it's just using english or your own human language to
express your thoughts succinctly methodically algorithmically so to speak but
pseudocode therefore because it's not python and it's not necessarily english it
just kind of allows you to outline your program even in advance so for instance
if i wasn't sure today how i wanted to go about writing this program but i did
know what i want to do i could have started today by just writing this in
hello.pi no code i could have written just a couple of comments to myself step
one ask user for their name step two say hello to user then once i've outlined my
program in pseudocode then i can go in there and say all right how do i ask the
user for their name well i can do input quote unquote what's your name question
mark and then on the left here i can maybe put a variable and assign it to that
okay how do i say hello to the user well i know i can use print to say things on
the screen let me say hello comma and let me okay let me now print the person's
name so again pseudocode is a nice way of structuring your to-do list especially
if you have no idea how to write the code because it breaks a bigger program down
into small bite-size tasks all right let me pause here to see if there are now
any questions on comments pseudo code return values or variables any questions
we can clear up here yeah my question is does the function input work for any
type of information or only for words yeah really good question so according to
its documentation and we'll look more at formal documentation soon input is going
to expect what's called a string that is a sequence of text be it in english or
any other human language but it's indeed going to be expecting text with which to
prompt the user a good question how about another question from the group if we
could i wanted to ask how i make a several m comments oh how do you do many
lines of comments if i'm hearing you correctly sure uh you would just keep doing
them like this you just prefix each of the lines with a hash symbol like i'm
doing here there is another technique for doing multi-line comments in python
that actually tend to have special meaning you can do three double quotes like
this and then anything in between here is a comment that's another technique or
you can use single quotes as well but more on those i think another time all
right well if you don't mind let me forge ahead here and see how we might improve
this program further and also introduce a few other features that we might want
to take into account over time so it turns out that we can certainly improve on
this program because it's a little disappointing that i'm cutting this corner and
saying hello comma and then on a new line printing out name like we can do better
and most programs you use on your phone or your laptop certainly keep text
together when people want so how can we go about doing that well there's a few
different ways and in fact the goal here is not so much to solve this one problem
but to demonstrate and emphasize that in programming python and other languages
there's so many ways sometimes to solve the same problem and here's one way to
solve this problem let me go in here and let me go ahead now and say hello comma
and let me just add to the end of that the user's name so i'm using plus in kind
of an interesting way this is not addition per se i'm not adding numbers
obviously but i do kind of want to add the person's name to the string of text
hello comma well let me go now down to my terminal window and run python if
hello.pi again enter what's your name i'm going to type in david enter okay it's
better it's better but there's a minor bug albeit aesthetic here there's missing
space but let's just use some intuition here well if i'm missing the space after
the comma why don't i go ahead and just add it manually here let me now rerun the
program python of hello.pi enter david enter and there we go now we have
something that looks a little prettier in terms of english grammar hello comma
space david and now if we rewind you might have noticed before or wondered why i
had this seemingly extra space after my question mark namely here there's a space
after the question mark before the double quote and that was just for aesthetics
too i wanted to move the user's cursor one space to the right so that when i type
their name or they type their name it's not immediately next to that same
question mark there but there's other ways we can do this it turns out that some
functions print among them actually take multiple arguments and it turns out that
if you separate the inputs to a function the so-called arguments to a function
with a comma you can pass in not just one but two three four five onward so let
me go ahead and pass in not just hello comma space
but that followed by name and this is a little confusing potentially at first
glance because now i've got two commas but it's important to note that the first
comma is inside of my quotation marks which is simply an english grammatical
thing the second comma here is outside of the quote but between what are now two
separate arguments to print the first argument is hello comma space the second
argument is the name variable itself so let's see how this looks python of hello
dot pi enter what's your name david enter okay i've kind of over corrected now
i've got two spaces for some reason well it turns out and this is subtle when you
pass multiple arguments to print it automatically inserts a space for you this
was not relevant earlier because i was passing in one big argument to print all
at once by using that plus operator this time i'm passing in 2 because of the
comma so if i don't want that extra space i don't need to pass in one myself i
can just do this and now notice if i run this program again python if hello.pi
type in my name david now it looks grammatically like i might want now which of
these approaches is better this approach uses a function print with two arguments
hello comma and the name variable the previous version recall technically used
one argument even though it looked a little curious it's one argument in the
sense that the computer just like mathematicians are going to do what's inside
of parentheses first so if inside of parentheses you have this string of text
hello comma and a space which i need to add back then you have a plus which means
not addition per se but concatenation to join the thing on the left and the thing
on the right this ultimately becomes the english phrase hello comma space david
and then what's being passed ultimately to the function is technically something
like this but it's doing it all dynamically it's not me typing in david as i i
secretly as i discretely did earlier it's figuring out dynamically what that
value is after concatenating hello with the value of name and then passing that
ultimately to print as the soul argument let me pause here to see if there's any
questions on numbers of arguments now to functions can we use a function many
times to solve a certain problem which we can encounter many times in our code
you can you can use a function many different times to solve some problem what
we'll soon see though is if you find yourself as the programmer solving a problem
the same way again and again and again it turns out you'll be able to make your
own function so that you don't have to keep reusing the basic ones that come with
the language i was curious about the comma and the plus sign so after plus sign
can we give just one variable and after form again we give multiple variables
like what is the difference a good question so in the context of strings and i
keep using that term string is a technical term in a programming language and
again it means a sequence of text a character a word a whole paragraph even so
the plus operator is not just used as we'll see for addition of numbers in python
like we do on paper pencil but it also is used for concatenation of strings on
the left and the right if you did want to combine not just two strings left and
right but a third and a fourth you can absolutely keep using plus plus plus plus
and chain them together just like in math eventually that's going to start to
look a little ugly i dare say especially if your line of code gets long so
there's better ways that we'll actually soon see and a good question as well well
let me come back to the code here in question and see if we can show you just a
couple of other ways to solve the same problem along the way emphasizing that
what we're technically talking about here yes are strings but there's even a
technical term for these strings in python it's just stir so to speak str for
short for string as you may know if you've programmed in other languages people
who invent programming languages like to be very succinct to the point so we tend
to use fairly short phrases to describe things not necessarily full words so
while you might say string technically in python what we're really talking about
these sequences of text are technically stirrers this is an actual type of data
in a program but we'll soon see that there's other types of data in programs as
well in fact let's see if we can't improve this in one other way i like the
progress we've made by keeping everything on the same line hello comma david all
on the same line what more though could we do in terms of solving this problem
well it turns out that we didn't have to give up entirely with using print twice
let me rewind a little bit and go back to that earlier version where i wasn't
really sure how to solve this problem so i was using print once to print out just
the hello in the space and the comma and then i use print again to call to print
name that strictly speaking wasn't bad but there was this visual side effect that
i just didn't like it just looked ugly to have these two lines of text separate
from one another but there's another way to fix this clearly it seems to be the
case that the print function is automatically outputting a blank line it's moving
the cursor automatically for me to the next line because that's why i'm seeing
hello on one line and david on the next and then my prompt the dollar sign on the
line below that so print seems to be presuming automatically that you want it to
move the cursor to the next line after you pass it some argument but you can
override that behavior again functions take arguments which influence their
behavior you just have to know what those arguments are and it turns out that if
we look at the documentation for python's print function we can actually look up
at this url here docs.python.org is where all of python's official documentation
lies if i poke around i can find my way to more specifically this url here where
i can find all of the available functions in python that and the documentation
therefore and if i go a little more precisely i can even find specific
documentation for the print function itself and rather than pull that up in a
browser i'm going to go ahead and highlight just one line from that same url
which is this and this is easily the most cryptic thing we've seen yet but this
is the official documentation for the print function and one of the best things
you can do when learning a programming language is honestly learn to read the
documentation because truly all of the answers to your questions will in some way
be there even though admittedly it's not always obvious and i will say too
python's documentation isn't necessarily the easiest thing especially for a first
time or novice programmer it too just takes practice so try not to get
overwhelmed if you're not sure what you're looking at but let me walk you through
this example this again is a line of text from python's official documentation
for the print function what this indicates as follows is this the name of this
function is of course print then there's a parenthesis over here and another
closed parenthesis way over there everything inside of those parentheses are the
arguments the potential arguments to the function however when we're looking at
these arguments in the documentation like this there's technically a different
term that we would use these are technically the parameters to the function so
when you're talking about what you can pass to a function and what those inputs
are called those are parameters when you actually use the function and pass in
values inside of those parentheses those inputs those values are arguments so
we're talking about the exact same thing parameters and arguments are effectively
the same thing but the terms you use from looking at the problem from different
directions when we're looking at what the function can take versus what you're
actually passing into the function so what does this imply well this syntax is
pretty cryptic but at the moment just know that an asterisk a star and then the
word objects means that the print function can take any number of objects you can
pass in zero strings of text one string like i did two strings like i did or
technically infinitely many if you you really want though that code's not going
to look very good after that we see a comma then we see another parameter here
called sep short for separator in english and notice the equal sign and the
single quote space single quote so quote unquote space i don't know what that is
yet but i i think we've seen a hint about it let's focus though for a moment on
this the print function takes another parameter called end and the default value
of that parameter is apparently based on this equal sign and these quotes
backslash n and what is backslash n if you'd like to chime in the chat anyone
who's programmed before has probably seen this though if you've never programmed
before this might look quite cryptic backslash n means new line and it's a way
textually of indicating if and when you want the computer effectively to move the
cursor to the next line create a new line of text and so technically if we read
into the documentation we'll see more detail on this the fact that there's a
parameter called end and the documentation for the print function just means that
by default this print function is going to end every line with backslash n you
don't literally see backslash n you see a new line you see the cursor moving to
the next line now by that logic let's move backwards sep for separator the
default value of separator is apparently
a single blank space well where have we seen that well recall in an earlier
example when i passed in not just one but two arguments to the print function
recall that they magically had a space between them in fact they had that space
plus my own space and that's why i deleted my space because at that point it was
extra so this just means that when you pass multiple arguments to print by
default they're going to be separated by a single space by default when you pass
arguments to print it's the whole thing is going to be ended with a new line now
just by knowing this and let me literally wave my hand at the rest of the
documentation for another day there's more things that print can do but we're
going to focus just on sep and on end let's see if we can't leverage this now to
solve that original problem the original problem was this i don't like how hello
comma david is on two different lines well that's happening again because print
is automatically printing out a new line so let's tell it not to do that let's
tell it by passing a second argument to the first use of print to say end equals
quote unquote not backslash n which is the default automatically let's make it
quote unquote nothing else let's override the default value so there is no new
line there's literally nothing there and let's see what happens let me now go
down to my terminal window and clear it and i'm going to run python of hello.pi
enter i'm going to type in my name david and i think now everything's going to
stay on the same line because and it did this line here 5 is going to print out
hello comma space but then nothing at the end of it because i changed it to be
quote unquote the second line is going to print the name david or whatever the
human's name is and it will move the cursor to the next line because i didn't
override the value of end there just to see this more explicitly if you do
something cryptic like well i have no idea what's going on let me just put in
temporarily three question marks here we'll see the results of this too let me go
back down to my terminal window run python of hello.pi what's your name david and
now you see literally really ugly output but you see literally what's going on
hello comma space then three question marks end that print statement and then
you see dav id so not a good outcome but it demonstrates just how much control we
have here too and let me rewind further recall that in our other version of this
when i passed in hello comma and name they were separated by a single space so
python of hello.pi david enter that just worked well what if we override the
value of sep for separator instead of being one space we could say something like
uh question mark question mark question mark just to wrap our minds around what's
going on there let me now do python of hello.pi david enter and you see two
these two inputs hello comma and the name are now separated in an ugly way by
three question marks because i've overridden the default behavior of sep and even
though the documentation uses single quotes i've been in the habit of using
double quotes in python you can use either strictly speaking it doesn't matter
but you should be consistent and i generally always use double quotes python's
documentation though always uses single quotes questions now on these types of
parameters and allow me to propose that we give these an official name up until
now when we've been passing values to print those are called positional
parameters positional in the sense that the first thing you pass to print gets
printed first the second thing you pass to print after a comma gets printed
second and so forth but there's also these things we've now seen called named
parameters named scp separator or end end for the line ending those are named
parameters because one they're optional and you can pass them in at the end of
your print statement but you can also call use them by name this may be a weird
question but i was wondering uh what if someone wants to like add actually quote
quotation marks within the quotation marks yeah i like how you think this is what
we would call a corner case right just when we've made right this is this is all
sounding great at least as programming goes but wait a minute what if you want to
print a quote that's a really good question well let's see if we can't figure
this out suppose that i want to print out not just the user's name let me
simplify this further let me go ahead and get rid of a lot of this and let me
just say something like hello um maybe i'm being a little sarcastic here hello
friend you know in that kind of tone well this is not going to work actually
because you were trying to use quotes to be like friend in finger quotes but
you're also trying to end the sentence and if i try running this let's do this
python if hello dot pi you'll see that this is just invalid syntax perhaps you
forgot a comma and this is actually a bit annoying sometimes the error messages
you see are misleading like the computer the language doesn't really know what's
going on so it gives its best guess but it's not necessarily correct but i can
solve this problem in a couple of ways i can do this i can change my outermost
quotes to single quotes because recall a moment again i said you could use double
quotes or single quotes so long as you're consistent so that's fine if you use
single quotes on the outside you can then use double quotes on the inside and
you'll see them literally so for instance if i run python if hello.pi there we go
hello friend but there's another way if you insist on using double quotes as you
might want to just to be consistent you can also use that backslash character
again we saw the backslash n a moment ago and that meant we don't want a literal
n to be in the output we wanted a new line so the backslash actually represents
what's called an escape character an escape character is one that you can't just
type necessarily once on your keyboard you need to express it with multiple
characters so i can actually put backslashes in front of these inner double
quotes so that the computer realizes oh wait a minute those aren't literal those
aren't quotes that finish or start the thought they're literal quotes so now let
me go back to my terminal window run python of hello.pi enter and now it's
working as well so escaping is a general technique that allows us to do that too
and if i may let me rewind now on these examples and go back to where we left off
with my code i'm just undoing all of that because i want to get back to the point
ultimately of specifying now a final way of solving this problem well it turns
out that we have yet another way we can solve this problem which is perhaps the
most frequently done now or at least the most elegant when it comes to setting us
up for longer and longer uses of strings you can use a relatively new feature of
python that allows you to do this you can literally put not the name of the
variable like that in your string because we already saw this is wrong right if
you do this you will literally see hello comma name but what if i do this what if
i put curly braces or curly brackets around the variable's name notice vs code is
actually very subtly changing the color of it so vs code knows something
interesting is going on here let me run this program but i'm not done yet python
of hello.pi enter david enter okay obviously not what i want but i need to tell
python that this is a special string this is what we're going to call a format
string or an f string a relatively new feature of python in the past few years
that tells python to actually format stuff in the string in a special way and the
symbol via which you do this is a little weird but this is what the world shows
if you put a f at the beginning of the string right before the first quote mark
that's a clue to python that oh this is a special string let me format this in a
special way for you let me now rerun the program pythonhello.pi enter david enter
and now we see the goal this whole time hello comma david we don't start with
this way because i think if we did this the first way you'd be like why are we
doing this what are all these magical symbols but this is just yet another way to
solve the same problem but let me propose that we consider now yet other things
we can do with strings and it turns out that even as we've been doing some
relatively simple operations here we've generally been trusting that the user's
going to cooperate and that is to say that they're going to actually type in what
we want them to type now just because they type a string though doesn't mean it's
going to look the way we want you and i honestly as humans are actually in the
habit on websites and apps of like accidentally hitting the space bar a lot
either at the beginning of our input or at the end maybe because the space bar
tends to be so big it's pretty common to get accidental spaces before or after
some user's input you and i are definitely in the habit of not necessarily
capitalizing words like we should if we're sending text messages we're probably
being a little quick and just sending everything in lower case for instance if
that's your style if your phone's not fixing it for you maybe in a formal letter
you would capitalize things properly but you and i as humans can't really be
trusted to type things in a nice way necessarily when using some piece of
software be it an app or website or something else but it turns out that strings
themselves come with a lot of built-in functionality and you can see all of that
in python's own documentation here the string data
type that we've been talking about comes with a lot of functionality built in
that means that we can manipulate the user's input to do more than just join it
with something else like hello we can actually clean it up or reformat it in a
way that hopefully looks a little better for us so let me go back to my code here
and let me just demonstrate what might happen if the user doesn't cooperate if i
go ahead here and run python of hello.pi enter let me just sloppily hit the
spacebar a few too many times why i just wasn't paying attention and i'm going to
type in my name david and i don't know i hit the spacebar a couple more times
like it's kind of a mess it's all lowercase that's not going to necessarily look
grammatically right it's got spaces here and here the program is going to print
exactly that and that looks really bad at least if we're prioritizing aesthetics
and grammar like why are there so many spaces after the comma this is not a very
nice way to greet your users but we can clean this up it turns out that built
into strings which again is this data type so to speak this type of data in
python is the ability to actually do things to that string so let me do this i
can actually go ahead and do something like this uh name equals name dot strip
and what does this do remove white space from string and what do i mean by this
well on the right hand side notice i've written the variable name called name
i've then used a period or a dot and then i seem to be doing what's a function
right anytime we've seen this function thus far we see it's the the function's
name print or input then we see a parenthesis then another parenthesis and that's
exactly what i see here but i'm using this function a little differently
technically this function is in this context called a method and what do i mean
by that well if name is a string aka stir well it turns out according to the
documentation there's a lot of functions that come with strings in python and you
can access that functionality by using the name of a string like literally name
here then a period then the name of the function and then an open parenthesis and
a closed parenthesis maybe some arguments inside of those parentheses but in this
case it doesn't need any arguments i just want to strip the space from the left
and the space from the right of the user's input but that's not enough i want to
remember that i've stripped off that white space on the left and the right so i'm
going to use the equal sign again here and notice that just as before this
doesn't mean equality this means assignment from right to left so when this line
of code here name.strip returns to me aka a return value it will return the
same thing that the user typed in but with no more white space to the left or to
the white to the right so then the equal sign assignment is going to copy that
value from the right to the left thereby updating the value inside of my name
variable so you can not only assign values to variables you can absolutely change
the value of variables by just using the assignment operator the equal sign again
and again and again and it will just keep copying from right to left whatever the
new value should be so now if i rerun this program python of hello.pi enter i
have david let's do it again space spacebase space space dav id and all lowercase
space space enter it's better it hasn't fixed my capitalization so i'm still
being a little sloppy with the first d but it has stripped off all of that extra
space super minor detail right like this isn't all that exciting but it just
speaks to the power of what you can do with just a single line of code now what
else can i do here well i could capitalize the user's input let me go ahead and
try this it turns out that i could also do this name dot capitalize so let me go
ahead and capitalize uh user's name and again i'm making comments and there's no
one right way to write the comments i'm just using some short english phrases
here to remind myself of what i'm doing what's now going on here well let me go
ahead and run python if hello.pi enter space spacebase spacebase david space
space enter okay now it's looking prettier right no matter how the user typed in
their name even a little sloppily i'm now fixing that but let's let's try
something i'm getting a little curious here how about this uh spacebase face-to-
face space david spacemailin i'll use my last name now enter okay so ironically
capitalize is not really capitalizing everything we want it's clearly
capitalizing what just the very first letter so it turns out that again there's
other functions in python that come with strings and if we poke around the
documentation scrolling through a url like that i bet we'll find another solution
one of which is actually this let's actually change this to title there's yet
another function that come with strings called title that do title based
capitalization just like a book or a person's name capitalizing the first letter
of each word and this is just going to do a little more work for us so let's go
ahead and run this and as an aside i'm kind of tired now at this point of typing
python python python all the time it turns out that when using a command line
interface like this you can actually go back through all of your old commands
what i just did a moment ago is i hit the up arrow that immediately goes back
through my history of all of the commands i've ever typed so this is just a
faster way now for rep repeat myself than typing everything manually let me go
ahead and hit enter space based basically space dav id mailing space space all
lower case enter now it's it's looking better now i've capitalized things and
cleaned things up but what about my code i've got like eight lines of code now
four of which are comments four of which are actual code do i really need this
much well not necessarily watch what i can also do in python let me not bother
capitalizing the user's name separately let me say this and capitalize
capitalize user's name i can chain these functions together i can add title to
the end of this and now what's happening well again with a line of code like this
you first focus on what's to the right of the equal sign then we'll get to the
left of the equal sign what's on the right of the equal sign this line here well
what does this mean get the value of the name variable like david space m-a-l-a-n
then strip off the white space on the left and the right that is going to return
a value it's going to return david space m-a-l-a-n without any white space to the
left or right what do you want to do with that return value you want python to
title case it that is go through every word in that resulting string and fix the
first letter of the first word the first letter of the second word and so forth
and then now we can finish our thought copy the whole thing from right to left
into that same name variable and you know what i can take this even one step
further why don't we go ahead and do this if we want let me get rid of all that
and let me just do strip and title all on that first line and now we've gone
from like eight lines of code to four it's a lot tighter it's a lot neater and
even though reasonable people might disagree it's arguably better because it's
just easier to read fewer lines of code fewer opportunities for mistakes it just
allows me to move on with my next problem to solve all right let me pause here
and see if there's any questions on these methods a method is a function that's
built in to a type of value like these functions are or on f strings which we saw
a moment ago yes hi thanks david um so is there a way to remove the spaces
between the spaces that i might have added a short answer no if you read the
documentation at that same url earlier you'll see that strip removes from the
left and the right but not in between in fact there's two other functions that
come with strings one's called l-strip the other is called r strip that allow you
to do one or the other if we want to start getting rid of space in the middle
we're gonna have to do a different trick all together how many functions can we
combine like this dot strip dot title you have combined so how many we can
combine yeah a really good question technically as many as you want but at some
point your code is going to start to look really really bad right because the
line of code is going to get really really long it's eventually going to maybe
wrap around again and again so at some point you just kind of say like uh that's
too many and you start breaking it up into multiple lines like i did maybe
reassigning the value to the variable as needed and this is actually a good
question if i can pivot off your question i mean what do people think if we could
go ahead and put everyone's hands down for a moment let me ask this is the way
i've done this now with strip and title and input all in the same line better
than my previous approach in zoom you can use the yes icon or the no icon if you
think this version is better say yes if you think this previous version was
better for instance this one here where we had everything broken out say no and
then we'll see why in just a moment i proposed earlier that reasonable people
can disagree and that's absolutely the case doing it one way or the other isn't
necessarily best at least if you can justify it let me go back to the most recent
version here all right so we're seeing a lot of yeses and a lot of no's why don't
we go ahead and call on one of the yeses if we could someone who's voting yes why
do you think the current version of this code is
indeed better than the previous longer version of the code i think it's more
readable so i can say hey this is the name from this is the name variable it
gets some input and then remove the space and give it a title and there you go
you have a hello name yeah i think that's pretty reasonable it's very readable at
least if you're in the habit as you are in english of reading left to right it
just kind of flows very naturally as a result the lines is not really that long
it's certainly fitting nicely onto the screen so i think that's a good argument
how about a counterpoint though someone who voted no if we could call on someone
who thinks this is worse because it's not reliable at all i seems like uh a
it's a very long line so i think it's better to separate yeah i i think that's
persuasive too right it's getting a little longer and even though my sentence
here what's your name is relatively short you could imagine that this could get
even uglier quickly if i were asking a longer question of the user that's going
to make this line of code even longer and therefore less readable it might be
less obvious to me or my colleagues that i am calling strip or that i am calling
title it might be kind of a unexpected surprise so i think that's reasonable too
in short there is no right answer here and in fact part of the process of getting
better at programming is getting your own sense of style or working for a company
where they might prescribe which way is better than the other because they just
want everyone doing the same thing even though reasonable people might uh
disagree ultimately though so long as you have what's a pretty good argument in
favor of one way or the other like ultimately that's what's important if you're
just doing things because you don't really know which one is better that's not
great but if if and when you start to acquire opinions and if your boss if your
teacher if your colleague your friend can challenge you and say wait why did you
do it like this they might not agree with you but at least have an answer and
that should be sufficiently persuasive in general now strings come with a whole
bunch of other methods as well among which is one called split which can as the
name suggests split a string into multiple smaller sub strings so to speak for
instance if the human here is in the habit of typing in their first name then a
space and then their last name and you want to go ahead and greet them only by
first name well we could actually leverage that single space between the first
name and last name and split that string into two smaller sub strings how can we
do this well let me go ahead and in between these lines proactively comment that
we're about to split user's name into first name and last name and then let's go
ahead and take that name variable which currently contains something like
presumably david spacemailin and let me go ahead and call split and pass in as
the argument to split a single white space thereby indicating that i indeed want
to split on that character now it turns out split's going to return a sequence of
values ideally a first name and then a last name and we can actually in python
assign both of those values from that sequence at once to some variables for
instance first comma last equals and that's going to have the effect from right
to left of putting the first such value in the first variable the second such
value in the second variable so now on my last line of code i can go in and say
hello not to the full name something like david malen i can just say hello comma
first all right let's go ahead and clear my terminal window run python of hello
dot pi and hit enter i won't bother with any leading white space this time but
let me go ahead and type in david space malin and crossing my fingers as usual
hello david is what we now see all right so we've seen so much so many examples
thus far involving strings but certainly programs and programming languages can
manipulate other types of data as well let's go ahead and transition then to
another very common type of data in python in programming more generally namely
integers otherwise known in python is int int so just as stir str is short for
string so is int in python short for integer well what's an integer well just
like in math it's a number like negative 2 negative 1 0 1 2 and all the way
toward negative infinity all the way toward positive infinity but there's no
decimal point in an integer it's just a number like negative two negative one
zero one and two onward that's an int of course in the world of mathematics
there's lots of symbols that we use and we've seen plus before although we used
it for a different purpose but python supports these symbols and more and python
allows you to add numbers together plus subtract numbers uh multiply numbers
divide numbers and the only one here that might look a little strange to people
or unfamiliar is this percent sign but it doesn't mean percent in this context if
you use a single percent sign in a python program that's actually the so-called
modulo operator the operator that allows you to take the remainder after dividing
one number by another so we'll see examples of that before long but the first
four of these are perhaps quite quite familiar well it turns out that in python
you cannot necessarily you don't necessarily have to keep writing code in a file
like hello.pi and then running it in a terminal window one of the features that
many people like about python is that it supports this so-called interactive mode
like you can start writing python code and immediately execute each of those
lines interactively if especially if you don't care about saving all of your
lines of code you just want to execute code and get back some answers so for
instance let me go back to vs code here and let me close hello.pi and let me
click on the little triangle over here in my terminal window just to make it much
bigger just temporarily for a moment so i'm not creating any dot pi file now i'm
just going to run python by itself at my prompt and you'll see when i do this i
get some cryptic looking output and the date and time at which the program was
last updated and so forth but i ultimately get three triple uh brackets like
this this is the interactive mode for python so i'm running the python
interpreter and anytime i type a comma a line of code in the interpreter it's
going to execute it immediately i don't have to keep running python again and
again it's as though in the human world if you were standing next to a human who
speaks some other language and you're just having a conversation with them back
and forth it's all happening the translation immediately so what might i do in
interactive mode well i could do something like one plus one enter that's
actually code right you might not think of it as code but if you know a bit of
arithmetic and you know numbers and you know plus that's valid python code and
you can use python really as a fancy calculator but i could do other things too
if i want to print to myself hello comma world i can also print out that line of
code there too hello world so it's interactive in the sense that the moment you
execute a line of code boom you see the result we're generally not going to do
that because at least when teaching the language we tend to want to do things
incrementally and we want you to be able to see where it is we came from and we
want to be able to try things again and again especially if we make mistakes but
know that this is indeed a feature of python this so-called interactive mode but
let's focus for a moment now not just on that interactivity but really on the
fact that python apparently supports integers and mathematics and some of those
basic operations and let's see if we can't make maybe our our own little
calculator so let me go ahead and open up vs code again and i'm going to shrink
down my terminal window and i'm going to create a new file called calculator.pi
so to do that recall i can type code down here and the name of the file i want to
create dot pi enter that gives me a new tab up top so i have already closed
hello.pi i'm now in calculator.pi and let's just make a simple calculator that
does some addition for me but i'm going to do it in a file so that we can iterate
on this and make changes for better for worse over time let me go ahead and first
declare a couple variables i'm going to do the mathematical thing of calling my
first variable x my second variable y and then i'm going to give myself a third
variable z equals x plus y and then i'm going to go ahead and print out z now
this program admittedly not very exciting or interesting in fact it's a little
less interesting than printing stuff on the screen like before with strings but
we'll build on this and see what other features exist in python that we can
leverage so hopefully if python knows its math as well as i do when i run python
of calculator.pi i should see hopefully that 1 plus 2 equals indeed 3. all right
so not that surprising and not that interesting and honestly this isn't the most
useful program because it's always going to calculate 1 plus 2 equals 3. let's at
least make this program say a little more interactive right we already know from
previous examples how we can get input from the user let's bring back that input
function and let's do this let me go ahead now and at the top of my code let's
change x to not be the number one always let's change it to be whatever the
return value is of asking the user for x and i can use any english or human
language i want here i'm going to say what's x just
like i asked before what's your name and i'm going to do the same thing for y
i'm going to use input again but this time change the question to be what's y
all right at this point i think i'm going to leave the rest of the code the same
z equals x plus y and then print z but what's nice now is that i think i have a
nice interactive calculator right now it's not going to do oneplus 2 all the time
it's going to do whatever the user types plus whatever the user types so let's
try this let me go ahead and run the program alright let's do it 1 is going to be
x 2 is going to be y and of course everyone in agreement 1 plus 2 equals 3 huh
what's going on there either your math class misled you or i have misled you why
don't we call on someone here to see if you can't help us reason through what the
bug is what's the mistake uh anjali if i'm saying it right i think the issue is
is that it's concatenating strings because you use the plus operator instead of
adding perfect so perfect intuition we've seen that plus is used a little
differently in the context of strings because it concatenates that as it joins
the two strings and that seems to indeed be what's happening here even though the
user typed a number but the interesting thing here is that when you get user
input because they're using a keyboard on their mac or pc or their phone it is
always going to be text it might look like a number but by default it's coming
from the keyboard as a string that is as text and so how do we go about resolving
this if ultimately we don't want to treat those inputs as strings we want to
treat them as actual numbers well we need another function and it turns out in
python that you can convert sometimes from one type of data to another type of
data for instance from string to int by doing something like this let me go back
into my code and let me change x before adding it to y to be whatever the integer
version of x is plus whatever the integer version of y is so it turns out that
int is not only a type of data in python it's also a function and it's a function
that if you pass in an input like a string so long as that string looks like a
number like one or like two it will convert it to an actual number that you can
perform mathematics on instead so if i now go back to my terminal window and run
python and let me show you another trick calculator is kind of a long word it's a
little tedious to type notice what i can do in my terminal window in a command
line interface in general if i start typing cal for calculator i can actually hit
tab to finish my thought so auto complete is possible in a terminal window like
this type the first letter or few letters and then boom with tab it'll finish
your thought for you or you can go back in your history like i did with the up
and down arrows let me go ahead and execute this what's x1 what's x2 and there we
go now we have a general purpose calculator that's going to support not just
addition of one and two but now any two integers that the user types and let me
now improve this right we've seen how we can make improvements to code and i i
don't know if it's gonna necessarily be better but let's try this do i really
need the z variable it's worth noting that i'm creating a variable called c z
and then i'm immediately using it on the next line of code now that's not that
compelling because if you're creating a variable and then immediately using it
but never again using it did you really need to take the time to introduce
another symbol and another variable just to use it once and only once well maybe
not maybe we don't really need z in this way maybe i should go and do something
like this maybe i should get rid of z here maybe i should change this to be int
up here change this to be int up here doing something that's pretty interesting
now even though it's a bit of new syntax notice that you can nest functions so to
speak you can put one function call that is the use of a function inside of the
use of another function so that the return value of the inner function becomes
the argument to or the input to the outer function just like in math if you have
parentheses parentheses parentheses your teacher probably taught you to focus on
what's inside the innermost parentheses first and then work your way out same
thing with programming that's what python is going to do it's going to look at
what's inside of the parentheses first it's going to get the answer and then it's
going to pass the return value to the outermost function so what happens on line
1 now is that the input function gets called first then the result of that quote-
unquote 1 becomes the input to the int function and same on line 2. the output of
what's y becomes the input to this int function and now there is no z i could
just do print x plus y and because i've taken the time to convert each of those
strings to an integer i think we're okay so let me try this python of
calculator.pi enter 1 and 2 and we're still getting 3. not 12 or not 12 1 2
we're indeed getting 3 and we've additionally gotten rid of the variable because
we didn't necessarily need it it seems especially if only using it once well here
too let me put everyone's hands down for just a moment and let me ask as before
this version now which uses int around the invocations of input and does not use
z is this better than the previous version if you want to vote yes go ahead or if
you prefer the old way vote no the old way i'll undo all of this as we vote
instead looked like this all right and let me go back to now the newest version
let's take a hand of the yeses someone who thinks this latest version is better
i think this way is better because it allows us to immediately see what the x and
y variables are with integers and so we know what to expect from them and also
the print argument is more intuitive we avoid too much clutter in the codes i
think those are all good reasons it's nice and succinct the lines of code are not
very long uh i don't need to know what z is because it doesn't exist it just c
print x plus y i like that but someone who prefers the older way where we did
have z and we more explicitly passed individual variables to the in function yeah
hi uh i think but the earlier version is better because when i mean if user input
something else other than let's say i mean let's say they type one and two like
so will be it will be easier to debug this version or the this version here or
the old version okay that's fair and in fact i'm i'm being very careful today as
best i can not to mess up i have thus far only inputted integers when i'm
expecting integers and rows actually pointing to something we'll come back to in
the coming weeks how do we actually handle errors what if the user doesn't type
in the number one or the number two or a number at all what if they type in a
word like cat c-a-t that's not a number and i bet i can't convert it to an
integer but for today i'm not going to focus on that i'm just going to hope that
the user cooperates but that's not going to be the case and so perhaps one way
would set us up for more success when it comes to handling those errors now for
today's purposes which is better i mean i like both and i think both of you made
very valid arguments and there too so long as you have a justification that feels
pretty reasonable i mean that's what ultimately matters but acquiring again a
sense of the trade-offs here well is this way better if so why or why not just
understanding what those trade-offs are but generally speaking prioritizing
readability is a very good thing making your code readable for someone else is a
very good thing and very good for you too so that when you wake up the next
morning or you come back the next week or the next year you too can read your own
code without having to waste time trying to remember what you did and simplicity
tends to be a good thing too keeping your code simple so is as you get more
comfortable with programming you might be tempted to try to like combine an
entire program into one long line for instance let me do right just that don't
technically speaking we don't really need x in a variable we don't really need y
in a variable we could also do this i could just get rid of x and y altogether i
could then now eliminate that and make it just one line of code okay so on some
sense you might be inclined to think wow that's really nice you made it one
simple line of code i would argue this actually isn't that simple now i think i'm
starting to nest too many things i have to think about print and int and input i
then have to notice that okay i've opened two parentheses i've closed two of them
there's a plus you're making me think too much and anytime you make me think
you're wasting time and any time you complicate the look of the code like this
you're just going to increase the probability of mistakes and tactical mistakes
or logical errors in your code so if all the things we've done this is the only
one that i would argue yes it's one line and it's nice and compact it's just not
readable enough i would shy away from doing this especially since two of those
function calls are getting input from the user but there too reasonable people
might disagree but that's the kind of like visceral reaction you should have
sometimes when code starts getting a little too complicated a little too clever
perhaps for its own good all right well it's not just integers we have access to
let me propose that we transition from integers to one more data type here namely
a float so again a string is a sequence of text an int is an integer like
negative
one zero and one a float is a number with a decimal point properly called a
floating point value and you can think of the floating point as being the decimal
that might be over here or over here with some number of digits to the left or
the right mathematically it's a real number a number that has a decimal point in
it so that's a third type of data that python supports right now our calculator
is somewhat naively assuming that the user is only going to type in integers but
if i want to support floating point values too i think i can just make a couple
of tweaks so i'm going to go back to vs code here and instead of just converting
the user's input x and y to integers on line 1 and 2 let's just make a simple
change let's actually convert it to a float on the first line and a float on the
second line here now i think if i go down to my terminal window and run python of
calculator.pi let's type in a number like 1.2 with a decimal point and 3.4 with
the decimal point and there we go we have 4.6 as the final answer so that
wouldn't have worked before if i was only expecting integers from the user but
now that i'm support expecting floating point values and accommodating it i can
actually now do floating point arithmetic as well but suppose that i don't really
want the final answer to be a floating point value like 4.6 i would be happy if
we just round to the nearest integer so i want to support the user typing in
floating point values with decimal points but at the end of the day i just want
to round the result to the nearest possible integer for instance well it turns
out that here too python comes with some functionality built in and in fact if we
return to this url from earlier wherein all of the python built-in functions are
listed there's one called round which does exactly as we would expect it takes as
input a number and then rounds it for us for instance to the nearest digit to the
nearest integer but if we look a little closer to that documentation as we can
here i'll provide an excerpt this is what the function looks like in the
documentation and recall that earlier we looked at the documentation for print
and this is similar in spirit that this shows us not just the name of the
function but its available parameters that is inputs that we can provide when
using this function but this is a little cryptic too just like prince was and it
adds some syntax so let's see the name of this function here is of course round
and it's first argument is a number notice this times there's no star there's no
star objects like there was for print the round function takes just one number as
its first argument period that's its positional parameter but notice this syntax
and this is a convention in programming or technology more generally generally
speaking when you see square brackets and documentation like this this means that
you're about to see something optional and so what this means is that if you want
to specify more precisely the number of digits that you want the round function
to round to you can specify it here by adding a comma and then that number so if
we read the documentation if you don't specify a number of digits you just
specify the number to round it rounds to the nearest integer but suppose you want
to round to the tenths place or the hundredths place that is one or two digits
after the decimal point you could additionally pass in comma one or comma two to
be more precise so that's what the documentation there is saying let's see if we
can't then translate this to some actual code for us so if i go back now to vs
code and i consider that i want to go ahead and round x and y i can do this in a
couple of ways i could do round x plus y but you know i'd actually kind of prefer
to break this now out into two lines i don't have to and reasonable people here
might disagree but i'd like to revert to a scenario where i'm printing z so that
i can just a little more clearly to myself to others say z equals the rounded
result of x plus y it's not necessarily the better way to do it but i'm a little
more comfortable with breaking out my thoughts one at a time especially if i want
to start commenting each of these chunks of code all right let me go down to my
terminal window now and run python of calculator.pi what's x let's do 1.2 again
then let's do 3.4 and now it was previously 4.6 but now it's been rounded up to
the nearest integer which of course is going to be 5. all right what if i wanted
to change this a little further what if i wanted to support maybe really big
numbers big numbers irrespective of rounding let's just do something like this
let me go ahead and run python i've calculated at pi again and let me just add
999 plus 1 and notice i don't have to type decimal points even though i'm
converting to float my program will just allow me to type decimal points but i
don't need to oblige the answer of course here it should be and is in fact 1000
whether or not we round so that's just arithmetic with integers here but in uh
the us we tend to format long numbers by putting commas uh after or before every
triple of digits other countries flip it and they use periods and commas instead
that's a system setting you can change that on your own mac or pc or device for
python or any language but for me i'm using the us approach here which is periods
for decimal points and commas for separators what if i wanted this to be
outputted as one comma zero zero zero just to make it a little more clear that
it's one thousand and not something like one hundred that's even more useful when
it's like one million one comma zero zero zero comma zero zero zero wouldn't it
be nice if we could automatically output those numbers as well well it turns out
that we can there is a way using python to actually specify that we want to
include commas like this and here we have an opportunity to bring back our old
friend the f string first let me do something that's not that productive first
let me do this let me print out the value of z but wait a minute i can't just say
quote unquote z because that's literally going to print z on the screen so let me
wrap it with those curly braces like i did before but that too was not enough i
literally needed to add an f at the beginning of my string to tell python that
this is an f string a format string that now is going to print out not very
interestingly just the value of z itself so that i'm going to great lengths just
to print z when really i could have just passed z as the sole argument but just
to ensure that i haven't broken it let's do this again 999 plus one enter okay
it's still a thousand so i didn't make anything worse but notice and this syntax
is unfortunately a bit cryptic notice that i can actually do this i can put a
colon after the z and i can put a comma thereafter this looks very cryptic
admittedly and even i have to constantly look things like this up in the
documentation to remember the syntax but here let me run it again python of
calculator.pi 999 1 and now notice that the number has been automatically
formatted for me if i were in a different country or locale i could absolutely
override this to use periods instead of commas or vice versa but in this case
here it's just happening for me automatically so there too we see a hint of what
it means to really format a string there's even more power more powerful
capabilities built into that all right let me pause here to see if there's any
questions now on floats on rounding or on this use of f strings yes so i have a
question so when using floats um is it like a cap to how many decimal points it
can have a really good question so floats yes and this is a problem we'll revisit
before long floats cannot represent numbers infinitely precisely in a nutshell
because computers only have so much memory they only have a finite amount of
memory you and i only have a finite amount of hardware inside of the computer so
at some point they're going to have to round right now i'm rounding automatically
effectively computers will eventually have to do that for us but we'll see that
as a fundamental problem before long allow me to turn back just for a few final
examples on float before we introduce a few final examples that allow us not just
to use functions but to make our own let me propose that we also try our hand at
a bit of division here let me propose that we modify this calculator now to still
take a couple of floats but let's now just do something a little simpler than a
little different from this just doing x divided by y and let me go ahead and get
rid of my format string and just keep it simple for now printing out z instead
and what are we going to see here well just some simple division so python of
calculator dot pi let's do something like 2 divided by 3 and of course i get
0.66666 and to ethan's question a moment ago it does seem to be finite it's not
rounding in a weird way here but i only seem to see so many digits that's a an
inevitability of using a float in this way by contrast just so you know integers
nowadays in python can be as big as you want them to be unlike other languages
there is no upper bound on how big an int can be now in python but there is a
bound on just how precise a floating point value can be all right now that i've
got some simple division working here let's go ahead and round this it would be
nice to round this really long number 0.666666 and so forth to maybe just two
decimal places we've seen how to do this with round though at least in its
documentation let's just round this not to the nearest int by passing in just x
divided by y which is one
argument once the math is done inside of the parentheses i don't want to pass in
just one argument i want to pass in two so that i can specify n digits number of
digits which recall was the second parameter for round let me go ahead and run
python of calculator to pi i'll do the same thing 2 and then 3 0.67 so here too
we see a way of rounding now not just to a nearest integer but to a nearest
number of digits but there's another way to do this here and in fact this evokes
our f f string example again let me go ahead and change this suppose that you
didn't remember the round function or for some reason you didn't want to use it
you instead want to just use a format string well let's go there let me do quote
unquote z but let me surround it with those curly braces let me add the f at the
beginning and again this is not interesting yet this is just going to print out z
but i'm adding a lot more complexity to turn it into an f string but notice i can
do something else after my variable name after the colon if this were going to be
a big integer i might want to use a comma like before to separate each triple of
numbers with commas but i don't i'm going to use a different sequence of
characters i'm going to say 0.2 f and this too is one of these very cryptic
things i have to constantly look up because i forget if i don't use it that often
so don't be intimidated if this looks especially weird but this is according to
the documentation the way you specify using an f-string how many digits you want
to print so let me run this version of the calculator type in 2 and then 3 we get
the exact same thing but again this is just consistent with my claim that in
programming we can so very often solve the same problem in multiple ways this is
just now the f string approach to that very same problem all right which one is
better it depends in this case they're pretty equivalent you could imagine though
it being useful to use a function sometimes so that you can pass in an argument
like n digits as that second argument or you can imagine just deciding in advance
that you want point two and then writing it like this let's transition now from
focusing on strings and on integers and on floats to focusing now on functions
themselves we began today by focusing on how you can use functions that come with
python but wouldn't it be nice if you could invent your own functions especially
if to our point earlier you find yourself solving the same kind of problem again
and again it's nice that python comes with the print function because it's really
useful to be able to print things on the screen but wouldn't it be nice if you
could print specific things on the screen by just calling your own function well
let me propose that we do this let me go back to vs code here and let me propose
that we go back to hello.pi i'm going to reopen hello.pi where we left it before
and i'm going to go ahead now and propose that we consider how we can start
improving this further by making our own function i have written so many programs
today that just say hello and each time i'm using print but wouldn't it have been
nice if from the beginning of today we could just call a function called hello
that just says hello for us now the authors of python years ago didn't think that
we need a special function just to say hello but i would like that to exist i'm
saying hello so many times i just want to be able to call a function hello so i'm
going to start from scratch here i'm going to delete all of my code from earlier
and i'm going to pretend for the moment that a function called hello exists and
i'm going to do just as i did before i'm going to get the user's name with the
input function asking what's your name question mark and now i'm going to call a
function hello and then i'm going to print out the user's name now i will admit
hello doesn't exist so bad things are about to happen but let's see what let me
go down to my terminal window let me run python of hello.pi i think the first
line is going to be okay because that worked before and indeed it's prompting me
for my name so let me type in david the second line of code is apparently calling
a function that looks like it's called hello because why is it a function it has
a parenthesis and a closed parenthesis immediately after it and that's what
every function we've used has looked like but python's not going to recognize
this one when i hit enter now i get a name error name hello is not defined did
you mean help i didn't although it's opportune that's what i need at this point
is some help but i am encountering this error because why the function just
doesn't exist so how do i make this function exist well i need to create it
myself using this keyword def def for define so here too just as stir is short
for string and into short for integer def is short for define if and when you
want to define create invent your own functions you can do so using now this
keyword in python so let me go back to my code here and let me propose that we
define this perhaps in this way at the very top of my file i'm going to first
take a moment to define a function called hello using def hello open parenthesis
close parenthesis colon what this means now is that python is going to treat
every line of code that i indent underneath this one as the meaning of this new
function hello so def is important as is the space i get to choose the name of
the function and i'm choosing to call it hello the parentheses with nothing
inside means that this function at the moment is not going to take any inputs no
arguments there too the colon means stay tuned for some indentation everything
that's indented beneath this line of code is going to be part of this function
it's going to be a super short function one line of code it's just going to print
out quote unquote hello but now on lines one and two i have invented my own
function hello notice these dots that have now magically appeared here this is
just a setting of my text editor vs code in this case that's just making super
explicit to me that i've hit the space bar four times or equivalently the tab key
once which is converted automatically to four spaces generally speaking i'm going
to need to make sure that all of my indented code lines up now so that python
knows that it's all part of the same thing but it's easy in this case because
it's just a single line but now thanks to lines one and two the function hello
will absolutely exist when i'm ready to use it on line six so let me go down to
my terminal window and run python of hello.pi enter here comes my name again and
now when i hit enter i now see hello david all right we've kind of regressed
though right this is not nearly as pretty as it once was i think we can probably
do better than this by improving things further why don't we consider though how
we might say parameterize the same function that is to say can we customize hello
to maybe take the user's name as input so that we can say not only hello but the
person's name all on one line all in one breath well i think we can do this let
me propose that we do this as follows let me go ahead and up in my code let me
inside of these parentheses let me come up with my own parameter name i have
complete choice here and i'm going to say that the name of my parameter will be
the word to why because i want my function to sound like the verb it represents
hello but who do you want to say hello to well i'm going to call my parameter for
this function 2 just because in english it kind of sounds nice to me hello to who
do you want to say hello to that's why i'm calling this parameter 2 instead of
something simpler like x or y or z all right well what do i want to do with the
word two well i can do a couple of different things we've seen like so many
different ways to implement hello let me just add a comma there for grammar sake
and then let me put the word to after that as the second argument to the function
hello there's other ways we can do this and we've seen so many but this one looks
a little clear to me i'll say what's going to happen next well i don't think i
need this extra print line here i think what i'm going to do is this i'm going to
go ahead here and print out not the person's name manually i'm going to sense
instead say hello parentheses name so what am i now doing on lines 1 and 2 i'm
defining my very own function called hello but this time that function has been
designed to take a parameter a single parameter as input and i'm using the value
of that parameter which i called 2 to plug into print so that i see not only
hello but also that person's name what am i doing on line 5. same as always i'm
just getting the user's name line six i'm not only calling hello i'm passing as
input the name variable as an argument so that that's what gets passed into
hello and what's happening here is essentially this even though the variable is
called name here when the function itself is called the computer assumes that
that same value is now called to so name is essentially copied to another
variable called to so that in the context of hello i can say hello to that
variable instead and we'll see in a moment what happens if we don't keep those uh
straight let me go ahead and run python if hello.pi enter what's your name and
now i'm crossing my fingers enter there we go we're back in business but now i
have my own custom function called hello that's allowing me to say hello to a
specific person and here's where now things can get really fancy what if you
wanted your hello function to say hello to someone specific but you know what if
you don't know who you want to say hello to you want to say hello to the whole
world you can give parameters default values we've seen that recall that with
print there was a default value for sep for the separator there was a default
value for end the line ending we can do that too and here's the syntax if you
want the value of this parameter by default if not provided by the programmer to
be equal to quote-unquote world you literally do that in the same line you're
defining the function and i'll admit it's starting to look more cryptic but i'm
still just defining a function called hello it takes a parameter called 2 but i'm
assigning it with the equal sign a default value of quote-unquote world just in
case the programmer doesn't call hello with an argument and we can see this here
let me change my code to use hello in two ways on line five i'm gonna very simply
call hello no arguments then on line six i'm going to get the name line seven i'm
going to call hello with an argument so you'll see hello now being used in two
ways let me go ahead and run pythonflo.pi i'll type in my name oh interesting
notice i already see hello world but that's expected because line five happens
before line six but once i type my name now the program is going to be a little
more polite and say hello to me personally so there too we see with relatively
simple but new syntax how you can implement functionality very similar in spirit
to what the print function gave us automatically now you have control over doing
that yourself but let me now make this point too one of the whole points of
defining your own functions is one just to avoid having to repeat yourself again
and again you don't have to actually keep reinventing the wheel and keep using
the print function again and again and again if you just want to say hello
wouldn't it be nice now if i could kind of move this code that i wrote for
defining the hello function and just to be dramatic i'm going to hit enter a
whole lot of times 50 lines down and put my definition of hello way further down
in this file why well just from the spirit of out of sight out of mind because if
i now rewind to the start of my program now you can sort of take for granted that
oh hello is a function y because it's there on line one and it has an open
parenthesis and a closed parenthesis which up until now has meant call this
function and then on line two we're getting a variable from the user by typing in
their name and then we're calling hello passing in that value well at this point
i can just take for granted that hello exists even if it's way down further in
the file or as we'll see in future weeks even if it's in a different file
altogether but there's a problem here and let me go ahead and run this version of
hello dot pi notice that as soon as i run the interpreter python of hello.pi i
see a name error name hello is not defined again did you mean help well again
fitting i do need some help here but i didn't mean to call the function help the
problem here though is that python is just taking me literally i have defined my
function hello all the way down here but i'm trying to use it way up here and
that's not allowed python's interpreter is going to take you literally and if you
use a function it must already exist by the time you are calling it so how do i
fix this well apparently i can't do that i have to define any functions i want at
the very top of my file but that too could get me into a bit of trouble
eventually because if i constantly have to define a function above where i want
to use it you're kind of writing code in reverse you're constantly writing
functions up here up here up here as opposed to like writing your code logically
top to bottom so let me fix this in a more standard way which is to do this
generally speaking you do want to put the main part of your code at the top of
your file and in fact i'm going to go so far as to define my function called main
it's not a requirement but it's indeed a convention and this just connotes to the
reader that this is the main part of my program i'm going to get rid of my empty
hello call now and only pass in one version with hello name and then down here a
couple lines further down i'll actually define my hello function unfortunately
now that i've reordered the functions in this way by putting the main part of my
code at the top and hello at the bottom so that my logic kind of flows top to
bottom if i go ahead and run python of hello.pi enter nothing whatsoever happens
if i do it again nothing whatsoever happens well why in the world is this well
just because i've defined a function called main and i've defined a function
called hello doesn't mean that i've actually called that is used either of them
yes i'm using hello inside of main but no one is telling python to actually use
or call main so in order to tidy this up the last thing i need to do in this file
it seems is actually call my main function and in fact by calling my main
function in this way it gets me out of trouble because now i'm defining main
first but i'm not calling hello yet i'm defining hello next but i'm not calling
hello next i only at the very end of this file call main which has the effect of
running this code up here which has the effect of running this code down here and
it allows me therefore to organize my file and order my functions in any way i
want including main at the very top and solving ultimately that problem of
python not knowing what's going on now it's important to note that i defined my
function hello as taking an argument too and then i passed into that function the
value of the variable that i wanted to say hello to that is the variable called
name because suppose i had done something a little bit differently suppose that i
hadn't defined hello as taking an argument so i just remove mention of two and
its default value help world and i go back up to my main function and i just call
hello itself without passing in any argument and now let me go ahead and make one
more change one more mistake technically let me go ahead and just try to naively
print out the value of name in the hello function so now to be clear in my main
function on line two i'm defining my variable called name and assigning it the
return value of the input function from the user i'm then just calling hello in
my hello function which now no longer takes any arguments i am calling print
passing in hello comma and then immediately passing in name the variable into
which i got the user's input but the catch is that name exists now only in main
and so watch what happens when i try to run this version of the program with
python hello.pi i hit enter i'm prompted for my name david enter and ah a name
error name name quote unquote is not defined so it turns out that this is
actually an issue of what's called scope scope refers to a variable only existing
in the context in which you defined it so insofar as i define this variable name
in my main function i can only use that variable in my name function i can't use
it as i've tried to here in my hello function it doesn't exist in that so-called
scope and so this is why now if i rewind and undo all of those changes you'll see
that i'm deliberately passing main from my main function into my hello function
and now in the hello function it technically has a different name it's called 2
in that context but that's fine it's completely up to each individual function to
name its own variables or name its own arguments but this is a way now that i'm
handing to the hello function the value of that variable so it can be printed by
hello as well and there's one final flourish we can add here now that we've
implemented hello you'll notice that hello only has a so-called side effect it
only prints out something to the screen well what if i also want my function to
not have a side effect per se but actually hand me back a value recall that the
input function returns a value the string that the user typed in recall that the
int function returns a value the float function returns a value that was passed
into it well you can use one final keyword here literally return to return a
value explicitly yourself in fact let me go back to vs code here and i think
we'll return our attention to calculator.pi and see if we can't implement one
other version of calculate.calculator.pi that actually has our own function that
even returns a value so i'm going to go ahead and open up calculator.pi and i
think this time i'm going to throw everything away as before and i'm just going
to start practicing what we're preaching here define a function called main which
is now going to be the main part of my function let's go ahead and now declare a
variable called x and assign it to the converted version of the user's input
after asking them what's x so again a line of code quite like we've done before
and suppose now that what i want to do is square this value i want to take the
number that the users typed in and raise it to the power of two so two squared
would be four three squared would be nine four squared would be sixteen and so
forth well how do i go about implementing a function literally called square
which actually doesn't come with python built in well let me assume for the
moment that it does exist and let me say something like this let me go ahead and
say that printing how about x squared is comma square of x so what have i done
i've defined a function called main and i've implemented two lines the first of
these lines prompts the user for a value x and converts it to an int and stores
it in a variable called x on line
three i then say x squared is and then i pass a second argument to the print
function whatever the return value is of a square function but square doesn't
exist and i'll show you this here if i now call main at the bottom and i run
python of calculator.pi i'll see that x is 2 and then i see a whole bunch of
errors a name error name square is not defined so this isn't a typo here it's
just the function doesn't exist but i think i can make it exist here let me go
ahead and define another function called square this one's going to take in a
number and i'm going to call it generically n as many a programmer would just to
represent any old number and then what do i want to do in order to square n well
a number squared is really just itself times itself so i'm going to do this n
times n but it's not enough just to do the math yourself n times n you're going
to have to return the actual value n times n and that's our new keyword here when
i now do this watch what happens python of calculator dot pi enter x say shall be
2 x squared is 4. let me go ahead now and say x is now 3 x squared is now 9. so
i've implemented my very own function that returns the square of a value and
because i'm using the return keyword that ensures that i can pass the return
value of this just like the return value of input or int or float to another
function like print instead and here too there's going to be so many ways to
solve the same problem i can actually raise n to the power of two we've not seen
the syntax before but if you use stu two asterisks like this two stars that
raises the thing on the left to the power on the right or it turns out there is
in python a function called pow for raising something to the power that takes two
arguments the first of which is the number the second of which is the exponent so
there too there's just so many ways to actually solve that same problem as well
so ultimately what we have we done here we first introduced functions these
actions are verbs many of which come built into python that you can just use in
your own code we then introduced variables by which you could store those return
values and then maybe do something more with it at the end of the day too you now
have the ability to create to invent your own functions to solve simple problems
like hello or in the weeks to come much more sophisticated more challenging more
fun problems as well
[‫]موسيقى‬

‫إذن‬

[‫]موسيقى‬

‫ هذه مقدمة لـ‬،‫ حسًنا‬cs50

‫ اسمي ديفيد‬،‫للبرمجة باستخدام بايثون‬

‫مالين وهذا هو أسبوعنا حول الوظائف‬

‫والمتغيرات ولكن هناك احتماالت أن‬

‫معظمكم لم‬

‫ لذا فلنبدأ‬،‫يبرمج أبًدا من قبل‬

‫فقط اسمحوا لي بالمضي قدًم ا هنا‬ ‫بالتنفيذ‬

‫وفتح جهاز الكمبيوتر الخاص بي وعليه برنامج‬

‫ يسمى‬Visual Studio Code ‫ أو‬vs code

‫وهو مجرد برنامج شائع جًدا‬

‫ اآلن‬،‫في الوقت الحاضر لكتابة التعليمات البرمجية فعلًيا‬

‫ليس عليك كتابة التعليمات البرمجية باستخدام هذه‬

‫ كل ما نحتاجه في‬،‫في الواقع‬ ‫األداة بالذات‬

‫نهاية اليوم هو ما يسمى‬

‫ وإذا‬،‫ وهو برنامج لكتابة النص‬،‫بمحرر النصوص‬

‫ فيمكنك حتى‬،‫كنت تريد ذلك حًقا‬

‫ استخدام شيء مثل مستندات‬Google ‫أو‬

Microsoft Word، ‫وسيتعين عليك حفظه بالتنسيق‬

‫ولكن في نهاية‬ .‫الصحيح‬


‫المطاف‪ ،‬كل ما تحتاجه هو برنامج‬

‫لكتابة النص ألن هذا هو النص‬

‫‪،‬النصي اآلن ضمن هذا البرنامج المحدد‬

‫وستكون لدي القدرة على إنشاء‬

‫ملف واحد أو أكثر عبر هذا الجزء العلوي‬

‫سأفعل ذلك من خالل‬ ‫من الشاشة و‬

‫الغوص مباشرة والقيام بذلك في الجزء‬

‫السفلي من شاشتي‪ ،‬في الجزء السفلي من‬

‫شاشتي يوجد ما يسمى بالنافذة الطرفية‬

‫وهذه هي واجهة سطر األوامر أو‬

‫للكمبيوتر األساسي سواء كان ذلك ‪ cli‬واجهة‬

‫أو جهاز الكمبيوتر الخاص بك أو حتى بعض ‪mac‬‬

‫الخوادم في السحابة وما سأفعله‬

‫هنا هو كتابة التعليمات البرمجية حرفًيا‬

‫ثم اسم الملف الذي أريد‬

‫حيث سنرى ‪ hello dot pi‬ترميزه على سبيل المثال‬

‫تكتب بلغة‬ ‫قريًبا أي برنامج‬

‫بشكل عام اسم ملف ‪python‬‬

‫لإلشارة ‪ dot pi‬ينتهي بـ‬

‫للكمبيوتر إلى أنه بالفعل برنامج‬

‫اآلن سترى هنا في ‪ python‬مكتوب بلغة‬

‫الجزء العلوي من شاشتي‪ ،‬لدي‬

‫مؤشر وامض وسطر واحد حيث‬

‫سيذهب السطر األول من الكود الخاص بي‬

‫ثم عالمة تبويب تذكرني‬

‫‪ hello.pi‬باسم هذا الملف‬

‫‪،‬وبدون معرفة الكثير من لغة بايثون‬

‫سأكتب أول برنامج لي‬

‫هنا كما يلي‪ ،‬قم بطباعة األقواس المفتوحة‬

‫اقتباس مرحًبا بعالم الفاصلة‪ ،‬إغالق االقتباس‬


‫وإغالق األقواس وسترى أنه على‬

‫لوحة المفاتيح الخاصة بي‪ ،‬تم االنتهاء من بعض أفكاري‬

‫بالنسبة لي‪ ،‬ولم يكن علي سوى كتابة‬

‫قوس واحد وظهر القوس اآلخر‬

‫تلقائًيا وهذه مجرد‬

‫أدوات مثل‬ ‫ميزة سنراها‬

‫هذه األداة هنا اآلن حتى لو لم تقم‬

‫بالبرمجة من قبل‪ ،‬فاالحتماالت هي أنه يمكنك تخمين‬

‫استنتاج ما سيفعله هذا البرنامج البسيط وهو عبارة عن‬

‫سطر واحد فقط‪ ،‬اطبع‬

‫‪،‬أقواس مفتوحة‪ ،‬اقتباس مرحًبا بالعالم‪ ،‬اقتباس قريب‬

‫أقواس مغلقة بالفعل عندما أقوم‬

‫إنه مجرد‬ ‫بتشغيل هذا البرنامج في النهاية‬

‫إلقاء التحية للعالم‪ ،‬وفي‬

‫الواقع يعد هذا برنامًج ا مشهوًر ا جًدا وربما يكون البرنامج‬

‫األكثر شهرة الذي يمكنك كتابته‬

‫كبرنامجك األول بلغة بايثون أو أي‬

‫لغة أخرى‪ ،‬وهذا ما‬

‫فعلته هنا‬

‫الكمبيوتر الشخصي حتى هاتفي‪ ،‬أنا ‪ Mac‬ولكن على جهاز‬ ‫الخاص بي‬

‫معتاد مثلك عموًم ا على‬

‫تشغيل البرامج عن طريق النقر المزدوج فوق‬

‫أيقونة أو النقر فقط على الشاشة ولكني‬

‫ال أرى مثل هذه الرموز هنا وفي الواقع‬

‫هذا بسبب واجهتي لجهاز‬

‫أو أن بعض الخوادم الموجودة في ‪Mac‬‬ ‫أو الكمبيوتر الشخصي الحالي الخاص بي على األقل‬

‫‪ cli‬السحابة هي مرة أخرى مجرد واجهة سطر أوامر‬

‫والتي على الرغم من أنها‬

‫قد تبدو وكأنها خطوة إلى الوراء من‬


‫القوائم واألزرار واأليقونات التي‬

‫نعتبرها أنا وأنت أمًر ا مفروًغ ا منه كل يوم‪ ،‬ستجد أننا‬

‫نعتقد أنه في النهاية‬

‫واجهة أكثر قوة وشعبية بشكل‬

‫ال يصدق لالستخدام بين‬

‫المبرمجين في العالم الحقيقي‪ ،‬لذا لتشغيل‬

‫هذا البرنامج‪ ،‬سيتعين علي استخدام‬

‫أمر وسأقوم بتحريك المؤشر مرة أخرى‬

‫إلى أسفل الشاشة‬

‫في السابق‪ ،‬قمت بالفعل بتشغيل‬ ‫هنا حيث‬

‫أمر واحد وهو رمز األمر الذي له‬

‫‪،‬في جهاز الكمبيوتر الخاص بي ‪ vs‬تأثير فتح كود‬

‫ثم قمت بتمرير اسم‬

‫الملف الذي أردت ترميزه‪ ،‬واآلن لدي‬

‫فرصة لكتابة أمر ثاٍن‬

‫أرى عالمة دوالر ثانية‬ ‫وستقوم بذلك‬

‫اآلن‪ ،‬عالمة الدوالر هنا ال‬

‫‪،‬تشير إلى أي نوع من العمالت أو األموال‬

‫إنها فقط الرمز الذي ُيستخدم بشكل عام‬

‫لإلشارة إلى الموجه الخاص بك حيث‬

‫تريد واجهة سطر األوامر منك وضع‬

‫تشغيل‬ ‫هذه األوامر اآلن‪ ،‬األمر أستطيع‬

‫هنا سيكون هذا سأقوم‬

‫اآلن لماذا هذا ‪ python of hello dot pi‬بتشغيل‬

‫جيد‪ ،‬اتضح أنه عندما‬

‫أكتب فعلًيا تعليمات برمجية بلغة مثل‬

‫يتم تخزينها بالطبع في هذا ‪python،‬‬

‫لتفسير ‪ hello.pi‬الملف‬ ‫ولكني بحاجة‬

‫الكود من أعلى إلى أسفل من اليسار إلى اليمين‬

‫حتى يعرف الكمبيوتر ما يجب فعله‬


‫بالفعل في نهاية اليوم حتى لو كنت‬

‫ال تعرف الكثير عن أجهزة الكمبيوتر‪ ،‬فمن‬

‫المحتمل أنك سمعت أن أجهزة الكمبيوتر ال‬

‫تفهم إال األصفار في واحد‬

‫‪ -‬يسمى النظام الثنائي حسًنا‪ ،‬إذا كان هذا هو‬ ‫لذا‬

‫الحال‪ ،‬فإن شيًئا يقول الطباعة‬

‫واألقواس واالقتباس مرحبًا‬

‫بالعالم ليس بالتأكيد أصفاًر ا‬

‫وعلينا أن نترجمها بطريقة ما إلى‬

‫األصفار واآلحاد التي‬

‫يفهمها الكمبيوتر اآلن لحسن الحظ طالما‬

‫لقد قمت بتثبيت مثل هذا البرنامج‬ ‫أنك'‬

‫مسبًقا‪ ،‬يوجد برنامج باإلضافة إلى‬

‫ليست ‪ python‬لذا فإن ‪ python،‬لغة تسمى‬

‫لغة سنكتب فيها‬

‫التعليمات البرمجية فحسب‪ ،‬بل هي أيًضا برنامج‬

‫ُيعرف أيًض ا باسم المترجم الفوري الذي تقوم بتثبيته‬

‫مجاًنا بنفسك‬ ‫أو كمبيوتر شخصي أو ‪mac‬‬

‫خادم ما في السحابة ويمكنك بعد ذلك تشغيل‬

‫هذا البرنامج الذي يمرره المترجم‬

‫إليه إلدخال اسم الملف الخاص بك مثل‬

‫ثم سيتولى هذا البرنامج ‪ hello.pi‬ملفي هنا‬

‫الذي مترجم عملية‬

‫من اليسار إلى‬ ‫قراءته من أعلى إلى أسفل‬

‫اليمين وترجمتها بشكل فعال‬

‫إلى تلك األصفار واآلحاد التي‬

‫‪،‬يمكن للكمبيوتر فهمها‬

‫لذا دعنا نفعل ذلك فقط‪ ،‬دعني أعود إلى‬

‫‪ python‬هنا‪ ،‬لقد كتبت بالفعل ‪ vs‬كود‬


‫‪،‬لكنني لم أضغط على زر اإلدخال بعد ‪of hello.pi‬‬

‫سأبدأ اآلن‬ ‫وهذا هو ما‬

‫هذا األمر وآمل إذا لم‬

‫أفسد أًيا من هذا‪ ،‬فيجب أن أرى‬

‫أول إخراج لبرنامجي على الشاشة وفويال‬

‫مرحًبا بالعالم‪ ،‬لذلك إذا قمتما‬

‫بكتابة نفس الرمز بالضبط وقمتا‬

‫بتنفيذ نفس الشيء بالضبط‬

‫ستكون قد كتبت‬

‫‪،‬برنامجك األول في هذه الحالة بلغة بايثون‪ ،‬حسًنا‬

‫فلنرجع اآلن خطوة إلى الوراء ونفكر في ما‬

‫فعلناه للتو وما الذي‬

‫ننظر إليه هنا على الشاشة‬

‫جيًدا أوًال وقبل كل شيء في معظم األحيان‬

‫لغة برمجة تميل إلى‬

‫‪،‬الوصول إلى ما يسمى بالوظائف‬

‫فالوظيفة تشبه اإلجراء أو الفعل‬

‫الذي يتيح لك القيام بشيء ما في‬

‫‪،‬البرنامج‪ ،‬وبشكل عام‬

‫تأتي أي لغة مع‬

‫مجموعة محددة مسبًقا من الوظائف‪ ،‬وبعض اإلجراءات‬

‫أو األفعال األساسية جًدا التي يقوم بها الكمبيوتر‬

‫ستعرف بالفعل كيفية القيام بذلك من أجلك‪ ،‬وستعرف اللغة‬

‫حًقا كيفية القيام بذلك من أجلك ومن‬

‫أجلك‪ ،‬يمكن للمبرمج أن يستخدم اإلنسان‬

‫هذه الوظائف حسب الرغبة لجعل‬

‫الكمبيوتر يقوم بهذه األشياء اآلن‬

‫البرنامج المعني هنا‬

‫‪ hello.pi‬يستخدم‬ ‫وظيفة واحدة وربما يمكنك‬

‫تخمين ما هي هذه الوظيفة‬


‫بالطبع ستكون هذه الوظيفة‬

‫الطباعة‬

‫ووظيفة الطباعة هذه بالطبع‬

‫ال تطبع سلسلة نصية محددة مسبًقا أي‬

‫للطباعة‬ ‫أنها تطبع ما تريده‬

‫وهنا أيًضا لدينا‬

‫مصطلح آخر في عالم البرمجة‬

‫وهو الحجج‪ ،‬والحجة هي مدخالت‬

‫لوظيفة‬

‫‪،‬تؤثر بطريقة ما على سلوكها‬

‫األشخاص الذين اخترعوا لغة بايثون بالطبع‬

‫لم يعرفوا بالضرورة ما هي أنت‬

‫سيرغبون في الطباعة على‬ ‫وأنا‬

‫الشاشة‪ ،‬لذلك صمموا وظيفة الطباعة هذه‬

‫باستخدام هذه األقواس مع‬

‫القدرة على إدخال بعض السالسل‬

‫النصية كمدخالت سواء كانت باللغة اإلنجليزية أو أي‬

‫لغة بشرية أخرى‪ ،‬وهو ما تريد أن‬

‫تطبعه هذه الوظيفة في النهاية على‬

‫الشاشة‬ ‫الشاشة‬

‫وما الذي‬

‫يفعله البرنامج في النهاية على الشاشة جيًدا‪ ،‬فهو‬

‫يطبع بالطبع ويظهر لنا مرحًبا‬

‫بالعالم على الشاشة وهذا بشكل عام‬

‫‪،‬في البرمجة ُيعرف باسم التأثير الجانبي‬

‫‪،‬يمكن أن يكون مرئًيا أو صوتًيا في هذه الحالة‬

‫على‬ ‫إنه شيء يظهر‬

‫الشاشة‬

‫والوظائف‪ ،‬وبالتالي يمكن أن يكون لها بالفعل‬


‫هذه اآلثار الجانبية‪ ،‬أحد األشياء التي‬

‫يمكنهم القيام بها ألن هذا الفعل هو‬

‫عرض شيء ما على الشاشة كأثر جانبي‬

‫مثل تلك الكلمات العالمية التي أردناها‬

‫‪،‬مرحًبا بالعالم‬

‫أعلم أنني‬ ‫لذلك هذا هو برنامجي األول وأنت‬

‫أشعر أنني بحالة جيدة جًدا‪ ،‬فكل شيء سار‬

‫كما هو مخطط له‪ ،‬ولم أرتكب أي‬

‫أخطاء‪ ،‬ولكن بصراحة عندما‬

‫تتعلم كيفية البرمجة‪ ،‬وحتى بمجرد أن‬

‫تتعلم كيفية البرمجة‬

‫‪،‬بعد سنوات‪ ،‬فسوف ترتكب أخطاء‬

‫بالطبع أو قم باإلشارة إلى‬ ‫وهذه األخطاء‬

‫" مصطلح قد تعرفه بالفعل وهو مصطلح‬

‫الخطأ" الخلل هو خطأ في‬

‫البرنامج ويمكن أن يتخذ أشكااًل كثيرة‬

‫ويشعر باالرتياح ربما عندما يعلم‬

‫أنك ستفعل ذلك خالل األسابيع المقبلة‬

‫العديد من األخطاء‪ ،‬سيكون لديك‬

‫الكثير من األخطاء في التعليمات البرمجية الخاصة بك تماًم ا كما‬

‫فعلت وما زلت أفعل‪ ،‬وتلك‬

‫األخطاء نفسها هي مجرد أخطاء تمثل‬

‫مشاكل عليك حلها وسنقدمها على مدار‬

‫األسابيع القادمة‬

‫لديك الكثير من األدوات العقلية‬

‫والتقنية التي يمكنك من خاللها حل هذه‬

‫المشكالت‪ ،‬ولكن ال تحبط‬

‫إذا لم يعمل برنامجك‬

‫ألول مرة‪ ،‬حتى‬


‫أنه سيعمل مع مرور الوقت مع‬

‫لذلك اسمحوا لي‬ ‫الممارسة والخبرة‪.‬‬

‫اآلن أن أتعمد ارتكاب خطأ بأن‬

‫هناك فرصة غير صفرية ربما‬

‫ارتكبتها عن طريق الخطأ بالفعل ولكنني كنت‬

‫محظوًظا‪ ،‬دعني أمضي قدًم ا وافترض فقط أنني‬

‫نسيت تضمين شيء مثل قوس‬

‫اإلغالق في نهاية هذا السطر من‬

‫أنت تعلم أن الكود‬ ‫التعليمات البرمجية‬

‫صحيح تقريًبا‪ ،‬فهو يشبه ‪ 99‬من الطريق‬

‫ولكن اآلن بعد أن أشرت إليه‪ ،‬من‬

‫الواضح تماًم ا أنه يفتقد هذا‬

‫القوس المغلق ولكن حتى‬

‫التفاصيل الصغيرة التي تبدو بسيطة مثل تلك التي‬

‫اهتم به‪ ،‬وإذا‬ ‫لن نفعلها أنا وأنت كبشر حًقا‬

‫كنت ترسل بريًدا إلكترونًيا أو‬

‫‪،‬رسالة نصية مهما كان األمر مجرد خطأ مطبعي‬

‫فهو ليس باألمر الكبير‪ ،‬سيكون‬

‫مشكلة كبيرة‬

‫للكمبيوتر‪ ،‬فسيأخذك الكمبيوتر‬

‫أنهي‬ ‫حرفًيا‪ ،‬وإذا لم تفعل ذلك‬

‫فكرتك بالطريقة التي تتوقعها اللغة والتي‬

‫‪،‬لن يتم تشغيلها بالضرورة على اإلطالق‬

‫لذا فلنفعل ذلك‪ ،‬سأستمر‬

‫هنا وأمسح شاشتي في‬

‫المضي‬ ‫األسفل فقط حتى أتمكن من البدء من جديد وسأبدأ‬

‫قدًم ا وتشغيل هذا اإلصدار‬

‫من برنامجي‬

‫بعد إجراء هذا التغيير عن طريق‬


‫حذف األقواس‪ ،‬سأمضي‬

‫‪ hello.pi‬مرة أخرى من ‪ python‬قدًم ا وأكتب‬

‫‪،‬وهذه المرة عندما أضغط على زر اإلدخال‬

‫العالم ولكن‬ ‫آمل أن أرى مرحًبا‬

‫هنا لدينا خطأ على الشاشة‪ ،‬وهو ما‬

‫يسمى بالخطأ النحوي الذي يشير إلى‬

‫‪،‬أنني ارتكبت خطًأ في لوحة المفاتيح الخاصة بي‬

‫ولحسن الحظ فإن هذا الخطأ واضح‬

‫ومباشر جًدا‪ ،‬فهو يشير بالفعل إلى أن هذا‬

‫القوس المفتوح لم يتم إغالقه أبًدا‪ ،‬لذا‬

‫بديهي اآلن‬ ‫ربما يكون هذا جيًدا‬

‫ما أحتاج إلى فعله‪ ،‬أحتاج بالطبع إلى‬

‫إغالقه لسوء الحظ‪ ،‬في بعض األحيان‬

‫لن تكون رسائل الخطأ التي سنراها في األسابيع المقبلة‬

‫سهلة االستخدام تقريًبا ولكن هناك أيًض ا مرة أخرى مع‬

‫الخبرة في الممارسة‪ ،‬هل‬

‫مثل هذه البرامج تسمح لي‬ ‫ستتحسن في تصحيح األخطاء‬

‫اآلن بالتأكد من أنني قمت بالفعل بإصالحها‬

‫بشكل صحيح‪ ،‬دعني أمضي قدًم ا في تشغيل‬

‫اآلن واضغط على زر اإلدخال وفويال‪ ،‬لقد ‪hello.pi‬‬

‫عدنا إلى العمل حسًنا‪ ،‬دعني أتوقف‬

‫هنا وأرى ما إذا كانت لدينا أي أسئلة‬

‫أو‬ ‫اآلن حول كتابة بايثون نفسها‬

‫حتى تشغيل أبسط‬

‫هذه البرامج‪ ،‬هل‬

‫يمكنني كتابة تعليمات برمجية داخل كلمة أو على‬

‫وما هو ‪ Microsoft Excel‬سبيل المثال‬

‫‪،‬العائق أمام القيام بذلك‬

‫سؤال جيد حًقا واسمح لي أن‬


‫أقول صراحًة‬

‫لإلنترنت بالكامل أنه ال ينبغي عليك كتابة تعليمات برمجية‬

‫‪ Microsoft‬باستخدام‬ ‫الكلمة التي ذكرتها‬

‫فقط ألنها أداة‬

‫يمكنك من خاللها كتابة النص والتعليمات البرمجية هي في نهاية‬

‫اليوم مجرد نص ولكنها ليست‬

‫األداة المناسبة للمهمة‪ ،‬فنحن ال نحتاج إلى‬

‫فقرات تحتها خط غامق وما‬

‫شيء‬ ‫شابه ذلك نريده بشكل عام‬

‫‪ Google‬أو ‪ Microsoft Word‬أبسط بكثير من‬

‫هو مثال ‪ vs Code‬لذا فإن ‪Docs،‬‬

‫‪،‬لمحرر نصوص ذي غرض عام‬

‫والغرض منه في الحياة هو السماح لك‬

‫‪،‬كإنسان بتحرير النص في الوقت الحاضر‬

‫تأتي برامج تحرير النصوص هذه مع العديد من الميزات اإلضافية في‬

‫ستالحظ أنه حتى في الكود الخاص بي‬ ‫الواقع‪.‬‬

‫هنا‪ ،‬على الرغم من أنه مجرد سطر واحد‪ ،‬إال أن هناك‬

‫القليل من األلوان به‪ ،‬حيث‬

‫‪،‬تظهر الكلمة المطبوعة باللون األزرق‬

‫واألقواس سوداء وسنرى أننا‬

‫قد نكتب المزيد من أسطر التعليمات البرمجية أكثر‬

‫ستظهر الحياة في العديد من السطور‬ ‫فأكثر‬

‫بألوان مختلفة‪ ،‬وهذه ميزة واحدة فقط‬

‫في محرر النصوص الذي سيالحظ أيًضا‬

‫أنه يحتوي على ميزات مثل هذه‬

‫النافذة الطرفية المضمنة‪ ،‬وسيكون لديه‬

‫أداة مضمنة لتصحيح األخطاء أو العثور على‬

‫إنها مجرد‬ ‫مشكالت في التعليمات البرمجية و‬

‫أداة شائعة جًدا في الوقت الحاضر ولكن هناك‬

‫العديد من األدوات األخرى‪ ،‬فنحن نرحب بك‬


‫الستخدامها في هذه الدورة التدريبية وما بعدها‪ ،‬لقد‬

‫صادف أننا استخدمنا هذه األداة في‬

‫‪ vs‬الجزء الثاني إلى حد كبير ألنه يمكنك أيًضا استخدام‬

‫في الوقت الحاضر مجاًنا في ‪code‬‬ ‫ماذا ‪cloud‬‬

‫عن سؤال آخر هنا حول‬

‫‪ helloworld‬أو ‪ python‬البرمجة باستخدام‬

‫أو بناء الجملة بشكل عام والذي سينفق‬

‫علينا إذا‬

‫لم يكن من الممكن تشغيل الكمبيوتر‬

‫باستخدام النافذة الطرفية‬

‫أعتقد أنني سمعت أليس كذلك إذا كان‬

‫من الممكن تشغيل البرنامج بدونها‬

‫‪،‬النافذة الطرفية‬

‫‪،‬هل أنت ممتلئ‬

‫حسًنا‪ ،‬لقد تجمدت بالنسبة لي مرة أخرى ولكن دعني‬

‫أستنتج ما هو السؤال في هذه‬

‫البيئة حيث قمت بتكوين‬

‫هذه إال ‪ python‬جهاز الكمبيوتر الخاص بي‪ ،‬ال يمكنني تشغيل برامج‬

‫‪،‬عبر النافذة الطرفية اآلن‬

‫وهذا أمر جيد بالنسبة لي كمبرمج أو‬

‫الشخص الذي يحاول تعلم كيفية‬

‫البرمجة ولكن ليس من الجيد جًدا إذا كنت‬

‫ترغب في شحن هذا البرنامج وجعل‬

‫‪،‬أشخاص آخرين يستخدمون الكود الفعلي الخاص بك‬

‫فيمكنك تماًم ا كتابة البرامج ثم‬

‫السماح ألشخاص آخرين باستخدام‬

‫واجهة سطر األوامر وليس واجهة سطر األوامر ولكن المستخدم الرسومي‬

‫الواجهة أو واجهة المستخدم الرسومية‪ ،‬هذه مجرد‬

‫آلية واحدة وربما أعتقد أنها‬


‫أفضل آلية لبدء كتابة‬

‫التعليمات البرمجية ألنها ستمنحنا في النهاية‬

‫‪،‬المزيد من التحكم‬

‫اسمح لي بالمضي قدًم ا هنا ولكن ال‬

‫تتردد في مواصلة طرح األسئلة‬

‫على طول‬

‫لنفكر اآلن في الطريقة التي يمكننا بها‬

‫تحسين هذا البرنامج‪ ،‬ولو عبر الدردشة فقط‪ ،‬فلنبدأ في‬

‫تحسين هذا البرنامج لجعله‬

‫أكثر تفاعلية قليًال وال‬

‫نفترض فقط أن الجميع سيرغبون في أن يتم‬

‫الترحيب بهم بشكل أكثر عمومية مثل "مرحًبا‬

‫إذا لم أتمكن من جعل هذا‬ ‫بالعالم"‪ ،‬دعونا نرى‬

‫البرنامج يقول شيًئا مثل مرحًبا‬

‫ديفيد أو مرحًبا جيريميا أو مرحًبا هوراشيو‬

‫أو أًيا كان اسم المستخدم الفعلي فهو‬

‫جيد للقيام بذلك‪ ،‬فسأعود‬

‫وسأضيفه ‪ hello.pi‬إلى‬

‫سطر آخر من التعليمات البرمجية في األعلى‬

‫يقول ببساطة على سبيل المثال‬

‫ما هو اسمك مع وضع عالمة االقتباس مع‬

‫مسافة إضافية في النهاية‪ ،‬لذلك أقوم بالطباعة‬

‫للمستخدم وأطرح عليه سؤاًال‬

‫لبعض المدخالت ولكني اآلن بحاجة إلى‬

‫من‬ ‫وظيفة أخرى للحصول على اإلدخال فعلًيا‬

‫‪ python‬المستخدم ويأتي ما يكفي من لغة‬

‫مع وظيفة تسمى اإلدخال‪ ،‬لذلك‬

‫سأمضي قدًم ا واستدعاء وظيفة‬

‫وهذا ‪ open paren Close paren‬اإلدخال‬

‫سيطالب المستخدم‬
‫بمؤشر وامض فقط في انتظار كتابة‬

‫اآلن اتضح أنه إذا قرأت‬ ‫شيء ما‬

‫‪،‬الوثائق الخاصة بوظيفة اإلدخال‬

‫‪،‬فإنها في الواقع تتطلب وسيطة بحد ذاتها‬

‫ولست بحاجة إلى استخدام الطباعة بشكل منفصل‬

‫ثم مطالبة المستخدم باإلدخال حتى أتمكن‬

‫بالفعل من تبسيط هذا الرمز قبل أن‬

‫سنمضي قدًم ا هنا‬ ‫نستخدمه حتى‪.‬‬

‫ونأخذ نفس السلسلة من الطباعة‬

‫ونضعها كوسيطة لوظيفة اإلدخال‬

‫ونتخلص من الطباعة تماًم ا‬

‫وفي الواقع كانت هذه الطباعة ستضيف‬

‫سطًر ا جديًدا على أي حال‪ ،‬لذا اآلن لدي‬

‫سينتهي األمر بمؤشر المستخدم‬ ‫مطالبة حيث‬

‫يومض في نهاية‬

‫السطر ويسأله ما اسمك‬

‫في نافذتي الطرفية‪ ،‬وسأقوم بتشغيل‬

‫‪python of hello.pi،‬‬

‫أدخل حسًنا‪ ،‬نحن نحرز تقدًم ا‪ ،‬يبدو‬

‫في الواقع‬ ‫‪،‬أن إدخال الوظيفة الجديدة هذا هو‬

‫يطالبني اإلنسان باإلدخال‪ ،‬لذلك‬

‫سأكتب اسمي ديفيد وأضغط على زر‬

‫اإلدخال‪ ،‬لسوء الحظ‪ ،‬ال يفعل‬

‫أي شيء مع اسمي‪ ،‬بل يخرجه‬

‫على الفور‪ ،‬حسًنا‪ ،‬حسًنا‪ ،‬يمكنني إصالح‬

‫السطر الثاني‬ ‫هذا األمر بشكل صحيح‪ ،‬يمكنني الذهاب إلى‬

‫ويمكنني تغيير العالم إلى ديفيد ثم‬

‫العودة إلى نافذتي الطرفية هنا يمكنني القيام بـ‬

‫أدخل ما ‪python of hello.pi‬‬


‫اسمك ديفيد أدخل وهناك نذهب‬

‫جيًدا اآلن أنا جاهز للعمل اآلن‬

‫المقصود بالطبع أن‬ ‫برنامجي يعمل كـ‬

‫هذا ال يعمل حًقا على النحو المقصود‬

‫هنا‪ ،‬دعني أمضي قدًم ا وأحاول التظاهر‬

‫‪،‬بأنني زميلي كارتر هنا حسًنا‬

‫اسم كارتر هو هذا‪ ،‬سأمضي‬

‫قدًم ا وأضغط على زر اإلدخال وسأرى‬

‫من الواضح أن هذا ليس‬ ‫بالطبع مرحًبا كارتر جيًدا‬

‫ألنني قمت بتشفيره‪ ،‬إذا جاز التعبير‪ ،‬لقد‬

‫كتبت اسمي حرفًيا داخل‬

‫السلسلة‪ ،‬لذا نحتاج إلى طريقة ما اآلن‬

‫الستعادة‬

‫ما أدخله المستخدم فعلًيا والقيام‬

‫بشيء به في النهاية‪ ،‬ولهذا‬

‫لالستفادة من‬ ‫نحن ذاهبون‬

‫ميزة أخرى للبرمجة على وجه التحديد إحدى‬

‫ميزات بعض الوظائف وهي أنه‬

‫يمكن أن يكون لها قيم إرجاع أيًض ا إذا‬

‫كنت تعتقد أن اإلدخال هو مرة أخرى هذا‬

‫اإلجراء هذا الفعل‪ ،‬فيمكنك في الواقع‬

‫تجسيده على أنه ربما شخص مثل‬

‫لقد طرحت‬ ‫صديق لك‬

‫سؤاًال وطلبت من صديقك‬

‫الذهاب للحصول على مدخالت من شخص آخر‪ ،‬فاسأل‬

‫هذا الشخص عن اسمه وإذا‬

‫عاد صديقك وهو يعرف اسم هذا الشخص‬

‫جيًدا‪ ،‬ألن يكون من الجيد أن‬

‫هذا نوع‬ ‫يعيدوا هذا االسم إليه‬

‫‪،‬من ما نحتاجه مجازًيا‬


‫الوظيفة التي يجب القيام بها هي الحصول على مدخالت المستخدم‬

‫ثم تسليمها إلّي مرة أخرى حتى يتمكن‬

‫المبرمج من فعل شيء ما بها‬

‫‪،‬ولكن إذا كان سيتم إعادتها إلي‬

‫في مكان ما حتى‬ ‫فأنا نوًعا ما أريد أن أضعها‬

‫أتمكن من طباعته مرة أخرى على‬

‫الشاشة‪ ،‬أحتاج إلى القيام بما يعادل‬

‫إخراج قطعة من الورق أو‬

‫مالحظة الصقة‪ ،‬أكتب على هذه القطعة من‬

‫الورق ما قاله اإلنسان حتى‬

‫يمكننا بعد ذلك إدخالها كمدخل في‬ ‫أتمكن من طباعته مرة أخرى على الشاشة‬

‫وظيفة الطباعة وللقيام بذلك‬

‫سنحتاج إلى ميزة أخرى‬

‫للبرمجة وهي المتغيرات واالحتماالت التي‬

‫يعرفها الجميع تقريًبا عن‬

‫‪ x‬المتغيرات الخاصة بفصل الرياضيات عندما تعود‬

‫وما شابه ذلك جيًدا ‪z‬و ‪y‬و‬

‫‪،‬تتمتع البرمجة بنفس القدرة‬

‫وهي القدرة على إنشاء متغير في‬

‫هذه الحالة في ذاكرة الكمبيوتر وليس‬

‫‪،‬فقط على قطعة من الورق‬

‫ويمكن لهذا المتغير تخزين قيمة أو رقم أو‬

‫نص أو حتى صورة أو فيديو أو أكثر‪ ،‬فالمتغير‬

‫بعض‬ ‫هو مجرد حاوية لـ‬

‫‪،‬المتغيرات‬

‫المتغير هو مجرد حاوية لبعض‬

‫القيمة داخل جهاز الكمبيوتر أو داخل‬

‫برنامجك الخاص‪ ،‬فكيف يمكنني‬

‫‪،‬التعبير عن نفسي بهذه الطريقة‬


‫أعتقد أن ما سأفعله هو تقديم‬

‫من‬ ‫متغير أكثر قليًال‬

‫يمكنني أن ‪ y‬أو ‪ x‬المثير لالهتمام أنه من‬

‫يساوي اإلدخال ولكني ‪ x‬أقول إن‬

‫سأستخدم اسًم ا أفضل من‬

‫المتغير الرياضي النموذجي هنا‬

‫وسأنادي حرفًيا‬

‫باسم المتغير الخاص بي لماذا حسًنا في البرمجة ألن‬

‫أمامي‬ ‫لدي لوحة مفاتيح كاملة‬

‫يمكنني استخدام المزيد من المصطلحات الوصفية‬

‫لوصف ما أكتبه‪ ،‬واآلن‬

‫على الرغم من أن هناك فرصة‬

‫للنظر في جزء معين من بناء الجملة‪ ،‬فقد‬

‫رأينا أقواًسا ورأينا اقتباسات‬

‫وكلها ضرورية عند تمرير‬

‫إلى دالة ولكن عالمة المساواة هذه‬ ‫المدخالت‬

‫الموجودة بين اإلدخال على‬

‫اليمين واالسم على اليسار هي في الواقع‬

‫مهمة وهي من الناحية الفنية ليست‬

‫عالمة يساوي في حد ذاتها وال تعني‬

‫‪،‬المساواة بقدر ما تعني التعيين‬

‫لذلك في بايثون والعديد من البرمجة‬

‫في اللغات‪ ،‬عالمة المساواة الواحدة هي‬

‫عامل التعيين وما يعنيه ذلك على‬

‫وجه التحديد هو أنك تريد التعيين‬

‫من اليمين إلى اليسار مهما كان‬

‫إدخال المستخدم‪ ،‬لذلك تنسخ عالمة المساواة من‬

‫اليمين إلى اليسار مهما كانت‬

‫قيمة اإلرجاع للدالة على‬

‫مرة أخرى‪ ،‬تحصل وظيفة اإلدخال‬ ‫اليمين‬


‫بوضوح على مدخالت من المستخدم ولهذا‬

‫السبب تمكنت من كتابة اسمي أو‬

‫اسمي‪ ،‬ولكنها أيًض ا نوع من األيدي خلف‬

‫الكواليس التي تعيد‬

‫القيمة إلى المبرمج وإذا‬

‫متغير‬ ‫استخدمت عالمة يساوي و‬

‫بغض النظر عن االسم الذي أسميه‪ ،‬يمكنني تخزين هذا‬

‫اإلدخال في هذا المتغير إلعادة استخدامه‬

‫الحًقا‪ ،‬لذا يوجد اآلن في‬

‫ذاكرة الكمبيوتر في مكان ما حاوية‬

‫تحتوي على اقتباس ديفيد أو‬

‫اقتباس كارتر أو اقتباس أو أي شيء كتبه اإلنسان‬

‫ولكن هنا من السهل ارتكاب خطأ‬

‫لنفترض أنني‬

‫قررت محاولة طباعة هذا االسم ولذا‬

‫فإنني أكتب نوًعا ما في‬

‫اسم الفاصلة المرحب هذا فقط نوع من توصيل‬

‫اسم المتغير حسًنا‪ ،‬اسمح لي‬

‫من‬ ‫‪ hello.pi‬بالمضي قدًم ا هنا وتشغيل بايثون‬

‫واضغط على زر اإلدخال الذي سيطالبني بإدخال‬

‫اسمي واسمحوا لي بكتابة اسمي‬

‫ديفيد لكنني لم أضغط على زر اإلدخال بعد‬

‫وربما عبر الدردشة ما الذي‬

‫سيحدث هنا عندما أضغط اآلن على زر اإلدخال كما‬

‫يقول مرحًبا يا ديفيد سأكون‬ ‫آمل‬

‫بخير إذا قال مرحًبا بالعالم ولكني ال‬

‫أريده أن يقول ما سيقوله بالفعل‬

‫ونعم ما نراه في‬

‫الدردشة جيًدا‪ ،‬فمن المحتمل أن يقول‬


‫ليس‬ ‫حرفًيا اسم الفاصلة مرحًبا‪ ،‬لذلك هذا‬

‫صحيًح ا تماًم ا‪ ،‬لذا نحتاج إلى طريقة أخرى‬

‫لطباعة القيمة داخل هذا‬

‫‪،‬المتغير بدًال من اسم الكلمة هذا فقط‬

‫حسًنا‪ ،‬دعني أجرب ذلك بعدة‬

‫طرق مختلفة‪ ،‬دعني أحاول ذلك على النحو‬

‫التالي‪ ،‬دعني أمضي قدًم ا وربما أتراجع عن‬

‫لقد أصبحت جيًدا‬ ‫هذا ألنني‬

‫بالفعل في قول مرحًبا‪ ،‬لذا دعنا‬

‫نعلمك‪ ،‬دعنا نرسم هذا الخط في الرمال ونقول‬

‫حسًنا‪ ،‬دعنا على األقل‬

‫نخرج فاصلة الترحيب من الباب‪ ،‬دعنا اآلن‬

‫نطبع االسم وعلى حدس سأحاول‬

‫سأستخدم الطباعة مرة أخرى‬

‫ألنه يمكنك استخدام هذه الوظائف‬

‫عدة مرات حسب حاجتك وسأقوم‬

‫بتمرير‬

‫إلى وظيفة الطباعة ولكن الحظ أنني ‪ name‬المتغير المسمى‬

‫أنا ال‬ ‫أصبحت ذكًيا بعض الشيء اآلن‬

‫أضعها بين عالمات اقتباس مزدوجة ألننا‬

‫رأينا بالفعل أن عالمات االقتباس المزدوجة‬

‫‪ n-a-m-e،‬تعني حرفًيا طباعة‬

‫وسأتخلص من عالمات االقتباس هذه المرة على‬

‫أمل أنه‬

‫‪ name‬من خالل تمرير المتغير المسمى‬

‫سيتم ذلك ‪ print،‬إلى الوظيفة المسماة‬

‫الحقيقة هي طباعة محتويات‬

‫هذا المتغير الذي يمثل‬

‫قيمته‪ ،‬حسًنا‪ ،‬دعنا نمضي قدًم ا ونفعل‬


‫أدخل ‪ python of hello.pi‬ذلك هنا‬

‫ما اسمك ديفيد واآلن أعبر‬

‫إصبعي ما زلت أرى مرحًبا فاصلة ديفيد‬

‫ليس البرنامج األفضل‬ ‫‪،‬حسًنا‪ ،‬لذا فهو‬

‫فأنا أختصر بعض التفاصيل هنا‬

‫إذا جاز التعبير‪ ،‬فأنا أقول مرحًبا ديفيد في‬

‫سطرين منفصلين‪ ،‬لذا فهو ليس أنيًقا وليس‬

‫جمياًل وليس‬

‫مناسًبا نحوًيا في اللغة اإلنجليزية مثل‬

‫في‬ ‫مجرد قول كل ذلك في نفس واحد‬

‫سطر واحد ولكن على األقل لم أقم بحل‬

‫المشكلة بشكل جيد حتى اآلن ولكن اسمحوا‬

‫لي أن أعود خطوة إلى الوراء اآلن وربما‬

‫أقدم بعض المفاهيم األخرى‬

‫التي يجب أن نكون على دراية بها‬

‫وهي أن برامجنا تصبح أطول‬

‫لم يعد مجرد سطر واحد أو سطرين‬ ‫وتصبح‬

‫‪،‬أو حتى ثالثة في نهاية المطاف‬

‫ستصبح برامجنا عشرات األسطر‬

‫وربما حتى مئات األسطر‪ ،‬فلنمهد‬

‫الطريق للنجاح والمضي قدًم ا‪ ،‬فقد‬

‫اتضح أن بايثون والعديد من‬

‫لغات البرمجة تدعم أيًضا‬

‫التعليقات عبارة عن‬ ‫شيًئا يسمى التعليقات‬

‫مالحظات لنفسك في التعليمات البرمجية الخاصة بك وتقوم‬

‫بتضمين التعليقات عن طريق‬

‫رمز خاص في لغة بايثون‪ ،‬وسيكون رمز‬

‫التجزئة عادًة وهذا يسمح‬

‫لك بكتابة ما يعادل مالحظة‬

‫لنفسك ولكن بطريقة لن‬


‫الكود الخاص بك‬ ‫تنكسر‬

‫يتجاهل الكمبيوتر تعليقاتك‪ ،‬إنه موجود من‬

‫‪،‬أجلك فقط‪ ،‬إنه موجود فقط من أجل معلمك‬

‫إنه موجود فقط من أجل زميلك‬

‫‪،‬الذي تشاركه في النهاية هذا الكود‬

‫هنا ‪ vs code‬لذلك إذا عدت إلى‬

‫تعليقات على هذا‬ ‫وأريد فقط إضافة بعض‬

‫البرنامج ألشرح لمعلمي‬

‫لنفسي لزمالئي ما الذي‬

‫يقوم به هذا البرنامج بشكل جيد‪ ،‬دعنا نمضي قدًم ا‬

‫ونفعل ذلك‪ ،‬سأذهب إلى أعلى‬

‫برنامجي وعلى السطر األول اآلن‬

‫سطر التعليمات البرمجية األصلي هذا‬ ‫سأنتقل‬

‫قلياًل ‪ ،‬سأضيف عالمة تجزئة‬

‫‪،‬وسأقول شيًئا مثل هذا‬

‫اسأل المستخدم عن اسمه اآلن‪ ،‬لست مضطًر ا الستخدام‬

‫تلك اللغة‪ ،‬لست مضطًر ا الستخدام‬

‫نص يمكنني استخدام أي‬ ‫ذلك‬

‫لغة بشرية مهما كانت‪ ،‬وليس من الضروري أن‬

‫‪،‬تكون اإلنجليزية ولكني سأقوم اآلن باألسفل‬

‫فقط قل شيًئا كهذا قل‬

‫مرحًبا للمستخدم وستالحظ أن‬

‫بشكل افتراضي هو نوع من اللون الرمادي ‪ vs‬كود‬

‫التعليقات لم تعد زرقاء وال يوجد‬

‫أحمر وال يوجد لون فيها‬

‫وهذا فقط ألنها مالحظات‬

‫لنفسي وسيتجاهلها الكمبيوتر في النهاية‬

‫ولكن ما لدينا‬

‫اآلن هو تعليقان يطلبان من المستخدم‬


‫التعليق الثاني قل مرحًبا‬ ‫اسمهما ثم‬

‫للمستخدم ولقد علقت نوًعا ما على‬

‫كل جزء من التعليمات البرمجية مثل كل سطر أو‬

‫مجموعة من التعليمات البرمجية التي تفعل‬

‫شيًئا جديًر ا بالمالحظة‪ ،‬لماذا حسًنا‬

‫صباح الغد عندما أستيقظ بعد أن تعلم‬

‫أنك نمت لبعض الوقت نسيت‬

‫لقد فعلت ذلك في اليوم السابق‪ ،‬ومن‬ ‫ما‬

‫المالئم مع التعليقات أن ترى فقط باللغة‬

‫اإلنجليزية أو بلغتك البشرية ما‬

‫يفعله هذا البرنامج بحيث‬

‫ال تضطر إلى قراءة الكود نفسه‬

‫واألفضل من ذلك إذا كان هناك خطأ‬

‫على الطريق‪ ،‬يمكنك قراءة ما‬ ‫في أسفل‬

‫كانت نيتك ومن ثم يمكنك إلقاء نظرة على‬

‫الكود ومعرفة ما إذا كان الكود الخاص بك‬

‫يفعل اآلن ما كنت تقصده‪ ،‬لذا فإن هذا‬

‫ليس ضرورًيا حًقا لبرنامج‬

‫صغير كهذا‪ ،‬فهو واضح جًدا باستخدام سطر‬

‫إن ما‬ ‫واحد أو سطرين أو ثالثة أسطر فقط‬

‫يفعله البرنامج هو أن‬

‫قراءة الكود سريعة جًدا مقارنة بالتعليقات‪ ،‬لكن‬

‫التعود على هذه العادة يعد‬

‫أمًر ا جيًدا بشكل عام للتعليق على الكود الخاص بك في كل‬

‫سطر أو بضعة أسطر لتذكير‬

‫نفسك واآلخرين بما هو‬

‫إن القيام بما هو‬ ‫هدفك وما هو الكود الخاص بك‪.‬‬

‫لطيف بشأن التعليقات أيًض ا هو أن هذه التعليقات‬

‫يمكن أيًض ا أن تكون بمثابة‬

‫قائمة مهام لنفسك‪ ،‬هناك هذه الفكرة في‬


‫برمجة الكود الكاذب‪ ،‬الكود الكاذب‬

‫ليس شيًئا رسمًيا‪ ،‬وليس‬

‫لغة واحدة محددة‪ ،‬فهو يستخدم‬

‫لغة بشرية‬ ‫اللغة اإلنجليزية أو لغتك فقط‬

‫للتعبير عن أفكارك‬

‫بإيجاز ومنهجية خوارزمية إذا جاز التعبير‬

‫ولكن كود زائف‪ ،‬ألنها‬

‫ليست لغة بايثون وليست بالضرورة‬

‫اإلنجليزية‪ ،‬فهي تسمح لك فقط‬

‫بتحديد الخطوط العريضة لبرنامجك حتى مقدًم ا لذلك‬

‫على سبيل المثال‪ ،‬إذا لم أكن متأكًدا اليوم‬

‫للبدء في كتابة هذا‬ ‫كيف أردت‬

‫البرنامج ولكني كنت أعرف ما أريد القيام به‪ ،‬كان من‬

‫الممكن أن أبدأ اليوم بمجرد‬

‫ال يوجد رمز‪ ،‬كان بإمكاني ‪ hello.pi،‬كتابة هذا في‬

‫كتابة تعليقين فقط‬

‫لنفسي‪ ،‬الخطوة األولى‪ ،‬اطلب من المستخدم‬

‫قل مرحًبا للمستخدم‪ ،‬وبعد‬ ‫اسمه‪ ،‬الخطوة الثانية‬

‫أن حددت برنامجي‬

‫بالكود الكاذب‪ ،‬يمكنني الذهاب إلى هناك‬

‫وأقول حسًنا‪ ،‬كيف أطلب من المستخدم‬

‫اسمه جيًدا‪ ،‬يمكنني القيام بإدخال االقتباس‬

‫وإلغاء االقتباس ما هو اسمك وعالمة االستفهام‬

‫هنا ربما يمكنني‬ ‫ثم على اليسار‬

‫‪،‬وضع متغير وتعيينه لذلك‬

‫‪،‬حسًنا‪ ،‬كيف أقول مرحًبا للمستخدم‪ ،‬حسًنا‬

‫أعلم أنه يمكنني استخدام الطباعة لقول أشياء على‬

‫الشاشة‪ ،‬دعني أقول مرحًبا بفاصلة‬

‫واسمحوا لي حسًنا‪ ،‬دعني اآلن أطبع‬


‫لذا مرة أخرى‪ ،‬يعد الكود الكاذب‬ ‫اسم الشخص‬

‫طريقة رائعة لتنظيم قائمة المهام الخاصة بك‬

‫خاصة إذا لم تكن لديك أي فكرة عن كيفية‬

‫كتابة الكود ألنه يقسم‬

‫برنامًج ا أكبر إلى‬

‫مهام صغيرة الحجم‪ ،‬حسًنا‪ ،‬دعني أتوقف‬

‫أي‬ ‫هنا لمعرفة ما إذا كان هناك اآلن‬

‫أسئلة حول التعليقات أو الكود الزائف الذي‬

‫ُيرجع قيًم ا‬

‫‪.‬أو متغيرات‬

‫أي أسئلة يمكننا توضيحها هنا‪ ،‬نعم‬

‫سؤالي هو هل‬

‫يعمل إدخال الوظيفة ألي نوع من المعلومات‬

‫أم للكلمات فقط‪ ،‬نعم‬

‫سؤال جيد حًقا‪ ،‬لذا وفًقا‬

‫في‬ ‫لوثائقه وسنبحث أكثر‬

‫التوثيق الرسمي قريًبا‪ ،‬سيتوقع اإلدخال‬

‫ما يسمى بسلسلة عبارة عن‬

‫تسلسل نصي سواء كان باللغة اإلنجليزية أو‬

‫أي لغة بشرية أخرى‪ ،‬ولكنه سيتوقع بالفعل‬

‫نًصا‬

‫يحث المستخدم على طرح‬

‫سؤال جيد ماذا عن‬

‫المجموعة‪ ،‬إذا أمكن‪ ،‬أردت‬ ‫سؤال آخر من‬

‫أن أسأل كيف يمكنني تقديم عدة‬

‫‪،‬تعليقات‬

‫أوه‪ ،‬كيف يمكنك كتابة العديد من أسطر التعليقات‬

‫إذا كنت أسمعك بشكل صحيح‪ ،‬متأكد من أنك‬

‫ستستمر في‬

‫القيام بها بهذه الطريقة‪ ،‬فقط قم‬


‫بوضع بادئة لكل سطر من السطور‬

‫رمز التجزئة كما أفعل هنا‪ ،‬هناك‬

‫أسلوب آخر للقيام‬

‫بتعليقات متعددة األسطر في بايثون والتي تميل في الواقع إلى أن يكون‬

‫لها معنى خاص‪ ،‬يمكنك عمل ثالث‬

‫عالمات اقتباس مزدوجة مثل هذا ثم‬

‫أي شيء بينهما هنا هو تعليق‬

‫يمكنني استخدام‬ ‫وهذا أسلوب آخر أو أنت‬

‫عالمات االقتباس المفردة أيًض ا ولكن المزيد عن تلك التي‬

‫أعتقد أنها في وقت آخر‪ ،‬حسًنا‪ ،‬إذا‬

‫كنت ال تمانع‪ ،‬اسمح لي بالمضي قدًم ا هنا ومعرفة‬

‫كيف يمكننا تحسين هذا‬

‫البرنامج بشكل أكبر وكذلك تقديم بعض‬

‫الميزات األخرى التي قد نرغب في‬

‫مع مرور الوقت‬ ‫‪،‬اتخاذها‬

‫اتضح أنه يمكننا بالتأكيد تحسين‬

‫هذا البرنامج ألنه من‬

‫المخيب لآلمال بعض الشيء أنني أقطع هذه‬

‫الزاوية وأقول مرحًبا بفاصلة ثم‬

‫على سطر جديد يطبع االسم كما لو أنه‬

‫استخدمه‬ ‫يمكننا القيام بعمل أفضل ومعظم البرامج التي تفضلها‬

‫على هاتفك أو الكمبيوتر المحمول الخاص بك بالتأكيد‬

‫احتفظ بالنص مًعا عندما يريد الناس‪ ،‬فكيف‬

‫يمكننا القيام بذلك بشكل جيد؟‬

‫هناك عدة طرق مختلفة وفي الواقع‬

‫الهدف هنا ليس حل‬

‫هذه المشكلة الواحدة بقدر ما هو توضيح‬

‫برمجة بايثون‬ ‫ذلك والتأكيد عليه‬

‫ولغات أخرى‪ ،‬هناك العديد من الطرق‬


‫أحياًنا لحل نفس المشكلة‪ ،‬وإليك‬

‫‪،‬طريقة واحدة لحل هذه المشكلة‬

‫دعني أدخل هنا واسمحوا لي بالمضي قدًم ا اآلن‬

‫وأقول مرحًبا بفاصلة‬

‫واسمحوا لي فقط أن أضيف إلى نهاية ذلك‬

‫المستخدم‬ ‫لذلك أستخدم عالمة الجمع ‪name‬‬

‫بطريقة مثيرة لالهتمام‪ ،‬هذه ليست‬

‫إضافة في حد ذاتها‪ ،‬فأنا ال أقوم بإضافة أرقام بشكل‬

‫واضح ولكني أرغب في إضافة‬

‫اسم الشخص إلى سلسلة النص‬

‫إلى‬ ‫مرحًبا فاصلة حسًنا‪ ،‬دعني أذهب اآلن لألسفل‬

‫إذا ‪ python‬نافذتي الطرفية وقم بتشغيل‬

‫مرة أخرى ما هو اسمك ‪ hello.pi‬أدخل‬

‫أدخل ‪ David‬وسأكتب‬

‫‪،‬حسًنا‪ ،‬إنه أفضل‬

‫إنه أفضل ولكن هناك خطأ بسيط‬

‫وإن كان جمالًيا هنا هناك‬

‫مساحة مفقودة ولكن دعنا نستخدم بعض الحدس‬

‫أنا أفتقد المساحة بعد‬ ‫هنا جيًدا إذا‬

‫الفاصلة‪ ،‬لماذا ال أمضي قدًم ا‬

‫وأضفها يدوًيا هنا‪ ،‬دعني اآلن أعد تشغيل‬

‫أدخل ‪ python of hello.pi‬برنامج‬

‫وها نحن اآلن لدينا ‪david enter‬‬

‫شيء يبدو أجمل قليًال‬

‫مصطلحات قواعد اللغة اإلنجليزية مرحًبا‬

‫بفاصلة ديفيد واآلن إذا قمنا بالترجيع‬

‫ربما الحظت من قبل أو تساءلت عن‬

‫سبب حصولي على هذه المساحة اإلضافية على ما يبدو بعد عالمة‬

‫االستفهام‪ ،‬أي هنا‬

‫توجد مسافة بعد عالمة االستفهام‬


‫قبل االقتباس المزدوج وكان ذلك‬

‫أردت أيًض ا تحريك‬ ‫فقط من أجل الجماليات‬

‫مؤشر المستخدم مسافة واحدة إلى اليمين‬

‫بحيث عندما أكتب اسمه أو‬

‫يكتب اسمه‪ ،‬فإنه ال يكون‬

‫بجوار عالمة االستفهام نفسها مباشرة‬

‫‪،‬ولكن هناك طرق أخرى يمكننا من خاللها القيام بذلك‬

‫وتبين أن بعض الوظائف تطبع‬

‫من بينها في الواقع تأخذ‬

‫وسيطات متعددة واتضح أنه إذا قمت‬

‫بفصل المدخالت إلى دالة‪ ،‬فإن ما‬

‫يسمى بالوسيطات إلى دالة‬

‫بفاصلة‪ ،‬يمكنك تمريرها ليس فقط واحدة بل‬

‫اثنين ثالثة أربعة خمسة فصاعًدا لذا اسمحوا لي بالمضي‬

‫ليس فقط مسافة فاصلة مرحًبا‬ ‫قدًم ا وتمريرها‬

‫ولكن‬

‫متبوعة باالسم وهذا‬

‫قد يكون مربًك ا بعض الشيء للوهلة األولى‬

‫ألنه لدي اآلن فاصلتان ولكن من‬

‫المهم مالحظة أن الفاصلة األولى‬

‫موجودة داخل عالمات االقتباس الخاصة بي والتي هي‬

‫الشيء‬ ‫ببساطة قواعد نحوية باللغة اإلنجليزية‬

‫الفاصلة الثانية هنا خارج‬

‫االقتباس ولكن بين ما أصبحا اآلن‬

‫وسيطتين منفصلتين لطباعة‬

‫الوسيطة األولى هي مساحة فاصلة مرحبا‬

‫والحجة الثانية هي متغير االسم نفسه لذلك‬

‫دعونا نرى كيف يبدو هذا‬

‫بيثون من مرحبا دوت بي أدخل ما‬


‫ديفيد أدخل‬ ‫اسمك‬

‫حسًنا‪ ،‬لقد قمت بالتصحيح الزائد نوًعا ما اآلن‬

‫‪،‬لدي مسافتان لسبب ما‪ ،‬حسًنا‬

‫اتضح أن هذا أمر دقيق عندما تقوم‬

‫بتمرير وسيطات متعددة لطباعتها‪ ،‬فإنه يقوم‬

‫‪،‬تلقائًيا بإدراج مسافة لك‬

‫وهذا لم يكن ذا صلة في وقت سابق ألنني‬

‫تمرير وسيطة واحدة كبيرة‬ ‫كنت‬

‫للطباعة كلها مرة واحدة باستخدام‬

‫عامل التشغيل الزائد هذه المرة‪ ،‬أقوم بتمرير ‪2‬‬

‫بسبب الفاصلة‪ ،‬لذلك إذا كنت ال أريد‬

‫تلك المساحة اإلضافية‪ ،‬فال أحتاج إلى تمرير‬

‫افعل هذا‬ ‫واحدة بنفسي‪ ،‬يمكنني فقط‬

‫والحظ اآلن إذا قمت بتشغيل هذا البرنامج مرة أخرى‬

‫بيثون إذا قمت‬

‫اآلن يبدو األمر ‪ david‬في اسمي ‪ hello.pi‬بكتابة‬

‫نحوًيا كما لو أنني قد أريد اآلن‬

‫‪،‬أي من هذه األساليب أفضل‬

‫يستخدم هذا األسلوب وظيفة طباعة مع‬

‫‪ hello comma‬وسيطتين‬

‫المتغير الذي‬ ‫واالسم‬

‫يتذكره اإلصدار السابق يستخدم من الناحية الفنية‬

‫وسيطة واحدة على الرغم من أنها تبدو‬

‫غريبة بعض الشيء‪ ،‬فهي وسيطة واحدة بمعنى‬

‫أن الكمبيوتر تماًم ا مثل‬

‫علماء الرياضيات سيفعلون ما هو موجود‬

‫داخل األقواس أوًال‪ ،‬لذلك إذا‬

‫كان لديك هذه السلسلة من‬

‫والمسافة التي‬ ‫النص داخل األقواس‪ ،‬فاصلة مرحًبا‬

‫أحتاج إلى إضافتها مرة أخرى‪ ،‬يكون لديك عالمة زائد‬


‫والتي تعني ليست إضافة في حد ذاتها ولكن‬

‫تسلسل لربط الشيء الموجود على‬

‫اليسار والشيء الموجود على اليمين‪ ،‬ويصبح‬

‫هذا في النهاية‬

‫‪ hello comma space david‬العبارة اإلنجليزية‬

‫في نهاية المطاف‬ ‫‪،‬ثم ما يتم تمريره‬

‫تعد الوظيفة شيًئا كهذا من الناحية الفنية‬

‫ولكنها تفعل كل ذلك‬

‫ديناميكًيا‪ ،‬لست أنا الذي أكتب ديفيد‬

‫سًر ا كما فعلت في‬

‫وقت سابق‪ ،‬إنها تكتشف ديناميكًيا‬

‫ما هي تلك القيمة بعد ربط‬

‫مرحًبا بقيمة االسم ثم‬

‫للطباعة‬ ‫تمريرها في النهاية‬

‫‪،‬كوسيطة الروح‬

‫اسمحوا لي بالتوقف هنا لمعرفة ما إذا كانت هناك أي‬

‫أسئلة حول‬

‫عدد الوسائط اآلن‬

‫للوظائف‪ ،‬هل‬

‫يمكننا استخدام دالة‬

‫عدة مرات لحل مشكلة معينة‬

‫يمكن أن نواجهها عدة مرات في الكود الخاص بنا‪ ،‬هل‬

‫قم بوظيفة عدة‬ ‫يمكنك استخدام‬

‫‪،‬مرات مختلفة لحل بعض المشكالت‬

‫لكن ما سنراه قريًبا هو أنك إذا‬

‫وجدت نفسك كمبرمج يحل‬

‫مشكلة بنفس الطريقة مراًر ا‬

‫وتكراًر ا‪ ،‬فسيتبين أنك ستتمكن من‬

‫ال يتعين عليك‬ ‫إنشاء وظيفتك الخاصة بحيث يمكنك‬


‫االستمرار في إعادة استخدام العناصر األساسية التي‬

‫تأتي مع اللغة‪ ،‬لقد‬

‫كنت مهتًم ا بالفاصلة وعالمة‬

‫الجمع‪ ،‬لذا بعد عالمة الجمع‪ ،‬يمكننا‬

‫إعطاء متغير واحد فقط وبعد النموذج‬

‫مرة أخرى نعطي متغيرات متعددة مثل‬

‫سؤال‬ ‫ما هو الفرق جيد‬

‫لذلك في سياق السالسل وأستمر في‬

‫هو ‪ string‬استخدام هذا المصطلح‬

‫مصطلح تقني في لغة البرمجة ومرة أخرى‬

‫يعني تسلسل نص وحرف‬

‫وكلمة فقرة كاملة حتى مع ذلك‬

‫ال يتم استخدام عامل الجمع فقط كما سنقول‬

‫راجع إضافة األرقام في لغة بايثون‬

‫مثلما نفعل على الورق والقلم الرصاص ولكنه‬

‫يستخدم أيًض ا لتسلسل السالسل على‬

‫اليسار واليمين إذا كنت تريد‬

‫الجمع ليس فقط بين سلسلتين من اليسار‬

‫واليمين ولكن أيًض ا سلسلتين ثالثة ورابع يمكنك ذلك‬

‫استمر في استخدام عالمة‬ ‫‪ plus plus‬تماًم ا‬

‫وربطها ببعضها البعض تماًم ا كما هو الحال ‪plus‬‬

‫في الرياضيات‪ ،‬وفي النهاية سيبدأ هذا في‬

‫الظهور بشكل قبيح بعض الشيء‪ ،‬وأجرؤ على القول‬

‫خاصًة إذا كان سطر التعليمات البرمجية الخاص بك‬

‫طوياًل ‪ ،‬لذا هناك طرق أفضل سنراها‬

‫قريًبا وسؤال جيد‬

‫حسًنا‪ ،‬دعني أعود إلى الكود‬

‫هنا المعني ونرى ما إذا كان بإمكاننا أن نعرض‬

‫لك بضع طرق أخرى لحل‬


‫نفس المشكلة على طول الطريق مع‬

‫التأكيد على أن ما‬

‫نتحدث عنه تقنًيا هنا نعم هو سالسل ولكن‬

‫مصطلح تقني لهذه‬ ‫هناك حتى‬

‫السالسل في بايثون‪ ،‬إنه مجرد تحريك إذا جاز‬

‫لالختصار للسلسلة كما ‪ str‬التعبير‬

‫تعلم إذا كنت قد برمجت‬

‫بلغات أخرى‪ ،‬فإن األشخاص الذين يخترعون‬

‫لغات البرمجة يحبون أن يكونوا‬

‫موجزين جًدا في صلب الموضوع لذلك نميل إلى االستخدام‬

‫عبارات قصيرة لوصف األشياء‬ ‫بشكل عادل‬

‫ليست بالضرورة كلمات كاملة‪ ،‬لذا بينما‬

‫قد تقول سلسلة من الناحية الفنية في لغة بايثون‪ ،‬فإن‬

‫ما نتحدث عنه حًقا هو أن‬

‫تسلسالت النص هذه عبارة عن‬

‫أدوات تحريك من الناحية الفنية‪ ،‬وهذا نوع فعلي من البيانات‬

‫في البرنامج ولكننا سنرى قريًبا أن هناك‬

‫أنواع أخرى من البيانات في البرامج‬

‫أيًض ا‪ ،‬في الواقع‪ ،‬دعونا نرى ما إذا كنا ال نستطيع‬

‫تحسين هذا بطريقة أخرى‪ .‬أحب‬

‫التقدم الذي أحرزناه من خالل الحفاظ على‬

‫كل شيء على نفس السطر‪ ،‬مرحًبا بفاصلة‬

‫‪،‬ديفيد‬

‫كل ذلك على نفس السطر‪ ،‬ما الذي‬

‫لقد قمنا بحل هذه‬ ‫يمكن أن يكون أكثر من ذلك‬

‫المشكلة بشكل جيد‪ ،‬اتضح أنه لم يكن علينا‬

‫أن نتخلى تماًم ا عن استخدام‬

‫الطباعة مرتين‪ ،‬دعني أرجع قليًال‬

‫وأعود إلى هذا اإلصدار السابق‬

‫حيث لم أكن متأكًدا حًقا من كيفية حل‬


‫المشكلة‪ ،‬لذلك كنت أستخدم الطباعة مرة واحدة‬ ‫هذه المشكلة‬

‫لطباعة الترحيب فقط في المسافة‬

‫والفاصلة‪ ،‬ثم أستخدم الطباعة مرة أخرى‬

‫لالتصال بطباعة االسم الذي‬

‫لم يكن بالمعنى الدقيق للكلمة سيًئا ولكن كان هناك هذا‬

‫التأثير الجانبي المرئي الذي لم أفعله‬

‫يبدو األمر قبيًح ا أن يكون هذين‬

‫السطرين من النص منفصلين عن بعضهما‬

‫البعض ولكن هناك طريقة أخرى إلصالح‬

‫هذا األمر بوضوح‪ ،‬يبدو‬

‫أن وظيفة الطباعة تقوم تلقائًيا‬

‫بإخراج سطر فارغ‪ ،‬فهي تحرك‬

‫المؤشر تلقائًيا لي إلى السطر التالي‬

‫سطر ألن هذا هو السبب في أنني أرى مرحًبا‬

‫على سطر واحد وديفيد في السطر التالي‬

‫ثم أطالب بعالمة الدوالر على‬

‫السطر أدناه‪ ،‬لذا يبدو أن الطباعة‬

‫تفترض تلقائًيا أنك تريد‬

‫نقل المؤشر إلى السطر التالي‬

‫قم بتمرير بعض الحجج ولكن‬ ‫بعدك‬

‫‪،‬يمكنك تجاوز هذا السلوك مرة أخرى‬

‫تأخذ الوظائف الحجج التي تؤثر على‬

‫سلوكها‪ ،‬كل ما عليك فعله هو معرفة‬

‫ما هي تلك الحجج‪ ،‬واتضح‬

‫أننا إذا نظرنا إلى الوثائق الخاصة‬

‫بوظيفة الطباعة الخاصة ببايثون‪ ،‬يمكننا بالفعل‬

‫هنا ‪ url‬البحث عن عنوان‬ ‫‪ docs.python.org‬هذا‬

‫‪ python‬هو المكان الذي تكمن فيه جميع الوثائق الرسمية لـ‬

‫إذا بحثت في المكان‬


‫يمكنني أن أجد طريقي إلى عنوان‬

‫هذا بشكل أكثر تحديًدا هنا حيث يمكنني العثور على جميع ‪url‬‬

‫‪ python‬الوظائف المتاحة في‬

‫والوثائق وبالتالي‬

‫وبشكل أكثر دقة‪ ،‬يمكنني‬ ‫إذا ذهبت‬

‫حتى العثور على وثائق محددة‬

‫لوظيفة الطباعة نفسها‪ ،‬وبدًال من‬

‫سحب ذلك في المتصفح‪ ،‬سأمضي قدًم ا‬

‫وأبرز سطًر ا واحًدا فقط‬

‫نفسه ‪ URL‬من عنوان‬

‫وهو هذا‪ ،‬وهذا هو‬

‫لقد رأينا ذلك حتى اآلن ولكن‬ ‫الشيء األكثر تشفيًر ا بسهولة‬

‫هذا هو التوثيق الرسمي‬

‫لوظيفة الطباعة وأحد أفضل‬

‫األشياء التي يمكنك القيام بها عند تعلم‬

‫لغة برمجة هو أن تتعلم بصراحة‬

‫قراءة التوثيق ألن‬

‫جميع اإلجابات على أسئلتك‬

‫هناك على الرغم من أنه من‬ ‫ستكون بطريقة ما‬

‫المسلم به أن األمر ليس واضًح ا دائًم ا وسأقول‬

‫أيًض ا أن وثائق بايثون‬

‫ليست بالضرورة أسهل شيء‬

‫خاصة بالنسبة ألول مرة أو‬

‫مبرمج مبتدئ‪ ،‬فهي أيًض ا تتطلب التدريب‪ ،‬لذا‬

‫حاول أال تصاب باإلرهاق إذا لم تكن‬

‫إعادة النظر ولكن اسمحوا لي أن أطلعكم‬ ‫متأكًدا مما تريده‪.‬‬

‫على هذا المثال‪ ،‬هذا مرة أخرى‬

‫عبارة عن سطر من النص من وثائق بايثون الرسمية‬

‫‪،‬لوظيفة الطباعة‬
‫ما يشير إليه هذا على النحو التالي هل هذا‬

‫اسم هذه الوظيفة هو بالطبع‬

‫طباعة ثم هناك قوسين‬

‫أقواس مغلقة‬ ‫‪،‬هنا وآخر‬

‫كل شيء داخل تلك‬

‫األقواس هو الحجج‬

‫‪،‬والوسائط المحتملة للوظيفة‬

‫ولكن عندما ننظر إلى هذه‬

‫‪،‬الحجج في الوثائق مثل هذا‬

‫هناك مصطلح مختلف تقنًيا‬

‫قد نستخدمه‪ ،‬وهذه هي‬

‫لذلك عندما‬ ‫المعلمات الفنية للوظيفة‬

‫تتحدث عما يمكنك تمريره‬

‫إلى دالة وما‬

‫تسمى تلك المدخالت‪ ،‬فهذه معلمات عندما‬

‫تستخدم الوظيفة فعلًيا وتمرر‬

‫القيم داخل تلك األقواس‪ ،‬تلك‬

‫المدخالت تلك القيم هي وسيطات لذلك‬

‫نفس الشيء بالضبط‬ ‫نحن نتحدث عن‬

‫المعلمات والوسائط هي‬

‫نفس الشيء بشكل فعال ولكن المصطلحات التي تستخدمها‬

‫من النظر إلى المشكلة من‬

‫اتجاهات مختلفة عندما ننظر‬

‫إلى ما يمكن أن تأخذه الوظيفة مقابل‬

‫ما تقوم بتمريره بالفعل إلى‬

‫حسًنا‪ ،‬بناء‬ ‫الوظيفة‪ ،‬فماذا يعني ذلك‬

‫الجملة هذا غامض جًدا‬

‫ولكن في الوقت الحالي‪ ،‬اعلم فقط أن العالمة‬

‫النجمية ثم كلمة‬

‫كائنات تعني أن وظيفة الطباعة‬


‫يمكن أن تأخذ أي عدد من الكائنات التي يمكنك‬

‫تمريرها في صفر سالسل نصية وسلسلة واحدة‬

‫‪ i‬كما فعلت سلسلتين مثل‬ ‫هل أو‬

‫عدد ال نهائي من الناحية الفنية إذا كنت‬

‫تريد حًقا على الرغم من أن هذا الرمز لن‬

‫يبدو جيًدا جًدا‬

‫بعد ذلك نرى فاصلة ثم نرى‬

‫اختصاًر ا ‪ sep‬معلمة أخرى هنا تسمى‬

‫للفاصل باللغة اإلنجليزية والحظ‬

‫عالمة التساوي ومسافة االقتباس‬

‫لذا اقتبس مسافة‬ ‫المفردة‬

‫ال أعرف ما هي بعد ولكن‬

‫أعتقد أننا رأينا تلميًح ا حول ذلك‪ ،‬فلنركز على الرغم من أن‬

‫وظيفة الطباعة تأخذ معلمة أخرى‬

‫ويبدو أن القيمة االفتراضية لتلك ‪ end‬تسمى‬

‫على‬ ‫المعلمة تعتمد على هذا للحظة‬

‫‪ n‬عالمة المساواة هذه وعالمات االقتباس هذه الشرطة المائلة العكسية‬

‫إذا كنت ترغب في ‪ n‬وما هي الشرطة المائلة العكسية‬

‫التناغم في الدردشة‪ ،‬فمن‬

‫المحتمل أن أي شخص تمت برمجته من قبل قد رأى هذا‬

‫على الرغم من أنه إذا لم تقم بالبرمجة من قبل‬

‫فقد يبدو هذا خًطا مائاًل عكسًيا غامًض ا تماًم ا‬

‫‪n‬‬

‫وهي طريقة نصية‬ ‫يعني سطًر ا جديًدا‬

‫لإلشارة إلى ما إذا كنت تريد من‬

‫الكمبيوتر تحريك المؤشر بشكل فعال‬

‫إلى السطر التالي ومتى‪ ،‬وإنشاء سطر جديد من‬

‫النص‪ ،‬ومن الناحية الفنية‪ ،‬إذا قرأنا‬

‫الوثائق‪ ،‬فسنرى المزيد من التفاصيل‬


‫حول‬

‫معلمة تسمى‬ ‫حقيقة أن هناك‬

‫النهاية والوثائق الخاصة‬

‫بوظيفة الطباعة تعني فقط أن‬

‫وظيفة الطباعة هذه ستنهي كل‬

‫سطر بخط مائل عكسي افتراضًيا‪ ،‬وال ترى حرفًيا شرطة مائلة‬

‫عكسية‪ ،‬وترى‬

‫سطًرا جديًدا‪ ،‬وترى المؤشر يتحرك إلى‬

‫اآلن من خالل هذا المنطق‪ ،‬دعنا ننتقل‬ ‫السطر التالي‬

‫إلى الوراء بالنسبة للفاصل‪ ،‬حيث‬

‫يبدو أن القيمة االفتراضية للفاصل هي‬

‫مساحة فارغة واحدة‪ ،‬حيث‬

‫رأينا ذلك جيًدا ونذكره في‬

‫مثال سابق عندما قمت بتمرير وسيطة واحدة فقط‬

‫ولكن وسيطتين إلى وظيفة الطباعة‬

‫كان هناك مسافة سحرية‬ ‫وتذكر أنهما‬

‫بينهما في الواقع كان لديهم تلك المساحة‬

‫باإلضافة إلى المساحة الخاصة بي ولهذا السبب قمت‬

‫بحذف المساحة الخاصة بي ألنها‬

‫كانت إضافية في تلك المرحلة‪ ،‬وهذا يعني فقط أنه‬

‫عندما تقوم بتمرير وسيطات متعددة‬

‫للطباعة بشكل افتراضي‪ ،‬ستكون كذلك‬

‫مفصولة بمسافة واحدة افتراضًيا‬

‫‪،‬عند تمرير الوسائط للطباعة‬

‫سينتهي األمر برمته‬

‫بسطر جديد اآلن فقط من خالل معرفة ذلك واسمحوا‬

‫لي أن ألوح بيدي حرفًيا على‬

‫‪،‬بقية الوثائق ليوم آخر‬

‫هذه الطباعة يمكن أن‬ ‫هناك المزيد من األشياء‬


‫تفعل ذلك ولكننا سنركز فقط على سبتمبر وفي‬

‫النهاية‬

‫دعونا نرى ما إذا كنا ال نستطيع االستفادة من هذا اآلن‬

‫لحل هذه المشكلة األصلية‪ ،‬المشكلة‬

‫األصلية كانت هذه‪ ،‬ال أحب‬

‫كيف تظهر فاصلة "مرحًبا ديفيد" في مكانين‬

‫حسًنا‪ ،‬هذا يحدث‬ ‫مختلفين‬

‫مرة أخرى ألن الطباعة‬

‫تطبع سطًرا جديًدا تلقائًيا‪ ،‬لذا دعنا نخبرها‬

‫بعدم القيام بذلك‪ ،‬دعنا نخبرها عن طريق تمرير‬

‫وسيطة ثانية إلى االستخدام األول للطباعة‬

‫وليس شرطة ‪ unquote‬لنقول إن النهاية تساوي اقتباس‬

‫وهو اإلعداد االفتراضي ‪ n‬مائلة عكسية‬

‫لنجعلها‬ ‫تلقائًيا‬

‫‪،‬تقتبس أي شيء آخر‬

‫دعنا نتجاوز القيمة االفتراضية حتى ال يكون هناك‬

‫سطر جديد‪ ،‬ال يوجد‬

‫‪،‬شيء حرفًيا هناك ودعنا نرى ما سيحدث‬

‫دعني اآلن أذهب إلى نافذتي الطرفية‬

‫‪ python‬وأمسحها وسأقوم بتشغيل‬

‫‪of hello.‬‬ ‫سأكتب ‪ pi‬أدخل‬

‫اسمي ديفيد وأعتقد اآلن أن‬

‫كل شيء سيبقى على نفس‬

‫السطر ألنه وقد فعل‬

‫هذا السطر هنا ‪ 5‬سوف يطبع‬

‫مسافة فاصلة مرحًبا ولكن بعد ذلك ال شيء في‬

‫غيرته ليكون اقتباًسا‬ ‫‪،‬نهايته ألنني‬

‫السطر الثاني سوف‬

‫يطبع اسم ديفيد أو أًيا‬


‫كان اسم اإلنسان وسيحرك‬

‫المؤشر إلى السطر التالي ألنني لم‬

‫أتجاوز قيمة النهاية هناك فقط‬

‫أنت تفعل‬ ‫لرؤية هذا بشكل أكثر وضوًح ا إذا‬

‫شيًئا غامًض ا‪ ،‬حسًنا‪ ،‬ليس لدي أي‬

‫فكرة عما يحدث‪ ،‬اسمح لي فقط بوضع‬

‫ثالث عالمات استفهام مؤقًتا هنا وسنرى‬

‫نتائج هذا أيًضا‪ ،‬دعني‬

‫أعود إلى نافذتي الطرفية لتشغيل‬

‫ما اسمك ‪python of hello.pi‬‬

‫ديفيد واآلن‬

‫ترى حرفًيا مخرجات قبيحة حًقا ولكنك‬

‫ترى حرفًيا ما يحدث مرحًبا‬

‫بمسافة الفاصلة‬

‫ثم ثالث عالمات استفهام تنهي عبارة الطباعة ثم‬

‫لذلك ليست ‪ dav،‬ترى معرف‬

‫نتيجة جيدة ولكنها توضح‬

‫مدى التحكم الذي لدينا هنا أيًضا و‬

‫اسمحوا لي أن أرجع مرة أخرى إلى التذكير أنه في‬

‫نسختنا األخرى من هذا‬

‫‪،‬عندما مررت بفاصلة الترحيب واالسم‬

‫تم فصلهما بمسافة واحدة‪ ،‬لذا فإن‬

‫كان يعمل بشكل جيد ماذا ‪ python of hello.pi david‬إدخال‬

‫‪ sep‬لو تجاوزنا قيمة‬

‫لكوننا مساحة واحدة‬ ‫للفاصل بدًال من ذلك‬

‫يمكننا أن نقول شيًئا مثل عالمة استفهام عالمة استفهام عالمة‬

‫استفهام فقط‬

‫اللتفاف أذهاننا حول ما يحدث‬

‫‪،‬هناك‬
‫‪ python of hello.pi david‬دعني اآلن أقوم بـ‬

‫أدخل وسترى هذين المدخلين‬

‫مرحًبا فاصلة واالسم هما‬

‫مفصول اآلن بطريقة قبيحة بثالث‬

‫عالمات استفهام ألنني تجاوزت‬

‫وعلى الرغم من أن ‪ sep‬السلوك االفتراضي لـ‬

‫الوثائق تستخدم عالمات‬

‫االقتباس المفردة‪ ،‬فقد كنت معتاًدا على استخدام‬

‫عالمات االقتباس المزدوجة في بايثون‪ ،‬يمكنك استخدام‬

‫ال‬ ‫أي منهما بالمعنى الدقيق للكلمة‬

‫يهم ولكن يجب أن تكون متسًقا وأنا‬

‫دائًم ا أستخدم‬

‫وثائق بايثون ذات عالمات االقتباس المزدوجة على الرغم من أنني‬

‫أستخدم دائًم ا أسئلة عالمات االقتباس المفردة‬

‫اآلن على هذه األنواع من‬

‫المعلمات واسمح لي أن أقترح أن‬

‫نعطيها اسًم ا رسمًيا حتى‬

‫اآلن عندما نقوم بتمرير القيم‬

‫لطباعة هذه تسمى‬

‫المعلمات الموضعية الموضعية بمعنى أن‬

‫أول شيء تمرره للطباعة تتم‬

‫طباعته أوًال والشيء الثاني الذي تمرره‬

‫للطباعة بعد طباعة الفاصلة‬

‫ثانًيا وما إلى ذلك ولكن هناك أيًض ا‬

‫هذه األشياء التي رأيناها اآلن تسمى المسماة‬

‫‪ end‬أو ‪ scp separator‬المعلمات المسماة‬

‫للسطر الذي ينتهي‪ ،‬تسمى هذه ‪end‬‬

‫المعلمات ألنها اختيارية‬

‫ويمكنك تمريرها في نهاية‬

‫بيان الطباعة الخاص بك ولكن يمكنك أيًضا‬


‫‪،‬االتصال بها باالسم‬

‫كنت‬ ‫قد يكون هذا سؤااًل غريًبا ولكن‬

‫أتساءل عما إذا كان شخص ما يريد‬

‫إضافة‬

‫عالمات اقتباس فعلية داخل‬

‫عالمات االقتباس‪ ،‬نعم‪ ،‬أحب الطريقة التي تعتقد بها أن‬

‫هذا ما يمكن أن نسميه‬

‫‪،‬حالة الزاوية تماًم ا عندما نصحح هذا األمر‬

‫على‬ ‫يبدو كل هذا رائًعا‬

‫األقل مع استمرار البرمجة ولكن انتظر‬

‫دقيقة‪ ،‬ماذا لو كنت تريد طباعة عرض أسعار‪ ،‬فهذا‬

‫سؤال جيد حًقا‪ ،‬حسًنا‪ ،‬دعنا‬

‫نرى ما إذا كنا ال نستطيع معرفة ذلك لنفترض‬

‫أنني أريد طباعة‬

‫ليس فقط اسم المستخدم‪ ،‬دعني أقوم بتبسيط‬

‫سأمضي قدًم ا وأتخلص‬ ‫هذا األمر بشكل أكبر‪ ،‬دعني‬

‫من الكثير من هذا واسمحوا لي أن أقول‬

‫‪،‬شيًئا مثل مرحًبا‬

‫‪،‬ربما أكون ساخًر ا بعض الشيء هنا‬

‫مرحًبا يا‬

‫‪،‬صديقي‬

‫‪،‬أنت تعرف بهذا النوع من النغمة‪ ،‬حسًنا‬

‫لن ينجح هذا في الواقع ألنك‬

‫الستخدام عالمات االقتباس لتكون مثل‬ ‫كنت تحاول‬

‫الصديق في عالمات اقتباس األصابع ولكنك‬

‫تحاول أيًض ا إنهاء الجملة وإذا حاولت‬

‫إذا ‪ python‬تشغيل هذا فلنقم بهذا باستخدام‬

‫سترى أن هذا ‪hello dot pi‬‬

‫مجرد بناء جملة غير صالح ربما نسيت‬


‫هذا في الواقع‬ ‫فاصلة و‬

‫مزعج بعض الشيء في بعض األحيان تكون رسائل الخطأ التي‬

‫تراها مضللة مثل أن الكمبيوتر‬

‫ال يعرف اللغة حًقا ما‬

‫يحدث‪ ،‬لذا فهو يقدم أفضل تخمين ولكنه‬

‫ليس صحيًح ا بالضرورة ولكن يمكنني‬

‫حل هذه المشكلة بعدة طرق‬

‫يمكنني القيام بذلك يمكنني تغيير‬

‫عالمات االقتباس الخارجية إلى عالمات اقتباس مفردة ألنني تذكر‬

‫لحظة مرة أخرى قلت أنه يمكنك استخدام‬

‫عالمات االقتباس المزدوجة أو عالمات االقتباس المفردة طالما‬

‫أنك متسق لذلك فال بأس إذا كنت‬

‫تستخدم عالمات االقتباس المفردة في الخارج‪ ،‬فيمكنك بعد ذلك‬

‫عالمات االقتباس في الداخل‬ ‫استخدام عالمات االقتباس المزدوجة‬

‫وستراها حرفًيا‪ ،‬لذلك على سبيل‬

‫‪ python if hello.pi‬المثال إذا قمت بتشغيل‬

‫هناك نذهب مرحًبا يا صديقي‬

‫ولكن هناك طريقة أخرى إذا كنت تصر على‬

‫استخدام عالمات االقتباس المزدوجة ألنك قد ترغب‬

‫استخدم‬ ‫فقط في أن تكون متسًقا‪ ،‬يمكنك أيًضا‬

‫حرف الشرطة المائلة العكسية مرة أخرى‪ ،‬لقد رأينا‬

‫منذ لحظة وهذا ‪ n‬الشرطة المائلة العكسية‬

‫حرفًيا في ‪ n‬يعني أننا ال نريد أن يكون الحرف‬

‫اإلخراج‪ ،‬أردنا سطًر ا جديًدا‪ ،‬لذا فإن‬

‫الشرطة المائلة العكسية تمثل في الواقع ما‬

‫يسمى بحرف الهروب‪ ،‬وحرف الهروب‬

‫ال يمكنني‬ ‫هو الذي يمكنك‬

‫‪،‬الكتابة بالضرورة مرة واحدة على لوحة المفاتيح‬

‫فأنت بحاجة إلى التعبير عنها‬


‫بأحرف متعددة حتى أتمكن بالفعل من وضع‬

‫خطوط مائلة عكسية أمام‬

‫عالمات االقتباس المزدوجة الداخلية بحيث‬

‫يدرك الكمبيوتر مهال‪ ،‬انتظر لحظة‪ ،‬هذه ليست‬

‫تنتهي‬ ‫حرفية‪ ،‬وليست عالمات اقتباس‬

‫أو تبدأ التفكير في أنها‬

‫اقتباسات حرفية‪ ،‬لذا دعني اآلن أعود إلى‬

‫‪ python of hello.pi‬نافذتي الطرفية‪ ،‬قم بتشغيل‬

‫وهي اآلن تعمل جيًدا‪ ،‬لذا فإن ‪enter‬‬

‫الهروب هو أسلوب عام‬

‫يسمح لنا بالقيام بذلك أيًضا‬

‫قد اسمحوا لي أن أرجع اآلن إلى هذه‬ ‫وإذا كنت‬

‫األمثلة وأعود إلى حيث‬

‫توقفنا مع الكود الخاص بي‪ ،‬فأنا فقط أتراجع عن كل‬

‫ذلك ألنني أريد العودة إلى‬

‫النقطة في نهاية المطاف وهي تحديد اآلن‬

‫من الواضح‬ ‫طريقة نهائية لحل هذه المشكلة بشكل جيد‪.‬‬

‫أن لدينا‬

‫طريقة أخرى يمكننا من خاللها حل هذه المشكلة والتي‬

‫ربما تكون األكثر شيوًعا اآلن أو‬

‫على األقل األكثر أناقة عندما يتعلق األمر‬

‫بإعدادنا الستخدامات أطول وأطول‬

‫‪،‬للسالسل‬

‫يمكنك استخدام ميزة جديدة نسبًيا في‬

‫يسمح لك بالقيام بذلك‬ ‫‪،‬لغة بايثون والتي‬

‫يمكنك حرفًيا عدم وضع اسم‬

‫المتغير مثل ذلك في السلسلة الخاصة بك‬

‫‪،‬ألننا رأينا بالفعل أن هذا خطأ‬

‫‪،‬إذا قمت بذلك‬

‫فسترى حرفًيا اسم فاصلة مرحًبا ولكن ماذا لو فعلت‬


‫األقواس أو‬ ‫هذا ماذا لو وضعت مجعًدا‬

‫األقواس المتعرجة حول إشعار اسم المتغير‬

‫مقابل الكود‬

‫أن ‪ vs code‬يغير لونه بمهارة شديدة‪ ،‬لذا يعرف‬

‫شيًئا مثيًر ا لالهتمام يحدث‬

‫هنا‪ ،‬اسمح لي بتشغيل هذا البرنامج لكنني لم‬

‫‪ david‬أدخل ‪ python of hello.pi‬أنتهي بعد من‬

‫‪enter‬‬ ‫حسًنا‪ ،‬من الواضح أن هذا ليس ما أريده ولكني‬

‫أن هذه ‪ python‬بحاجة إلى إخبار‬

‫سلسلة خاصة‪ ،‬وهذا ما سنسميه‬

‫وهي ‪ f،‬سلسلة تنسيق أو سلسلة‬

‫في ‪ python‬ميزة جديدة نسبًيا لـ‬

‫‪ python‬السنوات القليلة الماضية والتي تخبر‬

‫قم بتنسيق األشياء في السلسلة‬ ‫بالفعل‬

‫بطريقة خاصة والرمز الذي تفعل من خالله‬

‫هذا غريب بعض الشيء ولكن هذا‬

‫‪ f‬ما يظهره العالم إذا وضعت‬

‫في بداية السلسلة‬

‫مباشرة قبل عالمة االقتباس األولى فهذا‬

‫أوه‪ ،‬هذه ‪ python‬دليل على‬ ‫ذلك‬

‫سلسلة خاصة‪ ،‬دعني أقوم بتنسيقها‬

‫بطريقة خاصة لك‪ ،‬اسمح لي اآلن بإعادة تشغيل‬

‫‪ david enter‬أدخل ‪ pythonhello.pi‬البرنامج‬

‫واآلن نرى الهدف طوال الوقت‬

‫مرحًبا بفاصلة ديفيد‪ ،‬لم نبدأ بهذه‬

‫أعتقد أننا إذا فعلنا ذلك‬ ‫الطريقة ألنني‬

‫بالطريقة األولى‪ ،‬فسوف تتساءل لماذا‬

‫نفعل هذا‪ ،‬ما هي كل هذه‬

‫الرموز السحرية ولكن هذه مجرد طريقة أخرى‬


‫لحل نفس المشكلة ولكن‬

‫اسمحوا لي أن أقترح أن نفكر اآلن في‬

‫مع السالسل‬ ‫‪،‬أشياء أخرى يمكننا القيام بها‬

‫واتضح أنه حتى أثناء‬

‫قيامنا ببعض‬

‫‪،‬العمليات البسيطة نسبًيا هنا‬

‫كنا نثق بشكل عام في أن‬

‫المستخدم سيتعاون‪ ،‬وهذا‬

‫يعني أنهم سيكتبون بالفعل‬

‫اكتب اآلن لمجرد‬ ‫ما نريدهم أن يكتبوه‬

‫أنهم يكتبون سلسلة على الرغم من أن هذا‬

‫ال يعني أنها ستبدو بالطريقة التي‬

‫نريدها أنا وأنت بصراحة‪ ،‬حيث أن البشر معتادون‬

‫بالفعل على مواقع الويب‬

‫والتطبيقات مثل الضغط على‬

‫شريط المسافة كثيًر ا عن طريق الخطأ سواء في بداية‬

‫مدخالتنا أو في النهاية ربما ألن‬

‫شريط المسافة يميل إلى أن يكون كبيًر ا جًدا‪ ،‬فمن‬

‫الشائع جًدا الحصول على مسافات غير مقصودة‬

‫قبل أو بعد إدخال بعض المستخدمين‪ ،‬أنت‬

‫وأنا بالتأكيد معتادون على عدم‬

‫كتابة الكلمات بأحرف كبيرة كما‬

‫من‬ ‫ينبغي لنا إذا كنا نرسل‬

‫المحتمل أن نكون سريعين بعض الشيء في الرسائل النصية‬

‫ونرسل كل شيء بأحرف صغيرة‬

‫على سبيل المثال‪ ،‬إذا كان هذا هو أسلوبك إذا كان‬

‫هاتفك ال يصلح المشكلة لك‪ ،‬فربما‬

‫في خطاب رسمي ستكتب‬

‫األشياء بأحرف كبيرة بشكل صحيح ولكن أنت وأنا كبشر‬

‫يمكن الوثوق حًقا بكتابة األشياء‬ ‫ال نستطيع ذلك‬


‫بطريقة لطيفة بالضرورة عند استخدام‬

‫بعض البرامج سواء كان تطبيًقا أو‬

‫موقع ويب أو أي شيء آخر ولكن‬

‫اتضح أن السالسل نفسها تأتي مع‬

‫الكثير من الوظائف المضمنة‬

‫وثائق بايثون الخاصة‬ ‫ويمكنك رؤية كل ذلك في‬

‫‪،‬هنا‬

‫نوع بيانات السلسلة الذي تحدثنا عنه‬

‫يأتي مع الكثير من الوظائف المضمنة‬

‫مما يعني أنه يمكننا التعامل مع‬

‫مدخالت المستخدم للقيام بأكثر من مجرد ربطه‬

‫بشيء آخر مثل مرحًبا‪ ،‬يمكننا‬

‫أو أعد تنسيقه‬ ‫بالفعل تنظيفه‬

‫بطريقة نأمل أن تبدو أفضل قليًال‬

‫بالنسبة لنا‪ ،‬لذا اسمح لي بالعودة إلى الكود الخاص بي هنا‬

‫واسمحوا لي أن أوضح ما قد‬

‫يحدث إذا لم يتعاون المستخدم إذا‬

‫‪ python of‬واصلت هنا وقمت بتشغيل‬

‫‪hello.pi‬‬ ‫أدخل دعني أضغط على مفتاح‬

‫المسافة بشكل غير متقن عدة مرات لماذا‬

‫لم أكن منتبًها وسأكتب‬

‫اسمي ديفيد‬

‫وال أعرف أنني ضغطت على مفتاح المسافة‬

‫فوضى‬ ‫‪،‬عدة مرات كما لو كان األمر نوًعا ما‬

‫كل شيء بأحرف صغيرة لن يبدو‬

‫بالضرورة صحيًح ا من الناحية النحوية‪ ،‬فهو‬

‫يحتوي على مسافات هنا وهنا‬

‫سيقوم البرنامج بطباعة ذلك بالضبط‬

‫وهذا يبدو سيًئا حًقا على األقل إذا‬


‫كنا نعطي األولوية للجماليات‬

‫والقواعد مثل لماذا يوجد الكثير من‬

‫الفاصلة ليست‬ ‫المسافات بعد ذلك‬

‫طريقة لطيفة جًدا لتحية المستخدمين لديك ولكن‬

‫يمكننا تنظيف هذا‪ ،‬فقد‬

‫اتضح أن هذا النوع من البيانات مدمج في سالسل‬

‫وهو مرة أخرى نوع البيانات هذا‪ ،‬إذا جاز‬

‫التعبير هذا النوع من البيانات في بايثون هو‬

‫القدرة على فعل األشياء فعلًيا لذلك‬

‫سلسلة لذا اسمحوا لي أن أفعل هذا‪ ،‬يمكنني بالفعل‬

‫المضي قدًم ا والقيام بشيء مثل هذا‬

‫االسم يساوي شريط نقطة االسم وماذا يفعل‬

‫هذا‬

‫بإزالة‬

‫المسافة البيضاء من السلسلة‬

‫وماذا أعني بهذا جيًدا على‬

‫الجانب األيمن اإلشعار الذي كتبته‬

‫‪ name‬اسم المتغير المسمى‬

‫لقد استخدمت بعد ذلك نقطة أو نقطة‬

‫ثم يبدو أنني أفعل ما هو‬

‫دالة بشكل صحيح في أي وقت رأينا هذه‬

‫الوظيفة حتى اآلن نرى أنه‬

‫اسم الوظيفة مطبوًعا أو ُم دخاًل ثم‬

‫ثم قوس آخر‬ ‫نرى قوًسا‬

‫وهذا بالضبط ما‬

‫أراه هنا ولكني أستخدم هذه الوظيفة بشكل‬

‫مختلف قليًال من الناحية الفنية‪ ،‬هذه‬

‫الوظيفة في هذا السياق تسمى‬

‫الطريقة وماذا أعني بذلك جيًدا‬


‫إذا كان االسم عبارة عن سلسلة ويعرف أيًض ا باسم التحريك جيًدا‪ ،‬فقد‬

‫إلى الوثائق‬ ‫‪،‬اتضح وفًقا لذلك‬

‫هناك الكثير من الوظائف التي تأتي‬

‫مع سالسل في بايثون ويمكنك‬

‫الوصول إلى هذه الوظيفة باستخدام‬

‫اسم سلسلة مثل االسم الحرفي‬

‫هنا ثم فترة ثم اسم‬

‫الوظيفة ثم قوس مفتوح‬

‫بعض‬ ‫وقوس مغلق ربما‬

‫الوسائط داخل تلك األقواس‬

‫ولكن في هذه الحالة ال تحتاج إلى أي‬

‫وسيطات‪ ،‬أريد فقط تجريد المسافة‬

‫من اليسار والمسافة من‬

‫يمين مدخالت المستخدم ولكن هذا ليس‬

‫كافًيا‪ ،‬أريد أن أتذكر أنني قمت‬

‫خارج تلك المساحة البيضاء على‬ ‫بتجريدها‬

‫اليسار واليمين‪ ،‬لذلك سأستخدم‬

‫عالمة المساواة مرة أخرى هنا والحظ‬

‫أنه كما كان من قبل‪ ،‬هذا ال يعني‬

‫المساواة‪ ،‬بل يعني التعيين من‬

‫اليمين إلى اليسار‪ ،‬لذلك عندما يكون هذا السطر من التعليمات البرمجية‬

‫‪.‬هنا االسم‬

‫يعود الشريط لي ويعرف أيًض ا‬

‫باسم قيمة اإلرجاع‪ ،‬وسيعيد‬

‫نفس الشيء الذي كتبه المستخدم ولكن‬

‫مع عدم وجود مساحة بيضاء إضافية إلى اليسار أو‬

‫إلى اللون األبيض‬

‫إلى اليمين‪ ،‬لذا فإن تعيين عالمة المساواة‬

‫سيقوم بنسخ تلك القيمة‬

‫إلى اليسار وبالتالي‬ ‫من اليمين‬


‫تحديث القيمة داخل‬

‫متغير اسمي حتى ال تتمكن فقط من تعيين‬

‫قيم للمتغيرات‪ ،‬بل يمكنك أيًض ا‬

‫تغيير قيمة المتغيرات تماًم ا عن طريق‬

‫استخدام عامل التعيين‬

‫عالمة التساوي مراًر ا وتكراًر ا‬

‫من اليمين إلى‬ ‫وسيستمر في النسخ من‬

‫اليسار مهما كانت القيمة الجديدة‪ ،‬لذا‬

‫‪ python of‬اآلن إذا قمت بإعادة تشغيل هذا البرنامج‬

‫‪hello.pi‬‬

‫أدخل‬

‫‪ space‬فلنفعل ذلك مرة أخرى ‪ david‬لدي‬

‫وكل ‪spacebase space space dav id‬‬

‫المساحات الصغيرة تدخل‬

‫فمن األفضل أنه لم يتم إصالح‬

‫لذلك ما زلت غير‬ ‫الكتابة باألحرف الكبيرة‬

‫األول ولكنه ‪ d‬متقن بعض الشيء مع الحرف‬

‫جرد كل تلك المساحة اإلضافية‬

‫والتفاصيل الصغيرة للغاية‪ ،‬مثل هذا ليس‬

‫مثيًر ا للغاية ولكنه يتحدث فقط عن‬

‫قوة ما يمكنك فعله بمجرد‬

‫سطر واحد من التعليمات البرمجية اآلن‪ ،‬ما الذي يمكنني‬

‫فعله هنا بشكل جيد؟‬

‫يمكنني االستفادة من مدخالت المستخدم‪ ،‬دعني أمضي قدًم ا وأجرب‬

‫ذلك‪ ،‬اتضح أنه يمكنني أيًض ا‬

‫استخدام هذا االسم‬

‫بنقطة كبيرة‪ ،‬لذلك اسمحوا لي بالمضي قدًم ا‬

‫أنا‬ ‫وتكبير اسم المستخدم ومرة أخرى‬

‫أدلي بتعليقات وال توجد طريقة واحدة صحيحة‬


‫لكتابة التعليقات‪ ،‬أنا فقط أستخدم‬

‫بعض العبارات اإلنجليزية القصيرة هنا‬

‫لتذكير نفسي بما أفعله وما‬

‫يحدث اآلن هنا‪ ،‬حسًنا‪ ،‬اسمح لي بالمضي‬

‫‪ hello.pi‬إذا ‪ python‬قدًم ا وتشغيل‬ ‫أدخل‬

‫‪، Spacebase، Spacebase، David،‬مساحة‬

‫أدخل ‪Space،‬‬

‫حسًنا اآلن‪ ،‬إنها تبدو أجمل بغض‬

‫‪،‬النظر عن الطريقة التي كتب بها المستخدم اسمه‬

‫حتى ولو كان األمر بطيًئا بعض الشيء‪ ،‬لقد قمت اآلن بإصالح‬

‫ذلك ولكن دعونا نجرب شيًئا ما‪ ،‬أشعر‬

‫بالفضول قليًال هنا‪ ،‬ماذا عن‬

‫هذا اه‬ ‫‪ space‬وجًها لوجه ‪spacebase‬‬

‫‪،‬سأستخدم اسمي األخير ‪david spacemailin‬‬

‫أدخل اآلن‬

‫حسًنا‪ ،‬ومن المفارقات أن الكتابة باألحرف الكبيرة ال تستخدم‬

‫حرًفا كبيًر ا حًقا في كل ما نريده‪ ،‬فمن‬

‫الواضح أنها تستخدم‬

‫الحرف األول فقط بأحرف كبيرة‪ ،‬لذلك اتضح‬

‫‪ python‬مرة أخرى أن هناك وظائف أخرى في لغة‬

‫تأتي مع سالسل وإذا بحثنا‬

‫في الوثائق وقمنا بالتمرير‬

‫مثل هذا‪ ،‬أراهن أننا سنجد ‪ url‬عبر عنوان‬

‫حًال آخر‪ ،‬أحد هذه الحلول هو‬

‫في الواقع‬

‫‪،‬دعنا نغير هذا إلى عنوان‬

‫فهناك وظيفة أخرى تأتي‬

‫الكتابة‬ ‫مع سالسل تسمى العنوان والتي تعتمد على العنوان‬

‫باألحرف الكبيرة تماًم ا مثل الكتاب أو‬

‫اسم الشخص‪ ،‬كتابة‬


‫الحرف األول من كل كلمة بأحرف كبيرة وهذا‬

‫سيوفر لنا المزيد من العمل‪ ،‬لذلك‬

‫‪،‬دعونا نمضي قدًم ا ونقوم بتشغيل هذا‬

‫وكجانب فأنا متعب نوًعا ما اآلن في هذه‬

‫من كتابة‬ ‫طوال ‪ python python python‬المرحلة‬

‫الوقت‪ ،‬اتضح أنه عند استخدام‬

‫واجهة سطر أوامر مثل هذه‪ ،‬يمكنك‬

‫بالفعل الرجوع إلى جميع‬

‫أوامرك القديمة‪ ،‬ما فعلته منذ لحظة هو‬

‫أنني قمت بالضغط على السهم ألعلى الذي يعود على الفور‬

‫سجل جميع‬ ‫من خالل‬

‫األوامر التي كتبتها على اإلطالق‪ ،‬لذا فهذه مجرد‬

‫طريقة أسرع اآلن للممثل لتكرار نفسي‬

‫بدًال من كتابة كل شيء يدوًيا‪ ،‬اسمح لي‬

‫بالمضي قدًم ا والضغط على "إدخال‬

‫‪ dav id‬مساحة" أساًسا مساحة‬

‫جميع األحرف الصغيرة أدخل ‪mailing space space‬‬

‫أبدو أفضل اآلن‪ ،‬لقد‬ ‫اآلن‪ ،‬إنه كذلك‬

‫قمت برسملة األشياء وتنظيف األشياء‬

‫ولكن ماذا عن الكود الخاص بي‪ ،‬لدي‬

‫ثمانية أسطر من الكود اآلن أربعة منها عبارة عن‬

‫تعليقات‪ ،‬أربعة منها عبارة عن‬

‫كود فعلي‪ ،‬هل أحتاج حًقا إلى هذا جيًدا وليس‬

‫يمكنني أيًضا القيام بذلك في‬ ‫بالضرورة مشاهدة ما أقوم به‬

‫دعني ال أزعج نفسي بكتابة ‪python،‬‬

‫اسم المستخدم بأحرف كبيرة بشكل منفصل‪ ،‬دعني أقول‬

‫هذا وأكتب‬

‫اسم المستخدم باألحرف الكبيرة‪ ،‬يمكنني ربط هذه‬

‫الوظائف مًعا‪ ،‬يمكنني إضافة عنوان إلى‬


‫نهاية هذا واآلن ما يحدث‬

‫هذا‬ ‫جيًدا مرة أخرى باستخدام سطر من التعليمات البرمجية مثل‬

‫عليك أوًال التركيز على ما هو على يمين‬

‫عالمة التساوي ثم سنصل إلى‬

‫يسار عالمة التساوي ما هو على‬

‫يمين عالمة التساوي هذا السطر هنا‬

‫جيًدا ماذا يعني هذا احصل على قيمة‬

‫متغير االسم مثل ديفيد‬ ‫‪space‬‬

‫‪m-a-l-a-n‬‬

‫ثم قم بإزالة المساحة البيضاء على‬

‫اليسار واليمين التي ستعيد‬

‫قيمة وستعيد‬

‫دون أي ‪david space m-a-l-a-n‬‬

‫مسافة بيضاء إلى اليسار أو اليمين ماذا‬

‫تريد أن تفعل بهذه القيمة المرجعة التي‬

‫‪ python‬تريدها‬ ‫لحالة العنوان التي تمر‬

‫عبر كل كلمة في هذه‬

‫السلسلة الناتجة وتثبيت الحرف األول من‬

‫الكلمة األولى والحرف األول من‬

‫الكلمة الثانية وما إلى ذلك‪ ،‬وبعد ذلك يمكننا اآلن‬

‫إنهاء تفكيرنا ونسخ كل‬

‫شيء‬

‫نفس‬ ‫من اليمين إلى اليسار إلى‬

‫متغير االسم وأنت تعرف ما الذي يمكنني القيام‬

‫به حتى خطوة واحدة إلى األمام‪ ،‬لماذا ال‬

‫نمضي قدًم ا ونفعل هذا إذا أردنا‬

‫اسمحوا لي أن أتخلص من كل ذلك واسمحوا لي‬

‫فقط أن أقوم‬

‫بالتجريد والعنوان كل ذلك في السطر األول‬

‫لقد انتقلنا اآلن من ثمانية أسطر‬ ‫و‬


‫‪،‬من التعليمات البرمجية إلى أربعة‬

‫‪،‬فهي أكثر إحكاًم ا وأكثر إتقاًنا‬

‫وعلى الرغم من أن األشخاص المعقولين قد‬

‫يختلفون‪ ،‬يمكن القول إنها أفضل‬

‫ألنه من األسهل قراءة عدد أقل من‬

‫أسطر التعليمات البرمجية‪ ،‬وفرص أقل‬

‫تابع‬ ‫لألخطاء‪ ،‬فهي تسمح لي بذلك‬

‫مشكلتي التالية‬

‫لحلها حسًنا‪ ،‬دعني أتوقف هنا وأرى ما إذا كان هناك‬

‫‪،‬أي أسئلة حول هذه الطرق‬

‫فالطريقة هي وظيفة مدمجة في‬

‫نوع من القيمة مثل هذه الوظائف‬

‫التي رأيناها منذ لحظة ‪ f‬أو في سالسل‬

‫نعم مرحًبا‪ ،‬شكًر ا ديفيد أم‪ ،‬فهل هناك طريقة‬

‫إلزالة المسافات بين‬

‫المسافات التي ربما أضفتها‬

‫إجابة قصيرة ال إذا قرأت‬

‫‪،‬سابًقا ‪ url‬الوثائق على نفس عنوان‬

‫فسترى أن هذا الشريط تتم إزالته من‬

‫ليس بينهما‪ ،‬في‬ ‫اليسار واليمين ولكن‬

‫الواقع هناك وظيفتان أخريان‬

‫‪ l-strip‬تأتيان مع سالسل‪ ،‬إحداهما تسمى‬

‫والتي تتيح ‪ r strip‬واألخرى تسمى‬

‫لك القيام بواحدة أو أخرى إذا أردنا‬

‫‪،‬البدء في التخلص من المساحة في المنتصف‬

‫للقيام بخدعة مختلفة‬ ‫فسنحصل على‬

‫‪،‬مًعا‬

‫كم عدد‬
‫الوظائف التي يمكننا دمجها مثل هذا العنوان النقطي الذي‬

‫قمت بدمجه‪ ،‬فكم‬

‫يمكننا دمجها‪ ،‬نعم سؤال جيد حًقا من الناحية‬

‫الفنية بقدر ما تريد ولكن في‬

‫مرحلة ما ستبدأ التعليمات البرمجية الخاصة بك‬

‫يبدو األمر سيًئا حًقا ألن‬ ‫في‬

‫سطر التعليمات البرمجية سيصبح‬

‫طوياًل جًدا‪ ،‬وفي النهاية‬

‫ربما يلتف مراًر ا وتكراًر ا‪ ،‬لذلك في‬

‫مرحلة ما تقول نوًعا ما مثل اه‬

‫هذا كثير جًدا وتبدأ في تقسيمه‬

‫كما فعلت‬ ‫إلى أسطر متعددة‬

‫ربما إعادة تعيين القيمة للمتغير‬

‫حسب الحاجة وهذا في الواقع‬

‫سؤال جيد إذا كان بإمكاني التركيز‬

‫على سؤالك‪ ،‬أعني ما الذي‬

‫يعتقده الناس إذا كان بإمكاننا المضي قدًم ا ووضع‬

‫‪،‬أيدي الجميع جانًبا للحظة‬

‫دعني أسأل‬

‫الطريقة التي قمت بها اآلن باستخدام الشريط‬ ‫هذا‬

‫والعنوان وإدخال كل ذلك في نفس السطر‬

‫‪،‬أفضل من أسلوبي السابق في التكبير‪/‬التصغير‬

‫يمكنك استخدام أيقونة نعم أو أيقونة ال‬

‫إذا كنت تعتقد أن هذا اإلصدار أفضل فقل‬

‫نعم‬

‫كان اإلصدار‬ ‫إذا كنت تعتقد هذا السابق‬

‫أفضل‬

‫على سبيل المثال هذا اإلصدار هنا حيث أوضحنا‬

‫كل شيء قل ال‬


‫ثم سنرى لماذا اقترحت في وقت‬

‫سابق أن األشخاص المعقولين‬

‫يمكن أن يختلفوا وهذا هو الحال تماًم ا‪ ،‬فإن‬

‫القيام بذلك بطريقة أو بأخرى ليس كذلك‬

‫األفضل بالضرورة على األقل إذا كان بإمكانك‬

‫تبرير ذلك‪ ،‬دعني أعود إلى‬

‫‪،‬اإلصدار األحدث هنا‬

‫حسًنا‪ ،‬لذلك نحن نرى الكثير من اإلجابات بنعم‬

‫والكثير من ال‪ ،‬فلماذا ال نمضي قدًم ا‬

‫يمكننا أن‬ ‫ونطلب إحدى اإلجابات بنعم إذا‬

‫يصوت شخص ما بنعم‪ ،‬لماذا تعتقد أن‬

‫اإلصدار الحالي من هذا الرمز‬

‫أفضل بالفعل من‬

‫‪،‬اإلصدار األطول السابق من الكود‬

‫وأعتقد أنه أكثر قابلية للقراءة لذا أستطيع أن أقول‬

‫مرحًبا‪ ،‬هذا هو االسم من هذا هو‬

‫متغير االسم‬

‫أدخل ثم أزل‬ ‫الذي يحصل عليه‬

‫المسافة وأعطها عنواًنا وها أنت ذا‬

‫لديك اسم مرحًبا‪ ،‬نعم أعتقد أن هذا‬

‫معقول جًدا‪ ،‬إنه‬

‫قابل للقراءة للغاية على األقل إذا كنت معتاًدا‬

‫على القراءة من اليسار إلى اليمين ألنك في اللغة اإلنجليزية‬

‫يتدفق نوًعا ما بشكل‬

‫طبيعي جًدا ونتيجة لذلك‪ ،‬فإن الخطوط ليست‬

‫طويلة حًقا‪ ،‬ومن المؤكد أنها تتالءم بشكل‬

‫جيد مع الشاشة‪ ،‬لذلك أعتقد أن هذه‬

‫حجة جيدة ماذا عن النقطة المقابلة‬

‫"" إذا كان بإمكاننا‬


‫ال‬ ‫على الرغم من أن شخًص ا صوت بـ‬
‫االتصال بشخص يعتقد أن هذا أسوأ‬

‫إنه غير‬ ‫ألنه‬

‫‪،‬موثوق به على اإلطالق‬

‫يبدو‬

‫‪،‬أنه سطر طويل للغاية‬

‫‪،‬لذا أعتقد أنه من األفضل الفصل‬

‫‪،‬نعم‪ ،‬أعتقد أن هذا مقنع جًدا‬

‫‪،‬صحيح أنه أصبح أطول قليًال‬

‫وعلى الرغم من أن جملتي هنا ما هو‬

‫اسمك قصيرة نسبًيا‪ ،‬يمكنك أن‬

‫يمكن أن يصبح هذا أكثر قبًح ا‬ ‫تتخيل ذلك‬

‫بسرعة إذا كنت أطرح‬

‫سؤاًال أطول على المستخدم والذي سيجعل‬

‫هذا السطر من التعليمات البرمجية أطول‬

‫وبالتالي أقل قابلية للقراءة‪ ،‬وقد يكون أقل‬

‫وضوًح ا بالنسبة لي أو لزمالئي أنني‬

‫العنوان ‪ strip‬أتصل بـ‬ ‫أو أنني أتصل‬

‫‪،‬قد يكون بمثابة مفاجأة غير متوقعة نوًعا ما‬

‫لذلك أعتقد أن هذا معقول‬

‫أيًض ا باختصار‪ ،‬ال توجد إجابة صحيحة‬

‫هنا وفي الواقع جزء من عملية‬

‫التحسن في البرمجة هو الحصول على‬

‫إحساسك الخاص باألسلوب أو العمل في‬

‫يصفون‬ ‫شركة حيث يمكنهم ذلك‬

‫الطريقة األفضل من األخرى ألنهم‬

‫يريدون فقط أن يفعل الجميع نفس‬

‫الشيء على الرغم من أن األشخاص المعقولين‬

‫قد يختلفون في النهاية‪ ،‬على الرغم من أنه طالما أن‬

‫لديك‬
‫حجة جيدة لصالح طريقة أو‬

‫أخرى‪ ،‬في النهاية هذا هو‬

‫أنت تفعل األشياء فقط‬ ‫المهم إذا‬

‫ألنك ال تعرف حًقا أيها‬

‫أفضل‪ ،‬فهذا ليس أمًر ا رائًعا‪ ،‬ولكن إذا‬

‫بدأت في الحصول على آراء‪ ،‬ومتى‪ ،‬وإذا كان‬

‫رئيسك‪ ،‬إذا كان معلمك‪ ،‬إذا كان‬

‫زميلك‪ ،‬يمكن لصديقك أن يتحداك‬

‫إذا قمت بذلك بهذه الطريقة‬ ‫‪،‬ويقول انتظر‪ ،‬لماذا فعل ذلك؟‬

‫فقد ال يتفقون معك ولكن على‬

‫األقل لديهم إجابة ويجب أن تكون‬

‫مقنعة بدرجة كافية بشكل عام‪ .‬اآلن‬

‫تأتي السالسل مع مجموعة كاملة من‬

‫الطرق األخرى باإلضافة إلى طريقة‬

‫تسمى االنقسام والتي يمكن كما‬

‫سلسلة إلى عدة‬ ‫يوحي االسم االنقسام‬

‫سالسل فرعية أصغر إذا جاز التعبير‪ ،‬على‬

‫سبيل المثال إذا كان اإلنسان هنا‬

‫معتاًدا على الكتابة باسمه األول ثم‬

‫مسافة ثم اسمه األخير‬

‫وتريد المضي قدًم ا والترحيب بهم باالسم‬

‫في الواقع‬ ‫‪،‬األول فقط‪ ،‬حسًنا يمكننا ذلك‬

‫استفد من تلك المسافة المفردة بين‬

‫االسم األول واسم العائلة وقم بتقسيم هذه‬

‫السلسلة إلى سلسلتين فرعيتين أصغر‪ ،‬كيف‬

‫يمكننا القيام بذلك جيًدا‪ ،‬دعني أمضي قدًم ا‬

‫وأعلق بشكل استباقي بين هذه السطور بأننا على وشك تقسيم اسم المستخدم‬

‫واالسم األخير ثم‬ ‫إلى االسم األول‬


‫دعونا نمضي قدًم ا ونأخذ‬

‫متغير االسم الذي يحتوي حالًيا على‬

‫‪ david‬شيء مثل‬

‫المفترض واسمحوا لي بالمضي قدًم ا واستدعاء ‪spacemailin‬‬

‫االنقسام وتمريره كوسيطة‬

‫لتقسيم مساحة بيضاء واحدة مما‬

‫يشير إلى أنني أريد بالفعل التقسيم‬

‫اتضح اآلن أن هذا الحرف سُيرجع‬

‫االنقسام تسلسًال من‬

‫القيم‪ ،‬ومن األفضل أن يكون االسم األول ثم‬

‫االسم األخير‪ ،‬ويمكننا في الواقع في بايثون‬

‫تعيين كلتا القيمتين من هذا‬

‫التسلسل مرة واحدة لبعض المتغيرات على سبيل‬

‫المثال الفاصلة األولى واألخيرة تساوي‬

‫سيكون له تأثير من‬ ‫وهذا‬

‫اليمين إلى اليسار لوضع‬

‫القيمة األولى في المتغير األول‬

‫والقيمة الثانية في المتغير الثاني‪ ،‬لذا اآلن‬

‫في السطر األخير من الكود يمكنني الدخول‬

‫وأقول مرحًبا ليس لالسم الكامل شيًئا‬

‫مالين يمكنني فقط أن أقول مرحًبا‬ ‫مثل ديفيد‬

‫فاصلة أوًال حسًنا‪ ،‬دعنا نمضي قدًم ا‬

‫‪ python of‬ونمسح نافذتي الطرفية‪ ،‬قم بتشغيل‬

‫واضغط على زر اإلدخال‪ ،‬لن ‪hello dot pi‬‬

‫أزعجني بأي مسافة بيضاء بادئة هذه‬

‫المرة ولكن دعني أمضي قدًم ا وأكتب‬

‫‪david space malin‬‬ ‫وأعبر‬

‫‪،‬أصابعي كالمعتاد‬

‫مرحًبا ديفيد هو ما نراه اآلن‬

‫جيًدا‪ ،‬لذلك رأينا الكثير من‬


‫األمثلة حتى اآلن التي تتضمن سالسل ولكن‬

‫بالتأكيد يمكن للبرامج ولغات البرمجة‬

‫التعامل مع أنواع أخرى من‬

‫البيانات أيًضا‪ ،‬فلنمضي قدًم ا‬

‫نوع آخر شائع جًدا‬ ‫وننتقل بعد ذلك إلى‬

‫من البيانات في بايثون في البرمجة‬

‫بشكل عام‪ ،‬وهي األعداد الصحيحة والمعروفة‬

‫تماًم ا كما هو الحال في ‪ int int،‬في بايثون هو‬

‫‪ int‬قصير للسلسلة‪ ،‬فإن ‪str str‬‬

‫قصير لعدد صحيح‪ ،‬حسًنا‪ ،‬ما هو ‪ python‬في‬

‫عدد صحيح تماًم ا كما هو الحال في الرياضيات‪ ،‬إنه‬

‫مثل سالب ‪ 2‬سالب ‪2 1 0 1‬‬ ‫رقم‬

‫وعلى طول الطريق نحو الالنهاية السالبة على طول‬

‫الطريق نحو الالنهاية الموجبة ولكن‬

‫‪،‬ال توجد عالمة عشرية في عدد صحيح‬

‫إنه مجرد رقم مثل سالب اثنين‬

‫‪،‬سالب واحد صفر واحد واثنين فصاعًدا‬

‫عالم‬ ‫هذا عدد صحيح بالطبع في‬

‫الرياضيات هناك الكثير من الرموز التي‬

‫نستخدمها وقد رأينا عالمة الزائد من قبل‬

‫على الرغم من أننا استخدمناها‬

‫لغرض مختلف ولكن بايثون يدعم هذه‬

‫الرموز وأكثر والبايثون يسمح لك‬

‫بجمع األرقام مًعا باإلضافة إلى طرح‬

‫األرقام أه ضرب األرقام قسمة‬

‫عالمة واحدة فقط هنا قد‬ ‫األرقام و ال‬

‫تبدو غريبة بعض الشيء أو‬

‫غير مألوفة للناس هي عالمة النسبة المئوية ولكنها‬

‫ال تعني نسبة مئوية في هذا السياق إذا‬


‫كنت تستخدم عالمة مئوية واحدة في‬

‫برنامج بايثون وهو في الواقع‬

‫المشغل ‪ modulo‬ما يسمى بمشغل‬

‫عليك أن تأخذ الباقي‬ ‫الذي يسمح‬

‫بعد قسمة رقم على آخر‪ ،‬لذلك‬

‫سنرى أمثلة على ذلك قبل فترة طويلة‬

‫ولكن ربما تكون األربعة األولى منها‬

‫مألوفة تماًم ا‪ ،‬فقد اتضح‬

‫أنه في بايثون ال يمكنك بالضرورة أن‬

‫ال تضطر بالضرورة إلى االحتفاظ بها‬

‫‪ hello.pi‬كتابة التعليمات البرمجية في ملف مثل‬

‫‪،‬ثم تشغيله في نافذة طرفية‬

‫إحدى الميزات التي يحبها الكثير من األشخاص في‬

‫هي أنها تدعم ‪python‬‬

‫ما يسمى بالوضع التفاعلي كما يمكنك‬

‫‪ python‬البدء في كتابة تعليمات برمجية‬

‫األسطر‬ ‫وتنفيذ كل منها على الفور‬

‫بشكل تفاعلي‪ ،‬خاصة إذا كنت ال‬

‫تهتم بحفظ جميع أسطر‬

‫التعليمات البرمجية الخاصة بك‪ ،‬فأنت تريد فقط تنفيذ التعليمات البرمجية‬

‫‪،‬واستعادة بعض اإلجابات‪ ،‬لذا على سبيل المثال‬

‫هنا واسمحوا ‪ vs code‬اسمح لي بالعودة إلى‬

‫انقر على ‪ hello.pi‬لي بإغالق‬ ‫واسمحوا لي‬

‫المثلث الصغير هنا في‬

‫نافذتي الطرفية فقط لجعله‬

‫أكبر بكثير فقط مؤقًتا للحظة لذلك‬

‫اآلن‪ ،‬سأقوم ‪ dot pi‬أنا ال أقوم بإنشاء أي ملف‬

‫بمفرده في ‪ python‬فقط بتشغيل‬

‫أرى عندما أفعل هذا‬ ‫‪،‬موجهي وسوف تفعل ذلك‬


‫أحصل على بعض النتائج المبهمة‬

‫وتاريخ ووقت‬

‫آخر تحديث للبرنامج وما إلى ذلك‪ ،‬لكنني‬

‫في النهاية أحصل على ثالثة‬

‫أقواس ثالثية مثل هذا‪ ،‬وهذا هو‬

‫لذا أقوم ‪ python،‬الوضع التفاعلي لـ‬

‫‪ python‬بتشغيل‬ ‫مترجم وفي‬

‫أي وقت أكتب فاصلة سطًر ا من التعليمات البرمجية في‬

‫المترجم‪ ،‬سيتم تنفيذه‬

‫على الفور‪ ،‬ولست مضطًر ا إلى االستمرار في تشغيل لغة‬

‫بايثون مراًر ا وتكراًر ا‪ ،‬كما لو كنت في‬

‫عالم البشر إذا كنت تقف‬

‫بجوار إنسان يتحدث بعض الكلمات‬

‫لغة أخرى وأنت تجري‬

‫‪،‬محادثة معهم ذهاًبا وإياًبا‬

‫كل ذلك يحدث في الترجمة‬

‫على الفور‪ ،‬لذا ما الذي يمكنني فعله في‬

‫الوضع التفاعلي‪ ،‬حسًنا‪ ،‬يمكنني أن أفعل‬

‫شيًئا مثل إدخال واحد زائد واحد وهذا في‬

‫الواقع رمز صحيح‪ ،‬ربما ال تفكر‬

‫رمز ولكن إذا كنت تعرف القليل من‬ ‫فيه على أنه‬

‫الحساب وتعرف‬

‫األرقام وتعرف باإلضافة إلى ذلك‬

‫رمز بايثون صالح ويمكنك استخدام بايثون‬

‫حًقا كآلة حاسبة رائعة ولكن يمكنني‬

‫القيام بأشياء أخرى أيًضا إذا كنت أرغب في الطباعة‬

‫قم أيًض ا‬ ‫لنفسي مرحًبا بعالم الفاصلة أستطيع‬

‫بطباعة هذا السطر من التعليمات البرمجية هناك أيًضا‬

‫مرحًبا بالعالم‪ ،‬لذا فهو تفاعلي‬


‫بمعنى أنه في اللحظة التي تنفذ فيها سطًر ا‬

‫‪،‬من التعليمات البرمجية ترى النتيجة‬

‫فلن نفعل ذلك بشكل عام ألنه‬

‫على األقل عند تدريس اللغة‬

‫نريد أن نفعل األشياء بشكل تدريجي‬ ‫نميل إلى‬

‫ونريدك أن تكون قادًر ا على معرفة من أين‬

‫أتينا ونريد أن نكون‬

‫قادرين على تجربة األشياء مراًر ا وتكراًر ا‬

‫خاصة إذا ارتكبنا أخطاء ولكننا نعلم‬

‫‪،‬أن هذه بالفعل سمة من سمات بايثون‬

‫يسمى الوضع التفاعلي ولكن‬ ‫لذا‪-‬‬

‫دعونا نركز للحظة اآلن ليس فقط على‬

‫هذا التفاعل ولكن في الحقيقة على‬

‫حقيقة أن بايثون تدعم على ما يبدو‬

‫األعداد الصحيحة والرياضيات وبعض‬

‫تلك العمليات األساسية ودعونا نرى ما إذا‬

‫‪،‬كنا ال نستطيع إنشاء آلة حاسبة صغيرة خاصة بنا‬

‫سأمضي قدًم ا‬ ‫لذا دعنا‬

‫مرة أخرى وسأقوم بتقليص ‪ vs‬وأفتح رمز‬

‫نافذتي الطرفية وسأقوم‬

‫‪.pi،‬بإنشاء ملف جديد يسمى اآللة الحاسبة‬

‫لذا للقيام بهذا التذكير يمكنني كتابة الكود‬

‫الملف الذي‬ ‫هنا واسم‬

‫الذي ‪ dot pi enter‬أريد إنشاءه هو‬

‫يمنحني عالمة تبويب جديدة في األعلى‪ ،‬لذا فقد‬

‫‪.pi‬وأنا اآلن في اآللة الحاسبة ‪ hello.pi،‬أغلقت بالفعل‬

‫ودعنا نصنع آلة حاسبة بسيطة‬

‫تقوم ببعض اإلضافات بالنسبة لي ولكني‬

‫للقيام بذلك في ملف حتى نتمكن من‬ ‫سأفعل‬

‫تكرار هذا األمر وإجراء تغييرات لألفضل‬


‫لألسوأ بمرور الوقت‪ ،‬اسمح لي بالمضي‬

‫قدًم ا واإلعالن أوًال عن‬

‫متغيرين‪ ،‬سأقوم‬

‫باألمر الرياضي المتمثل في استدعاء‬

‫‪ x‬المتغير األول‬

‫المتغير الثاني‬ ‫‪y‬‬

‫ثم سأعطي نفسي‬

‫‪ y‬زائد ‪ x‬يساوي ‪ z‬متغيًر ا ثالًثا‬

‫ثم سأمضي قدًم ا وأطبع‬

‫اآلن‪ ،‬من المسلم به أن هذا البرنامج ليس ‪z‬‬

‫مثيًر ا لالهتمام أو مثيًر ا لالهتمام في الواقع‪ ،‬فهو‬

‫أقل إثارة لالهتمام من طباعة‬

‫على الشاشة كما كان من قبل مع‬ ‫األشياء‬

‫السالسل ولكننا سنبني على هذا ونرى‬

‫ما هي الميزات األخرى الموجودة في بايثون والتي‬

‫يمكننا االستفادة منها‪ ،‬لذا آمل أن‬

‫تعرف بايثون الرياضيات الخاصة بها كما أفعل عندما أقوم‬

‫‪.‬بتشغيل بايثون اآللة الحاسبة‬

‫أن ‪ 1‬زائد ‪2‬‬

‫يساوي‬

‫بالفعل ‪ .3‬حسًنا‪ ،‬ليس هذا‬

‫مفاجًئا وليس مثيًر ا لالهتمام‬

‫وبصراحة هذا ليس البرنامج األكثر فائدة‬

‫ألنه سيحسب دائًم ا‬

‫زائد ‪ 2‬يساوي ‪ .3‬دعنا على ‪1‬‬

‫األقل نجعل هذا البرنامج يقول‬

‫تفاعلي‪ ،‬نحن نعلم بالفعل‬ ‫أكثر قليًال‬

‫من األمثلة السابقة كيف يمكننا الحصول على‬

‫مدخالت من المستخدم‪ ،‬دعنا نعيد‬


‫‪،‬وظيفة اإلدخال هذه ودعنا نفعل ذلك‬

‫‪،‬دعني أمضي قدًم ا اآلن وفي الجزء العلوي من الكود الخاص بي‬

‫حتى ال نكون رقم ‪ x‬دعنا نغير‬

‫ستكون‬ ‫واحد‪ ،‬فلنتغير دائًم ا‬

‫مهما كانت قيمة اإلرجاع المتمثلة في مطالبة‬

‫ويمكنني استخدام أي لغة إنجليزية ‪ x‬المستخدم بـ‬

‫‪،‬أو لغة بشرية أريدها هنا‬

‫تماًم ا كما سألت من قبل ‪ x‬سأقول ما هو‬

‫ما هو اسمك وسأفعل ذلك‬

‫سأستخدم اإلدخال ‪ y‬نفس الشيء بالنسبة لـ‬

‫مرة أخرى ولكن هذه المرة قم بتغيير السؤال‬

‫‪ y‬ليكون ما هو‬

‫المناسب في هذه المرحلة وأعتقد أنني‬

‫سأترك بقية الكود‬

‫ثم أطبع ‪ y‬زائد ‪ x‬يساوي ‪ z‬بنفس‬ ‫‪z‬‬

‫ولكن الشيء الجميل اآلن هو أنني أعتقد أن‬

‫لدي آلة حاسبة تفاعلية رائعة في الوقت‬

‫طوال ‪ Oneplus 2‬الحالي‪ ،‬فهي لن تستخدم‬

‫الوقت‪ ،‬بل ستفعل كل ما‬

‫‪،‬يكتبه المستخدم باإلضافة إلى أي شيء يكتبه المستخدم‬

‫لذا دعنا نجرب هذا‪ ،‬دعني أمضي قدًم ا‬

‫البرنامج حسًنا‪ ،‬لنفعل ذلك ‪1‬‬ ‫وأجري‬

‫وبالطبع ‪ y‬سيكون ‪ x 2‬سيكون‬

‫الجميع متفقون ‪ 1‬زائد ‪ 2‬يساوي ‪3‬‬

‫هاه‬

‫ما الذي يحدث‬

‫هناك إما أن فصل الرياضيات الخاص بك قد ضللك‬

‫‪،‬أو لقد ضللك‬

‫اتصل بشخص ما هنا لمعرفة ما‬ ‫لماذا ال نفعل ذلك‬


‫إذا كنت ال تستطيع مساعدتنا في فهم سبب‬

‫الخطأ ما هو الخطأ أه أنجالي‬

‫إذا كنت أقول ذلك بشكل صحيح‬

‫أعتقد أن المشكلة تكمن في أنها‬

‫سالسل متسلسلة ألنك تستخدم‬

‫بدًال من اإلضافة ‪ plus‬عامل التشغيل‬

‫حدس مثالي للغاية لقد رأينا‬

‫أن عالمة الجمع ُتستخدم بشكل مختلف قليًال‬

‫في سياق السالسل ألنها تقوم‬

‫بتسلسل ذلك عند ربط‬

‫السلسلتين ويبدو أن هذا هو ما‬

‫يحدث هنا بالفعل على الرغم من أن‬

‫المستخدم كتب رقًم ا ولكن الشيء المثير لالهتمام‬

‫هنا هو أنه عندما تحصل على‬

‫إدخال المستخدم ألنه يستخدم لوحة مفاتيح‬

‫أو الكمبيوتر الشخصي أو هاتفه‪ ،‬فسيكون ‪ Mac‬على جهاز‬

‫دائًم ا نًص ا قد يبدو‬

‫كرقم ولكن بشكل افتراضي يأتي‬

‫من لوحة المفاتيح كسلسلة‬

‫نص‪ ،‬فكيف يمكننا‬

‫حل هذه المشكلة إذا كنا ال‬

‫نريد في النهاية التعامل مع تلك المدخالت كسالسل‪ ،‬بل‬

‫‪،‬نريد معاملتها كأرقام فعلية‬

‫فنحن بحاجة إلى وظيفة أخرى‪ ،‬وقد‬

‫اتضح في لغة بايثون أنه يمكنك التحويل‬

‫من البيانات إلى‬ ‫أحياًنا من نوع واحد‬

‫نوع آخر من البيانات على سبيل المثال من‬

‫‪ int‬السلسلة إلى‬

‫عن طريق القيام بشيء مثل هذا‪ ،‬دعني‬

‫‪ x‬أعود إلى الكود الخاص بي واسمحوا لي بتغيير‬


‫ليكون أًيا كان ‪ y‬قبل إضافته إلى‬

‫باإلضافة إلى أي ‪ x‬اإلصدار الصحيح من‬

‫‪ x‬إصدار صحيح من‬ ‫لذلك ‪y‬‬

‫ليس مجرد نوع من البيانات ‪ int‬اتضح أن‬

‫بل هو أيًض ا وظيفة وهي ‪ python،‬في‬

‫وظيفة إذا قمت بتمرير إدخال‬

‫مثل سلسلة طالما أن هذه السلسلة‬

‫تبدو كرقم مثل واحد أو مثل اثنين‬

‫سوف أقوم بتحويله إلى رقم فعلي‬

‫يمكنك إجراء العمليات الحسابية عليه‬

‫‪،‬بدًال من ذلك‬

‫لذا إذا عدت اآلن إلى‬

‫ودعني أريكم ‪ python‬نافذتي الطرفية وقمت بتشغيل‬

‫آلة حاسبة خادعة أخرى فهي عبارة عن‬

‫كلمة طويلة نوًعا ما‪ ،‬ومن الممل بعض الشيء أن‬

‫يمكنني القيام بذلك في‬ ‫أكتبها والحظ ما أقوم به‬

‫نافذتي الطرفية في واجهة سطر األوامر بشكل‬

‫لآللة ‪ cal‬عام إذا بدأت في كتابة‬

‫الحاسبة‪ ،‬فيمكنني بالفعل الضغط على عالمة التبويب‬

‫إلنهاء فكرتي‪ ،‬لذا يكون اإلكمال التلقائي‬

‫ممكًنا في نافذة طرفية مثل هذا‪ ،‬اكتب‬

‫الحرف األول أو بضعة أحرف‬

‫باستخدام عالمة التبويب‪ ،‬ستنهي‬ ‫ثم ازدهار‬

‫فكرتك أو يمكنك العودة إلى‬

‫سجلك كما فعلت مع‬

‫السهمين ألعلى وألسفل‪ ،‬اسمح لي بالمضي قدًم ا وتنفيذ‬

‫وها ‪ x2‬وما هو ‪ x1‬ما هو‬

‫نحن اآلن لدينا آلة حاسبة لألغراض العامة‬

‫لن ندعم فقط‬ ‫وهي‬


‫إضافة واحد واثنين ولكن اآلن أي‬

‫عددين صحيحين يكتبهما المستخدم واسمحوا لي‬

‫اآلن بتحسين هذا األمر بشكل صحيح‪ ،‬لقد رأينا كيف‬

‫يمكننا إجراء تحسينات على التعليمات البرمجية وأنا‬

‫ال أعرف ما إذا كان‬

‫دعنا نجرب هذا‬ ‫‪،‬سيكون بالضرورة أفضل ولكن‬

‫؟ تجدر‪ z‬هل أحتاج حًقا إلى المتغير‬

‫اإلشارة إلى أنني أقوم بإنشاء‬

‫‪ c z‬متغير يسمى‬

‫ثم أستخدمه على الفور في‬

‫السطر التالي من التعليمات البرمجية اآلن‪ ،‬وهذا ليس‬

‫مقنًعا ألنه إذا كنت تقوم بإنشاء‬

‫ثم استخدمه على الفور‬ ‫متغير و‬

‫ولكن ال تستخدمه مرة أخرى أبًدا‪ ،‬هل كنت‬

‫بحاجة حًقا إلى قضاء بعض الوقت في تقديم‬

‫رمز آخر ومتغير آخر فقط‬

‫الستخدامه مرة واحدة ومرة واحدة فقط حسًنا ربما‬

‫بهذه ‪ z‬ال ربما ال نحتاج حًقا إلى‬

‫وأفعل شيًئا‬ ‫الطريقة ربما يجب أن أذهب‬

‫‪،‬كهذا‬

‫‪،‬هنا ‪ z‬ربما يجب أن أتخلص من‬

‫‪ int‬ربما يجب علي تغيير هذا ليكون‬

‫‪،‬هنا‬

‫‪،‬هنا ‪ int‬تغيير هذا ليكون‬

‫أفعل شيًئا مثيًر ا‬

‫لالهتمام اآلن على الرغم من أنه عبارة‬

‫عن إشعار بناء جملة جديد أنه‬

‫إذا جاز التعبير‬ ‫‪،‬يمكنك تداخل الوظائف‬

‫يمكنك وضع استدعاء دالة واحد‬

‫وهو استخدام دالة‬


‫داخل استخدام دالة أخرى بحيث‬

‫تصبح القيمة المرجعة للوظيفة الداخلية هي الوسيطة أو المدخالت للوظيفة‬

‫الخارجية تماًم ا كما هو الحال في الرياضيات إذا‬

‫أقواس أقواس‬ ‫كان لديك‬

‫ربما علمك معلمك‬

‫التركيز على ما يوجد داخل‬

‫األقواس الداخلية أوًال ثم‬

‫العمل على حل المشكلة بنفس الطريقة مع‬

‫البرمجة‪ ،‬وهذا ما ستفعله لغة بايثون‪ ،‬حيث‬

‫ستنظر إلى ما‬

‫بداخل األقواس أوًال‬

‫اإلجابة ثم‬ ‫وستحصل على‬

‫ستقوم بتمرير قيمة اإلرجاع إلى‬

‫الوظيفة الخارجية‪ ،‬لذا فإن ما يحدث في‬

‫السطر ‪ 1‬اآلن هو أنه‬

‫يتم استدعاء وظيفة اإلدخال أوًال ثم‬

‫تصبح نتيجة االقتباس ‪ 1‬هي اإلدخال‬

‫‪ .‬يصبح ‪ int‬إلى الدالة‬ ‫ونفس الشيء في السطر ‪2‬‬

‫هو ‪ y‬إخراج ما هو‬

‫‪ int‬المدخل لهذه الدالة‬

‫‪ z‬واآلن ال يوجد‬

‫‪ y‬باإلضافة إلى ‪ x‬يمكنني فقط طباعة‬

‫وألنني أخذت الوقت الكافي لتحويل‬

‫‪،‬كل من هذه السالسل إلى عدد صحيح‬

‫اسمحوا لي أن أجرب هذا‬ ‫أعتقد أننا بخير لذلك‬

‫أدخل ‪.pi 1‬الثعبان من اآللة الحاسبة‬

‫و ‪2‬‬

‫وما زلنا نحصل على ‪ .3‬ليس ‪ 12‬أم ال‬


‫لقد حصلنا بالفعل على ‪ 3‬وتخلصنا ‪12 1 2‬‬

‫أيًض ا من المتغير‬

‫ألننا لم نفعل ذلك بالضرورة‬

‫يبدو أن األمر في حاجة إليه خاصة إذا تم استخدامه مرة واحدة فقط‬

‫جيًدا هنا أيًضا‪ ،‬دعني أضع‬

‫أيدي الجميع جانًبا للحظة واحدة فقط واسمحوا لي أن‬

‫أسأل كما كان من قبل‬

‫هذا اإلصدار اآلن‬

‫‪ int‬الذي يستخدم‬

‫حول استدعاءات اإلدخال‬

‫‪ z‬وال يستخدم‬

‫اإلصدار السابق‬ ‫هل هذا أفضل من‬

‫إذا كنت تريد التصويت بنعم‪ ،‬تفضل أو إذا‬

‫كنت تفضل الطريقة القديمة‪ ،‬قم بالتصويت ال‬

‫بالطريقة القديمة‪ ،‬سأتراجع عن كل هذا ألننا نصوت‬

‫بدًال من ذلك‪ ،‬بدا األمر‬

‫هكذا‪ ،‬حسًنا واسمحوا لي أن أعود إلى‬

‫يد‬ ‫اإلصدار األحدث اآلن‪ ،‬فلنأخذه‬

‫نعم شخص يعتقد أن هذا‬

‫اإلصدار األخير أفضل‬

‫وأعتقد أن هذه الطريقة أفضل ألنها‬

‫تتيح لنا أن نرى على الفور ما هي‬

‫مع ‪ y‬و ‪ x‬متغيرات‬

‫األعداد الصحيحة وبالتالي نعرف ما يمكن توقعه‬

‫منهم وكذلك وسيطة الطباعة‬

‫أكثر‬

‫بديهية نتجنب الكثير من الفوضى‬

‫في الرموز‬

‫أعتقد أن هذه كلها أسباب وجيهة ألنها‬

‫لطيفة وموجزة‪ ،‬أسطر التعليمات البرمجية‬


‫ليست طويلة جًدا‪ ،‬ال أحتاج إلى معرفة‬

‫‪،‬ألنه غير موجود ‪ z‬ما هو‬

‫فقط قم بطباعته‬ ‫يعجبني ذلك ولكن ‪x plus y‬‬

‫شخًص ا يفضل الطريقة القديمة حيث‬

‫وقمنا ‪ z‬كان لدينا‬

‫بتمرير المتغيرات الفردية بشكل أكثر وضوًحا إلى‬

‫نعم مرحًبا اه أعتقد ولكن ‪ in‬الوظيفة‬

‫اإلصدار السابق أفضل ألنه عندما‬

‫أعني أنه إذا قام المستخدم بإدخال شيء‬

‫آخر غير‬

‫لنفترض أنني أعني لنفترض أنهم يكتبون رقم واحد‬

‫واثنين مثل‬

‫ذلك‪ ،‬سيكون من األسهل تصحيح أخطاء‬

‫هذا اإلصدار أو هذا اإلصدار هنا أو‬

‫‪،‬اإلصدار القديم‬

‫حسًنا‪ ،‬هذا عادل وفي الحقيقة أنا‬

‫حريص جًدا اليوم على أفضل وجه‬

‫ال أستطيع أن أخطئ‪ ،‬لقد قمت حتى اآلن فقط‬

‫بإدخال األعداد الصحيحة عندما أتوقع أن‬

‫األعداد الصحيحة والصفوف تشير فعلًيا إلى‬

‫شيء سنعود إليه في‬

‫األسابيع القادمة كيف نتعامل فعلًيا مع‬

‫األخطاء وماذا لو لم يكتب المستخدم‬

‫الرقم واحد أو الرقم اثنان أو‬

‫رقم على اإلطالق ماذا لو كتبوا‬

‫فهذا ليس رقًم ا وأراهن ‪ cat c-a-t‬كلمة مثل‬

‫أنني ال أستطيع تحويله إلى‬

‫عدد صحيح ولكن اليوم لن‬

‫أتمنى فقط‬ ‫أركز على ذلك‬


‫أن يتعاون المستخدم ولكن هذا لن‬

‫يكون هو الحال‪ ،‬ولذا ربما تكون هناك‬

‫طريقة واحدة ستؤهلنا لتحقيق المزيد من النجاح‬

‫عندما يتعلق األمر بمعالجة هذه األخطاء‬

‫‪،‬اآلن ألغراض اليوم وهو األفضل‬

‫وأعتقد أن كًال‬ ‫أعني أنني أحب كليهما‬

‫‪،‬منكما قدم حجًج ا صحيحة جًدا‬

‫وطالما أن لديك مبرًر ا‬

‫يبدو معقواًل جًدا‪ ،‬أعني أن‬

‫هذا هو ما يهم في النهاية‪ ،‬ولكن‬

‫الحصول مرة أخرى على إحساس‬

‫بالمقايضات هنا جيًدا بهذه الطريقة أفضل‬

‫لماذا ال تفهم فقط‬ ‫إذا كان األمر كذلك‪ ،‬لماذا أو‬

‫‪،‬ماهية هذه المقايضات ولكن بشكل عام‬

‫يعد إعطاء األولوية لقابلية القراءة‬

‫أمًر ا جيًدا جًدا‪ ،‬مما يجعل التعليمات البرمجية الخاصة بك‬

‫قابلة للقراءة لشخص آخر هو أمر جيد جًدا‬

‫ومفيد جًدا بالنسبة لك أيًض ا‪ ،‬بحيث‬

‫عندما تستيقظ في صباح اليوم التالي أو‬

‫مرة أخرى في األسبوع التالي أو العام التالي‬ ‫‪،‬تأتي‬

‫يمكنك أيًض ا قراءة التعليمات البرمجية الخاصة بك دون الحاجة إلى‬

‫إضاعة الوقت في محاولة تذكر‬

‫ما فعلته‪ ،‬والبساطة تميل إلى أن تكون‬

‫أمًر ا جيًدا أيًض ا‪ ،‬كما أن الحفاظ على التعليمات البرمجية الخاصة بك‬

‫بسيطة‪ ،‬لذا كلما شعرت براحة أكبر‬

‫قد تميل إلى‬ ‫في البرمجة‪ ،‬قد تتمكن من ذلك‬

‫محاولة دمج برنامج كامل‬

‫في سطر واحد طويل على سبيل المثال‪ ،‬دعني‬

‫أفعل ذلك بشكل صحيح‪ ،‬ال يعني ذلك من الناحية الفنية‬

‫في ‪ x‬أننا ال نحتاج حًقا إلى‬


‫في ‪ y‬متغير‪ ،‬وال نحتاج حًقا إلى‬

‫إذا قمت بذلك‪ ،‬يمكنني‬ ‫متغير‪ ،‬يمكننا ذلك أيًض ا‬

‫‪،‬تماًم ا ‪y‬و ‪ x‬التخلص من‬

‫ثم يمكنني اآلن التخلص من ذلك‬

‫‪،‬وجعله مجرد سطر واحد من التعليمات البرمجية‪ ،‬لذا بمعنى ما‬

‫قد تميل إلى التفكير في أن‬

‫هذا رائع حًقا‪ ،‬لقد جعلته‬

‫قد أجادل أن هذا‬ ‫سطًرا واحًدا بسيًطا من التعليمات البرمجية‬

‫في الواقع ليس بهذه البساطة اآلن وأعتقد‬

‫أنني بدأت في دمج الكثير من األشياء التي‬

‫‪ int‬يجب أن أفكر فيها بشأن الطباعة و‬

‫واإلدخال‪ ،‬ثم يجب أن أالحظ أنه حسًنا‪ ،‬لقد‬

‫فتحت قوسين وأغلقت‬

‫هناك ميزة إضافية تجعلني‬ ‫قوسين‬

‫أفكر كثيًر ا وفي أي وقت تجعلني أعتقد‬

‫أنك تهدر الوقت وفي أي‬

‫وقت تقوم فيه بتعقيد مظهر الكود بهذه الطريقة‬

‫ستزيد من‬

‫احتمالية األخطاء واألخطاء التكتيكية‬

‫أخطاء منطقية في التعليمات البرمجية الخاصة بك‬ ‫‪،‬أو‬

‫لذا إذا كانت كل األشياء التي قمنا بها‪ ،‬فهذا هو الشيء‬

‫الوحيد الذي أود أن أقول‬

‫نعم إنه سطر واحد وهو جميل‬

‫ومضغوط ولكنه غير قابل للقراءة بدرجة كافية‬

‫وسأخجل من القيام بذلك‬

‫خاصة وأن اثنين من هذه الوظائف‬

‫تحصل المكالمات على مدخالت من المستخدم‬

‫ولكن هناك أشخاًصا عقالنيين جًدا قد‬

‫يختلفون ولكن هذا هو نوع من‬


‫رد الفعل العميق الذي يجب أن يكون لديك‬

‫في بعض األحيان عندما تبدأ التعليمات البرمجية في أن تصبح‬

‫معقدة بعض الشيء وذكية بعض الشيء‬

‫‪،‬ربما لمصلحتها‬

‫حسًنا‪ ،‬إنها ليست مجرد أعداد صحيحة‬

‫لدينا إمكانية الوصول للسماح لي باقتراح أن‬

‫ننتقل من األعداد الصحيحة إلى‬

‫‪،‬نوع بيانات آخر هنا وهو التعويم‬

‫‪،‬لذا مرة أخرى السلسلة عبارة عن تسلسل من النص‬

‫هو عدد صحيح مثل سالب واحد ‪int‬و‬

‫هو رقم ‪ float‬صفر وواحد‬

‫تسمى بشكل صحيح‬ ‫بفاصلة عشرية‬

‫قيمة النقطة العائمة ويمكنك التفكير في‬

‫النقطة العائمة على أنها الرقم العشري الذي‬

‫قد يكون هنا أو هنا مع‬

‫عدد من األرقام إلى اليسار أو‬

‫اليمين رياضًيا‪ ،‬إنه رقم حقيقي وهو‬

‫هذا هو‬ ‫رقم يحتوي على نقطة عشرية فيه‬

‫النوع الثالث من البيانات التي‬

‫تدعمها بايثون في الوقت الحالي‪ ،‬حيث تفترض اآللة الحاسبة الخاصة بنا‬

‫بسذاجة إلى حد ما أن‬

‫‪،‬المستخدم سيكتب فقط أعداًدا صحيحة‬

‫ولكن إذا كنت أرغب في دعم‬

‫قيم الفاصلة العائمة أيًض ا‪ ،‬فأعتقد أنه يمكنني إجراء‬

‫سأعود‬ ‫بعض التعديالت فقط‪.‬‬

‫هنا وبدًال من مجرد ‪ vs‬إلى رمز‬

‫إلى ‪y‬و ‪ x‬تحويل مدخالت المستخدم‬

‫أعداد صحيحة في السطر ‪ 1‬و‪ ،2‬فلنجري‬

‫تغييًر ا بسيًطا‪ ،‬فلنحوله فعلًيا‬

‫إلى عدد عشري في السطر األول وعدد‬


‫السطر الثاني هنا اآلن‬ ‫عشري في السطر األول‬

‫أعتقد أنه إذا ذهبت إلى نافذتي الطرفية وقمت‬

‫‪.‬الحاسبة ‪ python of‬بتشغيل‬

‫لذلك لم يكن هذا لينجح‬

‫من قبل إذا كنت أتوقع فقط أعداًدا صحيحة‬

‫من المستخدم ولكن اآلن بعد أن أؤيد‬

‫توقع قيم الفاصلة العائمة‬

‫واستيعابها‪ ،‬يمكنني اآلن إجراء حساب‬

‫الفاصلة العائمة أيًضا ولكن‬

‫لنفترض أنني ال أريد ذلك حًقا‬

‫اإلجابة النهائية هي‬

‫قيمة الفاصلة العائمة مثل ‪ 4.6‬سأكون‬

‫سعيًدا إذا قمنا بالتقريب إلى أقرب‬

‫عدد صحيح لذلك أريد دعم المستخدم في‬

‫كتابة قيم النقطة العائمة‬

‫بنقاط عشرية ولكن في نهاية اليوم‬

‫النتيجة إلى‬ ‫أريد فقط التقريب‬

‫‪،‬أقرب عدد صحيح ممكن على سبيل المثال‬

‫حسًنا‪ ،‬اتضح أن هنا أيًض ا‬

‫مع بعض الوظائف ‪ python‬تأتي‬

‫المضمنة وفي الواقع إذا عدنا إلى عنوان‬

‫هذا من وقت سابق حيث ‪url‬‬

‫‪،‬المضمنة ‪ python‬تم إدراج جميع وظائف‬

‫يفعل ‪ round‬فهناك واحدة تسمى‬ ‫والتي‬

‫تماًم ا كما نتوقع‪ ،‬فهو يأخذ‬

‫رقًم ا كمدخل ثم يقوم بتقريبه لنا‬

‫على سبيل المثال إلى أقرب رقم إلى‬

‫أقرب عدد صحيح‬


‫ولكن إذا نظرنا أقرب قليًال إلى تلك‬

‫الوثائق بقدر ما نستطيع هنا‪ ،‬فسوف‬

‫كيف‬ ‫أقدم مقتطًفا من هذا‬

‫تبدو الوظيفة في الوثائق‬

‫وتذكر أننا نظرنا سابًقا إلى‬

‫وثائق الطباعة وهذا‬

‫مشابه من حيث الروح حيث أن هذا يوضح لنا ليس‬

‫فقط اسم الوظيفة ولكن‬

‫معلماتها المتاحة وهي المدخالت التي‬

‫وظيفة‬ ‫يمكننا توفيرها عند استخدام هذا‬

‫ولكن هذا غامض بعض الشيء تماًم ا‬

‫كما كان األمير ويضيف بعض تركيب الجملة‬

‫لذلك دعونا نرى أن اسم هذه الوظيفة‬

‫هنا مستدير بالطبع والوسيطة األولى‬

‫عبارة عن رقم الحظ هذه المرة‬

‫أنه ال يوجد نجمة وال توجد كائنات نجمية‬

‫بالنسبة للطباعة‬ ‫‪،‬كما كان هناك‬

‫تأخذ الدالة المستديرة رقًم ا واحًدا فقط‬

‫كفترة وسيطة أولى وهي المعلمة الموضعية الخاصة بها‬

‫ولكن الحظ بناء الجملة‬

‫هذا وهو تقليد في البرمجة أو‬

‫التكنولوجيا بشكل‬

‫عام عندما ترى‬

‫أقواس مربعة ووثائق مثل هذا‬

‫على وشك رؤية‬ ‫فهذا يعني أنك‬

‫شيء اختياري‪ ،‬وما‬

‫يعنيه هذا هو أنه إذا كنت تريد تحديد‬

‫عدد األرقام التي‬

‫‪،‬تريد تقريب الدالة الدائرية إليها بشكل أكثر دقة‬


‫فيمكنك تحديدها هنا عن طريق إضافة‬

‫فاصلة ثم هذا الرقم لذلك إذا‬

‫التوثيق إذا لم تحدد‬ ‫قرأنا‬

‫عدًدا من األرقام‪ ،‬فما عليك سوى تحديد‬

‫الرقم لتقريبه إلى أقرب‬

‫عدد صحيح ولكن لنفترض أنك تريد التقريب إلى‬

‫خانة األجزاء من عشرة أو خانة األجزاء من المائة‬

‫التي تتكون من رقم أو رقمين بعد‬

‫باإلضافة إلى ذلك‪ ،‬قم‬ ‫العالمة العشرية التي يمكنك القيام بها‬

‫بتمرير فاصلة واحدة أو فاصلة اثنين لتكون‬

‫أكثر دقة‪ ،‬وهذا ما‬

‫تقوله الوثائق الموجودة‪ ،‬دعونا نرى ما‬

‫إذا كنا ال نستطيع بعد ذلك ترجمة هذا إلى‬

‫بعض التعليمات البرمجية الفعلية لنا‪ ،‬لذلك إذا عدت‬

‫وأعتبر ذلك ‪ vs‬اآلن إلى رمز‬

‫يمكنني ‪y‬و ‪ x‬أريد المضي قدًم ا وتقريب‬

‫القيام بذلك بعدة طرق يمكنني القيام بها‬

‫لكنك ‪ y‬باإلضافة إلى ‪ x‬حول‬

‫تعلم أنني أفضل في الواقع‬

‫تقسيم هذا اآلن إلى سطرين‬

‫ال أحتاج إلى ذلك ومعقول‬

‫قد يختلف األشخاص هنا ولكني أرغب في العودة إلى‬

‫حتى ‪ z‬السيناريو الذي أقوم فيه بطباعة‬

‫أتمكن من توضيح األمر بشكل أكثر وضوًح ا‬

‫يساوي ‪ z‬لنفسي لآلخرين ويقولون أن‬

‫فهي ليست ‪ y،‬زائد ‪ x‬النتيجة المقربة لـ‬

‫افعل ذلك ولكني أشعر‬ ‫بالضرورة الطريقة األفضل‬

‫براحة أكبر عند‬

‫عرض أفكاري واحدة تلو األخرى‬


‫خاصة إذا كنت أرغب في البدء في التعليق على‬

‫‪،‬كل جزء من هذه األجزاء من التعليمات البرمجية‪ ،‬حسًنا‬

‫دعني أذهب إلى نافذتي الطرفية اآلن‬

‫‪ python of Calculator.pi‬وأقوم بتشغيل‬ ‫‪ x‬ما هو‬

‫لنفعل ‪ 1.2‬مرة أخرى‬

‫ثم لنفعل ‪ 3.4‬واآلن كان في‬

‫السابق ‪ 4.6‬ولكن اآلن تم تقريبه‬

‫إلى أقرب عدد صحيح والذي‬

‫‪.‬سيكون بالطبع ‪5‬‬

‫حسًنا ماذا لو أردت‬

‫تغيير هذا قليًال ماذا لو كنت‬

‫‪،‬أردت دعم أرقام كبيرة جًدا‬

‫أرقام كبيرة بغض النظر عن‬

‫‪،‬التقريب‪ ،‬فلنفعل شيًئا كهذا‬

‫دعني أمضي قدًم ا وأجري عملية بايثون التي قمت‬

‫مرة أخرى واسمحوا لي فقط ‪ pi‬بحسابها عند‬

‫بإضافة ‪999‬‬

‫زائد ‪ 1‬والحظ أنني لست مضطًر ا إلى كتابة‬

‫نقاط على الرغم من أنني‬ ‫رقم عشري‬

‫أقوم بالتحويل إلى تعويم‪ ،‬فإن برنامجي‬

‫سيسمح لي فقط بكتابة نقاط عشرية ولكني‬

‫ال أحتاج إلى إلزام اإلجابة‬

‫بالطبع هنا يجب أن تكون وهي في الواقع‬

‫سواء قمنا بالتقريب أم ال‪ ،‬فهذه ‪1000‬‬

‫األعداد الصحيحة هنا ولكن‬ ‫مجرد عملية حسابية‬

‫في الواليات المتحدة نميل إلى تنسيق‬

‫األرقام الطويلة عن طريق وضع الفواصل أه بعد أو‬

‫قبل كل ثالثة أرقام‬

‫تقلبها البلدان األخرى وتستخدم النقاط‬


‫والفواصل بدًال من ذلك‪ ،‬هذا‬

‫إعداد نظام يمكنك تغيير ذلك على‬

‫جهاز لـ ‪ Mac‬جهاز‬ ‫أو أي ‪ python‬أو الكمبيوتر الشخصي الخاص بك أو‬

‫لغة ولكن بالنسبة لي أنا أستخدم‬

‫النهج األمريكي هنا وهو فترات‬

‫للنقاط العشرية والفواصل للفواصل‬

‫ماذا لو أردت أن يتم إخراج هذا‬

‫كفاصلة واحدة صفر صفر صفر فقط لجعل‬

‫إنها ألف‬ ‫األمر أكثر وضوًح ا‬

‫وليست شيًئا مثل‬

‫مائة‪ ،‬بل إنها أكثر فائدة عندما‬

‫تكون مثل مليون واحد فاصلة صفر صفر صفر‬

‫فاصلة صفر صفر صفر‪ ،‬أليس من‬

‫الرائع أن نتمكن من‬

‫إخراج هذه األرقام تلقائًيا أيًضا‪ ،‬فقد‬

‫هي طريقة‬ ‫اتضح أننا نستطيع ذلك هناك‬

‫تستخدم لغة بايثون‬

‫لتحديد أننا نريد‬

‫تضمين فواصل مثل هذه‪ ،‬وهنا‬

‫لدينا فرصة إلعادة‬

‫‪ f‬صديقنا القديم السلسلة‬

‫أوًال‪ ،‬دعني أفعل شيًئا غير‬

‫مثمر إلى هذا الحد أوًال‪ ،‬دعني أفعل هذا‪ ،‬دعني‬

‫قيمة‬ ‫ولكن انتظر ‪ z‬أطبع‬

‫‪ unquote z‬دقيقة‪ ،‬ال يمكنني فقط أن أقول اقتباس‬

‫ألن ذلك سيؤدي حرفًيا إلى طباعة‬

‫على الشاشة‪ ،‬لذا دعني أغلفها ‪z‬‬

‫بتلك األقواس المتعرجة كما فعلت من قبل ولكن‬

‫هذا أيًضا لم يكن كافًيا وكنت‬

‫بحاجة حرفًيا إلى إضافته‬ ‫في بداية ‪f‬‬


‫أن هذه ‪ python‬سلسلتي ألخبر‬

‫وهي سلسلة تنسيق ‪ f،‬سلسلة‬

‫سيتم اآلن طباعتها بشكل غير‬

‫‪،‬نفسها ‪ z‬مثير لالهتمام فقط قيمة‬

‫لذا سأبذل قصارى جهدي فقط‬

‫حًقا كان بإمكاني ‪ z‬لطباعة‬ ‫عندما‬

‫كوسيطة وحيدة ولكن فقط ‪ z‬تمرير‬

‫‪،‬للتأكد من أنني لم أكسرها‬

‫‪،‬فلنفعل ذلك مرة أخرى ‪ 999‬باإلضافة إلى إدخال واحد‪ ،‬حسًنا‬

‫ال يزال ألًفا‪ ،‬لذلك لم أجعل‬

‫أي شيء أسوأ ولكن الحظ أن بناء‬

‫مالحظة غامضة بعض الشيء‬ ‫الجملة هذا لألسف‬

‫أنه يمكنني فعل هذا بالفعل‪ ،‬يمكنني‬

‫‪ z‬وضع نقطتين بعد الحرف‬

‫‪،‬ويمكنني وضع فاصلة بعد ذلك‬

‫يبدو هذا غامًض ا للغاية‪ ،‬وحتى‬

‫ال بد لي من البحث باستمرار عن أشياء مثل هذه‬

‫في الوثائق لتذكر بناء‬

‫اسمح لي بتشغيله مرة أخرى‬ ‫الجملة ولكن هنا‬

‫‪.pi‬بيثون اآللة الحاسبة‬

‫‪999‬‬

‫واآلن الحظت أن الرقم قد تم ‪1‬‬

‫تنسيقه تلقائًيا بالنسبة لي إذا‬

‫‪،‬كنت في بلد أو لغة مختلفة‬

‫يمكنني تجاوز هذا تماًم ا الستخدام‬

‫الفترات بدًال من الفواصل أو العكس‬

‫في هذه الحالة‬ ‫‪،‬ولكن في هذا‬

‫يحدث هذا تلقائًيا بالنسبة لي‪ ،‬لذا‬

‫نرى أيًض ا تلميًحا لما يعنيه‬


‫تنسيق سلسلة بالفعل‪ ،‬فهناك المزيد من‬

‫القوة واإلمكانيات األكثر قوة المضمنة‬

‫في ذلك‬

‫حسًنا‪ ،‬دعني أتوقف هنا لمعرفة ما إذا‬

‫كانت هناك أية أسئلة اآلن على العوامات‬

‫التقريب‬

‫نعم‪ ،‬لذا لدي ‪ f‬أو على هذا االستخدام للسالسل‬

‫‪،‬سؤال‪ ،‬لذا عند استخدام األعدادات العائمة‬

‫هل يشبه الحد األقصى لعدد‬

‫النقاط العشرية التي يمكن أن يكون لها‬

‫سؤال جيد حًقا‪ ،‬لذا يطفو نعم‬

‫وهذه مشكلة سنعيد النظر فيها‬

‫ال يمكن لألعدادات أن تمثل أرقاًم ا بدقة ال‬ ‫قريًبا‬

‫متناهية باختصار‬

‫ألن أجهزة الكمبيوتر تحتوي فقط على قدر كبير‬

‫من الذاكرة‪ ،‬فهي تحتوي فقط على مقدار محدود من‬

‫الذاكرة‪ ،‬ولدي أنت وأنا فقط‬

‫كمية محدودة من األجهزة داخل‬

‫الكمبيوتر‪ ،‬لذلك في مرحلة ما سيتعين عليهم‬

‫اآلن أقوم بالتقريب‬ ‫التقريب‬

‫تلقائًيا بشكل فعال‪ ،‬وسيتعين على أجهزة الكمبيوتر في‬

‫النهاية القيام بذلك نيابًة عنا ولكننا‬

‫سنرى ذلك كمشكلة أساسية‬

‫‪،‬قبل فترة طويلة‬

‫اسمح لي بالعودة إلى الوراء فقط لبعض‬

‫األمثلة النهائية على التعويم قبل أن‬

‫نقدم بعض األمثلة النهائية التي‬

‫اسمح لنا ليس فقط باستخدام الدوال‪ ،‬بل بتكوين دوال‬

‫خاصة بنا‪ ،‬دعني أقترح أن نحاول‬

‫أيًض ا القيام ببعض عمليات القسمة‬


‫هنا‪ ،‬دعني أقترح أن نعدل هذه‬

‫اآللة الحاسبة اآلن لتستمر في أخذ عدد من‬

‫العوامات ولكن دعونا اآلن نفعل شيًئا ما‬

‫أبسط قليًال من‬

‫االختالف قليًال عن هذا‪ ،‬فقط قم‬

‫واسمحوا لي بالمضي قدًم ا ‪ y‬على ‪ x‬بتقسيم‬

‫والتخلص من سلسلة التنسيق الخاصة بي وأبقي‬

‫‪ z‬األمر بسيًطا اآلن لطباعة‬

‫بدًال من ذلك وما سنراه‬

‫لذا‬ ‫‪،‬هنا جيًدا مجرد قسمة بسيطة‬

‫‪ dot pi،‬من اآللة الحاسبة ‪ python‬دعونا نفعل شيًئا مثل‬

‫مثل ‪ 2‬مقسوًم ا على ‪3‬‬

‫وبالطبع أحصل على ‪ ،0.66666‬وبالنسبة‬

‫لسؤال إيثان منذ لحظة‪ ،‬يبدو أنه‬

‫محدود‪ ،‬وال يتم التقريب بطريقة غريبة‬

‫هنا ولكن يبدو أنني أرى عدًدا كبيًر ا من‬

‫هذه‬ ‫األرقام‬

‫حتمية الستخدام التعويم بهذه‬

‫الطريقة على النقيض من ذلك فقط حتى تعرف أن‬

‫األعداد الصحيحة في الوقت الحاضر في بيثون يمكن أن تكون‬

‫كبيرة كما تريدها أن تكون على عكس‬

‫اللغات األخرى‪ ،‬وال يوجد حد أعلى لمدى حجم‬

‫اآلن في بيثون ولكن ‪int‬‬

‫هناك حد لمدى دقة‬

‫قيمة النقطة العائمة‪ ،‬حسًنا‬

‫اآلن بعد أن أجريت عملية تقسيم بسيطة‬

‫هنا‪ ،‬فلنمضي قدًم ا ونقرب‬

‫هذا‪ ،‬سيكون من الجيد تقريب هذا‬

‫الرقم الطويل جًدا‬


‫وما إلى ذلك ربما فقط ‪0.666666‬‬

‫منزلتان عشريتان رأينا كيفية القيام بذلك‬

‫باستخدام التقريب على الرغم من أنه على األقل في‬

‫‪،‬وثائقه‬

‫دعنا نقرب هذا ليس إلى أقرب عدد صحيح‬

‫‪ y‬فقط مقسوًم ا على ‪ x‬عن طريق تمرير‬

‫وهي وسيطة واحدة بمجرد‬

‫‪ i‬إجراء العمليات الحسابية داخل األقواس‬ ‫ال‬

‫أريد تمرير وسيطة واحدة فقط‪ ،‬أريد‬

‫تمرير وسيطتين حتى أتمكن من تحديد‬

‫من األرقام التي كانت تذكر ‪ n‬عدد‬

‫المعلمة الثانية للجولة‪ ،‬اسمح لي بالمضي‬

‫‪ pi،‬إلى ‪ python of Calculator‬قدًم ا وتشغيل‬

‫نفس الشيء ‪ 2‬ثم ‪3‬‬ ‫سأفعل ذلك‬

‫وهنا أيًض ا نرى طريقة ‪0.67،‬‬

‫للتقريب اآلن ليس فقط إلى أقرب‬

‫عدد صحيح ولكن إلى أقرب عدد من‬

‫األرقام ولكن هناك طريقة أخرى للقيام‬

‫بذلك هنا وفي الواقع هذا يستحضر‬

‫المضي قدًم ا ‪ f f‬مثال السلسلة‬ ‫مرة أخرى‬

‫وتغيير هذا لنفترض أنك لم‬

‫تتذكر الوظيفة الدائرية أو‬

‫لسبب ما لم ترغب في استخدامها‪ ،‬بل‬

‫تريد بدًال من ذلك فقط استخدام سلسلة تنسيق‬

‫حسًنا‪ ،‬دعنا نذهب إلى هناك‪ ،‬دعني أقوم باقتباس‬

‫باستخدام ‪unquote z‬‬ ‫ولكن دعني أحيط‬

‫في ‪ f‬تلك األقواس المتعرجة‪ ،‬اسمحوا لي بإضافة حرف‬

‫البداية‪ ،‬ومرة أخرى‪ ،‬هذا ليس‬

‫مثيًر ا لالهتمام‪ ،‬ولكن هذا‬


‫فقط‪ ،‬لكنني أضفت المزيد من ‪ z‬سيطبع‬

‫‪ f‬التعقيد لتحويله إلى سلسلة‬

‫وإال بعد‬ ‫ولكن الحظت أنه يمكنني فعل شيء ما‬

‫‪،‬اسم المتغير الخاص بي بعد النقطتين‬

‫‪،‬إذا كان هذا عدًدا صحيًحا كبيًر ا‬

‫فقد أرغب في استخدام فاصلة كما كان من قبل‬

‫لفصل كل ثالثية من األرقام‬

‫بفواصل لكنني لن أستخدم‬

‫تسلساًل مختلًفا‬

‫سأقول ‪0.2‬‬ ‫‪ f‬من األحرف‬

‫وهذا أيًض ا أحد هذه األشياء الغامضة جًدا التي‬

‫يجب أن أبحث عنها باستمرار‬

‫ألنني أنسى إذا لم أستخدمها‬

‫كثيًر ا‪ ،‬لذا ال تخف إذا‬

‫بدا هذا غريًبا بشكل خاص ولكن هذا هو‬

‫وفًقا للوثائق‪ ،‬الطريقة التي‬

‫عدد ‪ f‬تحددها باستخدام سلسلة‬

‫األرقام التي تريد طباعتها‪ ،‬لذا اسمح لي بتشغيل‬

‫هذا اإلصدار من اآللة الحاسبة‪ ،‬اكتب ‪2‬‬

‫ثم ‪ 3‬نحصل على نفس الشيء تماًم ا‬

‫ولكن مرة أخرى‪ ،‬هذا يتوافق تماًم ا مع‬

‫أنه في البرمجة يمكننا في‬ ‫ادعائي‬

‫كثير من األحيان حل نفس المشكلة‬

‫بطرق متعددة‪ ،‬وهذا هو اآلن‬

‫‪ f‬نهج السلسلة‬

‫لهذه المشكلة تماًم ا‪ ،‬حسًنا‬

‫أيهما أفضل‪ ،‬يعتمد األمر‬

‫في هذه الحالة على أنهما متكافئان إلى حد كبير‬

‫من المفيد‬ ‫يمكنك تخيلهما على الرغم من كونه‬

‫استخدام دالة في بعض األحيان حتى تتمكن‬


‫باعتبارها ‪ n‬من تمرير وسيطة مثل أرقام‬

‫الوسيطة الثانية أو يمكنك أن تتخيل أنك‬

‫قررت مسبًقا أنك تريد‬

‫النقطة الثانية ثم‬

‫‪،‬كتابتها على هذا النحو‬

‫فلننتقل اآلن من التركيز على‬

‫وعلى العوامات‬ ‫السالسل واألعداد الصحيحة‬

‫‪،‬للتركيز اآلن على الوظائف نفسها‬

‫بدأنا اليوم بالتركيز على كيفية‬

‫استخدام الوظائف التي تأتي مع بايثون ولكن‬

‫ألن يكون من الجيد أن تتمكن من اختراع‬

‫وظائفك الخاصة خاصة إذا‬

‫وجدت نفسك في وقت سابق تحل‬

‫نوع من المشاكل مراًر ا وتكراًر ا‪ ،‬من‬ ‫نفس المشكلة‬

‫مع ‪ python‬الجيد أن تأتي لغة‬

‫وظيفة الطباعة ألنه من‬

‫المفيد حًقا أن تكون قادًر ا على طباعة األشياء على‬

‫الشاشة ولكن ألن يكون من الجيد أن‬

‫تتمكن من طباعة أشياء محددة على‬

‫الشاشة بمجرد استدعاء وظيفتك الخاصة‬

‫‪،‬حسًنا‪ ،‬دعني أقترح أن نفعل هذا‬

‫هنا واسمحوا لي أن ‪ vs‬دعني أعود إلى رمز‬

‫وسأعيد ‪ hello.pi‬أقترح أن نعود إلى‬

‫حيث تركناه ‪ hello.pi‬فتح‬

‫اآلن‬ ‫من قبل وسأمضي قدًم ا‬

‫ونقترح أن نفكر في كيفية‬

‫البدء في تحسين هذا األمر بشكل أكبر من خالل إنشاء‬

‫وظيفتنا الخاصة‪ ،‬لقد كتبت العديد من‬

‫البرامج اليوم التي تقول مرحًبا فقط وفي‬


‫كل مرة أستخدم الطباعة ولكن ألن‬

‫يكون األمر رائًعا لو كان ذلك منذ بداية‬

‫اليوم يمكننا فقط استدعاء وظيفة‬

‫‪.‬تسمى مرحًبا والتي تقول مرحًبا لنا فقط‬

‫اآلن لم يعتقد مؤلفو لغة بايثون منذ سنوات‬

‫أننا بحاجة إلى‬

‫وظيفة خاصة فقط لنقول مرحًبا ولكني‬

‫أود أن تكون موجودة‪ ،‬فأنا أقول مرحًبا‬

‫في األوقات التي أريد فيها فقط أن أكون قادًر ا على‬ ‫كثيًر ا‬

‫استدعاء وظيفة مرحًبا‪ ،‬لذا سأبدأ‬

‫من الصفر هنا وسأقوم‬

‫بحذف كل التعليمات البرمجية الخاصة بي التي كانت موجودة سابًقا‬

‫وسأتظاهر للحظة‬

‫بوجود وظيفة تسمى مرحًبا‬

‫وسأفعل تماًم ا كما فعلت قبل أن‬

‫أحصل على اسم المستخدم من خالل‬

‫وظيفة اإلدخال التي تسأل ما هو‬

‫اسمك وعالمة االستفهام‬

‫واآلن سأستدعي وظيفة‬

‫مرحًبا‬

‫أخرج‬ ‫ثم سأقوم بالطباعة‬

‫اسم المستخدم‬

‫اآلن وسأعترف بأن مرحًبا غير موجود‪ ،‬لذا فإن‬

‫األشياء السيئة على وشك الحدوث ولكن دعنا‬

‫نرى ما الذي يسمح لي بالنزول إلى‬

‫‪ python of hello.pi‬نافذتي الطرفية‪ ،‬دعني أقوم بتشغيل‬

‫وأعتقد أن السطر األول سيكون على‬

‫ألن ذلك نجح من قبل‪ ،‬وهو‬ ‫ما يرام‬

‫بالفعل يطالبني بإدخال اسمي‪ ،‬لذا‬

‫دعني أكتب ديفيد‪ ،‬يبدو أن السطر الثاني من‬


‫التعليمات البرمجية يستدعي وظيفة‬

‫‪،‬تبدو وكأنها تسمى مرحًبا‬

‫ألنه لماذا تحتوي هذه الوظيفة على‬

‫قوسين وقوسين مغلقين‬

‫هذا هو الشكل الذي تبدو عليه‬ ‫مباشرة بعد ذلك‬

‫كل وظيفة استخدمناها‬

‫ولكن بايثون لن يتعرف على‬

‫هذه الوظيفة عندما أضغط على زر اإلدخال اآلن أحصل على‬

‫خطأ في االسم‪ ،‬لم يتم تعريف االسم مرحًبا‪ ،‬هل‬

‫تقصد المساعدة‬

‫لم أفعل ذلك على الرغم من أن ذلك مناسب‪ ،‬وهذا‬

‫أحتاج في هذه المرحلة إلى بعض المساعدة‬ ‫ما‬

‫ولكني‬

‫أواجه هذا الخطأ ألن‬

‫الوظيفة غير موجودة‪ ،‬فكيف‬

‫أجعل هذه الوظيفة موجودة جيًدا؟‬

‫أحتاج إلى إنشائها بنفسي باستخدام هذه‬

‫الكلمة األساسية‬

‫‪def def‬‬

‫للتعريف‪ ،‬لذا هنا أيًض ا تماًم ا كما هو الحال‬

‫اختصار للسلسلة واختصار لـ‬

‫قصير لتعريف ‪integer def‬‬

‫ما إذا كنت تريد تحديد إنشاء‬

‫اختراع وظائفك الخاصة‪ ،‬يمكنك القيام بذلك‬

‫لذا ‪ python‬اآلن باستخدام هذه الكلمة األساسية في‬

‫دعني أعود إلى الكود الخاص بي هنا واسمحوا لي أن‬

‫ربما نحدد هذا‬ ‫أقترح ذلك‬

‫بهذه الطريقة‬

‫في الجزء العلوي من الملف الخاص بي‪ ،‬سأتوقف‬


‫أوًال للحظة لتعريف وظيفة‬

‫‪ def‬تسمى مرحًبا باستخدام‬

‫‪،‬افتح قوسين‪ ،‬وأغلق قوسين ‪hello،‬‬

‫ما يعنيه هذا اآلن هو أن بايثون‬

‫من التعليمات البرمجية التي قمت‬ ‫ستتعامل مع كل سطر‬

‫بوضع مسافة بادئة تحتها باعتبارها‬

‫‪،‬معنى هذه الوظيفة الجديدة مرحًبا‬

‫لذا فإن التحديد مهم كما هو الحال بالنسبة للمساحة التي‬

‫أحصل عليها الختيار اسم الوظيفة‬

‫وأنا أختار تسميتها مرحًبا بين‬

‫األقواس مع عدم وجود أي شيء بالداخل يعني‬

‫لن تأخذ الوظيفة في الوقت الحالي‬ ‫أن هذا‬

‫أي مدخالت وال توجد وسيطات‬

‫هناك أيًضا النقطتان تعني ترقب‬

‫بعض المسافة البادئة‪ ،‬كل ما يتم وضع‬

‫مسافة بادئة له أسفل هذا السطر من التعليمات البرمجية‬

‫سيكون جزًء ا من هذه الوظيفة‪ ،‬وستكون وظيفة قصيرة جًدا‬

‫الكود سوف يقوم فقط‬ ‫سطًر ا واحًدا منها‬

‫بطباعة عالمة االقتباس غير المقتبسة مرحًبا‬

‫ولكن اآلن في السطرين األول والثاني‬

‫اخترعت وظيفتي الخاصة‬

‫مرحًبا‪ ،‬الحظ هذه النقاط التي ظهرت اآلن‬

‫بطريقة سحرية هنا‪ ،‬هذا مجرد‬

‫إعداد لمحرر النصوص الخاص بي مقابل الكود في‬

‫من‬ ‫هذه الحالة هذا مجرد صنع‬

‫الواضح جًدا بالنسبة لي أنني قمت بالضغط على‬

‫شريط المسافة أربع مرات أو ما يعادل‬

‫مرة واحدة والذي يتم تحويله ‪ tab‬مفتاح‬

‫‪،‬تلقائًيا إلى أربع مسافات بشكل عام‬


‫سأحتاج إلى التأكد من‬

‫أن جميع التعليمات البرمجية ذات المسافة البادئة الخاصة بي تصطف‬

‫‪ python‬اآلن حتى يتمكن‬ ‫أعلم أن كل ذلك‬

‫جزء من نفس الشيء ولكن األمر سهل في‬

‫هذه الحالة ألنه مجرد‬

‫‪،‬سطر واحد ولكن اآلن بفضل السطرين األول والثاني‬

‫موجودة تماًم ا ‪ hello‬ستكون الدالة‬

‫عندما أكون مستعًدا الستخدامها في السطر السادس لذا‬

‫نزوًال إلى نافذتي الطرفية‬ ‫دعني أذهب‬

‫أدخل ‪ python of hello.pi‬وتشغيل‬

‫هنا يأتي اسمي مرة أخرى واآلن عندما‬

‫أضغط على زر اإلدخال أرى اآلن مرحًبا ديفيد‪ ،‬حسًنا‪ ،‬لقد‬

‫تراجعنا نوًعا ما على الرغم من أن‬

‫هذا ليس جمياًل كما كان‬

‫أعتقد أننا ربما نستطيع أن نفعل ما هو‬ ‫من قبل‬

‫أفضل من ذلك من خالل تحسين األمور‬

‫بشكل أكبر‪ ،‬لماذا ال نفكر في الطريقة‬

‫التي يمكن أن نقول بها تحديد معلمات نفس الوظيفة‪ ،‬أي هل‬

‫يمكننا تخصيص الترحيب‬

‫ربما ألخذ اسم المستخدم كمدخل حتى‬

‫مرحًبا ولكن‬ ‫نتمكن من قول ليس فقط‬

‫اسم الشخص كله في سطر واحد‪ ،‬كل ذلك في نفس واحد‬

‫حسًنا‪ ،‬أعتقد أنه يمكننا القيام بذلك‪ ،‬دعني‬

‫أقترح أن نفعل ذلك على النحو التالي‪ ،‬اسمح‬

‫لي بالمضي قدًم ا في الكود الخاص بي‪ ،‬دعني‬

‫داخل هذه األقواس‪ ،‬دعني‬

‫اسم المعلمة الخاصة لدي‬ ‫أتوصل إلى ما يلي‬

‫خيار كامل هنا‬

‫وسأقول أن اسم المعلمة الخاصة بي‬


‫سيكون الكلمة‬

‫لماذا ألنني أريد أن تبدو وظيفتي‬

‫مثل الفعل‬

‫الذي يمثل مرحًبا ولكن من تريد‬

‫سأقوم باستدعاء‬ ‫أن تقول مرحًبا جيًدا‬

‫المعلمة الخاصة بي لهذه الوظيفة ‪ 2‬فقط‬

‫ألنه باللغة اإلنجليزية يبدو األمر‬

‫لطيًفا بالنسبة لي‪ ،‬مرحًبا بمن تريد أن‬

‫تقول مرحًبا‪ ،‬ولهذا السبب أقوم باستدعاء هذه‬

‫المعلمة ‪ 2‬بدًال من شيء أبسط‬

‫‪ y‬أو ‪ x‬مثل‬ ‫‪ z‬أو‬

‫حسًنا‪ ،‬حسًنا‪ ،‬ماذا أريد أن أفعل‬

‫بالكلمة الثانية حسًنا‪ ،‬يمكنني القيام ببعض‬

‫األشياء المختلفة التي رأيناها مثل العديد من‬

‫الطرق المختلفة لتنفيذ مرحًبا‪ ،‬اسمح لي‬

‫فقط بإضافة فاصلة هناك من أجل القواعد النحوية‬

‫ضع الكلمة‬ ‫ثم اسمح لي‬

‫بعد ذلك باعتبارها الوسيطة الثانية‬

‫للدالة مرحًبا‪ ،‬هناك طرق أخرى يمكننا من خاللها‬

‫القيام بذلك وقد رأينا الكثير ولكن هذه‬

‫الطريقة تبدو واضحة بعض الشيء بالنسبة لي سأقول‬

‫ما الذي سيحدث بعد ذلك‪ ،‬حسًنا‪ ،‬ال أعرف‬

‫أعتقد أنني بحاجة إلى سطر الطباعة اإلضافي هنا‬

‫وأعتقد أن ما سأفعله هو أنني‬

‫سأمضي قدًم ا هنا وأطبع‬

‫اسم الشخص يدوًيا وليس اسم الشخص الذي‬

‫سأشعر به بدًال من ذلك أقول مرحًبا‬

‫باالسم الموجود بين قوسين‬

‫أفعل اآلن في السطرين ‪ 1‬و ‪2‬‬ ‫‪،‬فماذا أفعل‬


‫أقوم بتعريف وظيفتي الخاصة التي تسمى‬

‫مرحًبا ولكن هذه المرة تم تصميم هذه الوظيفة‬

‫ألخذ معلمة‬

‫معلمة واحدة كمدخالت وأنا أستخدم‬

‫قيمة تلك المعلمة التي‬

‫قم بتوصيله بالطباعة حتى‬ ‫اتصلت بها ‪2‬‬

‫ال أرى مرحًبا فحسب‪ ،‬بل أرى أيًض ا‬

‫‪.‬اسم ذلك الشخص ما الذي أفعله في السطر ‪5‬‬

‫كما هو الحال دائًم ا‪ ،‬أحصل على‬

‫السطر السادس من اسم المستخدم‪ ،‬وال‬

‫أتصل فقط بمرحًبا‪ ،‬بل أقوم بإدخاله كمدخل‬

‫‪،‬متغير كوسيطة ‪name‬‬

‫وهذا ما يتم تمريره إلى‬

‫وما يحدث هنا هو ‪hello‬‬

‫في األساس هذا على الرغم من أن‬

‫هنا عندما يتم ‪ name‬المتغير يسمى‬

‫‪،‬استدعاء الوظيفة نفسها‬

‫يفترض الكمبيوتر أن نفس القيمة يتم استدعاؤها اآلن‬

‫إلى‬ ‫لذلك يتم نسخ االسم بشكل أساسي‬

‫متغير آخر يتم استدعاؤه‬

‫حتى أنه في سياق مرحًبا يمكنني أن‬

‫أقول مرحًبا‬

‫لهذا المتغير بدًال من ذلك‬

‫وسنرى في لحظة ما سيحدث‬

‫‪،‬إذا لم نحافظ على تلك األمور بشكل مستقيم‬

‫‪ hello.pi‬دعني أمضي قدًم ا وأقوم بتشغيل بايثون إذا‬

‫أدخل ما هو اسمك واآلن‬

‫أعبر أصابعي أدخل هناك سنعود‬

‫إلى العمل ولكن اآلن لدي‬


‫وظيفتي المخصصة الخاصة التي تسمى مرحًبا والتي‬

‫تسمح لي بإلقاء التحية لشخص معين‬

‫وهنا يمكن أن تصبح األمور اآلن‬

‫تخيل ماذا لو كنت تريد أن تقول‬ ‫حقيقية‬

‫وظيفة الترحيب الخاصة بك مرحًبا لشخص‬

‫محدد ولكنك تعرف ماذا إذا كنت ال‬

‫‪،‬تعرف من تريد أن تقول مرحًبا‬

‫‪،‬وتريد أن تقول مرحًبا للعالم كله‬

‫يمكنك إعطاء المعلمات القيم االفتراضية التي‬

‫تذكر أنه مع‬ ‫‪ print‬رأيناها‬

‫‪ sep‬كانت هناك قيمة افتراضية لـ‬

‫للفاصل‪ ،‬وكانت هناك قيمة افتراضية‬

‫لنهاية السطر‪ ،‬يمكننا القيام بذلك أيًضا‬

‫وإليك بناء الجملة إذا كنت تريد‬

‫قيمة هذه المعلمة‬

‫افتراضًيا إذا لم يقدمها‬

‫لكي تكون مساوًيا لعالم االقتباس‬ ‫‪،‬المبرمج‬

‫فإنك تفعل ذلك حرفًيا‬

‫في نفس السطر الذي تحدد فيه‬

‫الوظيفة وسأعترف أنها بدأت تبدو‬

‫أكثر غموًض ا ولكني ما زلت أقوم فقط‬

‫بتعريف وظيفة تسمى مرحًبا‪ ،‬فهي‬

‫لكنني قمت‬ ‫تتطلب معلمة تسمى ‪2‬‬

‫بتعيينها بعالمة يساوي‬

‫قيمة افتراضية لعالم االقتباس وإلغاء االقتباس‬

‫فقط في حالة عدم استدعاء المبرمج‬

‫مرحًبا باستخدام وسيطة‬

‫ويمكننا رؤية ذلك هنا‪ ،‬دعني أغير الكود الخاص بي الستخدام‬

‫خمسة سأقول‬ ‫مرحًبا بطريقتين عبر اإلنترنت‬

‫مرحًبا بكل بساطة بدون وسيطات ثم‬


‫في السطر السادس سأحصل على اسم‬

‫السطر السابع سأقول مرحًبا مع‬

‫وسيطة لذلك سترى مرحًبا‬

‫لألمام‬ ‫يتم استخدامه اآلن بطريقتين دعني أذهب‬

‫‪ pythonflo.pi‬وتشغيل‬

‫سأكتب اسمي‪ ،‬يا مالحظة مثيرة لالهتمام‪ ،‬لقد‬

‫ولكن ‪ helloworld‬رأيت بالفعل‬

‫هذا متوقع ألن السطر الخامس‬

‫يحدث قبل السطر السادس ولكن بمجرد أن أكتب‬

‫اسمي اآلن‪ ،‬سيكون البرنامج‬

‫مرحًبا بي‬ ‫أكثر تهذيًبا وسيقول‬

‫‪،‬شخصًيا‬

‫لذلك نرى أيًض ا من خالل‬

‫بناء جملة بسيط نسبًيا ولكنه جديد كيف يمكنك‬

‫تنفيذ وظيفة مشابهة جًدا من حيث‬

‫الروح لما قدمته لنا وظيفة الطباعة‬

‫تلقائًيا‪ ،‬واآلن يمكنك التحكم‬

‫في القيام بذلك بنفسك‬

‫ولكن دعني اآلن أجعل هذه النقطة أيًضا واحدة‬

‫إن النقاط الكاملة لتحديد‬ ‫من‬

‫وظائفك الخاصة هي مجرد تجنب االضطرار إلى‬

‫‪،‬تكرار نفسك مراًر ا وتكراًر ا‬

‫وال يتعين عليك االستمرار في إعادة اختراع‬

‫العجلة واالستمرار في استخدام‬

‫وظيفة الطباعة مراًر ا وتكراًر ا إذا‬

‫كنت تريد فقط أن تقول مرحًبا‬

‫سيكون من اللطيف اآلن أن أتمكن من نقل هذا‬

‫الرمز الذي كتبته لتعريف‬

‫‪،‬وظيفة الترحيب ولمجرد أن أكون مثيًر ا‬


‫سأضغط على زر اإلدخال عدة مرات‬

‫سطًر ا ألسفل وأضع تعريفي ‪50‬‬

‫في هذا الملف‪ ،‬لماذا‬ ‫للترحيب بشكل أكبر‬

‫حسًنا فقط من الروح البعيدة عن‬

‫األنظار بعيًدا عن البال ألنه إذا‬

‫‪،‬عدت اآلن إلى بداية برنامجي‬

‫فيمكنك اآلن أن تعتبر أن أوه‬

‫ألنها موجودة ‪ y‬مرحًبا هي وظيفة‬

‫يحتوي على‬ ‫في السطر األول و‬

‫قوس مفتوح وقوس مغلق وهو‬

‫ما يعني حتى اآلن استدعاء هذه‬

‫الوظيفة ثم في السطر الثاني‬

‫نحصل على متغير من المستخدم عن طريق‬

‫كتابة اسمه ثم‬

‫ننادي مرحًبا ونمرر هذه القيمة جيًدا‬

‫هذه النقطة يمكنني أن أعتبرها أمًر ا‬ ‫في‬

‫مفروًغ ا منه أن الترحيب موجود حتى لو كان موجوًدا‬

‫في الملف أو كما‬

‫سنرى في األسابيع المقبلة حتى لو كان في‬

‫ملف مختلف تماًم ا ولكن هناك‬

‫مشكلة هنا واسمحوا لي بالمضي قدًم ا وتشغيل‬

‫نسخة من‬ ‫‪ hello dot pi‬هذا‬

‫الحظ أنه بمجرد تشغيل‬

‫أرى ‪ python of hello.pi‬المترجم‬

‫‪ hello‬خطأ في االسم لم يتم تعريف االسم‬

‫‪،‬مرة أخرى‪ ،‬هل تقصد المساعدة جيًدا مرة أخرى‬

‫فأنا بحاجة إلى بعض المساعدة هنا ولكني‬

‫استدعاء الوظيفة يساعد‬ ‫لم أقصد ذلك‬

‫المشكلة هنا على الرغم من أن بايثون‬

‫يأخذني حرفًيا فقط‪ ،‬لقد‬


‫حددت وظيفتي مرحًبا على طول‬

‫الطريق هنا ولكني أحاول استخدامها‬

‫‪،‬هنا وهذا غير مسموح به‬

‫سيأخذك مترجم بايثون‬

‫وإذا كنت تستخدم وظيفة‬ ‫‪،‬حرفًيا‬

‫فيجب أن تكون موجودة بالفعل في الوقت الذي‬

‫تتصل فيه بها‪ ،‬فكيف يمكنني إصالح هذا جيًدا؟‬

‫من الواضح أنني ال أستطيع فعل ذلك‪ ،‬يجب أن أقوم‬

‫بتحديد أي وظائف أريدها في‬

‫تسبب لي‬ ‫أعلى الملف الخاص بي ولكن هذا أيًض ا يمكن أن يحدث‬

‫في بعض المشاكل في النهاية ألنه‬

‫إذا كان علي دائًم ا تحديد‬

‫‪،‬وظيفة أعاله حيث أريد استخدامها‬

‫‪،‬فأنت نوع من كتابة التعليمات البرمجية في االتجاه المعاكس‬

‫فأنت تكتب باستمرار وظائف‬

‫هنا هنا هنا بدًال من‬

‫الكود الخاص بك منطقًيا من األعلى إلى‬ ‫الكتابة‬

‫األسفل‪ ،‬لذا اسمح لي بإصالح هذا‬

‫بطريقة أكثر معيارية وهي القيام بذلك‬

‫بشكل عام‪ ،‬فأنت تريد وضع الجزء‬

‫الرئيسي من الكود الخاص بك في الجزء العلوي من‬

‫ملفك وفي الواقع سأفعل ذلك‬

‫‪ main،‬فيما يتعلق بتحديد وظيفتي التي تسمى‬

‫فهذا ليس متطلًبا ولكنه في الواقع‬

‫اتفاقية وهذا يشير ضمًنا‬

‫للقارئ إلى أن هذا هو الجزء الرئيسي من‬

‫برنامجي وسأتخلص من‬

‫مكالمة الترحيب الفارغة اآلن وسأمررها فقط‬

‫إصدار واحد باسم مرحًبا ثم‬


‫هنا بعد سطرين لألسفل‪ ،‬سأقوم‬

‫بالفعل بتعريف وظيفة الترحيب الخاصة بي‬

‫لسوء الحظ اآلن بعد أن قمت بإعادة ترتيب‬

‫الوظائف بهذه الطريقة عن طريق وضع‬

‫الجزء الرئيسي من الكود الخاص بي في األعلى‬

‫بحيث يتدفق المنطق الخاص بي من‬ ‫ومرحًبا في األسفل‬

‫األعلى إلى األسفل إذا مضيت‬

‫أدخل ‪ python of hello.pi‬قدًم ا وقمت بتشغيل‬

‫شيًئا على اإلطالق يحدث إذا فعلت ذلك‬

‫مرة أخرى‪ ،‬ال شيء يحدث جيًدا على اإلطالق‬

‫لماذا هذا جيد في العالم‬

‫لمجرد أنني حددت وظيفة تسمى‬

‫ولقد حددت وظيفة تسمى ‪main‬‬

‫ال تعني أنني ‪hello‬‬

‫اتصلت بالفعل وتم استخدام أي منهما نعم‬

‫ولكن ال ‪ main‬داخل ‪ hello‬أنا أستخدم‬

‫استخدام ‪ python‬أحد يطلب من‬

‫لترتيب هذا األمر ‪main‬‬ ‫‪،‬أو االتصال به بالفعل بالترتيب‬

‫آخر شيء يجب علي فعله في هذا الملف‬

‫هو في الواقع استدعاء وظيفتي الرئيسية‬

‫وفي الواقع من خالل استدعاء وظيفتي الرئيسية‬

‫بهذه الطريقة يخرجني من‬

‫المشاكل ألنني اآلن أقوم بتعريف الوظيفة الرئيسية‬

‫أنا ال أتصل مرحًبا بعد‪ ،‬فأنا‬ ‫أوًال ولكني‬

‫أحدد مرحًبا بعد ذلك ولكني ال أتصل‬

‫مرحًبا بعد ذلك‪ ،‬فقط في نهاية‬

‫هذا الملف‪ ،‬اتصل بالرئيسي‬

‫والذي له تأثير تشغيل هذا‬

‫الرمز هنا والذي له تأثير‬

‫وبالتالي‬ ‫تشغيل هذا الرمز هنا‬


‫يسمح لي بتنظيم ملفي‬

‫وترتيب وظائفي بأي طريقة أريدها بما في ذلك‬

‫في األعلى ‪main‬‬

‫وحل مشكلة‬

‫‪،‬التي ال تعرف ما يحدث اآلن ‪python‬‬

‫من المهم مالحظة أنني حددت‬

‫الوسيطة أيًضا ‪ hello‬وظيفتي‬ ‫على أنها تأخذ‬

‫ثم مررت إلى تلك الوظيفة‬

‫قيمة المتغير الذي أردت أن‬

‫أقول مرحًبا به وهو المتغير المسمى‬

‫ألنه لنفترض أنني فعلت ‪name‬‬

‫شيًئا مختلًفا قليًال‬

‫لنفترض أنني لم أعّر ف مرحًبا على أنه‬

‫لقد قمت فقط بإزالة‬ ‫قبول وسيطة‬

‫اإلشارة إلى اثنين وقيمتها االفتراضية‬

‫وأعود إلى ‪help World‬‬

‫وظيفتي الرئيسية وأدعو فقط مرحًبا‬

‫دون المرور بأي وسيطة واآلن‬

‫اسمحوا لي بالمضي قدًم ا وإجراء تغيير آخر‬

‫وخطأ آخر من الناحية الفنية دعني أذهب‬

‫لألمام وحاول فقط طباعة‬

‫‪،‬قيمة االسم في وظيفة الترحيب بسذاجة‬

‫لذا اآلن لكي أكون واضًح ا في وظيفتي الرئيسية‬

‫في السطر الثاني‪ ،‬أقوم بتعريف المتغير الخاص بي‬

‫المسمى االسم وتعيين له‬

‫القيمة المرجعة لوظيفة اإلدخال من‬

‫‪ i‬المستخدم‬ ‫ثم أقوم فقط باستدعاء مرحًبا في‬

‫وظيفة الترحيب الخاصة بي والتي لم تعد اآلن تأخذ‬

‫أي وسيطات‪ ،‬أقوم باستدعاء طباعة وتمرير‬


‫فاصلة مرحًبا ثم‬

‫تمرير اسم المتغير الذي‬

‫حصلت على مدخالت المستخدم إليه على الفور ولكن المهم هو أن‬

‫هذا االسم موجود اآلن فقط في‬ ‫وهكذا ‪main‬‬

‫شاهد ما يحدث عندما أحاول تشغيل‬

‫‪ python‬هذا اإلصدار من البرنامج باستخدام‬

‫لقد قمت بالضغط على زر اإلدخال‪ ،‬وتمت مطالبتي بإدخال ‪hello.pi،‬‬

‫‪ david enter،‬اسمي‬

‫‪،‬ولم يتم تعريف خطأ في االسم‪ ،‬واقتباس االسم‬

‫هذه في الواقع‬ ‫لذلك اتضح أن‬

‫مشكلة تتعلق بما يسمى نطاق النطاق‬

‫يشير إلى متغير موجود فقط في‬

‫‪،‬السياق الذي قمت بتعريفه فيه‬

‫وبقدر ما أقوم بتحديد اسم المتغير هذا‬

‫في وظيفتي الرئيسية‪ ،‬يمكنني فقط استخدام هذا‬

‫استخدمه‬ ‫المتغير في وظيفة اسمي‪ ،‬ال أستطيع‬

‫‪،‬كما حاولت هنا في وظيفة الترحيب الخاصة بي‬

‫فهو غير موجود في هذا‬

‫النطاق المزعوم‪ ،‬ولهذا السبب اآلن‬

‫إذا قمت بالترجيع والتراجع عن كل هذه‬

‫التغييرات‪ ،‬سترى أنني أقوم بتمرير‬

‫من وظيفتي الرئيسية إلى‬ ‫المفتاح الرئيسي عمًدا‬

‫وظيفتي الترحيبية واآلن في‬

‫وظيفة الترحيب‪ ،‬أصبح لها اسم مختلف تقنًيا‬

‫ُيطلق عليه ‪ 2‬في هذا السياق ولكن‬

‫ال بأس‪ ،‬فاألمر متروك تماًم ا لكل‬

‫وظيفة على حدة لتسمية المتغيرات الخاصة بها‬

‫أو تسمية الوسائط الخاصة بها ولكن‬

‫اآلن بعد أن قمت بتسليم‬ ‫هذا أمر‬

‫‪ hello‬قيمة هذا المتغير إلى الدالة‬


‫‪ hello‬بحيث يمكن طباعته بواسطة‬

‫أيًضا‬

‫وهناك لمسة أخيرة يمكننا‬

‫إضافتها هنا‬

‫‪ hello‬اآلن بعد أن قمنا بتنفيذ‬

‫ما يسمى ‪ hello‬ستالحظ أن‬ ‫تحتوي فقط على‬

‫بالتأثير الجانبي‪ ،‬فهو يطبع شيًئا ما‬

‫على الشاشة جيًدا فقط‪ ،‬ماذا لو كنت أريد أيًضا‬

‫أال يكون لوظيفتي تأثير جانبي في حد ذاته ولكن‬

‫في الواقع يعيد لي قيمة تذكر‬

‫أن وظيفة اإلدخال ترجع قيمة‬

‫تذكر‬ ‫السلسلة التي كتبها المستخدم‬

‫‪،‬ترجع قيمة ‪ int‬أن الدالة‬

‫قيمة ‪ float‬وترجع الدالة‬

‫تم تمريرها إليها جيًدا‪ ،‬يمكنك استخدام‬

‫كلمة رئيسية أخيرة هنا تعود حرفًيا‬

‫إلرجاع قيمة بشكل صريح بنفسك‪ ،‬في‬

‫هنا ‪ vs‬الواقع دعني أعود إلى كود‬

‫سنعيد انتباهنا إلى‬ ‫وأعتقد أننا‬

‫ونرى ما إذا كنا ال نستطيع ‪.pi‬الحاسبة‬

‫تنفيذ إصدار آخر من‬

‫الحاسبة‬

‫‪.‬‬

‫هذه المرة أنني سأتخلص من ‪pi‬و ‪ pi‬أعتقد‬

‫كل شيء كما كان من قبل وسأبدأ في‬

‫ممارسة ما‬

‫‪،‬ندعو إليه هنا‬


‫والتي ‪ main‬حدد وظيفة تسمى‬

‫ستكون اآلن الجزء الرئيسي من‬

‫لألمام واآلن أعلن عن‬ ‫وظيفتي‪ ،‬فلنبدأ‬

‫وقم بتعيينه ‪ x‬متغير يسمى‬

‫للنسخة المحولة من إدخال المستخدم‬

‫لذا مرة أخرى ‪ x،‬بعد سؤاله عن‬

‫سطر من التعليمات البرمجية تماًم ا كما فعلنا‬

‫من قبل وافترض اآلن أن ما أريد‬

‫أريد‬ ‫القيام به هو تربيع هذه القيمة‬

‫أخذ الرقم الذي كتبه المستخدمون‬

‫ورفعه إلى قوة اثنين‪ ،‬بحيث‬

‫يكون اثنان تربيع يساوي أربعة وثالثة تربيع يساوي‬

‫تسعة وأربعة تربيع يساوي‬

‫ستة عشر وهكذا‪ ،‬حسًنا‪ ،‬كيف يمكنني‬

‫تنفيذ دالة تسمى حرفًيا‬

‫والذي في الواقع ال‬ ‫مربع‬

‫المدمج جيًدا‪ ،‬دعني ‪ python‬يأتي مع‬

‫أفترض في الوقت الحالي أنه موجود‬

‫واسمحوا لي أن أقول شيًئا كهذا‪ ،‬دعني‬

‫أمضي قدًم ا وأقول إن‬

‫طباعة ما يتعلق بـ‬

‫تربيع هي ‪x‬‬

‫‪ x،‬فاصلة مربعة لـ‬

‫لقد قمت بتعريف دالة‬ ‫فماذا فعلت‬

‫وقمت ‪ main‬تسمى‬

‫بتنفيذ سطرين‪ ،‬أول هذه‬

‫‪ x‬األسطر يطالب المستخدم بقيمة‬

‫ويخزنها في ‪ int‬ويحولها إلى‬

‫في السطر الثالث ثم ‪ x‬متغير يسمى‬

‫ثم أقوم ‪ x‬أقول‬ ‫تربيع هو‬


‫بتمرير وسيطة ثانية إلى وظيفة الطباعة‬

‫مهما كانت القيمة المرجعة لدالة مربعة‬

‫ولكن المربع غير موجود وسأعرض‬

‫لك هذا هنا إذا اتصلت اآلن بـ‬

‫في األسفل ‪main‬‬

‫‪ python of Calculator.pi‬وقمت بتشغيل‬

‫يساوي ‪ x 2‬سأرى أن‬

‫‪،‬ثم أرى مجموعة كاملة من األخطاء‬

‫لم يتم تعريف مربع اسم خطأ في االسم‪ ،‬لذلك‬

‫هذا ليس خطأ مطبعي هنا‪ ،‬إنها مجرد‬

‫وظيفة غير موجودة ولكن أعتقد أنه يمكنني‬

‫اسمحوا لي بالمضي قدًم ا‬ ‫جعلها موجودة هنا‬

‫‪،‬وتحديد وظيفة أخرى تسمى المربع‬

‫ستأخذ هذه الوظيفة رقًم ا وسأسميها‬

‫كما يفعل ‪ n‬بشكل عام‬

‫العديد من المبرمجين فقط‬

‫لتمثيل أي رقم قديم ثم ماذا‬

‫المربع‬ ‫‪ n‬أريد أن أفعل من أجل‬

‫حسًنا‪ ،‬الرقم المربع هو في الحقيقة مجرد‬

‫نفسه مضروًبا في نفسه‪ ،‬لذا سأفعل‬

‫لكن ال يكفي مجرد ‪ n،‬مضروًبا في ‪ n‬ذلك‬

‫حيث سيتعين عليك ‪ n،‬مضروًبا في ‪ n‬إجراء العمليات الحسابية بنفسك‬

‫إرجاع القيمة الفعلية‬

‫‪ n‬مضروبة في ‪n‬‬ ‫وهذه هي كلمتنا الرئيسية الجديدة‬

‫هنا عندما أفعل هذا اآلن‪ ،‬شاهد ما‬

‫‪ dot pi،‬يحدث في بايثون اآللة الحاسبة‬

‫على سبيل المثال يجب أن يكون ‪ x 2‬أدخل‬

‫تربيع هو ‪ .4‬دعني أمضي قدًم ا اآلن ‪x‬‬

‫وأقول‬
‫لقد ‪ x‬أصبح اآلن ‪x 3‬‬ ‫تربيع أصبح اآلن ‪ .9‬لذا أنا'‬

‫قمت بتنفيذ وظيفتي الخاصة التي‬

‫ُترجع مربع القيمة‬

‫‪ return‬وألنني أستخدم الكلمة األساسية‬

‫التي تضمن أنه يمكنني تمرير‬

‫القيمة المرجعة لهذا تماًم ا مثل القيمة المرجعة‬

‫أو التعويم ‪ int‬لإلدخال أو‬

‫بدًال من ذلك ‪ print‬إلى وظيفة أخرى مثل‬

‫وهنا أيًض ا سيكون هناك العديد من‬

‫الطرق لحل نفس المشكلة‪ ،‬يمكنني‬

‫‪،‬إلى قوة اثنين ‪ n‬بالفعل رفع‬

‫ولم نر بناء الجملة من قبل ولكن إذا‬

‫النجمتين مثل ‪ stu two‬استخدمت‬

‫من اليسار‬ ‫النجمتين هذا فإن ذلك يرفع الشيء على‬

‫إلى القوة الموجودة على اليمين أو‬

‫اتضح أن هناك في بايثون وظيفة تسمى‬

‫األسرى لرفع شيء ما إلى القوة‬

‫‪،‬التي تأخذ وسيطتين‬

‫أولهما هو الرقم والثاني هو‬

‫األس‪ ،‬لذلك هناك أيًضا‬

‫طرق لحل‬ ‫الكثير‬

‫هذه المشكلة فعلًيا أيًض ا‪ ،‬وفي‬

‫النهاية ما قمنا به هنا‬

‫قدمنا أوًال وظائف‪ ،‬وهذه‬

‫اإلجراءات عبارة عن أفعال تم تضمين الكثير منها‬

‫في لغة بايثون والتي يمكنك استخدامها فقط‬

‫في التعليمات البرمجية الخاصة بك‪ ،‬ثم قدمنا‬

‫تلك‬ ‫المتغيرات التي يمكنك من خاللها تخزينها‬

‫القيم المرجعة ومن ثم ربما تفعل‬

‫شيًئا أكثر بها في نهاية‬


‫اليوم أيًض ا‪ ،‬لديك اآلن القدرة على‬

‫اإلبداع الختراع وظائفك الخاصة لحل‬

‫المشكالت البسيطة مثل الترحيب أو في‬

‫األسابيع المقبلة‪ ،‬وهي مشكالت أكثر‬

‫تعقيًدا وأكثر تحدًيا وأكثر متعة‬

‫أيًضا‬

You might also like