/srv/irclogs.ubuntu.com/2010/09/25/#ubuntu-classroom.txt

tomswartz07exit02:43
ibuclawHi DiegoTc14:54
DiegoTchi ibuclaw14:55
ibuclawDiegoTc, what will you be covering?14:57
DiegoTcibuclaw, the basic of c++(cout,cin, the if and else, the switch)14:58
ibuclawSTL...15:00
ibuclawyucky... :-)15:01
DiegoTcibuclaw, whats STL?15:03
ibuclawC++ Standard Template Library15:04
=== emma is now known as mc44
=== mc44 is now known as emma
=== saji89 is now known as opensorcerer
Bodsdahey DiegoTc15:58
DiegoTchi Bodsda15:58
DiegoTcokay it is time16:01
Bodsdaw00t16:01
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat || Event: Beginners Team Dev Academy - Current Session: C++ for Beginners - Instructors: DiegoTc
DiegoTchi guys16:02
DiegoTcI am going to start with the C++ for Beginners16:02
DiegoTcif you had any question just let me know16:02
DiegoTcand i will try to answer it :)16:03
DiegoTcokay so lets see, did everyone install the build-essential?16:03
DiegoTcand the codeblocks?16:03
DiegoTcokay, if you haven't installed codeblocks we can use gedit for writting our code16:04
DiegoTcopensorcerer, codeblocks it is not necessary16:05
DiegoTci recommend because its a great IDE for beginning on c++16:05
DiegoTcif you are going to use codeblocks create a new file16:06
DiegoTcand lets save it with the name of helloworld.cpp16:06
DiegoTcif you are going to use gedit, les open also a new file and save it with the same name helloworld.cpp16:07
DiegoTcdid everyone did this?16:07
DiegoTcokay16:08
DiegoTcso know we are going to type this16:08
DiegoTc#include <iostream>16:09
DiegoTcI will explain what the iostream means16:09
DiegoTcCarroarmato0, yes16:09
DiegoTcfor c++ the comments consist this way16:10
DiegoTc/16:10
DiegoTc/16:10
DiegoTc"//"16:10
DiegoTcwith out the ""16:10
DiegoTcokay http://paste.ubuntu.com/500372/16:10
DiegoTcthats the code of the hello world16:11
DiegoTccopy it or type it  :)16:11
DiegoTcand in some seconds we will compile it and i will explain it16:11
DiegoTc!q16:12
DiegoTc!question16:13
DiegoTcI am having a little trouble with the ClassBot so lets answer this questions16:13
DiegoTc<Mohan_chml> QUESTION: why we need "using namespace std;"16:14
ClassBotMohan_chml asked: why we need "using namespace std;"16:15
DiegoTcnamespace definition=is an abstract container providing context for the items (names, or technical terms, or words) it holds and allowing disambiguation of homonym items residing in different namespaces.16:15
DiegoTcin normal words iostream is the library that contains many functions so I am using the using namespace std;16:16
DiegoTcto include them all16:16
DiegoTcbut we can especify which ones to use16:16
ClassBotpedro3005 asked: Shouldn't we "return 0;" at the end of the program?16:17
DiegoTcyes and no16:17
DiegoTcif you are using it on windows you have to do it, on linux no16:17
DiegoTci think i amswer all of your question right now? opensorcerer in some min i will answer yours16:19
DiegoTceveryone copy this code? http://paste.ubuntu.com/500372/16:20
DiegoTclets continue16:20
DiegoTcif we are using codeblocks16:21
DiegoTclets save the file16:21
DiegoTcin gedit also16:21
DiegoTcso the codeblock users lets go to the Build menu16:21
DiegoTclets click on the BUILD option16:22
DiegoTcand on the down part we are going to see the message that process end it with 0 errors16:22
DiegoTcif you are using gedit16:23
DiegoTclets open the terminal16:23
DiegoTclets go to the directory where you save the file16:23
DiegoTclets suppose you are save it on home16:23
DiegoTcso do a cd /home16:23
DiegoTcand you are going to do this16:24
DiegoTcg++ -o helloworld helloworld.cpp16:25
DiegoTcready?16:26
DiegoTcfor everyone this is important16:26
DiegoTcthe g++ is the compiler16:26
DiegoTc-o indicates the  name of the executable16:26
DiegoTcwhich will be helloworld16:27
DiegoTcand for last the name of the cpp file16:27
DiegoTcso if you are in the terminal16:27
DiegoTcfor executting the program just do this16:27
DiegoTc./helloworld16:28
DiegoTcin codeblocks go to to BUILD16:28
DiegoTcand click on run16:28
DiegoTcand a small terminal will appear with16:28
DiegoTcHello World16:28
DiegoTcdid everyone got the hello world?16:29
DiegoTcOkay guys16:30
DiegoTcgive 1 min16:30
DiegoTcokay lets continue16:31
DiegoTceach time you begin to program on c++16:32
DiegoTcyou have to have the main(){CODE}16:32
DiegoTcthe cout<< it is for showing the message16:33
DiegoTcit is equivalent to the print of python or c16:33
DiegoTceach time you are going to print a string you must include ""16:34
DiegoTcand here each end of line ends in ;16:34
DiegoTcdid everyone got that16:34
DiegoTcnow16:34
DiegoTcif we want to read from the keyboard16:34
DiegoTcwe have to use cin>>16:35
DiegoTcc++ has different types of variables16:35
DiegoTcfor numbers = int, double, float16:36
DiegoTcchar16:36
DiegoTcbool16:36
DiegoTcstring16:36
DiegoTcso lets make this small exercise16:36
DiegoTcfibolinux, yuo have to use "\n"16:38
DiegoTcokay copy this example http://paste.ubuntu.com/500395/16:40
DiegoTclet me know when you have it16:41
DiegoTcokay if you copy it build it and run it :)16:42
DiegoTcdid everyone got it?16:43
DiegoTcokay i suppose that is a yes16:44
DiegoTcwith c++16:44
DiegoTcyou can use it for math operations16:44
DiegoTcexamples16:44
DiegoTc5+5=1016:44
DiegoTcwe declare a variable int ans16:46
DiegoTcans=5+5;16:46
DiegoTcand that will give us 1016:46
ClassBotopensorcerer asked: How do i use \n while printing num   ?16:46
DiegoTcopensorcerer, could you be a little more specific16:47
DiegoTci don't get you16:47
DiegoTcokay opensorcerer16:48
DiegoTceasy16:48
DiegoTccout<<"The number is: "<<number<<"\n";16:49
DiegoTcopensorcerer, i thinks you referred to that16:50
DiegoTci forgot this detail16:50
DiegoTcon the cout you can print everything in the same line16:50
DiegoTcusing  <<16:50
DiegoTcon the last example we did this16:51
DiegoTc  cout<<"The number is: ";16:51
DiegoTc cout<< number;16:51
DiegoTcbut we can do it his way also16:51
DiegoTccout<<"The number is: "<<number;16:51
DiegoTcso know that we know some basic concepts lets make another exercise together16:52
DiegoTclets make a small program that the user enters 2 numbers16:52
DiegoTcand we have show the sum of this 2 numbers16:52
DiegoTcso lets begins what w have to do first¿?16:53
DiegoTcif anyone ones to do it alone write and if it works copy it on http://paste.ubuntu.com this way everyone could see it16:54
DiegoTcokay first of all we need to write this16:55
DiegoTc#include <iostream>16:55
DiegoTcusing namespace std;16:55
DiegoTcmain()16:55
DiegoTc{16:55
DiegoTcwehave to declare 3 variables of (int) type16:56
DiegoTcit could be double or floats if we are going to add decimals16:56
DiegoTcCarroarmato0, very good16:57
DiegoTchttp://paste.ubuntu.com/500403/16:57
DiegoTche declare his 2 variables16:58
DiegoTcint var1, var2;16:58
DiegoTche use the cout for telling the user what the program wants16:58
DiegoTccout << "Give me 1st number: ";16:58
DiegoTcthe cin for reading the number16:59
DiegoTc    cin >> var1;16:59
DiegoTcand in the same cout he gave the answer cout << "The sum is: " << var1 + var2;16:59
DiegoTcthere are many ways of doing it17:00
DiegoTchttp://paste.ubuntu.com/500411/17:00
DiegoTcin this one i declared a third variable for giving the answer17:00
ClassBotpedro3005 asked: how do we convert between types?17:01
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat ||
DiegoTcpedro you are referring how to convert an int to a string=17:01
pedro3005DiegoTc, and float to int, etc17:01
DiegoTcpedro3005, you have to make typecasting17:02
DiegoTchttp://www.cplusplus.com/doc/tutorial/typecasting/17:02
pedro3005thanks for the lesson DiegoTc17:03
bihari_i have problem on C language !17:03
pedro3005bihari_, what is it?17:03
DiegoTcnp pedro300517:03
bihari_write a programme to print greatest and smallest number inside 1d array17:04
bihari_use codepad.org17:04
bihari_pedro3005, write a programme to print greatest and smallest number inside 1d array17:05
pedro3005bihari_, well, do it like this: declare a variable called max_num, and set it to 0. Iterate over the array and compare max_num to the elements. If they are greater, put that in max_num and continue. by the end, you'll have the greatest number in max_num.17:07
pedro3005then just print max_num, obviously17:08
bihari_humm can you wright that code?17:11
pedro3005sorry, not going to do homework for you17:11
opensorcererbihari_: Otherwise just sort the numbers in the array and take the largest and smallest, since you need both.17:12
pedro3005oh yeah, he needs the smallest too17:12
pedro3005opensorcerer, but as C has no standard function to sort an array, that might be too much work17:12
pedro3005just have two vars, max_num and min_num17:13
opensorcererpedro3005: He can use the usual nested loop sorting technique. Its really easy. :)17:13
bihari_yes17:13
pedro3005opensorcerer, you mean bubble sort?17:13
bihari_it will be short out in the nested loop17:14
bihari_yes what is buuble sort17:14
bihari_?17:14
opensorcererpedro3005: I guess so.17:14
bihari_i wants to know17:14
pedro3005http://en.wikipedia.org/wiki/Bubble_sort17:14
opensorcererbihari_: http://en.wikipedia.org/wiki/Bubble_sort17:15
pedro3005opensorcerer, yeah, but that algorithm spends a great amount of computational steps which are simply not needed.17:15
opensorcererpedro3005: :)17:15
opensorcererpedro3005: Ok.17:15
pedro3005I'd rather expend the memory to store two ints than sort an array, specially through an inefficient algorithm17:15
pedro3005just my opinion17:15
opensorcererpedro3005: I go with your opinion, it seems more efficient. :)17:16
pedro3005opensorcerer, since we're talking about C here, both will be so fast that the difference is not notable. But let's stick to theory :P17:17
opensorcererpedro3005: Yes, for the sake of larger programs that he/we may do in future. :)17:17
pedro3005if it was python, though17:17
opensorcererpedro3005: So now we have to variables max_num and min_num. How exactly will we porceed?17:18
opensorcererproceed*17:18
opensorcererpedro3005: You have inbuilt sorting methods in python. right?17:18
pedro3005opensorcerer, well, i'll write some pseudo-code (so we avoid ctrl + c methods :D)17:18
opensorcererpedro3005: we compare once to get max_num and then again to get the min_num?17:18
pedro3005opensorcerer, yeah, hold on17:19
opensorcererpedro3005: Sure. I'm also trying it. :)17:21
pedro3005opensorcerer, with python, we have two standard functions: max() and min()17:21
pedro3005:)17:21
opensorcererpedro3005: Python is super-awesome. :)17:23
pedro3005opensorcerer, this pseudo-code would work http://paste.pocoo.org/show/267209/17:24
opensorcererpedro3005: See my pm.17:26
bostikforeverpedro3005: you are awesome17:26
pedro3005bostikforever, ..thank you17:27
bostikforeverDo you use Netbeans?17:27
pedro3005nope, too slow17:27
pedro3005Geany17:27
bostikforeverOkay17:28
bostikforeverI'm having some link ish's with my Netbeans17:28
bostikforevercould you be of any help?17:28
=== JoeMaverickSett is now known as Guest74138
=== JoeMaver1ckSett is now known as JoeMaverickSett
=== opensorcerer is now known as Guest90146
=== Guest90146 is now known as saji89
saji89bihari_: how's is your program going?17:51
bihari_saji89,  its going good17:52
bihari_how about you saji89 what you doing?17:52
saji89bihari_: I'm fine. Just gonna attend the Pythin session here. Let's move to #ubuntu-classroom-chat, since the session will be starting here now. :)17:53
bihari_ok17:53
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat || Event: Beginners Team Dev Academy - Current Session: Introduction to Python: Part 2 - Instructors: pedro3005
pedro3005Alright18:01
pedro3005First, I'd like to say that now we can properly use the question system, since the stupid pedro3005 finally learned how to use the bot.18:02
pedro3005Well, it was quite complex. I mean, three commands?!? not something you grasp over an afternoon18:02
pedro3005But let us go on18:02
pedro3005We'll begin with a revision of what we saw last class18:03
pedro3005We learned what is the python shell and we learned it can evaluate commands18:03
pedro3005That is great for testing purposes, but at some time you must be able to create a file with your program in it18:04
pedro3005So that's what we're going to do now18:04
pedro3005First, open your favorite editor18:05
ClassBotalbel12051 asked: do this has the slides18:06
pedro3005No. the instructor is too lazy (damn that guy, huh)18:06
pedro3005Well, in your editor, let's begin with the old classic we also learned last class18:07
pedro3005print "Hello, world!"18:07
pedro3005After you write that, save your file as hello.py18:07
pedro3005You can now execute your file by running the command 'python hello.py'18:07
pedro3005Now, we can notice some important distinctions in python18:08
pedro3005for instance18:08
pedro3005Go to your file and write:18:08
pedro3005print 1+118:08
pedro3005now run that file with the above method and you'll see it outputs 218:08
pedro3005Go back to the file and change that to simply18:09
pedro30051+118:09
pedro3005Now run it. You will notice nothing happens18:09
pedro3005The expression was evaluated, but since there was nothing to do with it, it was thrown away18:09
pedro3005Guys18:11
pedro3005I am TERRIBLY sorry. I need to get away for 5 little minutes. I'll be right back after that to continue the session. Really sorry, this is just a thing I could not have foreseen18:11
pedro3005hope you will excuse me18:11
pedro3005I'll be back as soon as I can, should take just some minutes18:12
=== guest_ is now known as Scio
=== Scio is now known as Guest64706
=== fred_ is now known as Guest24596
pedro3005I AM BACK18:20
pedro3005well, it took 8 minutes. I'm not the fastest runner18:20
pedro3005sorry18:20
pedro3005ok18:21
pedro3005where were I18:21
pedro3005Oh yeah, files18:21
pedro3005So as I was saying, if you don't tell python to do something with the expression, it is thrown away18:21
pedro3005We also learned about variables, their types and how to declare them18:21
pedro3005you can for instance declare an int variable x that is 218:22
pedro3005x = 218:22
pedro3005Consider this case: you want to raise x's value by one18:22
pedro3005Python does NOT have ++ like other languages18:22
pedro3005in your file, you could try18:22
pedro3005x + 118:22
pedro3005but we all know that won't work18:22
pedro3005it'll be evaluated then thrown away18:22
pedro3005we must do18:23
pedro3005x = x + 118:23
pedro3005But wait!18:23
pedro3005There is a short way to say that18:23
pedro3005we can say that:18:23
pedro3005x += 118:23
pedro3005that is the exact same thing as x = x + 118:23
pedro3005You can also do it with other signs18:24
pedro3005like18:24
pedro3005x -= 118:24
pedro3005or x *= 218:24
pedro3005and, of course, x /= 218:24
ClassBotsaji89 asked: When we put it in a file don't we put the shebang line at the top?18:25
pedro3005Yeah, I'm sorry. I always forget that line18:25
pedro3005Start your files with:18:25
pedro3005#!/usr/bin/env python18:26
pedro3005with this method, you don't need the python18:26
pedro3005just make the file executable18:26
pedro3005and then do ./file.py18:26
ClassBotsaji89 asked: IS #!/bin/python18:27
pedro3005I haven't seen such a thing, so I don't know. Presumably it is18:27
pedro3005But, as we were learning about python's arithmetic system, I forgot one operator18:27
pedro3005that's the mod operator, or %18:28
pedro3005it is similar to other languages18:28
pedro3005it gives you the rest of the division18:28
pedro3005for instance, 10 % 3 = 118:28
pedro3005it divides 10 by 3, and what is left is 118:29
pedro3005What's the usability of this? A common one is checking whether the number is even or odd18:29
pedro3005if the mod of a number by two is greater than zero, this number is odd18:29
pedro3005And if % is mod, how do we get percentage?18:30
pedro3005Well, that's easy18:30
pedro3005if you want 80 percent of a variable, just do:18:30
pedro300580/100 * x18:30
pedro3005or 0,8 * x18:31
pedro3005that's basic math18:31
pedro3005Any questions?18:31
ClassBotDiegoTc asked: why by x?18:31
pedro3005Well, x is just an example18:31
ClassBotsaji89 asked: 0,8 * x means?18:32
pedro3005wait18:32
pedro3005I'm wrong18:32
pedro3005sorry all18:32
pedro3005that's18:32
pedro30050.8 * x18:32
ClassBotHoellP asked: is there an operator for root?18:32
pedro3005Yes, there is18:33
pedro3005you need to import the module math18:33
pedro3005import math18:33
pedro3005then you can do18:33
pedro3005math.sqrt(9)18:33
pedro3005for instance18:33
pedro3005The math module has many other functions such as sine etc.18:33
pedro3005you can learn about them here http://docs.python.org/library/math.html18:33
pedro3005Questions?18:34
pedro3005Alright, let's move on18:34
pedro3005We learned about ifs last class18:34
pedro3005We can get an answer from python to boolean expressions, as we saw18:35
pedro3005and if directs our program flow given these answers18:35
pedro3005for instance, if we have a variable x18:35
pedro3005and we want to see if it's positive or negative18:35
pedro3005if x > 0:18:35
pedro3005    print "It is positive"18:35
pedro3005else:18:36
pedro3005    print "It is negative OR equal to zero"18:36
pedro3005Well, what if I want to know exactly? We have the keyword elif18:36
pedro3005that means "else if"18:36
pedro3005so we can rewrite our program using that18:36
pedro3005if x > 0:18:36
pedro3005    print "It is positive"18:37
pedro3005elif x == 0:18:37
pedro3005    print "It is zero"18:37
pedro3005else:18:37
pedro3005    print "It is negative"18:37
pedro3005Questions?18:37
pedro3005Lastly, we learned about the raw_input() command18:38
pedro3005that's what we use to get input from the user18:38
pedro3005its return type is a string18:38
pedro3005we can say18:39
pedro3005answer = raw_input("What is your name? ")18:39
pedro3005Well, what if we want to get a number from it?18:40
pedro3005age = raw_input("What is your age? ")18:40
pedro3005yes, but that's still type string18:40
pedro3005we can't do age + 1, because age is a string18:40
pedro3005We need to use type conversion18:41
pedro3005we can do:18:41
pedro3005int(age)18:41
pedro3005but that is insecure18:41
pedro3005what if the user (never doubt the sheer stupidity of a user) decides to enter "bananas" when asked for his age?18:41
pedro3005int("bananas") will make your program crash18:41
pedro3005So, we have a method for strings18:42
ClassBotHoellP asked: how do i combine variables and text in a print statement?18:42
pedro3005You have some options18:42
pedro3005you can do18:42
pedro3005print "the variable is:" + x18:43
pedro3005You can also do18:43
pedro3005print "the variable is %s" % x18:43
pedro3005The latter seems harder, but if you need a specific formatting that is your option18:43
ClassBotsaji89 asked: IF we need to print two/more variables in one print statement.18:44
pedro3005well, you can do18:44
pedro3005print "var one is %s, var two is %s" % (var_one, var_two)18:44
pedro3005the thing in parenthesis is called a tuple18:45
pedro3005we'll be learning about them later18:45
pedro3005But back to our bananas problem18:45
pedro3005strings have a method called isdigit()18:45
pedro3005you can call it by "any string".isdigit()18:45
pedro3005that will return True or False18:45
pedro3005in our case, we can simply do18:45
pedro3005age = raw_input("What is your age? ")18:46
pedro3005if age.isdigit():18:46
pedro3005   print "Your %s years old!" % age18:46
pedro3005else:18:46
pedro3005    print "Get off the computer and go back to your crib, kid"18:46
pedro3005there are many more methods like this one18:47
pedro3005you can learn about them at http://docs.python.org/library/stdtypes.html#str.isalnum (go scrolling down for more)18:47
pedro3005Excuse my horrible english18:48
pedro3005it is "you're"18:48
pedro3005thank you saji8918:48
pedro3005also, that line lacked one space for indentation18:49
ClassBotOtto48 asked: is there a function to validate a string as a float number?18:50
ClassBotOtto48 asked: or a negative?18:50
pedro3005There are ways of doing that, but not with (standard) functions18:50
pedro3005What you would need is a try statement18:51
pedro3005If you could store that question for a later class18:51
pedro3005I really didn't want to go over try with you guys before some things18:51
pedro3005Thanks for the comprehension18:51
pedro3005but you may notice that int("-4") works just fine :)18:52
pedro3005and so does float("4.5")18:52
ClassBotnewboon2age asked: QUESTION: Were is the log for this session?18:52
pedro3005nhandler usually takes care of the logs18:52
pedro3005So, basically that is what we learned last class18:54
pedro3005I wanted to go over it again to really get it complete18:54
pedro3005Let's learn a  couple things about ifs18:54
pedro3005What if we have to check several expressions?18:55
pedro3005we have things like not, and, or18:55
pedro3005for instance18:55
pedro3005say we have an int x18:55
pedro3005if x < 0 and x != 0:18:55
pedro3005    print "x is negative"18:55
pedro3005and another example18:56
pedro3005if x < 0 or x > 0:18:57
pedro3005    print "x is not zero"18:57
pedro3005of course, it'd be better to just do18:57
pedro3005if x != 018:57
pedro3005but I couldn't think of a better example :)18:57
ClassBotOtto48 asked: Is shortcut evaluation used or are both sides evaluated regardless of the result of one side of the expression?18:58
pedro3005Shortcuts are used. if the first expression fails, the other is not checked (if we're dealing with ands, of course)18:58
pedro3005We also have not, as I mentioned18:59
pedro3005if x > 1 and not x > 10:19:00
pedro3005which is equivalent to x < 10, of course (my examples always suck)19:00
pedro3005but not _is_ useful sometimes19:00
pedro3005Questions?19:00
ClassBotOtto48 asked: is 1<x<10 valid syntax?19:01
pedro3005It is not19:01
pedro3005unfortunately, for the math-inclined, you'll have to do x > 1 and x < 1019:01
pedro3005Let me introduce you guys to a new type now19:03
pedro3005In python, we have lists19:04
pedro3005If you are familiar with C and other languages, they're like arrays19:04
pedro3005but only MUCH better19:04
pedro3005A list is a set of elements19:04
pedro3005You can define them with the following syntax:19:05
pedro3005numbers = [1, 2, 3, 4, 5]19:05
pedro3005To get the nth element of a list, we do list[n-1]. That's because the first element is 019:06
pedro3005in this case19:06
pedro3005numbers[0] == 119:06
pedro3005and so forth19:06
pedro3005Questions?19:06
pedro3005Ok19:07
pedro3005To get the total number of elements in a list you can use the function len()19:07
pedro3005len(numbers) == 519:07
pedro3005Lists are a mutable type, meaning you can add and remove elements from it19:07
pedro3005you can use the method append() to add something to a list at the rightmost position19:08
pedro3005numbers.append(6)19:08
pedro3005now we have numbers = [1, 2, 3, 4, 5, 6]19:08
ClassBotsaji89 asked: can we make a list immutable?19:09
pedro3005Tuples are sort of like immutable lists19:09
pedro3005so if you want something immutable go ahead and use a tuple (I'll go over them shortly)19:09
pedro3005We also have the remove() method for lists19:10
pedro3005numbers.remove(6) will remove the first instance of 6 it finds19:10
pedro3005if we had, for instance, num = [6, 6, 6] and did num.remove(6), we'd end up with [6, 6]19:11
pedro3005But what if I had [1, 2, 3, 1, 4] and I wanted to remove exactly the last 1?19:12
pedro3005well, that is the 3rd element (remember, the first one is 0)19:12
pedro3005so we can do19:12
pedro3005del numbers[3]19:12
pedro3005Or, we can use the pop() method19:13
pedro3005list.pop(i) will remove the ith element and return it19:13
pedro3005pop(), without arguments, defaults to the last element19:14
pedro3005Any questions about that?19:14
ClassBotOtto48 asked: Is there a push method too?19:16
pedro3005yes, the method append()19:16
pedro3005(see above)19:16
ClassBotsaji89 asked: list.pop(i) will remove the ith element and return it. Returns it, as in prints it?19:16
pedro3005Not exactly19:16
pedro3005for instance, consider this case19:16
pedro3005if numbers.pop() > 0:19:16
pedro3005    print "the last element of numbers is bigger than zero"19:17
pedro3005but that wouldn't work with del :)19:17
pedro3005Does that explain it?19:17
pedro3005If you want to put an element at some exact position, you have insert() for that19:17
pedro3005list.insert(i, x) will insert x at position i19:18
ClassBotHoellP asked: is pop how i would evaluate command line options?19:18
pedro3005No19:18
pedro3005if you want to see command line arguments, import the module sys and access sys.argv19:18
pedro3005For example19:19
pedro3005if we had numbers = [1, 2, 3]19:19
pedro3005we could do19:19
pedro3005numbers.insert(0, 0)19:19
pedro3005that would insert 0 at position 019:19
pedro3005But behold!19:20
pedro3005lists can hold elements of different types19:21
pedro3005(take that, C)19:21
pedro3005for instance19:21
pedro3005things = [1, "hi", 4.3]19:21
pedro3005here we have an int, a string and a float19:21
pedro3005Let's go over some more list methods and then go to slicing19:22
pedro3005We have:19:22
pedro3005list.extend(another_list)19:22
pedro3005this simply adds another list to a list19:22
pedro3005a = [1, 2]  and  b = [3, 4].  a.extend(b) = [1, 2, 3, 4]19:23
pedro3005this is different from a.append(b)19:23
pedro3005a.append(b) would result in19:23
pedro3005[1, 2, [3, 4]]19:23
pedro3005Yes, friends! Lists inside lists!19:23
pedro3005Say, we had, for instance19:26
pedro3005a = [[1, 2], [3, 4]]19:26
pedro3005len(a) == 219:26
pedro3005it has two elements, two lists19:26
pedro3005a[0] == [1, 2]19:26
pedro3005suppose we want the first element of a[0]19:26
pedro3005a[0][0]19:26
pedro3005That may look weird but it's perfectly fine19:27
pedro3005any questions?19:27
pedro3005We have some other methods19:28
pedro3005list.count(x) returns how many times x occurs in list19:28
pedro3005for instance19:28
pedro3005a = [1, 1, 2, 3]19:28
pedro3005a.count(1) == 219:28
pedro3005We also have the sort() method19:29
pedro3005this sorts a list in order19:29
pedro3005a = [3, 2, 4, 1]19:29
pedro3005a.sort()19:29
pedro3005a == [1, 2, 3, 4]19:29
pedro3005what if there are strings in the list?19:29
pedro3005a = ["b", "c", "a"]19:30
pedro3005a.sort()19:30
pedro3005a == ["a", "b", "c"]19:30
pedro3005Yes, it sorts alphabetically too :)19:30
ClassBotOtto48 asked: How does sort() handle different types? EG a = [1, "w", [5, 6]]?19:30
pedro3005you got me. Your example yields [1, [5, 6], 'w']. Python must have a priority hierarchy, but unfortunately it's not mentioned in the docs (or I couldn't find it)19:32
pedro3005You also have the function sorted(), which is similar to sort()19:33
pedro3005but sort() alters the list in place, and sorted() merely returns a sorted list19:34
pedro3005let me explain that further19:34
pedro3005a = [3, 1, 2]19:34
pedro3005a.sort()19:34
pedro3005now a == [1, 2, 3]19:34
pedro3005but what if we did:19:34
pedro3005a = [3, 1, 2]19:34
pedro3005sorted(a)19:34
pedro3005a STILL is [3, 1, 2]19:34
pedro3005because sorted() does not modify a19:34
pedro3005it only returns the sorted list19:34
pedro3005did you understand the difference?19:35
pedro3005great19:36
ClassBotsaji89 asked: simply sorted(a) throws away the result. Isn't it? So we use b=sorted(a). Right?19:36
pedro3005Perfect, saji89 !19:37
pedro3005You're grasping the concept19:37
pedro3005that is correct19:37
pedro3005We also have the list method reverse(), which reverses a list in place19:37
pedro3005a = [1, 2, 3]19:37
pedro3005a.reverse()19:37
pedro3005a = [3, 2, 1]19:37
pedro3005but it does not SORT the list in reverse19:37
pedro3005it merely switches positions19:38
pedro3005to sort it in reverse, do:19:38
pedro3005a.sort(reverse = True)19:38
pedro3005Questions?19:38
ClassBotOtto48 asked: does "a.sort(reverse = True)" hint at named parameters?19:39
pedro3005Yes19:39
pedro3005many functions called will have that19:39
pedro3005We'll be learning more about them in the session about functions19:40
pedro3005Let's have a quick look at for loops, and while if we manage19:40
pedro3005A for loop is a method of iterating over a list, or any iterable type19:40
pedro3005in this loop, we have a temporary variable that will be set to each of the values in the list, run the commands with, then changed19:41
pedro3005for instance19:41
pedro3005values = [1, 2, 3, 4, 5]19:41
pedro3005for val in values:19:41
pedro3005    print val19:41
pedro3005Go ahead and try that19:42
pedro3005the example is fairly obvious19:42
pedro3005For each val inside values, print it19:42
=== stalcup is now known as vorian
pedro3005We may, however, raise complexity19:43
pedro3005Say I would like to only print the even values19:43
pedro3005you may recall how to check if a number is even19:43
pedro3005we use %19:43
pedro3005so19:43
pedro3005for val in values:19:43
pedro3005    if not val % 2:19:44
pedro3005        print val19:44
pedro3005Any questions about that?19:44
pedro3005Alright, that's good19:45
pedro3005We have time for while loops then19:45
pedro3005A while loop is a loop that's run as long as a certain expression evaluates to True19:46
pedro3005for instance19:46
pedro3005x = 519:46
pedro3005while x > 0:19:46
pedro3005    x -= 119:46
pedro3005    print "x is now %s" % x19:46
pedro3005go ahead and try that19:47
pedro3005Questions?19:48
ClassBotOtto48 asked: Does python have other loops? Does it have constructs similar to "Do ...Until" or "for (a=1;a<5;a+=)"?19:50
pedro3005Nope19:50
pedro3005python does not have a C-like for19:50
pedro3005no do while either, IIRC19:50
pedro3005But be not afraid19:51
pedro3005we can do it all with justs fors and whiles :)19:51
pedro3005Loops have two important keywords19:51
pedro3005break and continue19:51
pedro3005break stops a loop immediately19:51
pedro3005even_numbers = [2, 4, 6]19:52
pedro3005for x in even_numbers:19:52
pedro3005    if x % 2: # x is ODD19:52
pedro3005        break19:52
pedro3005we found a non-even number!19:52
pedro3005That stops the iteration completely19:52
pedro3005Each of the "loops" is called an iteration19:52
pedro3005and looping over is calling iterating19:53
pedro3005the keyword continue stops an iteration and moves on to the next19:53
pedro3005say we only want to print numbers greater than 019:53
pedro3005well, scratch that19:54
pedro3005hm..19:54
pedro3005alright... my examples suck19:54
pedro3005but it's pretty simple19:54
pedro3005a = [1, 2, 3, 4, 5]19:54
pedro3005for x in a:19:54
pedro3005wait19:55
pedro3005<saji89> pedro3005: x%2 = something suld be there, right?19:55
pedro3005I forgot to mention this19:55
pedro3005no, saji89 , we can do without it19:55
pedro3005say19:55
pedro3005x = 019:55
pedro3005if x: blabla19:55
pedro3005it will not run19:55
pedro3005if x: is just a way of saying if x == True19:56
pedro3005things that are == False: 0, "", [],19:56
pedro3005empty things19:56
pedro3005Questions?19:57
pedro3005Good19:58
pedro3005in these last minutes, I'll introduce you guys to two great resources for beginners19:58
pedro3005The UF beginner programming challenges - http://ubuntuforums.org/showthread.php?p=549948619:58
pedro3005You should be able to do problem 119:58
* pedro3005 checks if stlsaint is around19:59
pedro3005No? Good19:59
pedro3005We also have another resource19:59
pedro3005http://projecteuler.net :)19:59
pedro3005It is a series of math questions19:59
pedro3005if you like math, they are great20:00
pedro3005You should also be able to solve the first problem from project euler20:00
pedro3005and the second20:00
pedro3005but they are REALLY hard20:00
pedro3005so if you manage, kudos20:00
pedro3005and if you're not managing, don't worry20:00
pedro3005with your amount of knowledge you could solve 1, 2, 6 and 5 :P20:01
=== ChanServ changed the topic of #ubuntu-classroom to: Welcome to the Ubuntu Classroom - https://wiki.ubuntu.com/Classroom || Support in #ubuntu || Upcoming Schedule: http://is.gd/8rtIi || Questions in #ubuntu-classroom-chat ||
pedro3005This concludes the session20:01
pedro3005Next class I will be going over how to solve the 1st UF beginner problem and the 1st Euler problem20:02
saji89pedro3005: COol.... :)20:02
pedro3005I encourage all to try them20:02
Mohan_chmlWell Done pedro300520:02
saji89pedro3005: I'll try the first ones in the two reaosurces. :)20:03
pedro3005saji89, I'll gladly take PMs20:04
pedro3005:)20:04
apperceptionsthanks pedro!20:04
pedro3005thank you apperceptions , hope you liked it20:04
saji89pedro3005: Cool.20:05
* etank is starting to think that the times on the classroom calendar are UTC and he is about 4 hours late22:15
=== yofel_ is now known as yofel

Generated by irclog2html.py 2.7 by Marius Gedminas - find it at mg.pov.lt!