Coding

Python Tutorial for Beginners – Learn Python by Building a Blockchain & Cryptocurrency

  • 00:00:02 welcome I got a new course and it's
  • 00:00:04 called learn Python by building a
  • 00:00:07 blockchain
  • 00:00:07 I guess the title speaks for itself it's
  • 00:00:10 a Python course you will learn Python
  • 00:00:12 from scratch no prerequisites required
  • 00:00:14 you will learn all the core syntax
  • 00:00:17 features like list comprehensions using
  • 00:00:19 more complex data structures like tuples
  • 00:00:22 or dictionaries you will learn
  • 00:00:24 object-oriented programming with Python
  • 00:00:26 by using classes and inheritance you
  • 00:00:29 will learn how to spin up a webserver
  • 00:00:31 and handle incoming requests with flask
  • 00:00:33 you will learn how to send requests how
  • 00:00:35 to work with files and so much more
  • 00:00:39 so really a fertile Python course that
  • 00:00:41 introduces you to the language and it
  • 00:00:43 does so by building a real cool course
  • 00:00:46 project a basic blockchain and your own
  • 00:00:49 cryptocurrency now that sounds very
  • 00:00:51 fancy well why did I choose that course
  • 00:00:54 project well I chose to build a
  • 00:00:57 blockchain because it turns out that we
  • 00:00:59 actually can gradually enhance the
  • 00:01:02 blockchain start very basic and then add
  • 00:01:04 more and more core features off the
  • 00:01:06 blockchain technology as we learn more
  • 00:01:08 about Python so it's a really cool
  • 00:01:09 project that allows you to apply your
  • 00:01:12 newly gained knowledge and build
  • 00:01:14 something realistic that's always
  • 00:01:15 important to me I want to build course
  • 00:01:18 projects that are connected to something
  • 00:01:21 you know from real life that aren't too
  • 00:01:24 abstract now the blockchain on the other
  • 00:01:26 end is kind of abstract you hear about
  • 00:01:28 that technology a lot and I'd argue that
  • 00:01:32 most people don't really know how it
  • 00:01:34 works and that's another cool thing
  • 00:01:36 about the course whilst it is not a
  • 00:01:38 blockchain course a it's not about the
  • 00:01:40 blockchain only it's about Python but
  • 00:01:42 whilst that is the case you will learn a
  • 00:01:44 lot about the blockchain there you will
  • 00:01:46 learn about the core concepts behind it
  • 00:01:48 and therefore you hopefully understand
  • 00:01:51 their technology a bit better too so it
  • 00:01:54 really has these two parts Python and
  • 00:01:56 the blockchain two very popular topics
  • 00:01:59 now connected in one course which
  • 00:02:02 hopefully allows you to learn a lot
  • 00:02:03 about Python and also learn a bit about
  • 00:02:05 the blockchain whilst you're going
  • 00:02:07 through it I'd be more than happy to
  • 00:02:09 welcome you in the course if you are
  • 00:02:11 interested you'll find a link with a
  • 00:02:13 nice discount in the video description
  • 00:02:15 and I also attached a couple of lectures
  • 00:02:18 at the end of this video to allow you to
  • 00:02:21 see if that course is for you as I said
  • 00:02:24 I'd be more than happy to welcome there
  • 00:02:25 otherwise see you in all the other
  • 00:02:27 videos on this channel bye
  • 00:02:37 so what is – – is a programming language
  • 00:02:41 which you can use for many different
  • 00:02:43 things it's an extremely powerful
  • 00:02:45 easy-to-use and object-oriented
  • 00:02:47 programming language now what does this
  • 00:02:50 mean it's powerful because it offers
  • 00:02:52 good performance it runs on all major
  • 00:02:55 operating systems so it doesn't matter
  • 00:02:57 if you're using Mac Windows or Linux
  • 00:02:59 it'll work on all of these and you can
  • 00:03:01 use all of these for this course and
  • 00:03:03 it's extremely versatile to be precise
  • 00:03:06 if we have a look at its versatility we
  • 00:03:08 can see that we can use it to build
  • 00:03:10 simple scripts or programs running on
  • 00:03:12 our desktop with or without an UI that
  • 00:03:15 you can use it for web development to
  • 00:03:17 run server-side code so code that does
  • 00:03:21 something on the server sends back HTML
  • 00:03:24 documents to your users and stuff like
  • 00:03:26 that and another very popular way of
  • 00:03:29 using Python is for data science and
  • 00:03:32 after this course you will be able to
  • 00:03:35 dive into all these different areas
  • 00:03:37 because in this course you will learn
  • 00:03:38 all the core basics about Python on
  • 00:03:41 which all these pillars basically build
  • 00:03:43 up now what do these pillars mean in
  • 00:03:45 detail though well we can build scripts
  • 00:03:48 and programs for example build a simple
  • 00:03:50 calculator which we execute from our
  • 00:03:52 system built-in command line we can
  • 00:03:56 write simple utility scripts like clean
  • 00:03:58 up scripts or scripts that save some
  • 00:04:00 data to files of course we can also
  • 00:04:02 build complex programs we can add a UI
  • 00:04:05 to build real desktop applications this
  • 00:04:08 is also something which is relatively
  • 00:04:09 easy to do thanks to pythons rich
  • 00:04:12 ecosystem another cool Pro argument
  • 00:04:15 it has a broad ecosystem of third-party
  • 00:04:18 packages and libraries you can add to
  • 00:04:21 easily add new functionalities to your
  • 00:04:23 programs when we have a look at web
  • 00:04:26 development we take advantage of that
  • 00:04:28 there are a lot of packages that make it
  • 00:04:30 easy for us to write server-side code
  • 00:04:32 and we're then able to use PI 4 as a
  • 00:04:34 server-side language we're able to build
  • 00:04:37 full stack apps where – takes in
  • 00:04:40 requests and sends back HTML responses
  • 00:04:43 or API applications where we also take
  • 00:04:46 requests and send responses but where we
  • 00:04:49 interact by just sending data and
  • 00:04:51 no HTML code that could be rendered by a
  • 00:04:53 browser this allows us to build web
  • 00:04:55 applications to which other programs
  • 00:04:57 could communicate and this is actually
  • 00:04:59 also what we'll dive into in this course
  • 00:05:01 but more on that later we can also use
  • 00:05:04 frameworks like Django or flask these
  • 00:05:06 are these for party libraries I
  • 00:05:07 mentioned to make our developers life a
  • 00:05:10 lot easier now when we have a look at
  • 00:05:13 data science another very popular way of
  • 00:05:16 using Python there we also have a rich
  • 00:05:20 set of third-party programs that make
  • 00:05:22 statistical calculations and working
  • 00:05:24 with numbers very very easy we also have
  • 00:05:27 easy ways of gathering import and
  • 00:05:29 cleaning and using data and we can do
  • 00:05:32 all kinds of statistical analysis
  • 00:05:34 implement machine learning models and
  • 00:05:37 use them in our apps so Python is
  • 00:05:39 extremely versatile and therefore
  • 00:05:41 learning it definitely is a great idea
  • 00:05:44 and all these areas build up on the
  • 00:05:47 basics you'll learn in this course but
  • 00:05:49 Python is not just powerful and
  • 00:05:51 versatile it's also easy to use it has a
  • 00:05:54 clean and simple syntax no extra fluff
  • 00:05:56 added to it it has a batteries included
  • 00:05:59 approach which means that just after
  • 00:06:01 installing pipe you already got a lot of
  • 00:06:04 possibilities of using it you don't need
  • 00:06:06 to import and implement third party
  • 00:06:09 packages which you have to download and
  • 00:06:11 install separately for many things you
  • 00:06:13 can't just use what's built in that's
  • 00:06:15 really great and it offers a great
  • 00:06:17 documentation so if there is something
  • 00:06:19 you're stuck with your you're not sure
  • 00:06:22 you dive into the talks and you get many
  • 00:06:24 examples there many ways of learning
  • 00:06:26 Python in case you can't find other
  • 00:06:29 resources which also help you because as
  • 00:06:31 I said there is a rich ecosystem
  • 00:06:32 offering many resources to it so that's
  • 00:06:35 pretty amazing and now lastly it's
  • 00:06:38 object orientated that is something
  • 00:06:40 which will become clearer once we dive
  • 00:06:42 into what this means in a separate
  • 00:06:44 module in this course if you already get
  • 00:06:46 some programming background you'll be
  • 00:06:48 happy to hear that Python users classes
  • 00:06:51 inheritance that it supports all these
  • 00:06:53 features doesn't force you to use them
  • 00:06:55 but offers the possibility and using
  • 00:06:58 these features offers many advantages
  • 00:06:59 and that makes the language even more
  • 00:07:02 powerful and easy to use in the air
  • 00:07:04 it's no wonder that python is very
  • 00:07:06 popular therefore if we have a look at
  • 00:07:09 this graph we see python is extremely
  • 00:07:11 popular and it's gaining popularity
  • 00:07:14 surpassing many other languages here and
  • 00:07:16 it is of course doing that because it's
  • 00:07:18 easy to learn it's very versatile and it
  • 00:07:22 has this cool ecosystem with many
  • 00:07:24 third-party packages and therefore it's
  • 00:07:27 highly extensible so learning Python was
  • 00:07:30 always a good idea now it's a better
  • 00:07:32 idea than ever before so with that let's
  • 00:07:36 just install Python and write our first
  • 00:07:38 simple program with it
  • 00:07:47 let's get started of using Python to use
  • 00:07:50 it we have to install it first and that
  • 00:07:52 is also true if you are on a Mac or
  • 00:07:55 Linux there it typically is
  • 00:07:57 pre-installed but also typically in an
  • 00:07:59 older version so make sure to install
  • 00:08:01 the latest one that's really important
  • 00:08:02 and you do install it by visiting
  • 00:08:04 python.org
  • 00:08:06 and as I said earlier you can use Python
  • 00:08:09 on Mac Linux and windows and you always
  • 00:08:11 install it from that page on that page
  • 00:08:14 choose downloads and then PI from free
  • 00:08:17 dot whatever the latest version is when
  • 00:08:19 you're viewing this in my case it's free
  • 00:08:21 dot 6.5 now it's automatically giving me
  • 00:08:23 the Mac OSX download here if you're on
  • 00:08:26 Windows you wizard that same page and
  • 00:08:28 you simply choose the windows download
  • 00:08:30 there it should automatically offer that
  • 00:08:32 on both operating systems simply click
  • 00:08:35 and the download should start once the
  • 00:08:38 download finished simply click the
  • 00:08:40 downloaded file and the Installer should
  • 00:08:42 open up now on Mac simply click continue
  • 00:08:45 continue continue agree to the License
  • 00:08:49 Agreement choose the place where you
  • 00:08:51 want to install it click continue one
  • 00:08:54 more time and finally install it now on
  • 00:08:57 Windows the steps are basically the same
  • 00:08:58 though there are two important things
  • 00:09:00 make sure to select this checkbox where
  • 00:09:03 you add pipe into your path variable
  • 00:09:06 this is important so that you can later
  • 00:09:08 execute it from within your command
  • 00:09:10 prompt and then if you choose the manual
  • 00:09:12 install option also make sure you
  • 00:09:15 install python this pip thing here and
  • 00:09:18 all the things you see checked here and
  • 00:09:19 also make sure that here the environment
  • 00:09:23 variable part is checked this is
  • 00:09:24 important so that you can conveniently
  • 00:09:26 execute Python from within your command
  • 00:09:29 prompt later with all that set on both
  • 00:09:32 operating systems you can simply hit
  • 00:09:33 install and wait for the Installer to
  • 00:09:35 finish once it's dead finish
  • 00:09:38 open the terminal on Mac and Linux or
  • 00:09:41 simply your normal system command prompt
  • 00:09:44 on Windows now these are the tools we'll
  • 00:09:46 start with in the course but no worries
  • 00:09:49 later in the course we will also use a
  • 00:09:50 real editor and work with real files
  • 00:09:53 where we write our code and far later
  • 00:09:55 into the course we'll also attach a more
  • 00:09:57 beautiful UI so this is really just what
  • 00:09:59 we're starting with
  • 00:10:01 now I'm using a Mac here that is why you
  • 00:10:02 are seeing a terminal but the steps are
  • 00:10:04 the exact same if you're using Windows
  • 00:10:06 in the command prompt after installing
  • 00:10:08 Python simply enter pioneer as a command
  • 00:10:12 directly in the terminal or command
  • 00:10:14 prompt after you do this you should
  • 00:10:16 enter this input mode here you see three
  • 00:10:21 greater than signs on the left and this
  • 00:10:23 now means you're now in the Python
  • 00:10:25 console so to say this is called the
  • 00:10:27 repple and i will come back to what
  • 00:10:29 rappelled means in the next module the
  • 00:10:32 cool thing is here you can now type 5
  • 00:10:34 commands and directly execute them later
  • 00:10:37 we will also store them in file so that
  • 00:10:39 we can execute a whole file in one go
  • 00:10:41 but here we can start simple and you can
  • 00:10:44 for example simply enter 2 plus 2 and
  • 00:10:46 get back 4
  • 00:10:47 obviously it's more than just a simple
  • 00:10:49 calculator though so let's build our
  • 00:10:51 first simple program in the next lecture
  • 00:11:01 so let's start building something more
  • 00:11:03 exciting than just a simple calculator
  • 00:11:06 with this rappelled thing and the steps
  • 00:11:08 I show here are shown on Mac but they
  • 00:11:11 are exactly the same on Windows so what
  • 00:11:14 I want to do here is and once you get
  • 00:11:16 some input from the user
  • 00:11:17 store that in a file and then also open
  • 00:11:20 that file and read what we stored there
  • 00:11:22 and output it here in this command
  • 00:11:25 prompt now to get input from the user we
  • 00:11:28 can use a function pipe and ships with
  • 00:11:30 the input function and you will learn
  • 00:11:32 more about all the functions and ships
  • 00:11:34 with and what exactly is happening here
  • 00:11:35 throughout the course of course input is
  • 00:11:38 a function you execute it by typing
  • 00:11:40 input and then two parenthesis between
  • 00:11:43 the parentheses you pass a so-called
  • 00:11:44 argument that is something that function
  • 00:11:47 will use and the input function is
  • 00:11:49 defined by Python it's built-in to show
  • 00:11:51 you the text you're entering here make
  • 00:11:54 sure to use to put a sure marks and then
  • 00:11:56 wait for the user to enter something so
  • 00:11:58 here we could say please enter your name
  • 00:12:02 colon and then a blank and if I hit
  • 00:12:04 enter you see it's outputting that text
  • 00:12:07 and waiting for me to input something
  • 00:12:09 you can tell that it's waiting by the
  • 00:12:11 fact that there are no three greater
  • 00:12:13 than signs on the left so I can type max
  • 00:12:16 here and if I hit enter it's outputting
  • 00:12:18 it below that was the input it received
  • 00:12:20 now that's nice but I want to store that
  • 00:12:23 enough file right here we can use an our
  • 00:12:26 concept variables something I'll also
  • 00:12:28 come back to in this course and we can
  • 00:12:30 simply add a variable by typing any name
  • 00:12:34 you want like name but you could also
  • 00:12:35 type my underscore name anything like
  • 00:12:38 that and then an equal sign and then
  • 00:12:40 input please enter your name what we're
  • 00:12:45 doing with that is we're getting the
  • 00:12:46 input and then we're storing it in that
  • 00:12:49 variable so that we can use it later if
  • 00:12:51 I now it enter I can again enter max and
  • 00:12:54 now you don't see max as an output
  • 00:12:56 instead now if I type my name here so
  • 00:12:59 the name of that variable now we see the
  • 00:13:02 output this allows us to now use that
  • 00:13:04 value as often as we want and now if we
  • 00:13:07 want to store it in a file we can call
  • 00:13:09 Navarrete function pi from ships with
  • 00:13:11 the open function which will open a file
  • 00:13:14 for us
  • 00:13:15 now I'll store the result of that
  • 00:13:18 function in another variable named file
  • 00:13:20 so file equals is whatever open returns
  • 00:13:23 to us and now open needs two things it
  • 00:13:26 needs to know which file to open and how
  • 00:13:28 to open it do you want to read do you
  • 00:13:30 want to write so I will open a file
  • 00:13:33 which I'll name name dot txt and I'll
  • 00:13:37 set mode equal to W 4 right and if that
  • 00:13:42 syntax is a bit unclear well just make
  • 00:13:44 sure to type it correctly here you will
  • 00:13:45 learn what all these things are two
  • 00:13:48 different arguments for one function
  • 00:13:50 throughout the course no worries but
  • 00:13:52 with that we're basically telling Pyfrom
  • 00:13:54 to open that file and give us right
  • 00:13:55 access to it and store that address or
  • 00:13:58 that pointer to that file in that file
  • 00:14:01 variable if I now it enter we can type
  • 00:14:04 file here and this is this strange
  • 00:14:06 output which essentially tells us that
  • 00:14:08 it somehow worked and now we can say
  • 00:14:10 file dot write my name and this tells
  • 00:14:15 Pyfrom to write the content of my name
  • 00:14:17 into that file if we now it enter you
  • 00:14:20 can ignore that output we can now close
  • 00:14:22 that file by calling file dot close with
  • 00:14:25 parentheses and if you do that let's
  • 00:14:28 quickly navigate into that folder where
  • 00:14:31 you're executing your terminal or
  • 00:14:33 command prompt in the finder or Windows
  • 00:14:35 Explorer you can tell which path this is
  • 00:14:37 running in on windows on the left side
  • 00:14:40 of your terminal when you enter python
  • 00:14:43 there you should find the path it's
  • 00:14:45 currently running in Omak it should
  • 00:14:48 simply be in your users main folder
  • 00:14:51 there you should find the name dot txt
  • 00:14:53 and if you open it you should see the
  • 00:14:54 name you entered in there now of course
  • 00:14:57 we can do something cooler than just
  • 00:14:59 store the value there we can also read
  • 00:15:01 it back into python we can use our file
  • 00:15:05 again and call open again name dot txt
  • 00:15:08 make sure to don't forget the quotation
  • 00:15:10 marks here and now set the mode to R for
  • 00:15:14 read if you now it enter your opening
  • 00:15:16 that same file but in read mode and now
  • 00:15:20 you can call file read and if you hit
  • 00:15:24 enter it gives you the output of the
  • 00:15:26 file here and that was
  • 00:15:29 lot of new stuff a lot of things where
  • 00:15:30 you might wonder what's happening here
  • 00:15:33 and we'll learn all these things no
  • 00:15:35 worries but I hope this gave you a first
  • 00:15:37 impression of what you can do with
  • 00:15:40 Python and how you can give your
  • 00:15:42 computer instructions to execute which
  • 00:15:45 in turn allow you to write more complex
  • 00:15:47 programs as you can use these
  • 00:15:50 instructions and way more other
  • 00:15:52 instructions and commands in any
  • 00:15:54 combination you want to build any kind
  • 00:15:56 of program you want and we'll do this in
  • 00:15:59 this course we'll build a block chain so
  • 00:16:01 what is a block chain then
  • 00:16:10 you probably heard about blockchains or
  • 00:16:12 at least about Bitcoin and
  • 00:16:14 cryptocurrencies in general Bitcoin is a
  • 00:16:16 cryptocurrency and cryptocurrencies are
  • 00:16:18 based on the blockchain technology the
  • 00:16:22 blockchain technology can sound very
  • 00:16:24 abstract but in the end a blockchain is
  • 00:16:26 just a distributed data storage so the
  • 00:16:29 same copy of the data is kept on
  • 00:16:31 multiple machines which are connected
  • 00:16:34 and each data storage so on a machine
  • 00:16:37 the blockchain is just a chain a list of
  • 00:16:41 data stores containers the so called
  • 00:16:44 blocks which in turn are connected to
  • 00:16:47 each other so the order of the blocks
  • 00:16:49 matters and every block is read just
  • 00:16:51 like a folder on your system which
  • 00:16:53 contains some data it's not technically
  • 00:16:56 a folder but you can think of it as one
  • 00:16:58 so we got all these folders where each
  • 00:17:01 folder knows about the folder before it
  • 00:17:04 this builds up a chain so the order does
  • 00:17:07 matter and then the whole folder holding
  • 00:17:10 all these subfolders this is your
  • 00:17:12 blockchain and what's inside a subfolder
  • 00:17:14 that's the data stored in the blockchain
  • 00:17:16 and the blockchain is not limited to
  • 00:17:19 transactions as you learned the
  • 00:17:22 blockchain is a data store and a single
  • 00:17:24 block in the end can hold any data you
  • 00:17:27 want this could be some text this could
  • 00:17:30 be a number this could be a program you
  • 00:17:33 can execute or as in the case of Bitcoin
  • 00:17:37 it can be a list of transactions more on
  • 00:17:41 that in a second we of course don't just
  • 00:17:44 have one block we have multiple blocks
  • 00:17:46 in a blockchain and the interesting part
  • 00:17:48 is that they know about each other to
  • 00:17:50 ensure that you can't manipulate data in
  • 00:17:53 blocks because it's distributed across
  • 00:17:55 multiple machines you want to make sure
  • 00:17:57 that the machines can check each other
  • 00:17:58 each block receives a hash or is hashed
  • 00:18:02 a hash simply is some long text you
  • 00:18:05 could say that is generated
  • 00:18:07 automatically from all the parts that
  • 00:18:09 make up the block so if you got a block
  • 00:18:11 with the data hi I'm in a block and then
  • 00:18:13 let's say you have some metadata in that
  • 00:18:15 block like a unique ID for every block
  • 00:18:17 then you could take both use some
  • 00:18:19 hashing algorithm we'll use a plug-in
  • 00:18:21 for that in this course
  • 00:18:23 and then it spits out some text and
  • 00:18:25 you'll always get the same text for the
  • 00:18:28 same inputs and that hash as it is
  • 00:18:30 called is stored in the next block so
  • 00:18:32 whenever you change the previous block
  • 00:18:34 the next block will detect hey the hash
  • 00:18:37 I stored doesn't match the recalculated
  • 00:18:39 hash for the updated data because same
  • 00:18:42 input yields the same output if you
  • 00:18:44 change the input you change the output
  • 00:18:45 and therefore the next block which
  • 00:18:47 stored the old hash for the old input
  • 00:18:50 would recognize did something went wrong
  • 00:18:52 that is an important security mechanism
  • 00:18:54 and that is how the blockchain is built
  • 00:18:56 you got all these blocks and the next
  • 00:18:58 block always knows about the previous
  • 00:19:00 block now that's a lot of theory what's
  • 00:19:03 a cryptocurrency then because a
  • 00:19:05 cryptocurrency is the most prominent use
  • 00:19:07 case of a blockchain and a
  • 00:19:09 cryptocurrency is really just something
  • 00:19:11 which uses the blockchain technology if
  • 00:19:13 the data you store in a block is a list
  • 00:19:16 of transactions and a transaction is
  • 00:19:18 basically just a piece of information
  • 00:19:19 where you say who is sending how much to
  • 00:19:23 whom these free things are important if
  • 00:19:25 you get a list of such transactions in a
  • 00:19:28 given block you obviously can take
  • 00:19:30 advantage of the security mechanism that
  • 00:19:32 these transactions can't be edited
  • 00:19:33 without the next block recognizing it
  • 00:19:36 and therefore now you have a
  • 00:19:38 cryptocurrency you have your own
  • 00:19:40 cryptocurrency managed in your
  • 00:19:41 blockchain which is distributed across
  • 00:19:43 many machines and which is therefore
  • 00:19:45 secured and that is what's used by
  • 00:19:48 Bitcoin aetherium ripple and all the
  • 00:19:50 cryptocurrencies you know so back to the
  • 00:19:54 picture from before our data now simply
  • 00:19:56 is that list of transactions it's that
  • 00:19:58 simple
  • 00:19:59 and what are the coins then well the
  • 00:20:02 coins is just something you come up with
  • 00:20:04 you can give the things you're
  • 00:20:06 transferring in the transactions
  • 00:20:07 whichever name you want coins are
  • 00:20:10 transferred with transactions that's all
  • 00:20:12 you have to from and how much the how
  • 00:20:14 much that is your coins and you can name
  • 00:20:16 them whatever you want
  • 00:20:17 the coins normally can't be changed to
  • 00:20:20 our real currencies unless you find
  • 00:20:22 someone of course who says hey please
  • 00:20:24 send me some coins and I will give you
  • 00:20:27 this amount of US Dollars or this amount
  • 00:20:30 of euros this is how it works
  • 00:20:32 the worth of the coins is really just
  • 00:20:35 what people think it is
  • 00:20:37 and this is how it works for all these
  • 00:20:39 cryptocurrencies that also explains
  • 00:20:40 their high volatility
  • 00:20:42 it's a new market and the true value of
  • 00:20:47 these things is still something we have
  • 00:20:49 to evaluate and finally coins are
  • 00:20:53 created with mining because of course we
  • 00:20:56 can send them around but this doesn't
  • 00:20:57 create them mining is a crucial part and
  • 00:20:59 mining is how new blocks are added we
  • 00:21:03 will have open transactions where you
  • 00:21:05 want to send something to someone and
  • 00:21:07 for that transaction to be confirmed it
  • 00:21:09 needs to be included in a new block
  • 00:21:11 which is added at the end of the
  • 00:21:12 blockchain people who are adding this
  • 00:21:15 are doing the so-called mining because
  • 00:21:17 this process actually takes a bit longer
  • 00:21:19 they have to solve a complex algorithm
  • 00:21:21 and they get some new coins out of the
  • 00:21:24 system as a reward so if we want to
  • 00:21:28 summarize it in a big picture this is
  • 00:21:30 how a blockchain looks like it actually
  • 00:21:32 runs on multiple machines so called
  • 00:21:34 nodes you also might have heard of the
  • 00:21:37 term wallet a wallet in the end is just
  • 00:21:40 a note that is a simplification we'll
  • 00:21:42 use in that course a wallet in the end
  • 00:21:44 is what enables you to send and receive
  • 00:21:45 coins it's a piece of data which allows
  • 00:21:49 you to identify yourself you can say and
  • 00:21:51 then you can send coins fruit
  • 00:21:53 transactions to our wallets and if such
  • 00:21:55 a new transaction is created it's
  • 00:21:57 broadcast to the nodes nodes also
  • 00:22:00 distributed across the whole node
  • 00:22:01 network and they will verify a
  • 00:22:04 transaction while doing so to make sure
  • 00:22:05 you have enough funds and to make sure
  • 00:22:08 that you really are who you claim to be
  • 00:22:10 and once a couple of transactions have
  • 00:22:13 been collected one node will eventually
  • 00:22:16 bundle them up in a new block and then
  • 00:22:19 inform the other nodes about that new
  • 00:22:21 block so that it is added to the block
  • 00:22:23 chains of all the nodes and all the
  • 00:22:25 different computers only after this new
  • 00:22:27 block and the whole chain that belongs
  • 00:22:29 to has been verified but all the nodes
  • 00:22:31 of course so that's the blockchain in a
  • 00:22:34 nutshell as I said I'll come back to all
  • 00:22:36 these core concepts throughout the
  • 00:22:38 course in the end it's a distributed
  • 00:22:40 secure data storage where each block is
  • 00:22:44 a folder with some content some data
  • 00:22:46 inside of it and it's a cryptocurrency
  • 00:22:49 if that content is
  • 00:22:51 transaction where we send coins from A
  • 00:22:53 to B
  • 00:23:01 so now that we had a first look at what
  • 00:23:04 pipe Ness and what the blockchain is and
  • 00:23:06 that we installed Python and wrote our
  • 00:23:09 first program let's see what the course
  • 00:23:12 as a whole has to offer for you
  • 00:23:14 we're almost done getting started in the
  • 00:23:17 next module we'll dive into the core
  • 00:23:19 base syntax of Python and regarding our
  • 00:23:22 blockchain we'll start building a simple
  • 00:23:24 chain a simple list of coins for now a
  • 00:23:28 coin will just be an amount just a
  • 00:23:30 number but it will be a great start
  • 00:23:32 thereafter we'll dive into loops and
  • 00:23:35 conditionals useful constructs that
  • 00:23:37 allow us to execute code either multiple
  • 00:23:40 times or only under certain conditions
  • 00:23:43 regarding our blockchain we'll be able
  • 00:23:45 to then verify our blockchain and check
  • 00:23:48 if the previous block is still the block
  • 00:23:51 it was when we added its next block so
  • 00:23:54 this connection between blocks we can
  • 00:23:55 start verifying it here thereafter we'll
  • 00:23:59 start working with more complex data
  • 00:24:01 structures because thus far we'll only
  • 00:24:03 have had a look at numbers text true and
  • 00:24:07 false so called boolean and lists now
  • 00:24:10 we'll get into more complex data
  • 00:24:12 structures which will then also allow us
  • 00:24:14 to work with real blocks and
  • 00:24:16 transactions which is more than just a
  • 00:24:18 number we'll dive deeper into built-in
  • 00:24:21 functions so functions pipe and ships
  • 00:24:23 width as well as text so called strings
  • 00:24:26 and will be able to then calculate and
  • 00:24:29 output the balances of the different
  • 00:24:31 users of our blockchain
  • 00:24:33 well thereafter have a look at the so
  • 00:24:35 called standard library this is this
  • 00:24:37 batteries included approach I was
  • 00:24:39 referring to the many packages – already
  • 00:24:42 has built in we'll have a look at them
  • 00:24:45 and some selected ones in this module
  • 00:24:47 and therefore we'll be able to start
  • 00:24:50 creating real hashes and the so called
  • 00:24:53 proof of work an important blockchain
  • 00:24:55 concept will then learn more about
  • 00:24:58 reading and writing files something we
  • 00:25:00 already did in our first pipe nap here
  • 00:25:02 at the beginning of this course but here
  • 00:25:05 of course we'll dive into it a bit
  • 00:25:06 deeper and learn more about what's
  • 00:25:08 happening there and how we use it and
  • 00:25:10 how we can do different kinds of reads
  • 00:25:12 and writes regarding our block
  • 00:25:14 we'll finally be able to store and read
  • 00:25:17 our blockchain in files and not just in
  • 00:25:20 memory
  • 00:25:20 we'll then also have a look at what we
  • 00:25:22 can do with things go wrong how we can
  • 00:25:24 handle errors how we can debug our code
  • 00:25:27 and find problems in our code this will
  • 00:25:31 allow us to make our blockchain more
  • 00:25:32 robust and more secure more safe will
  • 00:25:37 then also have a look at object
  • 00:25:39 orientated programming with classes this
  • 00:25:42 is super important it's a cool feature
  • 00:25:44 of modern programming languages and –
  • 00:25:47 has it – even though python is actually
  • 00:25:49 a bit older but – has that – it allows
  • 00:25:53 us to write cleaner code I'd argue
  • 00:25:55 easier to understand code and we'll use
  • 00:25:58 it on our block can to improve it and
  • 00:26:01 also to finally add a wallet this
  • 00:26:04 identification data which will in the
  • 00:26:06 end be super important for sending and
  • 00:26:09 receiving funds once we're done with
  • 00:26:11 that it's time to dive deeper into
  • 00:26:13 modules and packages we'll see how we
  • 00:26:16 can install third-party packages and
  • 00:26:18 split our code over multiple files
  • 00:26:20 regarding a blockchain we'll be able to
  • 00:26:22 sign transactions an important security
  • 00:26:24 mechanism and then we're already nearing
  • 00:26:27 the end of this course but we already
  • 00:26:29 learned a lot about the basics but we
  • 00:26:32 won't finish the course before all the
  • 00:26:34 diving a bit into HTTP and how we can
  • 00:26:36 handle incoming HTTP requests with
  • 00:26:39 Python and the flask package for our
  • 00:26:42 blockchain this will mean that we can
  • 00:26:43 control our blockchain with a nice web
  • 00:26:45 UI and therefore leave that terminal
  • 00:26:48 based UI we will have used until then
  • 00:26:50 and thereafter we'll also have a look at
  • 00:26:52 how we can not just receive requests
  • 00:26:54 with – but also how we can send them and
  • 00:26:57 therefore how we can communicate between
  • 00:26:59 different Python programs we'll also use
  • 00:27:02 that in our blockchain to simulate
  • 00:27:05 multiple so-called nodes so servers
  • 00:27:08 running on different machines which now
  • 00:27:10 can communicate with each other to
  • 00:27:11 broadcast new transactions blocks and
  • 00:27:14 all that stuff and there after we're
  • 00:27:16 done but of course I'll also let you
  • 00:27:18 know what possible next steps are where
  • 00:27:21 and how you can dive deeper and what you
  • 00:27:24 have learned in this course so a lot of
  • 00:27:26 cool content in there
  • 00:27:28 all the basics you need to know about
  • 00:27:30 Python and I can't wait to dive fully
  • 00:27:33 into it with you just a couple of other
  • 00:27:35 things about Python which you should
  • 00:27:37 know before we do get started
  • 00:27:46 there are many good reasons for learning
  • 00:27:49 a language like Python I already
  • 00:27:50 mentioned a lot it's versatility ease of
  • 00:27:53 use and what you can do with it I guess
  • 00:27:56 that's the worst Atilla t you can build
  • 00:27:58 all kinds of things with it but of
  • 00:28:00 course there are other languages than
  • 00:28:02 Pyfrom – there is C++ for example this
  • 00:28:06 awls is an extremely powerful language
  • 00:28:08 and it also can be used to build all
  • 00:28:10 kinds of things but it's a bit more
  • 00:28:12 complex than – you write things more
  • 00:28:15 from scratch than with Python it has
  • 00:28:18 less things built in you have to solve
  • 00:28:21 more problems on your own I'd say so
  • 00:28:23 it's a bit harder and we mostly use it
  • 00:28:26 for desktop apps it's very popular for
  • 00:28:28 creating games for example but they're
  • 00:28:31 also often it just runs behind the
  • 00:28:33 scenes and you use some framework some
  • 00:28:35 engine that builds up on it so you can
  • 00:28:38 absolutely write C++ code but it's a
  • 00:28:41 complex language and often you have
  • 00:28:43 other languages you use instead and for
  • 00:28:46 example PI from the version we're using
  • 00:28:47 builds up on C which is not exactly the
  • 00:28:51 same as C++ but generally related to it
  • 00:28:54 and Python is an extra layer on top of
  • 00:28:56 that you could say which is more
  • 00:28:57 accessible we also have languages like
  • 00:29:01 Java Java is also very versatile app
  • 00:29:04 which we can use to build desktop apps
  • 00:29:05 web apps and even mobile apps with
  • 00:29:07 Android
  • 00:29:08 and speaking of Android we also have
  • 00:29:11 things like Swift or objective-c which
  • 00:29:13 you use to build iOS apps or Mac OS apps
  • 00:29:16 we have languages like JavaScript which
  • 00:29:19 run in the browser or also can be used
  • 00:29:22 on the server so which you'd only use
  • 00:29:25 for building web apps PHP would be
  • 00:29:27 another example a language you really
  • 00:29:29 only use for building websites and web
  • 00:29:31 apps and then you will again have Python
  • 00:29:34 which is very versatile which used for
  • 00:29:36 data science desktop apps utility
  • 00:29:38 scripts and web apps so which one should
  • 00:29:42 you choose why is Python the right
  • 00:29:44 choice it obviously depends on what's
  • 00:29:47 you're building the cool thing about
  • 00:29:49 versatile languages like Python is that
  • 00:29:52 if you learn it you are basically free
  • 00:29:54 to build whatever you want at any given
  • 00:29:56 time in the future there are some
  • 00:29:58 languages like Java
  • 00:30:00 the script in the browser where do you
  • 00:30:01 have no alternative – can't be used to
  • 00:30:03 run in a browser – you build the front
  • 00:30:06 end of a website so to say and – all who
  • 00:30:09 can't really be used to build mobile
  • 00:30:11 apps but Python can be used for a lot of
  • 00:30:14 things that happen on a typical computer
  • 00:30:16 or when we talk about web development to
  • 00:30:19 build a web server and therefore this
  • 00:30:22 versatility really is a huge plus in
  • 00:30:24 case you're starting off and you're not
  • 00:30:26 100% sure where you want to go or if you
  • 00:30:29 know you want to go into a direction
  • 00:30:31 where Python is really strong like
  • 00:30:33 server-side web development or data
  • 00:30:35 science it's all important to recognize
  • 00:30:38 that only the two languages here on the
  • 00:30:40 right
  • 00:30:41 Swift and JavaScript in this example
  • 00:30:43 here have a UI attached to them by
  • 00:30:47 definition these allow you to build
  • 00:30:49 things where you see something with a
  • 00:30:51 nice UI relatively fast because you're
  • 00:30:54 either building mobile apps or you're
  • 00:30:55 building websites and both lead to
  • 00:30:58 things you can see you can touch you can
  • 00:31:00 use other languages like Python C++ and
  • 00:31:04 Java are more on the built the logic
  • 00:31:07 part less on the presentation part and
  • 00:31:09 therefore you can add you eyes to them
  • 00:31:12 and we will in this course but the core
  • 00:31:14 of Python and so on is really just the
  • 00:31:17 logic that runs on your machine which is
  • 00:31:19 all that the reason why for the majority
  • 00:31:21 of this course we will work in the
  • 00:31:23 terminal and execute our code on our
  • 00:31:26 computer because it's doing a lot of
  • 00:31:28 logical work which of course is the
  • 00:31:31 driver of any important application the
  • 00:31:34 logic that happens in your program not
  • 00:31:37 the presentation but as I said we'll add
  • 00:31:39 the presentation in this course to
  • 00:31:47 now to come to an end in this module
  • 00:31:49 let's have a brief look at the Python
  • 00:31:52 history and more importantly at the two
  • 00:31:54 important versions that exist we get PI
  • 00:31:57 from 2.7 and free Dex currently free dot
  • 00:32:00 six is the latest version soon it will
  • 00:32:02 be free dot seven now what's the
  • 00:32:04 difference
  • 00:32:05 Q dot seven is an older version it's the
  • 00:32:08 legacy of python learning it right now
  • 00:32:11 is not really something I would
  • 00:32:13 recommend or anyone would recommend
  • 00:32:15 there are some very very very niche
  • 00:32:17 cases and if you need to cover them
  • 00:32:19 learning the more modern version free
  • 00:32:21 Dex is still a better choice because it
  • 00:32:23 will then make it easy for you to learn
  • 00:32:25 in 2007 anyways but generally you don't
  • 00:32:27 need 2.7 free dot X is the future so
  • 00:32:31 whichever version of free is currently
  • 00:32:33 the latest one this will be the future
  • 00:32:35 of python eventually we'll probably also
  • 00:32:38 have a version 4 but for now we have
  • 00:32:40 version 3 and that will stick around for
  • 00:32:43 quite a long time version 4 is not even
  • 00:32:45 something that is known when it will be
  • 00:32:48 released right now
  • 00:32:49 so that is version 2.7 versus version 3
  • 00:32:53 dot X 2 dot 7 was released in 2010 and
  • 00:32:57 will be supported in 2020 so in case you
  • 00:32:59 need to use it you got two years to go
  • 00:33:02 but as I said free dot X is the one
  • 00:33:04 which is currently still active under
  • 00:33:06 development 2.7 is the last 2 dot X
  • 00:33:09 version there will be no 2.8 free dot X
  • 00:33:13 is still under development still gets
  • 00:33:15 new features and improvements it is the
  • 00:33:17 future of Python this is why you should
  • 00:33:18 learn it 2.7 has a slightly different
  • 00:33:21 syntax most is the same but there are
  • 00:33:24 some differences because free dot X
  • 00:33:27 added a lot of improvements and
  • 00:33:29 additions to the language made it better
  • 00:33:31 brought some things in line which
  • 00:33:34 previously behaved a bit differently and
  • 00:33:36 strange so it really added a lot to the
  • 00:33:39 language there still are some very few
  • 00:33:42 libraries that only work with Python 2.7
  • 00:33:45 but the vast majority of all third-party
  • 00:33:48 packages you might want to use have been
  • 00:33:50 ported to free X so this should very
  • 00:33:53 rarely be an argument for picking 2007
  • 00:33:56 and therefore learnfree X it's all the
  • 00:33:59 diversion we're using in this course for
  • 00:34:00 that reason
  • 00:34:01 it's the future of Python and of course
  • 00:34:03 you want to learn that future in this
  • 00:34:05 course
  • 00:34:13 so we're pretty much done getting
  • 00:34:15 started in the next module we'll dive
  • 00:34:17 into the base syntax of Python but I
  • 00:34:19 won't go there without letting you know
  • 00:34:21 how you get the most out of the course
  • 00:34:22 because that is really important to me
  • 00:34:24 you get the most out of the course by
  • 00:34:27 watching the videos now that seems
  • 00:34:29 obvious but there's more than one way of
  • 00:34:31 watching videos make sure to watch them
  • 00:34:33 at your speed if I'm going too fast you
  • 00:34:36 can use the controls in the udemy player
  • 00:34:38 to slow me down if I'm going too slow
  • 00:34:40 you can use them to speed me up also
  • 00:34:42 pause regularly if you're coding along
  • 00:34:46 which I strongly recommend you might
  • 00:34:48 need to pause a video so you can catch
  • 00:34:51 up with me because if I take too long
  • 00:34:54 breaks everyone who's not coding along
  • 00:34:56 will have a hard time and will get bored
  • 00:34:58 so I'm trying to find the right balance
  • 00:35:00 and therefore it's important that you
  • 00:35:02 adjust the speed that you take pauses
  • 00:35:04 and also that you sometimes rewind the
  • 00:35:06 video if there's a concept which is not
  • 00:35:08 clear it's on the montt course so take
  • 00:35:11 advantage of this this is really how you
  • 00:35:13 get the most out of that so code along
  • 00:35:16 that is something I do encourage you to
  • 00:35:18 do do the exercises I offer in this
  • 00:35:20 course they are great for checking
  • 00:35:22 whether you really understood a certain
  • 00:35:24 concept the things taught in a certain
  • 00:35:26 module and of course use the course
  • 00:35:30 resources so all the source code I show
  • 00:35:33 is attached to this course and not just
  • 00:35:36 one code attachment per module but most
  • 00:35:39 of the time I have multiple snapshots
  • 00:35:41 per module you'll find them attached to
  • 00:35:43 the last lecture of each module and
  • 00:35:45 download and compare them to your code
  • 00:35:48 if there is something which is unclear
  • 00:35:49 if you're facing some strange issue then
  • 00:35:52 my code is really there to help you the
  • 00:35:55 same goes for links which you find in
  • 00:35:57 the last lectures of each module there I
  • 00:36:00 provide useful links to other resources
  • 00:36:02 which allow you to dive deeper learn
  • 00:36:04 more or look things up so definitely
  • 00:36:06 take advantage of this too now sometimes
  • 00:36:09 you just get stuck though or there is
  • 00:36:11 something you you don't fully understand
  • 00:36:13 ask in QA I'm happy to help there I do
  • 00:36:16 read a reply there regularly so I do my
  • 00:36:19 best to provide quick help in QA but if
  • 00:36:23 you can help our students that's even
  • 00:36:25 better not
  • 00:36:26 because you're helping me but because I
  • 00:36:28 really mean it like that this is the way
  • 00:36:30 how you learn the most I guarantee you
  • 00:36:33 that this is an awesome way of learning
  • 00:36:35 way more because asking it's super
  • 00:36:38 simple but answering solving a problem
  • 00:36:41 that's difficult and I can only
  • 00:36:44 encourage you to do that it can be
  • 00:36:46 challenging to go out there and put your
  • 00:36:49 opinion out there and solve a problem
  • 00:36:51 and potentially be wrong but what could
  • 00:36:54 happen you're wrong
  • 00:36:55 so what then you get corrected and
  • 00:36:57 you'll learn again so definitely give
  • 00:36:59 that a try and answer in the Q&A section
  • 00:37:02 you'll find a lot of problems there and
  • 00:37:04 solving them that is something where you
  • 00:37:06 can learn a lot and at the same time
  • 00:37:09 also help other students this is how you
  • 00:37:12 will get the most out of the course
  • 00:37:14 follow these steps and you should have a
  • 00:37:16 more than solid knowledge of the Pythian
  • 00:37:20 fundamentals and core concepts and how
  • 00:37:22 to write Python programs by the end of
  • 00:37:25 the course and a nice side effect you'll
  • 00:37:28 also have a very solid understanding of
  • 00:37:30 what the blockchain actually is and how
  • 00:37:32 it works so with that let's finally dive
  • 00:37:34 into the next module and start with the
  • 00:37:36 base syntax of Python
  • 00:37:45 so let's start diving deeper into – now
  • 00:37:48 that we played around with it a bit in
  • 00:37:50 the command line and did we learn the
  • 00:37:53 basics about what a blockchain actually
  • 00:37:55 is it's time to learn the base syntax of
  • 00:37:58 Python its core features and of course
  • 00:38:01 also to start building our basic
  • 00:38:04 blockchain in this module we'll
  • 00:38:07 therefore have a look at the rabble so
  • 00:38:10 this command line again and we'll also
  • 00:38:12 explore how we write a real Python
  • 00:38:15 program outside of this command line
  • 00:38:17 with normal files and an IDE and
  • 00:38:20 integrated developer environment now
  • 00:38:23 that's of course just the setup part
  • 00:38:25 thereafter we'll dive into the Python
  • 00:38:28 language and we'll start working with
  • 00:38:30 variables and see which data types we
  • 00:38:34 can store in these variables and of
  • 00:38:36 course what variables are to begin with
  • 00:38:38 now we'll also see that with variables
  • 00:38:41 alone we can't do that much so we'll
  • 00:38:44 also have a look at operators will
  • 00:38:47 understand what operators are and which
  • 00:38:49 operators exist and how we can use them
  • 00:38:51 now we will build a block chain in this
  • 00:38:55 course and as the name suggests a block
  • 00:38:57 chain includes that chain part so
  • 00:39:00 multiple pieces of something therefore
  • 00:39:03 in this module we'll also explore what
  • 00:39:05 lists are in how we work with lists in
  • 00:39:07 Python and you round up this module and
  • 00:39:11 write a program that is a little bit
  • 00:39:13 more useful than what we had thus far
  • 00:39:16 we'll also dive into functions a very
  • 00:39:19 useful language construct which we'll
  • 00:39:21 use quite a lot throughout the course
  • 00:39:23 now that's the Python part you could say
  • 00:39:26 what about the Plock chain well
  • 00:39:28 regarding the block chain at the end of
  • 00:39:30 the module we'll be able to manage a
  • 00:39:32 list of blocks and each block will
  • 00:39:35 actually contain some data or a list of
  • 00:39:38 data so we will start working with our
  • 00:39:40 coins in the section even though our
  • 00:39:43 block chain won't be finished by the end
  • 00:39:45 of it of course we'll be able to add or
  • 00:39:48 mine data to our block chain and we'll
  • 00:39:51 also be able to output our block chain
  • 00:39:53 so that we can see it so with that let's
  • 00:39:55 get started and let's dive right into it
  • 00:40:04 before we start setting up a local
  • 00:40:06 environment let's go back to that
  • 00:40:08 command line approach I called it the
  • 00:40:11 rebel now rebel stands for read eval
  • 00:40:15 print loop the read part is basically
  • 00:40:18 the part where we enter something into
  • 00:40:20 the command line like two plus two
  • 00:40:22 evaluate or evil is the next phase where
  • 00:40:26 Python analyzes our command once we hit
  • 00:40:28 enter now once it's done analyzing it
  • 00:40:31 prints the result for in this case and
  • 00:40:35 the L part the loop is basically where
  • 00:40:38 it well goes back to start and waits for
  • 00:40:41 the next input that is why we refer to
  • 00:40:43 that command line interface to the rebel
  • 00:40:45 it is an interactive shell where we can
  • 00:40:48 just issue commands and let – do the job
  • 00:40:50 it's not really helpful for writing big
  • 00:40:54 Python programs but it's a nice
  • 00:40:56 interactive way of working with Python
  • 00:40:58 and great for simple calculations and
  • 00:41:01 practicing so let's go back to the
  • 00:41:03 wrapper
  • 00:41:04 before we actually set up a real project
  • 00:41:06 with an IDE of course we'll go back to
  • 00:41:09 the rabble to start working on our block
  • 00:41:12 chain and for that let's leave out the
  • 00:41:14 node and transaction part and let's
  • 00:41:16 focus on the chain of blocks which
  • 00:41:18 contain data now that of course means we
  • 00:41:21 have Q essential parts we got the data
  • 00:41:25 and we got the chain now for data in
  • 00:41:28 this module we'll have a look at which
  • 00:41:31 types of data we can use in Python
  • 00:41:33 because no matter which Python program
  • 00:41:36 you're going to write if it's a flight
  • 00:41:39 booking system if it's your own search
  • 00:41:41 engine or if it's a block chain you will
  • 00:41:44 work with data all the time
  • 00:41:46 data your user enters data you read from
  • 00:41:49 some file anything like that and for
  • 00:41:52 data we can basically work with numbers
  • 00:41:55 strings which is text or boolean and
  • 00:41:59 we'll have a closer look at what this is
  • 00:42:00 and what all these data types are in
  • 00:42:02 just a second we also have more complex
  • 00:42:05 data structure said which we'll have a
  • 00:42:07 look at later in this course now the
  • 00:42:09 chain in the end is just a list a
  • 00:42:12 special type of data in Python as you
  • 00:42:14 will learn now let's go back to the
  • 00:42:16 basic data types
  • 00:42:18 boring and boolean numbers are divided
  • 00:42:22 up in integers which is basically a
  • 00:42:24 number without a decimal place and
  • 00:42:26 floats which is a number with a decimal
  • 00:42:29 place and we have both negative and
  • 00:42:32 positive values for numbers we obviously
  • 00:42:34 work with numbers a lot in most programs
  • 00:42:37 be that your accounting software you're
  • 00:42:39 creating where users might enter their
  • 00:42:41 data which typically would be numbers or
  • 00:42:43 be that your plug chain where a user
  • 00:42:46 sent money around most programs work
  • 00:42:48 with some kind of numbers we also get
  • 00:42:51 boolean stowe and boolean are two kinds
  • 00:42:54 of values true and false now this might
  • 00:42:58 not look super useful if you're brand
  • 00:43:00 new to programming but actually being
  • 00:43:03 able to tell if something is true or
  • 00:43:05 false is very useful if you're working
  • 00:43:08 with conditions something we'll also
  • 00:43:10 dive in in the course sometimes you want
  • 00:43:12 to check if something is true if a
  • 00:43:14 certain condition is met and then
  • 00:43:16 execute a certain part of your code and
  • 00:43:19 execute a different part of your code if
  • 00:43:21 something is false and therefore boolean
  • 00:43:24 are a great way of handling that but of
  • 00:43:27 course again we'll see how we use them
  • 00:43:28 in practice throughout this course and
  • 00:43:30 strings are just texts well just like
  • 00:43:34 numbers working with text is obviously
  • 00:43:36 something you do in pretty much any
  • 00:43:38 program again be it as your accounting
  • 00:43:41 software where you enter the name of a
  • 00:43:45 certain transaction which is going on or
  • 00:43:48 be that your blockchain where you enter
  • 00:43:50 the sender and receiver of a transaction
  • 00:43:52 which might have a unique name or
  • 00:43:54 something like that now strings in
  • 00:43:57 Python can be written with single or
  • 00:43:59 double quotation marks and you should
  • 00:44:01 simply pick either of the two approaches
  • 00:44:03 whichever you prefer
  • 00:44:04 and then stick to it they are exactly
  • 00:44:07 the same
  • 00:44:08 now finally I already mention it we got
  • 00:44:11 complex data types like dictionaries and
  • 00:44:13 objects will not worry about them right
  • 00:44:15 now we'll come back to that later in the
  • 00:44:18 course now with that let's see what
  • 00:44:21 exactly numbers are and how we can use
  • 00:44:23 them in a typical Python program
  • 00:44:32 now to work with numbers I'm back in the
  • 00:44:35 rabble or right now it would be precise
  • 00:44:38 only in the terminal or command prompt
  • 00:44:41 of my machine we entered the Python
  • 00:44:44 rebel by typing Python assuming that
  • 00:44:47 Python was installed which we did in the
  • 00:44:48 first course module now once you hit
  • 00:44:51 enter you're in the Python R Apple and
  • 00:44:53 you can always quit it by hitting ctrl
  • 00:44:55 set now let's get back into it though a
  • 00:44:58 number is simply something like this –
  • 00:45:01 if I enter it just basically prints out
  • 00:45:04 what I entered now often you work with
  • 00:45:07 data so not just with numbers but also
  • 00:45:09 with strings and boolean by storing them
  • 00:45:12 in so-called variables variables are
  • 00:45:15 data containers you could say which you
  • 00:45:17 define so you could create a variable by
  • 00:45:20 simply typing a name for it which could
  • 00:45:23 be age the name is totally up to you and
  • 00:45:25 then the variable only becomes useful if
  • 00:45:28 you store a value in it so you can think
  • 00:45:31 of a variable as an address like the
  • 00:45:34 number of your house you then store
  • 00:45:37 something in there by adding an equal
  • 00:45:38 sign and on the right side of the equal
  • 00:45:40 sign you write what you want to store in
  • 00:45:43 that address so in that variable for
  • 00:45:45 example 29 which would be my age if you
  • 00:45:48 had entered here in the rebel it will
  • 00:45:50 not output anything but if you now just
  • 00:45:52 type H it will print out the value you
  • 00:45:55 stored in age now this already is a very
  • 00:45:59 useful and important part you gotta
  • 00:46:01 understand we will work with variables a
  • 00:46:05 lot in this course any program uses a
  • 00:46:08 lot of variables because as the name
  • 00:46:10 suggests they allow you to do dynamic
  • 00:46:12 calculations you cannot just hard-coded
  • 00:46:16 value in there you can also manipulate
  • 00:46:18 that value whilst your program runs for
  • 00:46:21 example here if I type age again and
  • 00:46:23 again prints 29 cost test value which we
  • 00:46:26 stored and it keeps that in memory it
  • 00:46:28 does not clear it after we output it one
  • 00:46:31 time and if we now for example set H
  • 00:46:33 equal to 30 and a type age again it
  • 00:46:37 changed it it overrode this we could
  • 00:46:40 also write H equals age minus 1 so now
  • 00:46:45 we would take the Privy
  • 00:46:46 value which restored in age deduct one
  • 00:46:49 from it and store that new value which
  • 00:46:51 is calculated on the right side of the
  • 00:46:54 equal sign back into the H variable if I
  • 00:46:57 hit enter and type H again it now
  • 00:46:59 outputs 29 for that reason and this is
  • 00:47:02 the usefulness of variables we can do a
  • 00:47:05 lot with them we can run all types of
  • 00:47:08 calculation store in between results
  • 00:47:11 which we might need at a later point of
  • 00:47:13 time in our program and of course also
  • 00:47:15 output them to the users and thus far in
  • 00:47:19 this case here we only work with numbers
  • 00:47:21 with integers to be precise with numbers
  • 00:47:24 that don't have a decimal place of
  • 00:47:25 course you can also store floats in a
  • 00:47:30 variable like here now it's not 29.5 and
  • 00:47:34 if I output this we see 29.5 and
  • 00:47:36 obviously you're not limited to numbers
  • 00:47:39 you can also create a variable which
  • 00:47:42 isn't is old the naming convention here
  • 00:47:45 by the way in Python is to name it like
  • 00:47:46 this if you got one variable that
  • 00:47:49 consists of multiple words you cannot
  • 00:47:51 write is old with a blank that's invalid
  • 00:47:54 it has to be one word but you still make
  • 00:47:56 it easier to read you use underscores to
  • 00:47:59 separate words so you got all lowercase
  • 00:48:01 names with underscores between the
  • 00:48:03 different words this ensures readability
  • 00:48:06 and this valid Python syntax but back to
  • 00:48:09 what I originally wanted to say you can
  • 00:48:11 store for example true in here
  • 00:48:13 that's a boolean do you remember that
  • 00:48:15 slide true and false now you got is old
  • 00:48:18 in here and with that you can of course
  • 00:48:21 also output that and later once we learn
  • 00:48:24 about conditional checks you can use
  • 00:48:26 that cue check if is old is true because
  • 00:48:29 let's say that is calculated dynamically
  • 00:48:32 and then run a different code if that is
  • 00:48:35 the case instead of running the normal
  • 00:48:37 code which you might run if is old is
  • 00:48:40 false the last but not least let's also
  • 00:48:42 have a look at strings if I enter name
  • 00:48:44 the important part here is the quotation
  • 00:48:47 marks so single or double quotation
  • 00:48:49 marks both works and then the text in
  • 00:48:52 this case max make sure to also closed
  • 00:48:55 equitation marks now if you output this
  • 00:48:57 you got the name printed here so these
  • 00:49:00 the free core values or datatypes with
  • 00:49:03 which we work in Python numbers where we
  • 00:49:06 have integers and floats boolean swear
  • 00:49:08 we have true and false and strings which
  • 00:49:11 are just text enclosed by double or
  • 00:49:14 single quotation marks
  • 00:49:22 now that we had a first look at the
  • 00:49:24 important data types and how we use them
  • 00:49:27 with the very important concept of
  • 00:49:29 variables let's have a brief look at
  • 00:49:32 what exactly can be done with numbers or
  • 00:49:35 some important information you should
  • 00:49:36 have on them so we got integers and
  • 00:49:38 floats that's no news to you here are
  • 00:49:41 examples for integers and the important
  • 00:49:44 part here is an integer in Python can be
  • 00:49:46 as big or small if it's a negative
  • 00:49:49 number as supported by your memory and
  • 00:49:51 operating system because 64-bit systems
  • 00:49:54 for example can leverage more of the
  • 00:49:57 memory than 32-bit systems that's why
  • 00:49:59 the operating system also matters but
  • 00:50:02 the important part is this is the only
  • 00:50:04 limiting factor there is no built-in cap
  • 00:50:07 or limit so if you want to store a
  • 00:50:09 really big number in the variable you
  • 00:50:12 can probably do that if your memory
  • 00:50:14 supports it now another important piece
  • 00:50:17 of information sometimes you have some
  • 00:50:19 data in a different data type let's say
  • 00:50:22 you have a valid number which for some
  • 00:50:24 reason is stored in a string so text
  • 00:50:27 let's say you have the text 10 you
  • 00:50:30 recognize that it's text because of
  • 00:50:32 these rounding quotation marks but you'd
  • 00:50:34 like to do a calculation with it now let
  • 00:50:37 me show you how this would work here's
  • 00:50:39 an example let's say I have my age which
  • 00:50:41 is 29 but notice the surrounding
  • 00:50:45 quotation marks if I now output H +1
  • 00:50:48 here you see I get an error that this
  • 00:50:51 must be a string not an integer because
  • 00:50:53 H is a string but 1 is an integer so
  • 00:50:57 what would work is H plus 1 and now we
  • 00:51:02 would have 291 because it's now not
  • 00:51:05 doing the arithmetic addition where it
  • 00:51:08 adds 1 to 29 it's just concatenating the
  • 00:51:12 two strings and creating one word out of
  • 00:51:15 the tube now this is of course fine if
  • 00:51:18 you had something like hello plus world
  • 00:51:22 and you wanted to create one combined
  • 00:51:24 string but it's not what you want if
  • 00:51:26 you're working with numbers and to fix
  • 00:51:30 this or to have a solution for this case
  • 00:51:31 2 there is a helpful function int
  • 00:51:35 if you wrap a string or a float with
  • 00:51:39 that function it actually converts it to
  • 00:51:43 an integer which you can of course then
  • 00:51:45 use as a normal integer because it is a
  • 00:51:47 normal integer so back to the example
  • 00:51:49 here we could say int age and this would
  • 00:51:54 convert the age string which is 29 Q an
  • 00:51:58 integer plus 1 and now we don't get an
  • 00:52:02 error anymore so this error from above
  • 00:52:04 is gone but now we get Ferdie as a
  • 00:52:06 result no as a side note if you wrap
  • 00:52:09 something which can't be converted you
  • 00:52:11 get an error if you wrap a float you get
  • 00:52:15 the well rounded integer to be precise
  • 00:52:19 as you can see with 1.8 it always rounds
  • 00:52:22 down so it basically creates the floor
  • 00:52:24 of it it cuts off the decimal place and
  • 00:52:26 if you wrap true for example it gives
  • 00:52:29 you 1 and in false would be 0 that's
  • 00:52:32 also an interesting convention to keep
  • 00:52:33 in mind true converted to an integer is
  • 00:52:36 1 false is 0 and of course we can also
  • 00:52:40 do these conversions with negative
  • 00:52:42 numbers as you can see there it all just
  • 00:52:44 cuts the decimal place so this is the
  • 00:52:47 int function for floats we also got some
  • 00:52:51 examples here it's all important to know
  • 00:52:54 that your memory and operating system is
  • 00:52:57 the limiting factor when it comes to the
  • 00:52:59 size or the biggest number or the
  • 00:53:01 smallest number you can store and just
  • 00:53:03 as with int you got the float function
  • 00:53:06 to convert our types to float here's
  • 00:53:09 another example let's say H is 29 as a
  • 00:53:13 string again now we can also output
  • 00:53:15 float H and we get 29.0 because it's
  • 00:53:21 converted to a float so to a number and
  • 00:53:23 as a special number a float therefore
  • 00:53:26 float H plus 1 would give us free dot 0
  • 00:53:30 if we said float true or if we write
  • 00:53:33 this we get 1.0 and float false is what
  • 00:53:38 would you guess it's 0 dot 0 so it's
  • 00:53:41 basically the same result as with the in
  • 00:53:43 function but always attaching a decimal
  • 00:53:46 place so just a Stian function this is
  • 00:53:48 very you
  • 00:53:49 if you got some data in the wrong data
  • 00:53:52 type but with a value which you know
  • 00:53:55 that it can be treated as a number and
  • 00:53:57 then you got an easy way of converting
  • 00:53:59 that data type to a number now one final
  • 00:54:02 piece of important information I want to
  • 00:54:04 share with you both integers and floats
  • 00:54:08 can also be written like this year at
  • 00:54:10 the bottom you can add underscores to
  • 00:54:14 make long numbers easier to read so
  • 00:54:18 let's say if I would store the amount of
  • 00:54:20 fans I got where it certainly is 1
  • 00:54:23 million then I can of course do it like
  • 00:54:25 this there's a bit harder to read though
  • 00:54:27 so what I can do is I can also write it
  • 00:54:29 like this
  • 00:54:30 the output fans I still get the outer
  • 00:54:33 format because that's the number with
  • 00:54:35 which Python will calculate but when
  • 00:54:38 assigning the value this clearly is
  • 00:54:40 easier to read if I output fans +1 I
  • 00:54:44 still get the more unreadable output
  • 00:54:46 though because again internally there
  • 00:54:48 only is one type of number this
  • 00:54:50 underscores syntax is only there for
  • 00:54:53 improved readability
  • 00:55:01 we had a closer look at numbers now
  • 00:55:04 before we have a closer look at boolean
  • 00:55:06 x' and strings let's have a look at
  • 00:55:09 something where we already used one part
  • 00:55:11 operators now we already use the plus
  • 00:55:14 operator to add two numbers I guess it's
  • 00:55:17 pretty clear what the plus operator does
  • 00:55:20 it's called operator because we perform
  • 00:55:22 an operation with it we add two numbers
  • 00:55:25 in this case now of course Python has
  • 00:55:28 more than the addition operator we can
  • 00:55:30 also do subtractions for example we can
  • 00:55:33 simply take two numbers and subtract
  • 00:55:35 them from with Java now of course we're
  • 00:55:38 not limited to that we can do all the
  • 00:55:40 basic arithmetic so we can also multiply
  • 00:55:42 numbers we can also divide numbers like
  • 00:55:45 this and here the important part just is
  • 00:55:47 here note that we're dividing two
  • 00:55:50 integers 5 & 10 but the result by
  • 00:55:54 default actually is a float so Python
  • 00:55:57 does not give us an integer something
  • 00:56:00 like 0 maybe because it cuts off the
  • 00:56:02 decimal place but it does the conversion
  • 00:56:04 to a float for us and gives us that
  • 00:56:07 float result now if you want to have the
  • 00:56:11 other behavior though if you want to get
  • 00:56:13 an integer as a result you can use the
  • 00:56:15 double slashes that is the floor
  • 00:56:18 division operator here for the same
  • 00:56:22 operation with the double slash operator
  • 00:56:25 we get 0 as a result because it
  • 00:56:28 automatically cuts off the decimal place
  • 00:56:31 we also get two asterisks which would be
  • 00:56:35 the power operator so if we got 5 to the
  • 00:56:38 power of 10 for example we would get
  • 00:56:40 dead result here so this is really just
  • 00:56:43 allowing us to do some exponential
  • 00:56:44 calculations now last but not least
  • 00:56:47 whenever a core operator we gotta know
  • 00:56:49 is the modulus operator we can use it to
  • 00:56:52 get the remainder of division so in this
  • 00:56:55 case here 5 divided by 10 we can't
  • 00:56:58 divide this 10 doesn't fit into 5 one
  • 00:57:00 single time so the remainder is 5 and
  • 00:57:03 that would be the result now one
  • 00:57:05 important note about these operators
  • 00:57:07 plus operator all that works with
  • 00:57:10 strings as you already saw you can
  • 00:57:12 concatenate two strings
  • 00:57:15 and to multiply the multiplication
  • 00:57:17 operator also kind of works with strings
  • 00:57:20 but only if used correctly let me show
  • 00:57:23 you what this means if we got a string
  • 00:57:25 like hello and another string like world
  • 00:57:28 if we add the plus so the addition
  • 00:57:31 operator – will concatenate both into
  • 00:57:34 one string so if we create a greeting
  • 00:57:38 variable and we store that hello world
  • 00:57:40 text I just created and we are for
  • 00:57:43 treating there after then we just get
  • 00:57:45 hello world here so one string now
  • 00:57:48 that's good to know I also mentioned
  • 00:57:50 that you can multiply strings now what I
  • 00:57:53 don't mean is that you can do hello
  • 00:57:55 times world if you try that you'll get
  • 00:57:59 an error because this is a type of
  • 00:58:01 multiplication Python is not capable of
  • 00:58:03 doing what you can do however is you can
  • 00:58:07 take a character or also some text like
  • 00:58:10 hello and multiply it with a number like
  • 00:58:13 10 and then Python will repeat that text
  • 00:58:16 10 times and concatenate it into one
  • 00:58:19 string this is often useful if you
  • 00:58:22 create some kind of graphical output
  • 00:58:26 with let's say some separation you want
  • 00:58:28 to create a horizontal line well you can
  • 00:58:31 do it like that
  • 00:58:32 this is what you can do with strings the
  • 00:58:35 other operators which you use just with
  • 00:58:38 numbers and that includes both integers
  • 00:58:40 and floats should be pretty clear and
  • 00:58:42 simply play around with them to learn
  • 00:58:45 more about them
  • 00:58:53 with these very basics about numbers and
  • 00:58:57 operators out of the way you're probably
  • 00:58:59 wondering how we'll use that in the
  • 00:59:02 blockchain well we'll use that data to
  • 00:59:05 store it as data in our blockchain so as
  • 00:59:08 the amount of coins we're sending with a
  • 00:59:10 transaction for example but before we do
  • 00:59:12 that there are two important things I
  • 00:59:14 want you to understand about numbers
  • 00:59:16 because you might encounter it and then
  • 00:59:18 it might look strange
  • 00:59:19 one is you can also write longer numbers
  • 00:59:22 in an exponential notation like 1e 10
  • 00:59:26 that is a valid number which pipe will
  • 00:59:28 read and store as this number so this
  • 00:59:33 year would be 1 million for example and
  • 00:59:35 as you can see it automatically stores
  • 00:59:37 it as a float and the even more
  • 00:59:40 important part is if you do something
  • 00:59:42 subtraction like 1 minus dot 9 what
  • 00:59:46 would you expect as a result well you
  • 00:59:49 clearly would expect 0.1 right but if
  • 00:59:53 you had entered you actually get a value
  • 00:59:55 close to 0.1 now this clearly is not the
  • 00:59:59 value we expected we expected 0.1 as a
  • 01:00:02 result now the reason for this
  • 01:00:04 strange-looking result is dead as you
  • 01:00:07 might know computers work where zeros
  • 01:00:09 and ones they work in the binary system
  • 01:00:12 therefore any floating-point number is a
  • 01:00:15 challenge for them and that's not just a
  • 01:00:17 case for Python but for any programming
  • 01:00:19 language the reason for this being a
  • 01:00:22 challenge is that anything which is a
  • 01:00:26 decimal place no matter if it's 100 dot
  • 01:00:28 1 or 0.1 is lower than 1 so the part
  • 01:00:34 after the dot always is lower than 1 per
  • 01:00:36 definition now values lower than 1 are
  • 01:00:40 difficult because they only know 0 and 1
  • 01:00:43 so values lower than 1 have to be
  • 01:00:46 reflected as a fraction of 1 and when
  • 01:00:51 doing these fractions they run into
  • 01:00:53 problems if you're working with values
  • 01:00:55 which are hard to devise by 2 so if it's
  • 01:01:00 not a binary fraction and 0.9 isn't then
  • 01:01:04 you'll have a problem for there
  • 01:01:06 reason one Minister five actually leads
  • 01:01:10 to a clean result because dot v can be
  • 01:01:13 written by the computer or understood by
  • 01:01:15 the computer as 1/2 now 0.9 cannot be
  • 01:01:20 written like this because there is no
  • 01:01:22 integer by which we can divide 1 which
  • 01:01:24 will lead to dot 9 hence it will
  • 01:01:26 actually chain a couple of divisions and
  • 01:01:28 that would really be too complex here
  • 01:01:30 which leads to the computer getting
  • 01:01:32 closer to 0.9 but it never fully reaches
  • 01:01:36 it and therefore 1 minus 0.9 actually
  • 01:01:40 leads to this strange output and
  • 01:01:42 therefore even if you type 1 minus dot
  • 01:01:44 nine minus dot 1 you get a value very
  • 01:01:46 close to 0 but not exactly zero so this
  • 01:01:50 is a limitation or a certain missing
  • 01:01:53 precision you have to be aware of it's
  • 01:01:55 not really an issue in your day-to-day
  • 01:01:57 life because these approximations
  • 01:01:59 actually give you a very close
  • 01:02:02 approximation but it is something to be
  • 01:02:05 aware of and most of all I don't want
  • 01:02:07 you to wonder if somewhere in the course
  • 01:02:10 you see a value like this it's coming
  • 01:02:12 from that missing floating point
  • 01:02:14 precision which is inherent to computers
  • 01:02:16 and also therefore to Python it's not an
  • 01:02:19 issue of pipe though it's something the
  • 01:02:21 computer basically well has its issues
  • 01:02:24 with
  • 01:02:31 now let's also have a brief look at how
  • 01:02:34 we work with strings strings are
  • 01:02:36 generally really easy to use as you
  • 01:02:39 learn you create a string and store it
  • 01:02:41 in a variable for example by using
  • 01:02:43 quotation marks either double single
  • 01:02:46 quotation marks with your text in
  • 01:02:48 between or double double quotation marks
  • 01:02:51 with the text in between either of the
  • 01:02:53 two can be used you can't combine them
  • 01:02:55 though so you can't open with a double
  • 01:02:57 quotation mark and close with a single
  • 01:02:58 one or the other way around so you have
  • 01:03:00 to stay consistent the advantage of
  • 01:03:03 double quotation marks is that there you
  • 01:03:07 could also use the single quotation mark
  • 01:03:09 as part of the text like in I'm pretty
  • 01:03:13 cool
  • 01:03:15 where is for single quotation marks this
  • 01:03:19 will fail I am pretty cool there still
  • 01:03:23 is a way around this with single
  • 01:03:24 quotation marks Q you can escape that
  • 01:03:26 character and I'll again dive into
  • 01:03:28 escaping later in this course but you
  • 01:03:30 can add a backslash in front of the
  • 01:03:32 character you want to escape and now it
  • 01:03:34 is accepted and if you output the string
  • 01:03:36 you see now you just got the single
  • 01:03:38 quotation mark and double quotation
  • 01:03:40 marks around it but there are your
  • 01:03:42 options double or single quotation marks
  • 01:03:44 now if you've got a longer text you also
  • 01:03:47 sometimes want to have multi-line text
  • 01:03:49 and you can create multi-line strings by
  • 01:03:51 using three double quotation marks
  • 01:03:53 opening and closing and in between you
  • 01:03:56 can add your text which can also span
  • 01:03:59 multiple lines as you can see here if I
  • 01:04:02 don't close it but just open it you can
  • 01:04:05 add your text then you can't continue
  • 01:04:07 typing on the next line which can span
  • 01:04:10 multiple lines and close it with three
  • 01:04:14 double quotation marks once you're done
  • 01:04:15 and then you're a longer text is
  • 01:04:17 actually in line if you output it with
  • 01:04:20 another special character in between for
  • 01:04:22 the line breaks backslash n marks a line
  • 01:04:25 break but this makes entering it much
  • 01:04:28 more convenient since you can really
  • 01:04:30 type it across multiple lines too now
  • 01:04:33 we'll come back to working with strings
  • 01:04:34 and escape characters like this later in
  • 01:04:37 the course for now it's important to
  • 01:04:39 know that you can have single or double
  • 01:04:40 quotation marks or for multi-line text
  • 01:04:43 also triple
  • 01:04:44 double quotation marks
  • 01:04:52 enough about the basic datatypes we want
  • 01:04:56 to build a blockchain we've had a first
  • 01:04:59 look at the data we're going to store in
  • 01:05:02 that blockchain so in the blocks of our
  • 01:05:04 blockchain and later in the course we'll
  • 01:05:06 of course refine that data to reflect a
  • 01:05:08 full transaction and not just one word
  • 01:05:10 or one number but for the blockchain we
  • 01:05:15 also need to chain part and for that
  • 01:05:17 we'll use a list a list in Python is
  • 01:05:21 created with square brackets you might
  • 01:05:23 know it as an array in other programming
  • 01:05:26 languages a list holds different values
  • 01:05:30 they can be of the same type so it can
  • 01:05:32 be a list of numbers or text but it can
  • 01:05:35 also be a mixed list like this one this
  • 01:05:38 list has text a floating number boolean
  • 01:05:42 and even a nested list which in turn
  • 01:05:45 holds some text and a number important
  • 01:05:48 the elements in the list are separated
  • 01:05:50 with commas and a list in Python has a
  • 01:05:55 certain length which we can get from it
  • 01:05:57 in this case but before it this gives us
  • 01:06:00 the information about how many values
  • 01:06:01 are inside of a list now typically we
  • 01:06:04 don't just want to have a list to look
  • 01:06:07 at it we also want to be able to access
  • 01:06:10 the elements in our list and change them
  • 01:06:13 or output them and for that we'll need
  • 01:06:16 something which is called the index each
  • 01:06:19 element in the list has its own index
  • 01:06:21 and here's something very important
  • 01:06:23 which is something you have to memorize
  • 01:06:25 the index and pipe lists starts with 0
  • 01:06:30 so we get 0 1 2 3 we've got 4 values but
  • 01:06:34 the first value has an index of 0
  • 01:06:36 therefore the last value in this case
  • 01:06:38 has an index of 3 and accessing values
  • 01:06:42 by their index is actually super simple
  • 01:06:44 we take the variable name in which we
  • 01:06:47 store the list let's say it's my list
  • 01:06:49 and then we use square brackets and the
  • 01:06:52 index to output or to access the value
  • 01:06:55 stored at this index in this case it
  • 01:06:57 would be some text let's have a look at
  • 01:07:00 this in practice let's say we're
  • 01:07:02 creating our block chain and therefore
  • 01:07:03 all create my block chain variable
  • 01:07:06 and this holds a list as I said we
  • 01:07:09 created with square brackets where we
  • 01:07:11 now add values now we obviously are
  • 01:07:14 building our coin in this course here so
  • 01:07:17 let's say our values are the amounts of
  • 01:07:21 coins we're sending with transactions
  • 01:07:23 right now we unfortunately have no way
  • 01:07:25 of storing the sender and receiver of
  • 01:07:27 the coins we'll do that later in the
  • 01:07:29 course of course once we had a look at
  • 01:07:31 more complex data structures but the
  • 01:07:33 amounts is something we can already
  • 01:07:35 store so let's say in the first
  • 01:07:37 transaction we send one coin in the
  • 01:07:40 second transaction it's 8.6 coins in the
  • 01:07:43 third transaction
  • 01:07:44 it was 5.1 coin of course no one's
  • 01:07:48 stopping us from also adding some text
  • 01:07:50 here so this is really something you as
  • 01:07:52 a developer set up so this is our
  • 01:07:55 blockchain obviously with hard-coded
  • 01:07:57 values no chance for the user to input
  • 01:08:00 his own value if I hit enter and I then
  • 01:08:03 output blockchain like this here is our
  • 01:08:06 list now let's say I want to have a look
  • 01:08:08 at that second transaction only then I
  • 01:08:12 can access it with blockchain and then
  • 01:08:16 here what one because remember the index
  • 01:08:21 starts with zero so the first element 1
  • 01:08:24 in this case has an index of 0 if we
  • 01:08:27 want to access the second element 8.6
  • 01:08:30 it's in X 1 and correctly we get 8.6 as
  • 01:08:34 an output here now we can also use these
  • 01:08:37 values in operations let's say we're
  • 01:08:40 some kind of hacker and we're changing
  • 01:08:44 the amount of coins sent in these second
  • 01:08:47 transaction so we could say blockchain
  • 01:08:50 plus 2 if we enter this we get 10.6 but
  • 01:08:55 interesting enough if I then output the
  • 01:08:57 blockchain list again you see here the
  • 01:09:00 value is unchanged
  • 01:09:01 now why is that the reason for this is
  • 01:09:05 that with blockchain 1
  • 01:09:08 we're basically copying the value out of
  • 01:09:11 the list we're not setting up some live
  • 01:09:14 connection to the element so if I add 2
  • 01:09:16 here I'm adding this to the copied
  • 01:09:19 element
  • 01:09:20 and hence if I do add this here we
  • 01:09:23 correct to get 10.6 because we copy 8.6
  • 01:09:27 out of the list and then we add too but
  • 01:09:29 the original list as you can see is
  • 01:09:32 unchanged
  • 01:09:33 now that copying part is actually
  • 01:09:36 something which will become important
  • 01:09:38 later again once we also work with more
  • 01:09:40 complex data structures because there I
  • 01:09:43 will show you how you actually have to
  • 01:09:46 differentiate between different data
  • 01:09:48 types whereas some are copied
  • 01:09:50 automatically whilst others are not but
  • 01:09:52 for now with strings numbers and boolean
  • 01:09:55 it works exactly as I explained it here
  • 01:10:04 typically you're not looking to
  • 01:10:07 hard-code your array and then leave it
  • 01:10:10 like this you got certain circumstances
  • 01:10:13 where you want to do something to your
  • 01:10:15 list at certain points of time in your
  • 01:10:18 code let's say we want to add an element
  • 01:10:20 now our list is set up up there and of
  • 01:10:25 course we could add an element by simply
  • 01:10:27 redefining the entire list like this and
  • 01:10:30 now we add ten as a new element and if I
  • 01:10:33 output blockchain we correctly see the
  • 01:10:34 new list this is very cumbersome though
  • 01:10:37 manually recreating lists all the time
  • 01:10:40 not really something we want to do
  • 01:10:42 thankfully there is a special function
  • 01:10:45 or method we can use now I'll dive
  • 01:10:48 deeper into what functions and methods
  • 01:10:49 are in just a second but for now let's
  • 01:10:53 simply call it by adding dot append
  • 01:10:56 after our list
  • 01:10:58 that's a built-in method shipping with
  • 01:11:00 Python and add parentheses here now if
  • 01:11:04 you add free here and you then output
  • 01:11:07 the blockchain again you see that free
  • 01:11:09 was ended at the end so whatever this
  • 01:11:12 method or function is again we'll have a
  • 01:11:14 look at this in just a second it seems
  • 01:11:17 to take that value we pass to it between
  • 01:11:20 the parentheses and in the case of the
  • 01:11:22 append method added to our list now we
  • 01:11:27 also have a different method which we
  • 01:11:30 can call and there are more methods than
  • 01:11:31 just these two by the way
  • 01:11:33 pop and this will actually return a
  • 01:11:35 value instantly free which was the last
  • 01:11:39 value on our list and if I now output
  • 01:11:41 the list you see the last value is
  • 01:11:43 missing so pop is actually removing the
  • 01:11:46 last element of the list outputs it or
  • 01:11:49 it gives us a chance to store it in a
  • 01:11:51 variable if we would use blockchain pop
  • 01:11:53 on the right side of an equal sign where
  • 01:11:55 we store it in a variable and then it
  • 01:11:58 manipulates the original list and
  • 01:12:00 removes the element from there so that's
  • 01:12:03 a first glimpse at functions something
  • 01:12:06 we'll dive on in in the next lectures
  • 01:12:08 and this is how we can in general work
  • 01:12:10 with the list and how we can start
  • 01:12:12 building our blockchain and actually
  • 01:12:14 whilst this blockchain is very simple
  • 01:12:17 is how we will work with it throughout
  • 01:12:19 the course we'll manage our blockchain
  • 01:12:22 in a list and later the things we'll
  • 01:12:24 change is of course how we work with it
  • 01:12:26 how we manipulate it how we verify it
  • 01:12:29 and check if it's valid the type of data
  • 01:12:32 we store it'll be more complex and of
  • 01:12:35 course we'll also learn how we store it
  • 01:12:37 in a file so that it's not lost if we
  • 01:12:39 restart program because right now if I
  • 01:12:42 quit here and I go back into my
  • 01:12:48 rappelled mode the blockchain is gone
  • 01:12:51 this variable does not exist anymore
  • 01:12:53 because it basically resets the old
  • 01:12:56 state whenever we restart this so that's
  • 01:12:59 all something we'll fix throughout the
  • 01:13:00 course once we can store that in files
  • 01:13:10 so we got the very basics about Python
  • 01:13:12 out of the way
  • 01:13:13 to learn about the core data types and
  • 01:13:15 how we store them in variables however
  • 01:13:18 thus far we did everything in the rattle
  • 01:13:20 now we'll switch to a real development
  • 01:13:23 environment and to a local IDE and we'll
  • 01:13:27 continue building our blockchain in the
  • 01:13:29 end we won't ever chain which will be a
  • 01:13:31 list which holds our blocks of data and
  • 01:13:34 here's something new I want each block
  • 01:13:36 to be a list on its own on itself and in
  • 01:13:40 that list of data basically so inside of
  • 01:13:43 the block will then store our values and
  • 01:13:46 for the first block it might be one
  • 01:13:48 value but the important part is for the
  • 01:13:51 other blocks in the blockchain each
  • 01:13:53 nested list will actually always contain
  • 01:13:57 the previous values to kind of simulate
  • 01:14:00 that part which I mentioned early and
  • 01:14:02 the course where each block knows about
  • 01:14:04 its previous block by its hash now we
  • 01:14:08 won't hatch it here because that
  • 01:14:09 requires some knowledge we don't have
  • 01:14:11 yet but we'll kind of fake it
  • 01:14:13 by ensuring that every block is just a
  • 01:14:16 nested list which contains the previous
  • 01:14:18 values as well as the new value so it's
  • 01:14:21 basically like that game where you're
  • 01:14:23 packing your bags for leaving and you
  • 01:14:26 name one word like toothbrush and your
  • 01:14:30 partner then has to name your word
  • 01:14:32 toothbrush as well as a new word towel
  • 01:14:34 and then you say a toothbrush towel and
  • 01:14:37 let's say a comp and that is basically
  • 01:14:40 what we'll do here each block contains
  • 01:14:42 the old data plus a new one and this is
  • 01:14:44 something we can totally build with the
  • 01:14:46 knowledge we already have and of course
  • 01:14:48 we don't want to use random data we're
  • 01:14:50 going to build our own coin so let's
  • 01:14:52 start by storing transaction values and
  • 01:14:55 obviously we'll refine the values
  • 01:14:57 throughout the course now as I said we
  • 01:15:00 won't do this in the rappelled l will
  • 01:15:02 use a local IDE and a more elaborate
  • 01:15:05 setup which you typically use for any
  • 01:15:07 bigger project you build
  • 01:15:15 to work locally we'll work with text
  • 01:15:19 false and we want to write code in these
  • 01:15:21 text files which is easy for us to write
  • 01:15:23 and to understand and for this we'll use
  • 01:15:26 an IDE an integrated development
  • 01:15:28 environment which is a tool basically a
  • 01:15:31 text editor with a lot of additional
  • 01:15:34 features which make writing code more
  • 01:15:36 fun and easier because we'll get
  • 01:15:38 suggestions errors will be shown to us
  • 01:15:41 automatically we get different colors
  • 01:15:44 for keywords and so on it's more fun to
  • 01:15:47 write code with an IDE
  • 01:15:48 theoretically you can't use a normal
  • 01:15:50 text editor like the one shipping with
  • 01:15:52 your operating system though for this
  • 01:15:54 course I'll use Visual Studio code it's
  • 01:15:58 a free IDE and you can download it from
  • 01:16:00 code dot visual studio comm make sure to
  • 01:16:04 choose your version for your operating
  • 01:16:05 system and simply download and install
  • 01:16:08 it there should be nothing complicated
  • 01:16:09 about that now they're all just an
  • 01:16:12 alternative which I want to bring to
  • 01:16:13 your attention
  • 01:16:14 it's called pycharm and you can simply
  • 01:16:17 google for it and click on that link you
  • 01:16:20 should find there and whilst there is a
  • 01:16:22 paid version of pycharm there also is a
  • 01:16:25 free version of it you simply have to
  • 01:16:27 scroll down a bit and then pick that
  • 01:16:29 pycharm Community Edition can download
  • 01:16:32 that for free and use it for free and
  • 01:16:34 this is a different idea
  • 01:16:35 all the bringing the advantages like
  • 01:16:37 color coding and so on which you can
  • 01:16:39 also try out however I'll go with Visual
  • 01:16:41 Studio code once you installed Visual
  • 01:16:44 Studio code you can open a folder which
  • 01:16:47 you should simply create in your Mac
  • 01:16:49 finder or Windows Explorer where he will
  • 01:16:52 store your project so that's blockchain
  • 01:16:54 project simply create a new folder
  • 01:16:56 somewhere on your machine for that an
  • 01:16:58 empty folder then you pick open folder
  • 01:17:01 and simply navigate to that folder here
  • 01:17:04 I selected my Python blockchain folder
  • 01:17:06 and I'll click open and now it is opens
  • 01:17:08 the folder in the IDE I'll let me hide
  • 01:17:13 this here and now if your installation
  • 01:17:15 looks a bit differently I simply entered
  • 01:17:18 presentation mode here and remove the
  • 01:17:20 toolbar at the bottom for example and
  • 01:17:22 what I also did is I switch the theme to
  • 01:17:25 dark you can do that from the menu
  • 01:17:29 by clicking on preferences color theme
  • 01:17:31 there you have two choice of different
  • 01:17:34 built-in themes and I simply choose dark
  • 01:17:36 plus this default dark theme this is my
  • 01:17:41 setup here now to work with Python we
  • 01:17:44 also want to install some extensions
  • 01:17:47 that make that a bit easier for this you
  • 01:17:49 can simply go to view and then
  • 01:17:51 extensions or if you didn't remove that
  • 01:17:55 sidebar you'll also have an extension
  • 01:17:57 buttons here on the left
  • 01:17:58 but view extensions will always work and
  • 01:18:00 in there you can search for Python now I
  • 01:18:05 already installed it but this first pipe
  • 01:18:08 package here is the one I recommend
  • 01:18:11 installing as I said I have it installed
  • 01:18:13 but you'll have an install button here
  • 01:18:15 simply click it and restart the IDE when
  • 01:18:18 prompted to do so now additionally in
  • 01:18:21 this course and this is totally optional
  • 01:18:23 all you see material I can theme here
  • 01:18:27 which simply changes the file icons
  • 01:18:29 which will be displayed in this project
  • 01:18:31 you can all install that but again this
  • 01:18:33 is purely optional the PI flex tension
  • 01:18:35 is one you should install though
  • 01:18:37 thereafter you can go back to view
  • 01:18:39 Explorer now in here you can create a
  • 01:18:43 new file by clicking this new file
  • 01:18:44 button or a new folder with that button
  • 01:18:46 or with command N and here I'll add my
  • 01:18:50 blockchain file the important part is
  • 01:18:52 since we're working with Python it has
  • 01:18:54 to end with dot P why this turns it into
  • 01:18:58 a Python file now let me quickly switch
  • 01:19:00 to my uber icon theme yeah I prefer that
  • 01:19:04 so this is my blockchain Python file and
  • 01:19:08 this is the file with which I'll work
  • 01:19:10 for the next lectures now obviously you
  • 01:19:13 might be wondering how do we execute
  • 01:19:15 that file now and the answer is from
  • 01:19:18 within your terminal in Visual Studio
  • 01:19:21 code you can also go to view integrate a
  • 01:19:25 terminal or use the shortcut which is
  • 01:19:27 displayed there and this will open your
  • 01:19:29 normal operating system command prompt
  • 01:19:32 or terminal automatically navigate it in
  • 01:19:34 to your project folder and there you can
  • 01:19:37 run Python and then a blank and then the
  • 01:19:39 name of the file like blockchain dot PI
  • 01:19:42 people
  • 01:19:43 in this case and it'll execute that file
  • 01:19:45 and in this case it does nothing because
  • 01:19:47 there's nothing in the file but if I
  • 01:19:49 were to for example use print function
  • 01:19:54 which output something to the console if
  • 01:19:56 you're not using the repple you'll have
  • 01:19:58 to explicitly tell python to output
  • 01:20:00 something so if you use print and you
  • 01:20:03 output 2+2 for example and you save the
  • 01:20:06 file don't forget that if you run the
  • 01:20:08 command again you see 4 down here so
  • 01:20:11 this is how we work with files and how
  • 01:20:13 we work with visual studio code as our
  • 01:20:15 IDE and with that let's continue and I
  • 01:20:17 mentioned print is a function right and
  • 01:20:20 we saw that append and pop function Q so
  • 01:20:24 let's dive into functions next because
  • 01:20:26 this will allow us to create a better
  • 01:20:28 block chain
  • 01:20:36 what are functions if we use built-in
  • 01:20:41 functions we get some utility out of the
  • 01:20:43 box some magic happens behind the scenes
  • 01:20:45 which we don't know but which does good
  • 01:20:48 things for us typically but of course we
  • 01:20:50 can also define functions for our
  • 01:20:52 ourselves and function simply allow us
  • 01:20:55 to define code which is executed later
  • 01:20:58 and possibly multiple times and we of
  • 01:21:00 course define when it's executed a
  • 01:21:03 function in Python is defined like this
  • 01:21:07 we use the DEF keyword then the name of
  • 01:21:10 the function then parentheses and then a
  • 01:21:13 colon if you're coming from a different
  • 01:21:16 programming language like JavaScript you
  • 01:21:18 would have the function keyword and
  • 01:21:20 curly braces now Pi uses Def instead of
  • 01:21:23 function and colon instead of curly
  • 01:21:26 braces and now that's very important to
  • 01:21:29 understand it then uses indentation so
  • 01:21:32 some white space to the left you should
  • 01:21:34 use four spaces here too from white or
  • 01:21:39 write the code that will be executed
  • 01:21:42 whenever you call the function now
  • 01:21:45 calling a function of something which is
  • 01:21:47 done like that or like that this is the
  • 01:21:51 multiple times execution I'm referring
  • 01:21:52 to when you define a function so the
  • 01:21:55 first part nests box nothing will happen
  • 01:21:58 Python will recognize that but not run
  • 01:22:00 the code in there as soon as you call it
  • 01:22:02 though by using that function name with
  • 01:22:05 parentheses it will execute the function
  • 01:22:08 and run the code that's in there now
  • 01:22:10 regarding the syntax also note that in
  • 01:22:12 Python not only in functions but
  • 01:22:15 actually entirely in Python you don't
  • 01:22:17 use semicolons at the end of the line
  • 01:22:19 and again you don't use curly braces you
  • 01:22:23 use indentation and you should use for
  • 01:22:26 spaces instead of tabs here however the
  • 01:22:29 good thing is in ideas like visuals to
  • 01:22:31 you keep code you can't actually hit the
  • 01:22:33 tab button and it will enter four spaces
  • 01:22:36 for you so you get that convenience back
  • 01:22:39 to the functions though this is a simple
  • 01:22:41 function here it simply outputs
  • 01:22:44 something now let's actually use that in
  • 01:22:46 conjunction with our block chain let's
  • 01:22:49 start building our
  • 01:22:50 blockchain with the help of functions in
  • 01:22:52 our blockchain dot py code here I will
  • 01:22:56 first of all recreate my blockchain
  • 01:22:57 variable and also as a side note unlike
  • 01:23:00 in other languages you don't have a var
  • 01:23:03 or something like that in front of that
  • 01:23:05 you just type the name of the variable
  • 01:23:06 and this should be a list and actually
  • 01:23:10 it's an empty list at the beginning now
  • 01:23:12 we obviously wanna add values to that
  • 01:23:15 list and for that we can use a function
  • 01:23:18 so we define a function here and I'll
  • 01:23:22 name it ad underscore value this again
  • 01:23:25 is that naming convention which also
  • 01:23:27 applies to functions where you write an
  • 01:23:29 entirely lowercase and you separate
  • 01:23:31 words with an underscore now important
  • 01:23:34 when the finding a function you have to
  • 01:23:36 add a pair of parenthesis and then a
  • 01:23:39 colon and the nice thing is you can
  • 01:23:41 already see the color coding here in
  • 01:23:43 Visual Studio code so Dada support for
  • 01:23:46 the keyword which is marked blue and
  • 01:23:48 your own name which is yellow
  • 01:23:49 if you hit enter now you see it
  • 01:23:52 automatically indents this line so it
  • 01:23:54 helps you with that that's part of the
  • 01:23:56 reason why we use an IDE now here we
  • 01:23:59 write the logic we want to execute when
  • 01:24:01 we call this function and this can be
  • 01:24:03 more than one line by the way now here I
  • 01:24:05 want to add a value to the blockchain
  • 01:24:08 here we see an a variety II feature we
  • 01:24:11 get suggestions on which methods we can
  • 01:24:13 call on a list so it detects that
  • 01:24:16 blockchain holds a list and helps us
  • 01:24:18 with working with it now what to call a
  • 01:24:20 pen here now we also get some
  • 01:24:23 information about what we can do with a
  • 01:24:25 pen and I want to append a value now
  • 01:24:29 let's say we append 5.3 I will now also
  • 01:24:33 add a second line still in that function
  • 01:24:36 I would be outside of the function if I
  • 01:24:38 remove the indentation but if I keep it
  • 01:24:40 I'm inside of the function body and here
  • 01:24:43 I will use a built in function shipping
  • 01:24:45 with Python which is print print is a
  • 01:24:49 function which allows us to output
  • 01:24:51 something to the console and here I
  • 01:24:53 simply want to output the latest version
  • 01:24:55 of my blockchain now we can go down and
  • 01:24:59 I simply enter an empty line to make the
  • 01:25:02 code more readable
  • 01:25:03 and I removed the indentation by using
  • 01:25:06 shift and tap and I can now simply call
  • 01:25:09 add value by using the function name as
  • 01:25:13 well as the parentheses like this if I
  • 01:25:16 save that file and I go into my terminal
  • 01:25:18 I can now execute my blockchain dot py
  • 01:25:21 file with the Python command and as you
  • 01:25:24 see it outputs my list here now if I
  • 01:25:27 execute it again I still only got dead
  • 01:25:29 list so it's not storing that list
  • 01:25:31 between executions because basically our
  • 01:25:34 program runs and then it's done and it
  • 01:25:36 loses its state thereafter if you want
  • 01:25:38 it to persist it you'd have to store it
  • 01:25:40 in a file something we'll also do later
  • 01:25:42 but of course we can go back and we can
  • 01:25:45 replicate or call add value multiple
  • 01:25:47 times and if we do so and we execute
  • 01:25:49 this again
  • 01:25:50 you see our chain grows now right now
  • 01:25:54 we're just storing float values there
  • 01:25:56 though if you remember that slide from
  • 01:25:59 earlier I said that I want to store
  • 01:26:02 nested lists in there which have the old
  • 01:26:05 transactions as well as the new one for
  • 01:26:09 this back in our function I'll actually
  • 01:26:11 append a new list here and that list
  • 01:26:14 holds the new value but I also want to
  • 01:26:17 hold the last value so the last value of
  • 01:26:22 my blockchain here to do this all
  • 01:26:25 reference blockchain as a first value
  • 01:26:28 however blockchain should not be entered
  • 01:26:32 entirely just the last value we have in
  • 01:26:34 there so we need to access a certain
  • 01:26:37 index now index zero would always be the
  • 01:26:40 first value if we do that and we save
  • 01:26:44 that file and I execute Python
  • 01:26:45 blockchain we actually get an error
  • 01:26:48 because we try to access a value which
  • 01:26:50 doesn't exist at the beginning right we
  • 01:26:53 try to enter blockchain 0 when it is
  • 01:26:55 empty at the beginning if we start with
  • 01:26:57 a value of 1 let's say and I reacts
  • 01:27:00 acute this now it works and you see
  • 01:27:03 we're growing and we always have that
  • 01:27:05 value in there but of course we always
  • 01:27:09 refer to the first value now there is a
  • 01:27:12 convenient trick about indexing which
  • 01:27:14 helps us get that last value
  • 01:27:24 so we start working with functions and
  • 01:27:27 we start storing data in our blockchain
  • 01:27:29 by appending a new value and also
  • 01:27:32 referring to the old value now right now
  • 01:27:36 we're always referring to the first
  • 01:27:37 value though which always is a 1 because
  • 01:27:39 that is what we start with if we want to
  • 01:27:42 get the last value of the blockchain
  • 01:27:44 list we actually can use minus window
  • 01:27:48 that's a convenient trick they'll trick
  • 01:27:52 sounds like it's some workaround it's
  • 01:27:54 actually a built-in functionality which
  • 01:27:56 simply X's us to blockchain from the
  • 01:27:59 right so 0 is the first element from the
  • 01:28:02 left but minus 1 is the last element
  • 01:28:06 because we start accessing it from the
  • 01:28:07 right and why not – 0 because there
  • 01:28:11 exists no such thing as a negative 0 so
  • 01:28:14 minus 1 we could also use minus 2 to use
  • 01:28:17 the second last element but minus 1 will
  • 01:28:20 actually now give us the last one if we
  • 01:28:23 save that and I run that code again you
  • 01:28:26 see now what we have is we got our
  • 01:28:29 blockchain here in the last line where
  • 01:28:33 we have 1 and the new array with 1 & 5
  • 01:28:38 free and then we're nesting this fervour
  • 01:28:41 and fervor as you can see because we're
  • 01:28:43 always referring to the last element of
  • 01:28:46 our blockchain and the last element will
  • 01:28:48 always be a nested list now actually for
  • 01:28:51 that reason we should also wrap that
  • 01:28:53 starting one with another pair of square
  • 01:28:56 brackets so that this is a nested list
  • 01:28:58 too now with that if I clear this to
  • 01:29:00 make it easier to read here we see how
  • 01:29:03 we now execute our code we always add an
  • 01:29:06 additional level of nesting because if
  • 01:29:09 we access the last element in the second
  • 01:29:12 add value call we get this result
  • 01:29:15 because here the last element actually
  • 01:29:17 is this list so when we append a new
  • 01:29:20 value to our blockchain we take the old
  • 01:29:23 blockchain which is this part and then
  • 01:29:26 we append the old last value so this one
  • 01:29:30 we appended as the first element in that
  • 01:29:33 new array we're adding remember we're
  • 01:29:36 adding an array here
  • 01:29:37 square brackets this is what we append
  • 01:29:39 and then this new array where this new
  • 01:29:41 list actually also has this value here
  • 01:29:44 at the end and for the last add value
  • 01:29:47 call this is the part we're starting
  • 01:29:49 with and then we're appending a new list
  • 01:29:53 hence the outer squared brackets here
  • 01:29:56 and in that new list we have 5.3 as a
  • 01:29:59 new value this.value but then we access
  • 01:30:02 the last value of the old blockchain
  • 01:30:05 which is this value here and this is why
  • 01:30:08 you now see this value with free opening
  • 01:30:11 and one closing square bracket in here
  • 01:30:15 so this is the same value as this up
  • 01:30:17 here and that simply is added because
  • 01:30:19 it's last value of the old blockchain so
  • 01:30:21 this is the logic we have right now each
  • 01:30:23 new block we add contains the old data
  • 01:30:27 as well as the new data the problem we
  • 01:30:30 of course is the new data always is the
  • 01:30:33 same
  • 01:30:33 it never differs it's always 5.3 so it
  • 01:30:37 would be nice if we could actually
  • 01:30:39 manipulate that and tell the function
  • 01:30:42 here to use different values
  • 01:30:52 we learned how functions can work in
  • 01:30:54 general one nice feature of functions is
  • 01:30:57 they can do much more than what we
  • 01:30:59 previously did they can receive
  • 01:31:01 arguments this means that in our
  • 01:31:03 function we can expect arguments
  • 01:31:06 parameters between the opening and
  • 01:31:09 closing parentheses next to the function
  • 01:31:12 name and we can use these arguments
  • 01:31:14 inside of the function like that if we
  • 01:31:18 then call the function we have to pass a
  • 01:31:20 value for that parameter and this is
  • 01:31:23 then dynamically injected into the
  • 01:31:25 function so this would actually print
  • 01:31:27 hello max here now let's apply this to
  • 01:31:29 our block chain in add value we can add
  • 01:31:33 an argument and you can name that
  • 01:31:35 argument whatever you want like Val or
  • 01:31:37 value or transaction amount something
  • 01:31:41 like this and you can use that value
  • 01:31:44 inside of your function body not outside
  • 01:31:47 of it it's only known inside of it so we
  • 01:31:50 can use that like a variable name here
  • 01:31:52 and add it here instead of 5.3 and we
  • 01:31:56 could have Corozal use that in
  • 01:31:58 calculations or in any operations we
  • 01:32:01 perform on it so this is my transaction
  • 01:32:04 amount here and now if I save that and I
  • 01:32:08 clear that just to make it easier to
  • 01:32:09 read if I execute the file we get an
  • 01:32:13 error we get an error that add value is
  • 01:32:16 missing one required positional argument
  • 01:32:19 transaction amount and we also see in
  • 01:32:22 the IDE we got these red squiggly lines
  • 01:32:25 the reason for this is obviously add
  • 01:32:27 value requires this data to work
  • 01:32:30 correctly so we have to pass it can pass
  • 01:32:34 to dot nine ten point eight nine for
  • 01:32:40 example and now if we save that file
  • 01:32:42 again and we execute it again now this
  • 01:32:47 works and now you see that our values
  • 01:32:50 are indeed inserted 20.9 10.8 nine this
  • 01:32:57 is now taking advantage of the fact that
  • 01:33:00 you can pass arguments to functions and
  • 01:33:02 if you have a look at this append
  • 01:33:04 function the strange thing
  • 01:33:05 stillest that we call it on a list well
  • 01:33:08 that is something to do with objects and
  • 01:33:11 methods we'll have a look at this later
  • 01:33:13 in the course but in that score is a
  • 01:33:15 function and there you can see we also
  • 01:33:17 pass an argument right we pass the
  • 01:33:19 argument we want to append so append
  • 01:33:21 actually alters a function that takes
  • 01:33:23 some input
  • 01:33:31 we already learned some nice things
  • 01:33:33 about functions but they can do even
  • 01:33:35 more than what we learned this far they
  • 01:33:37 can also return values and what does
  • 01:33:40 this mean here's an example we got our
  • 01:33:43 sum function here and as you can see
  • 01:33:44 this function takes two arguments but
  • 01:33:47 any function no matter if it takes any
  • 01:33:50 arguments or how many it takes can also
  • 01:33:52 return a value with the return keyword
  • 01:33:55 if you use that inside of a function
  • 01:33:57 whatever comes after it is returned
  • 01:34:00 which means that whoever called that
  • 01:34:03 function will get that return value as a
  • 01:34:06 result in this case here we call sum
  • 01:34:09 inside of the print function which will
  • 01:34:12 output the value returned by sum we
  • 01:34:15 could of course alternatively also
  • 01:34:17 simply store the result of some in a
  • 01:34:19 variable by using the variable name and
  • 01:34:21 then an equal sign and then the sum
  • 01:34:24 function call now let's also apply this
  • 01:34:26 to our block chain right now we get one
  • 01:34:28 function which does two things actually
  • 01:34:31 it gives us the last value in our block
  • 01:34:34 chain and then it appends this value as
  • 01:34:37 well as our new transaction amount Q the
  • 01:34:41 unique block chain let's actually split
  • 01:34:44 it into functions so all define a new
  • 01:34:46 function with the DEF keyword and lamé
  • 01:34:48 it get last blockchain value now that's
  • 01:34:53 obviously a pretty long name you can
  • 01:34:55 shorten it but one idea behind Python
  • 01:34:58 really is that you write code that is
  • 01:35:01 readable readable code is really a core
  • 01:35:04 value of the Python community so using
  • 01:35:07 function names like this is actually
  • 01:35:09 perfectly fine this function will not
  • 01:35:12 take any arguments still requires a
  • 01:35:15 colon of course and what we'll do in
  • 01:35:17 here is we'll return blockchain minus
  • 01:35:22 one so this will do the extraction for
  • 01:35:25 us what this simply means is that in
  • 01:35:28 this function here we can call get last
  • 01:35:31 block chain value now arguably that got
  • 01:35:35 longer but from a code reusability
  • 01:35:39 perspective here we got cleaner because
  • 01:35:42 now each function has only one chopped
  • 01:35:44 extract
  • 01:35:45 and a pent well actually this function
  • 01:35:48 still all the prints it but besides that
  • 01:35:51 it only has this one job and we can
  • 01:35:53 remove that print statement of course
  • 01:35:55 and simply print the blockchain at the
  • 01:35:58 end once we're done so with this we got
  • 01:36:02 two functions now and now it's all the
  • 01:36:04 convention that we should have q empty
  • 01:36:07 lines between our function definitions
  • 01:36:09 this is something from the official code
  • 01:36:12 style guide I will tell you more about
  • 01:36:15 that guide at the end of this module in
  • 01:36:17 visuals to your code you all's got a
  • 01:36:19 nice tool if you go to code preferences
  • 01:36:23 keyboard shortcuts and there you search
  • 01:36:26 for format you will find that format
  • 01:36:28 document shortcut find it to anything
  • 01:36:30 you want
  • 01:36:31 and with that shortcut you can
  • 01:36:34 automatically format your code in a way
  • 01:36:36 that it follows the official style
  • 01:36:38 guides so that's a very convenient so
  • 01:36:40 now we got two functions here if I now
  • 01:36:42 clear the console and execute my script
  • 01:36:45 again you see we get an error here but
  • 01:36:48 the reason for this simply is that I
  • 01:36:50 accidentally deleted the opening square
  • 01:36:52 bracket I need to keep this because that
  • 01:36:56 last value I'm extracting should still
  • 01:36:58 go into a new list so if I save this and
  • 01:37:01 we now execute this again now we only
  • 01:37:04 get one output because I only got one
  • 01:37:06 print statement now but here you see
  • 01:37:08 it's still extracting the old list and
  • 01:37:12 inserting this here and then also this
  • 01:37:16 one into here so this is still working
  • 01:37:19 but now split up over two functions with
  • 01:37:22 the help of the return keyword
  • 01:37:32 returning values is pretty neat now
  • 01:37:35 let's go back to arguments though you
  • 01:37:37 learned how you can use function
  • 01:37:39 arguments when additional nice feature
  • 01:37:41 is the fact that you can also use
  • 01:37:43 default arguments this means you can set
  • 01:37:47 up a default argument which will be used
  • 01:37:49 if you call that function without
  • 01:37:52 passing the data for that argument
  • 01:37:54 remember previously we got an error in
  • 01:37:56 this case with default arguments this is
  • 01:37:59 actually something we can do without
  • 01:38:00 errors here's how it works
  • 01:38:02 we get a function definition with the
  • 01:38:06 greet function this case again and now
  • 01:38:08 the H part here actually is a default
  • 01:38:12 argument we provide a default value for
  • 01:38:15 it 29 then we use it in our function and
  • 01:38:19 the interesting part of course is when
  • 01:38:21 we call the function you see I actually
  • 01:38:23 don't pass a second argument without a
  • 01:38:26 default argument we would have to do
  • 01:38:29 that otherwise we'll get an error but in
  • 01:38:31 this case it works fine without it
  • 01:38:33 let's also use that in our blockchain
  • 01:38:36 example we have a good use case for that
  • 01:38:39 with our starting value here let's go
  • 01:38:42 back to initializing the blockchain as
  • 01:38:44 an empty array now clearly if I now
  • 01:38:47 clear that and execute my code we get an
  • 01:38:50 error because with get last locked-in
  • 01:38:52 value we try to extract the value which
  • 01:38:55 doesn't exist now let's fix this by
  • 01:38:59 using a default argument here an add
  • 01:39:02 value I'll use my or my add my last
  • 01:39:06 transaction value and this is now
  • 01:39:09 actually what I will append as a first
  • 01:39:11 value in the new block last transaction
  • 01:39:16 now what I can do is I can of course
  • 01:39:19 pass that as an argument to my add value
  • 01:39:22 call now I do that by for example
  • 01:39:25 passing get last blockchain value get
  • 01:39:29 last locked-in value and don't forget
  • 01:39:31 the opening and closing parentheses now
  • 01:39:34 if I do that what we provide for the
  • 01:39:36 first add value well I can provide my
  • 01:39:41 starting array here so what we
  • 01:39:44 previously added
  • 01:39:46 directly to our blockchain definition up
  • 01:39:48 here with this code if I run it it'll
  • 01:39:51 work fine and it'll give me the exact
  • 01:39:53 same result as before but we're not
  • 01:39:56 using default arguments here and
  • 01:39:58 actually we can improve our code by
  • 01:40:00 doing so the default obviously is that
  • 01:40:04 we want to get the last blockchain value
  • 01:40:06 so it would be nice if we could still
  • 01:40:08 use add value like this but obviously if
  • 01:40:11 this gives us an error as my IDE shows
  • 01:40:14 so what we can do is we can go into our
  • 01:40:17 add value definition and add our default
  • 01:40:20 argument by adding an equal sign after
  • 01:40:23 the argument name and then the default
  • 01:40:26 value in this case this function call to
  • 01:40:29 get last blockchain value if when I
  • 01:40:32 always save this and I execute the file
  • 01:40:35 again this unfortunately doesn't work
  • 01:40:38 the reason for this is it still executes
  • 01:40:41 that even if we do provide a value and
  • 01:40:44 therefore it still executes that for the
  • 01:40:46 first add value call where we have no
  • 01:40:49 last value so we'll have to fall back to
  • 01:40:52 the second pass solution will provide
  • 01:40:54 our one here so this one array as a
  • 01:40:57 default value so that we can omit it
  • 01:41:00 here for the first add value call and
  • 01:41:03 unfortunately we'll have to use the
  • 01:41:04 longer wish for the subsequent add value
  • 01:41:07 calls but still we now got a different
  • 01:41:10 way of making sure that we don't run
  • 01:41:13 into this below error and we're taking
  • 01:41:16 advantage of default arguments something
  • 01:41:20 will of course also see a lot throughout
  • 01:41:21 the course so now with that if I clear
  • 01:41:25 this and now I execute that file it
  • 01:41:27 works again the second best solution as
  • 01:41:30 I mentioned but also just one of the
  • 01:41:32 many examples you'll see throughout the
  • 01:41:34 course
  • 01:41:41 now we're almost done with functions but
  • 01:41:44 here's one last nice feature you should
  • 01:41:46 be aware of and Dad our keyword
  • 01:41:48 arguments no what's that again it's
  • 01:41:51 actually not that difficult
  • 01:41:53 we got a function with two arguments
  • 01:41:54 name and age here's what happens in the
  • 01:41:57 function we just output it and now with
  • 01:42:00 keyword arguments we can actually alter
  • 01:42:02 call a function like this in our
  • 01:42:05 function call we use the name of the
  • 01:42:08 argument name and H and an equal sign
  • 01:42:11 and then the value we want to pass
  • 01:42:13 normally if you don't refer to the names
  • 01:42:16 in the function call the position
  • 01:42:19 defines which argument receives which
  • 01:42:21 value the first data you pass goes into
  • 01:42:24 the first argument the second day that
  • 01:42:26 goes into the second argument of course
  • 01:42:28 with keyword arguments you can switch to
  • 01:42:32 position or also omit some argument
  • 01:42:36 after which another argument comes which
  • 01:42:38 you don't want to omit for example now
  • 01:42:41 let's also add this to our course and
  • 01:42:43 let's take advantage of positional
  • 01:42:46 arguments by simply reversing the order
  • 01:42:49 for this second add value call so Onam
  • 01:42:53 use get last blockchain value as a first
  • 01:42:56 argument now of course I can switch it
  • 01:42:58 like this I'm not using my names here
  • 01:43:02 and if I execute this on the first sight
  • 01:43:06 it looks like it worked but of course it
  • 01:43:09 didn't work we're actually now adding
  • 01:43:12 our values incorrectly for that second
  • 01:43:15 add value call which is the reason why
  • 01:43:18 here we have 0.9 as a first value and
  • 01:43:22 the old blockchain as a second value
  • 01:43:24 instead of the other way around which we
  • 01:43:26 normally want if for some reason we
  • 01:43:29 wanted to call it like this however we
  • 01:43:31 can do this with the named arguments or
  • 01:43:35 with keyword arguments how it's
  • 01:43:37 officially called we can say or we can
  • 01:43:39 pass that information to the function
  • 01:43:41 which data should go into which argument
  • 01:43:44 so I could say hey this should actually
  • 01:43:46 be the last transaction by using last
  • 01:43:49 transaction equal and then the value
  • 01:43:51 that should be put into it and this on
  • 01:43:54 the other end here
  • 01:43:55 is the transaction amount although with
  • 01:43:58 a equal sign and now with this if we
  • 01:44:01 repeat that it still works but you can
  • 01:44:04 already see there's a difference this
  • 01:44:06 now is the correct blockchain again and
  • 01:44:08 not the wrong one so here we're now
  • 01:44:12 taking advantage of keyword arguments
  • 01:44:14 and this is everything you need to know
  • 01:44:16 about functions right now how you define
  • 01:44:19 them in general that you can pass
  • 01:44:21 arguments that you can return data that
  • 01:44:24 you can provide default arguments and
  • 01:44:26 that you can use keyword arguments which
  • 01:44:28 simply means you use the argument name
  • 01:44:31 when calling the function and you got
  • 01:44:33 greater control over which argument
  • 01:44:35 receives which data therefore
  • 01:44:44 so we're working on our blockchain it's
  • 01:44:47 slowly taking shape but wouldn't it be
  • 01:44:50 nice if the user could actually enter
  • 01:44:52 the values we want to append so let's
  • 01:44:55 add a functionality for that we can't
  • 01:44:58 get the user input with the help of a
  • 01:45:01 special built-in function the input
  • 01:45:04 function this now allows us to provide a
  • 01:45:07 string remember that's a different data
  • 01:45:10 type some text which is presented to the
  • 01:45:13 user and then the user enters something
  • 01:45:15 in hits enter and whatever the user
  • 01:45:17 entered is put back into our code so to
  • 01:45:20 say so we could say your transaction
  • 01:45:24 amount please like that and we could
  • 01:45:29 store that in T X amount DX for
  • 01:45:32 transaction now we could then use T X
  • 01:45:36 amount here as the value we store please
  • 01:45:40 note that I don't replace it for the
  • 01:45:42 outer function calls for now if I now
  • 01:45:44 save this and I execute this file you
  • 01:45:46 actually see down there the execution
  • 01:45:48 stops it's not printing the final
  • 01:45:52 blockchain instead it's executing this
  • 01:45:54 code where we're prompted for the
  • 01:45:57 transaction amount now I can enter 5.99
  • 01:46:00 if I hit enter the execution continues
  • 01:46:04 but you now see that this was entered as
  • 01:46:07 text though because and that's something
  • 01:46:10 you have to memorize whatever you pass
  • 01:46:12 into your code with input is treated as
  • 01:46:15 text and we learned how we can convert
  • 01:46:17 it to a number do you remember you can
  • 01:46:21 wrap it with int to convert it to an
  • 01:46:22 integer but obviously we want to support
  • 01:46:25 floating-point numbers so we can wrap it
  • 01:46:26 with float and now we're able to handle
  • 01:46:30 user input with just text as a float so
  • 01:46:33 if we repeat this and I now enter 5.99
  • 01:46:36 again now you see there are no quotation
  • 01:46:39 marks anymore now it's really stored as
  • 01:46:41 a number now obviously we don't just
  • 01:46:45 want to execute this once once you
  • 01:46:48 execute this before every transaction
  • 01:46:53 here so before every add value call
  • 01:46:57 for this we fetch a new input before
  • 01:47:00 every call with the input method and we
  • 01:47:03 store it in the same variable all the
  • 01:47:05 time and of course we should then use
  • 01:47:08 that variable in our add value function
  • 01:47:11 so now if we save that and clear and I
  • 01:47:15 execute that file again I am asked for a
  • 01:47:18 value like 100 then 99.9 and 4.8 and
  • 01:47:24 once I'm done you see the 100 also
  • 01:47:29 present in the old value of course the
  • 01:47:31 99.9 and for the date they're all stored
  • 01:47:34 in our blockchain now we're doing one
  • 01:47:38 thing here which we should avoid though
  • 01:47:48 the one thing we're doing which we
  • 01:47:50 should try to avoid is we are actually
  • 01:47:53 always repeating ourselves we're calling
  • 01:47:58 that same code we're executing the same
  • 01:48:00 code three times here now we should try
  • 01:48:04 to minimize the code where we repeat
  • 01:48:07 ourselves and to be honest this will
  • 01:48:10 become easier after the next module
  • 01:48:12 where we learn how to loop through code
  • 01:48:15 and therefore repeated automatically but
  • 01:48:18 one thing we can already do is we can
  • 01:48:21 define a new function with the DEF
  • 01:48:23 keyword which is get user input
  • 01:48:28 I don't expect arguments here and then
  • 01:48:31 the function body I'll simply execute my
  • 01:48:34 input statement and already converted to
  • 01:48:37 a float like this now obviously the
  • 01:48:40 value we get here is not useful inside
  • 01:48:43 of our function so what can we do we can
  • 01:48:48 return the value and by doing so we can
  • 01:48:52 simply store it in T X amount thereafter
  • 01:48:55 get user input like this we're still
  • 01:48:59 repeating ourselves to be honest but
  • 01:49:01 again this is something we can fix after
  • 01:49:03 the next module but at least it's
  • 01:49:05 shorter code now and we outsource the
  • 01:49:08 core functionality the input and the
  • 01:49:10 float method here into you a separate
  • 01:49:13 function which we now call and where we
  • 01:49:15 store the result which is returned with
  • 01:49:17 return in a new variable which we then
  • 01:49:20 pass to add value and with that we can
  • 01:49:24 execute that file and we're again
  • 01:49:26 prompted for some input here and as you
  • 01:49:31 can
  • 01:49:32 see this still works ass before but we
  • 01:49:35 get leaner code we're repeating
  • 01:49:37 ourselves less and that really is
  • 01:49:39 something you should take with you try
  • 01:49:41 to minimize or totally avoid repeating
  • 01:49:45 yourselves and we're again using a
  • 01:49:48 function this time again with the return
  • 01:49:50 statement
  • 01:49:58 we're almost done with the module we
  • 01:50:01 learned a lot about functions and now
  • 01:50:03 also how to handle user input there's
  • 01:50:05 one important concept which we already
  • 01:50:07 used but which I want you to understand
  • 01:50:09 though and that is the scope of
  • 01:50:11 variables and what do I mean with that
  • 01:50:14 we get global and local scope now what's
  • 01:50:18 global scope if we define a variable
  • 01:50:21 directly in our file so on the root
  • 01:50:23 level so to say not inside a function
  • 01:50:26 like here then we can use that variable
  • 01:50:29 anywhere in that fall we can use it an
  • 01:50:31 agreed function and we could also use it
  • 01:50:34 outside of there a local variable on the
  • 01:50:37 apprehend is a variable only available
  • 01:50:41 inside of a function this would include
  • 01:50:43 function arguments or variables which
  • 01:50:46 are defined inside a function like the H
  • 01:50:48 variable here using this variable
  • 01:50:51 outside of that function would actually
  • 01:50:53 fail let me show this to you back in our
  • 01:50:56 code we get that block chain variable
  • 01:50:59 here we define it at the very beginning
  • 01:51:01 of our code and most importantly we
  • 01:51:03 define it outside of a function doesn't
  • 01:51:06 matter that we define it here at the top
  • 01:51:08 of the file well our our code wouldn't
  • 01:51:11 work but theoretically it would also be
  • 01:51:13 a global function if we define it down
  • 01:51:16 there however the important part is as
  • 01:51:19 you can see we can absolutely use that
  • 01:51:22 variable inside a function because its
  • 01:51:25 global we can use it outside of it as we
  • 01:51:27 do here when we are printing it and we
  • 01:51:30 can use it inside of a function like
  • 01:51:32 we're doing here
  • 01:51:32 now on the other hand local variables
  • 01:51:35 are seen here for example transaction
  • 01:51:38 amount and last transaction now these
  • 01:51:40 are obviously arguments of our function
  • 01:51:43 but in the function we use them just as
  • 01:51:45 variables if we try to output our
  • 01:51:49 variable outside of a function though
  • 01:51:51 that won't work so if we try to output
  • 01:51:57 transaction amount here after our first
  • 01:52:01 add value call so it certainly is
  • 01:52:03 populated with a value if we do that if
  • 01:52:06 I execute the file and I enter 10 you
  • 01:52:10 see thereafter I get an error that
  • 01:52:12 the name transaction amount is not
  • 01:52:15 defined the reason for this is that is
  • 01:52:17 indeed is not defined because it's not a
  • 01:52:19 global variable it's not defined in the
  • 01:52:22 file itself it's only available in that
  • 01:52:24 function the same would be true for the
  • 01:52:27 user input by the way if we create a
  • 01:52:29 user input variable in which we store
  • 01:52:31 the user input and then we simply return
  • 01:52:34 that here this will continue to work
  • 01:52:36 we're just using an in-between step
  • 01:52:38 maybe to make it easier to read but if
  • 01:52:41 we now output user input here well
  • 01:52:44 you're probably already guessing it but
  • 01:52:46 if I repeat that execution and I enter
  • 01:52:48 some value I also get an error on that
  • 01:52:51 name user input is not defined because
  • 01:52:53 it's a local variable only available
  • 01:52:56 inside of a function this is a key
  • 01:52:59 concept you also have to be aware of
  • 01:53:01 variables defined in a function be that
  • 01:53:04 by doing it like this or as the argument
  • 01:53:08 are only available in that function or
  • 01:53:11 the other hand global variables are
  • 01:53:14 available everywhere
  • 01:53:16 in your entire file here now there's one
  • 01:53:19 important thing you have to know about
  • 01:53:20 global and local scope and I'll show
  • 01:53:23 this in the repple first of all we can
  • 01:53:25 define a function there too with the DEF
  • 01:53:27 keyword like print name colon and if you
  • 01:53:30 had enter you see three dots at the
  • 01:53:32 beginning you can now hit tab to indent
  • 01:53:34 and write your function body here and
  • 01:53:36 here we can of course use a local
  • 01:53:39 variable like name Max and thereafter
  • 01:53:43 indent again print name and we can
  • 01:53:46 obviously as you know you get out of the
  • 01:53:48 body here by hitting Enter twice you can
  • 01:53:51 now call print name like that and you
  • 01:53:53 see max
  • 01:53:54 now that's not that new now let's say we
  • 01:53:57 have a name here again max and this now
  • 01:54:01 is a global variable because it's not
  • 01:54:03 defined inside of a function then we
  • 01:54:06 define a function maybe get name and
  • 01:54:08 here we're prompting the user for input
  • 01:54:11 and we want to store that input in the
  • 01:54:14 name variable so name input your name
  • 01:54:19 like that so this will store whatever we
  • 01:54:22 enter in the name variable and
  • 01:54:23 thereafter
  • 01:54:25 outside of the function I first of all
  • 01:54:27 execute getname to get that input and
  • 01:54:31 after executing it I'll print name so
  • 01:54:34 first of all let's hit that and let's
  • 01:54:37 enter Michael so a different name then
  • 01:54:39 is stored up here so Michael my case and
  • 01:54:43 then I'll call print name if I hit enter
  • 01:54:46 we see max the reason for that is that
  • 01:54:50 if you use a variable in a function and
  • 01:54:53 you assign a value to it and that always
  • 01:54:56 happens if you use a single equal sign
  • 01:54:58 like this so variable on the left and
  • 01:55:00 then equal sign and then whatever you
  • 01:55:02 want to store on the right if you do
  • 01:55:03 that inside of a function it will always
  • 01:55:06 by default create a new local variable
  • 01:55:08 it will not store it in the global
  • 01:55:11 variable even if such a variable with
  • 01:55:14 the same name exists if you want to
  • 01:55:16 force python to do that so if you want
  • 01:55:19 to force it to actually restore this in
  • 01:55:22 the global variable instead of creating
  • 01:55:23 a local one there is a special keyword
  • 01:55:26 you can use and you have to use so if we
  • 01:55:30 quit this clear on the windows this
  • 01:55:33 would be CLS by the way and then we run
  • 01:55:35 Python again to go into the rabble let's
  • 01:55:38 again try this named Max global variable
  • 01:55:41 and then let's self define our function
  • 01:55:44 get name like this indent and now the
  • 01:55:47 special keyword is global and then the
  • 01:55:50 name of the variable name and just like
  • 01:55:54 that you don't immediately assign a
  • 01:55:55 value but you just tell – hey there is
  • 01:55:58 this global variable please if I use
  • 01:56:01 name in this function use the global
  • 01:56:04 version of that of course if no such
  • 01:56:07 global variable exists you'll get a
  • 01:56:08 problem so let's now enter and indent
  • 01:56:12 again and now we can again use name
  • 01:56:14 input your name like that and now this
  • 01:56:18 will actually use the global name
  • 01:56:19 variable because we're telling it to do
  • 01:56:21 so here so now if I hit enter again and
  • 01:56:25 I quit this function if I now call get
  • 01:56:28 name here and I enter Michael now and I
  • 01:56:31 now print name you see now it's Michael
  • 01:56:35 and not max anymore because thanks to
  • 01:56:37 global name
  • 01:56:38 this actually targets the global name
  • 01:56:41 variable instead of a new local one
  • 01:56:51 who we covered a lot of ground in this
  • 01:56:54 module you learned a lot about the core
  • 01:56:56 basics now we'll continue diving into
  • 01:56:59 the basics a great place for you to all
  • 01:57:01 to dive deeper are the official docks
  • 01:57:03 which you can find on python.org make
  • 01:57:06 sure to pick the dot free at the free
  • 01:57:08 dot x docks and there the tutorial is a
  • 01:57:11 great place to learn all these basics
  • 01:57:14 again numbers strings lists these are
  • 01:57:16 things we learn about now if and so on
  • 01:57:19 these are things we'll learn about in
  • 01:57:20 the next module so you may jump ahead if
  • 01:57:23 you want but there actually is no reason
  • 01:57:24 to do so something important or
  • 01:57:27 interesting I also want to show you from
  • 01:57:28 the official docks are deep apps though
  • 01:57:31 but what's that if you go back to
  • 01:57:34 python.org and hover back over
  • 01:57:37 documentation and you go to Pep index
  • 01:57:40 you'll find a list of all the Python
  • 01:57:43 enhancement proposals these are
  • 01:57:45 basically documents articles giving you
  • 01:57:48 hints about how you can write your
  • 01:57:50 Python code how you should write it new
  • 01:57:53 developments in future pile versions
  • 01:57:55 things like that now if you scroll down
  • 01:57:58 you'll find the index of all the peps
  • 01:58:00 and you'll find a title about well what
  • 01:58:03 is covered in that pet now very
  • 01:58:05 important tab is this one here pep
  • 01:58:07 number 8 contains the style guide for
  • 01:58:10 Python code click on it you find useful
  • 01:58:13 information on how you should style your
  • 01:58:16 Python code now you don't need to learn
  • 01:58:18 that by heart but scanning through it is
  • 01:58:20 definitely recommended you'll learn
  • 01:58:22 things like that there should be two
  • 01:58:23 empty lines between function definitions
  • 01:58:26 that you should use spaces instead of
  • 01:58:28 tabs which visual studio does by default
  • 01:58:31 and how you should name your functions
  • 01:58:33 and variables that lowercase thing with
  • 01:58:36 the underscores between the names
  • 01:58:38 I'm already teaching you a good style in
  • 01:58:41 this course but there also might be some
  • 01:58:43 features we just don't use in that
  • 01:58:45 course or which you're not sure if you
  • 01:58:47 remember them correctly so therefore
  • 01:58:50 pepp 8 is a great place to dive in and
  • 01:58:53 learn more about these styling d our
  • 01:58:56 paths can be interesting but I recommend
  • 01:58:59 not going too deep into them they can be
  • 01:59:01 really confusing and they're not
  • 01:59:02 official documentation they are req
  • 01:59:04 nations they are proposals if you want
  • 01:59:07 to learn the chloral C language watch
  • 01:59:08 the scores of course and fall back to
  • 01:59:11 the official Docs for deep dives
  • 01:59:20 now one thing you'll also find mentioned
  • 01:59:22 in the paps is how you comment your code
  • 01:59:25 and that is something we didn't have a
  • 01:59:26 look at yet you can create comments in
  • 01:59:29 Python by using the hash symbol like
  • 01:59:32 here we could say initializing our
  • 01:59:37 blockchain list comments should be used
  • 01:59:41 to improve the readability of your code
  • 01:59:42 but one core principle of Pi in all's is
  • 01:59:45 that your code should be readable on its
  • 01:59:48 own so you should have clear function
  • 01:59:50 and variable names and your code
  • 01:59:53 actually shouldn't need too many
  • 01:59:55 comments I'll still add quite a lot of
  • 01:59:58 comments also outside of these videos
  • 02:00:00 for the code which you can download so
  • 02:00:03 that it's easier to understand for you
  • 02:00:05 but in general not many comments should
  • 02:00:07 be required this however is one way of
  • 02:00:09 commenting a common technique also is to
  • 02:00:12 sometimes comment out a certain value
  • 02:00:14 like this if you just want to test
  • 02:00:16 something without that being defined now
  • 02:00:19 here it just doesn't make sense but
  • 02:00:20 you'll see this occasionally throughout
  • 02:00:22 the course there also is a other way of
  • 02:00:25 commenting it and that are so-called
  • 02:00:27 dark strings these are essentially the
  • 02:00:30 multi-line strings you learn about with
  • 02:00:32 the free quotation marks you use them
  • 02:00:36 for example at the beginning of own
  • 02:00:38 functions like this free opening and
  • 02:00:40 free closing quotation marks and
  • 02:00:43 in-between you comment your function
  • 02:00:45 like returns the last value of the
  • 02:00:50 current blockchain the cool thing here
  • 02:00:55 is most IDs actually picked this up now
  • 02:00:58 by default this would be a normal Python
  • 02:01:00 string it's not an official comment but
  • 02:01:02 it also doesn't do any damage to your
  • 02:01:04 code it kind of emerged or it simply is
  • 02:01:08 the case now that we use this syntax to
  • 02:01:10 comment our own files or our own
  • 02:01:13 functions so we can reuse this and add
  • 02:01:17 this to a function and now when we call
  • 02:01:19 get last blockchain value or if we use
  • 02:01:21 this in the IDE we actually get support
  • 02:01:24 as you can see if I hover over this here
  • 02:01:27 you see the text which I entered above
  • 02:01:29 so that's very handy especially if
  • 02:01:32 you're writing code
  • 02:01:33 shared with other developers and this is
  • 02:01:36 a good practice to comment your own
  • 02:01:38 functions like here append a new value
  • 02:01:42 as well as the last blockchain value to
  • 02:01:49 the blockchain and docstrings are
  • 02:01:53 typically all written in a way that you
  • 02:01:55 have a first summary sentence and if
  • 02:01:58 it's on a function like here you then
  • 02:02:00 all doer should add something like
  • 02:02:02 arguments and the exact style is up to
  • 02:02:05 you I'll use digital arguments then I'll
  • 02:02:08 use a colon but this again is really up
  • 02:02:11 to you and the name of an argument like
  • 02:02:13 trends action amount and then a short
  • 02:02:18 information about that argument the
  • 02:02:20 amount that should be added and we get a
  • 02:02:24 second argument last transaction the
  • 02:02:27 last blockchain transaction we can also
  • 02:02:30 add default 1 now the advantage of this
  • 02:02:33 clearly is that whenever we use add
  • 02:02:36 value we get that information and if
  • 02:02:39 we're not the creator of that function
  • 02:02:40 this of course is very helpful when it
  • 02:02:43 comes to us understanding that syntax
  • 02:02:45 and this is also why we get highlighting
  • 02:02:48 for example for input here we also get
  • 02:02:50 some help in the IDE well because the
  • 02:02:52 input is implemented like this and
  • 02:02:54 because it also has a dark string
  • 02:02:57 now let's therefore complete our
  • 02:03:00 commenting exercise here but also adding
  • 02:03:02 something to get user input returns the
  • 02:03:05 input of the user new transaction amount
  • 02:03:10 as a float try to be as precise as
  • 02:03:15 possible so that if our developers were
  • 02:03:17 to use your code they can understand
  • 02:03:20 what's happening so these are comments
  • 02:03:23 added for general readability and to
  • 02:03:26 help our developers as well as
  • 02:03:28 docstrings also added to help other
  • 02:03:30 developers but also as a general good
  • 02:03:32 practice to make your code easier to
  • 02:03:35 understand and read
  • 02:03:43 now when working with Python you
  • 02:03:46 sometimes end up with code that's
  • 02:03:49 relatively long now for example this add
  • 02:03:51 value line here is a bit longer or this
  • 02:03:54 talk string here now for the dark string
  • 02:03:57 you can easily add a new line by simply
  • 02:04:00 hitting Enter because you can write
  • 02:04:02 multi-line text with triple quotation
  • 02:04:03 marks so that's no problem with our code
  • 02:04:07 so with non common code you also can
  • 02:04:10 break lines if you're in a list like
  • 02:04:13 here the arguments you're passing you
  • 02:04:16 can add a new line by hitting enter
  • 02:04:18 after the comma and this will still be
  • 02:04:21 valid code now I press the autoformat'
  • 02:04:24 button in my IDE you can find it in the
  • 02:04:27 key bindings under preferences keyboard
  • 02:04:31 shortcuts and there if you search for
  • 02:04:33 format it's this format document
  • 02:04:35 shortcut and then you see it ordered
  • 02:04:37 this a bit differently it made sure that
  • 02:04:39 the arguments are beneath each other it
  • 02:04:41 would work otherwise too so if you have
  • 02:04:43 it indented like this it would still
  • 02:04:45 work but that's easier to read hence
  • 02:04:47 that's the formatting it applies but
  • 02:04:49 this is something you can do if you want
  • 02:04:51 to structure your code in such a way if
  • 02:04:54 you want to avoid long lines which you
  • 02:04:56 should in this course though I will
  • 02:04:59 typically try to put expressions and
  • 02:05:01 code into one line because once this can
  • 02:05:05 lead to you being required to scroll to
  • 02:05:08 the right a bit I believe it helps
  • 02:05:10 understanding what belongs together
  • 02:05:13 which expressions work together so that
  • 02:05:16 is why I tend to use one or two lines
  • 02:05:19 only instead of splitting everything
  • 02:05:21 across multiple lines but you can do
  • 02:05:23 that so that's just a side note so that
  • 02:05:26 you don't wonder why I use relatively
  • 02:05:29 long lines we're scrolling can be
  • 02:05:31 required you have the opportunity of
  • 02:05:33 splitting that across multiple lines if
  • 02:05:35 you want to
  • 02:05:43 so that's actually it for the module we
  • 02:05:45 learned a lot about Python we learned
  • 02:05:48 that we structure our code with
  • 02:05:50 indentation and colons for block
  • 02:05:53 statements like functions we'll all to
  • 02:05:55 learn about different block statements
  • 02:05:57 in the next module we learned that we
  • 02:05:59 create functions with the special death
  • 02:06:01 keyword and we learned that in general
  • 02:06:03 we should follow PAP eight when it comes
  • 02:06:06 to styling our code from a feature
  • 02:06:09 perspective in this module we learned
  • 02:06:10 about the core datatypes
  • 02:06:12 numbers which comprises integers and
  • 02:06:15 floats strings as well as boolean s– we
  • 02:06:19 also had a look at operators we can do
  • 02:06:21 some base or medic with plus minus times
  • 02:06:24 and by dividing values but we also have
  • 02:06:27 the modulus operator which allows us to
  • 02:06:30 get the remainder of the division we got
  • 02:06:32 the floor division which always returns
  • 02:06:34 an integer instead of a float we got the
  • 02:06:37 power operator to calculate the power of
  • 02:06:40 a number to some other number and it's
  • 02:06:43 important we all learned that strings
  • 02:06:45 can be added and multiplied
  • 02:06:48 multiplication only works with one
  • 02:06:50 string and one integer though it also
  • 02:06:52 doesn't work with floats by the way we
  • 02:06:55 had a look at lists and we learned how
  • 02:06:57 we can create lists with T squared
  • 02:06:59 brackets that we can add items with the
  • 02:07:02 append function that we can access items
  • 02:07:05 with their index which starts at 0 and
  • 02:07:09 that we can use minus 1 to get the last
  • 02:07:11 value and that there are other list
  • 02:07:14 operations like pop and so on we saw
  • 02:07:17 them blink up when we used the list in
  • 02:07:19 the IDE for example now also important
  • 02:07:21 we had a look at functions we learned
  • 02:07:24 that indentation and colons define the
  • 02:07:26 function block that we can use arguments
  • 02:07:29 that we can return values that we can
  • 02:07:33 also use default arguments which allows
  • 02:07:35 us to omit arguments when calling the
  • 02:07:37 function and that we can also assign
  • 02:07:40 values data to arguments by their names
  • 02:07:45 so called keyword arguments where we can
  • 02:07:48 reorder arguments or skip them provided
  • 02:07:51 that they have a default value now last
  • 02:07:54 but not least we also had a look at
  • 02:07:55 scope there
  • 02:07:56 is global scope for variables defined
  • 02:07:59 outside of functions and local scope for
  • 02:08:03 variables to find inside of functions
  • 02:08:05 and this is actually it now what about
  • 02:08:08 our blockchain status
  • 02:08:10 we got a chain of data we got a list of
  • 02:08:12 data of course only a basic
  • 02:08:14 implementation it's no complex data yet
  • 02:08:17 but we'll refine this throughout the
  • 02:08:18 course we're able to mine new blocks and
  • 02:08:22 that of course is also something which
  • 02:08:24 will improve throughout the course but
  • 02:08:26 now mining is a big word for asking for
  • 02:08:29 a user input and additionally all in a
  • 02:08:33 basic way we're able to hash our blocks
  • 02:08:36 we always got this previous data in the
  • 02:08:39 next block now we're not really taking
  • 02:08:41 advantage of this we're not really able
  • 02:08:43 to analyze our data based on that but
  • 02:08:46 it's a nice first step and this of
  • 02:08:49 course is all something we'll improve
  • 02:08:50 now there also are some things we
  • 02:08:53 haven't even had a look at but hey it's
  • 02:08:56 still only the first real module of this
  • 02:08:58 course so let's continue to the next
  • 02:09:01 module and learn more about the
  • 02:09:03 blockchain and of course most
  • 02:09:05 importantly about Python
  • 02:09:14 you