Skip to main content

Posts

How to delete an item from a list

So... we  made a list of names called, by the way, names  (this is the name of the list, the label, the object is the list, while names is just the label, or name, of that object). names = [ "John","Carl","Mary","Sean","Sally"] What if I want to delete Carl? If I know that Carl index is 1, as it is, I can write this code: >>> del names[1] But, what if I don't know the index of Carl? Well, we can find the index of it and then delete it. Let's make that Python does all the work. >>> del names[names.index("Carl")] If you want you could have done, taking it easier, this way >>> indexOfCarl = names.index("Carl") >>> del names[indexOfCarl] It's a bit too long, but the result is the same. In fact: >>> names [ "John","Mary","Sean","Sally"] There in not the name of Carl in the list any more. Let's po...

Let's make a list in python

Python is known to b easy, useful and fun to use. And it is. Lists are what in other languages they call array. Let's make a list of names >>> names = ["John","Mary","Sean"] To add something to a list: >>> names.append("Sally") >>> names ["John","Mary","Sean","Sally"] There's another way, too: >>> names += ["Jim"] >>> names ["John","Mary","Sean","Sally"] Lists are mutable objects If you want to isert an item from a list you can >>> names.insert(1,"Carl") >>> names In the next post we will see how to delete an item from a list

Python, objects and Garbage Collections

An object, in Python, is a structure of data generated by the code in execution. Until the objects are useful they are kept in memory. In practice, they will be deleted, through a mechanism called garbage collection , when there will be no labels (names) refferring to them. The objects got type and identity (id) attribute. Let's create an object that has a label "varname" and that is an int type Now, if I give another value to varname >>> varname = 500 The label varname now refers to another object 500 of int type. The old object 33 does not have a label anymore, so python cannot use it anymore. The garbage collection mechanism will free the memory for this object then.

Your first program

Ok, you installed python from www.python.org The version 3.5 is good. Now open the window console (as I will speak about the windows version version, even if python works in almost the same way in linux and MacOs systems). To open the command line, click on the windows button + r, then write cmd and you are done. After that write python and you will see that you are ready to start. The console will show you some informations about the version of python. First program write this: >>> print("Hello World") Then click enter and you will see your first program appear. You second program Let's make another program >>> a = 1 >>> print(a) 1 In this one you created a variable called a and you gave a numeric value (1). After that you printed to the console the number. Let's write on a file You can easily put your values in a file instead of using the console to display your data. >>>with open("newfile.txt",...

Introduction to python

Python Python is a very nice, easy and useful programming language. If you are new to programming you should start from this, because it's very easy to fall in love with it, once you start knowing how it easily can be used by anyone for a lot of purposes, more than you can imagine. If you want to install python, just go here https://www.python.org/downloads/ when you have it installed, go in cmd (ctrl + r and write cmd in the window that appears) and write python. The interpreter will start. Write the following code: >>> print("Hello World") Hello World Hello World Ok, you're done. It's easy, isn't it? Ok, you made your first line of code in Python. It was easy and all of Python is easy.