tomswartz07 | exit | 02:43 |
---|---|---|
ibuclaw | Hi DiegoTc | 14:54 |
DiegoTc | hi ibuclaw | 14:55 |
ibuclaw | DiegoTc, what will you be covering? | 14:57 |
DiegoTc | ibuclaw, the basic of c++(cout,cin, the if and else, the switch) | 14:58 |
ibuclaw | STL... | 15:00 |
ibuclaw | yucky... :-) | 15:01 |
DiegoTc | ibuclaw, whats STL? | 15:03 |
ibuclaw | C++ Standard Template Library | 15:04 |
=== emma is now known as mc44 | ||
=== mc44 is now known as emma | ||
=== saji89 is now known as opensorcerer | ||
Bodsda | hey DiegoTc | 15:58 |
DiegoTc | hi Bodsda | 15:58 |
DiegoTc | okay it is time | 16:01 |
Bodsda | w00t | 16: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 | ||
DiegoTc | hi guys | 16:02 |
DiegoTc | I am going to start with the C++ for Beginners | 16:02 |
DiegoTc | if you had any question just let me know | 16:02 |
DiegoTc | and i will try to answer it :) | 16:03 |
DiegoTc | okay so lets see, did everyone install the build-essential? | 16:03 |
DiegoTc | and the codeblocks? | 16:03 |
DiegoTc | okay, if you haven't installed codeblocks we can use gedit for writting our code | 16:04 |
DiegoTc | opensorcerer, codeblocks it is not necessary | 16:05 |
DiegoTc | i recommend because its a great IDE for beginning on c++ | 16:05 |
DiegoTc | if you are going to use codeblocks create a new file | 16:06 |
DiegoTc | and lets save it with the name of helloworld.cpp | 16:06 |
DiegoTc | if you are going to use gedit, les open also a new file and save it with the same name helloworld.cpp | 16:07 |
DiegoTc | did everyone did this? | 16:07 |
DiegoTc | okay | 16:08 |
DiegoTc | so know we are going to type this | 16:08 |
DiegoTc | #include <iostream> | 16:09 |
DiegoTc | I will explain what the iostream means | 16:09 |
DiegoTc | Carroarmato0, yes | 16:09 |
DiegoTc | for c++ the comments consist this way | 16:10 |
DiegoTc | / | 16:10 |
DiegoTc | / | 16:10 |
DiegoTc | "//" | 16:10 |
DiegoTc | with out the "" | 16:10 |
DiegoTc | okay http://paste.ubuntu.com/500372/ | 16:10 |
DiegoTc | thats the code of the hello world | 16:11 |
DiegoTc | copy it or type it :) | 16:11 |
DiegoTc | and in some seconds we will compile it and i will explain it | 16:11 |
DiegoTc | !q | 16:12 |
DiegoTc | !question | 16:13 |
DiegoTc | I am having a little trouble with the ClassBot so lets answer this questions | 16:13 |
DiegoTc | <Mohan_chml> QUESTION: why we need "using namespace std;" | 16:14 |
ClassBot | Mohan_chml asked: why we need "using namespace std;" | 16:15 |
DiegoTc | namespace 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 |
DiegoTc | in normal words iostream is the library that contains many functions so I am using the using namespace std; | 16:16 |
DiegoTc | to include them all | 16:16 |
DiegoTc | but we can especify which ones to use | 16:16 |
ClassBot | pedro3005 asked: Shouldn't we "return 0;" at the end of the program? | 16:17 |
DiegoTc | yes and no | 16:17 |
DiegoTc | if you are using it on windows you have to do it, on linux no | 16:17 |
DiegoTc | i think i amswer all of your question right now? opensorcerer in some min i will answer yours | 16:19 |
DiegoTc | everyone copy this code? http://paste.ubuntu.com/500372/ | 16:20 |
DiegoTc | lets continue | 16:20 |
DiegoTc | if we are using codeblocks | 16:21 |
DiegoTc | lets save the file | 16:21 |
DiegoTc | in gedit also | 16:21 |
DiegoTc | so the codeblock users lets go to the Build menu | 16:21 |
DiegoTc | lets click on the BUILD option | 16:22 |
DiegoTc | and on the down part we are going to see the message that process end it with 0 errors | 16:22 |
DiegoTc | if you are using gedit | 16:23 |
DiegoTc | lets open the terminal | 16:23 |
DiegoTc | lets go to the directory where you save the file | 16:23 |
DiegoTc | lets suppose you are save it on home | 16:23 |
DiegoTc | so do a cd /home | 16:23 |
DiegoTc | and you are going to do this | 16:24 |
DiegoTc | g++ -o helloworld helloworld.cpp | 16:25 |
DiegoTc | ready? | 16:26 |
DiegoTc | for everyone this is important | 16:26 |
DiegoTc | the g++ is the compiler | 16:26 |
DiegoTc | -o indicates the name of the executable | 16:26 |
DiegoTc | which will be helloworld | 16:27 |
DiegoTc | and for last the name of the cpp file | 16:27 |
DiegoTc | so if you are in the terminal | 16:27 |
DiegoTc | for executting the program just do this | 16:27 |
DiegoTc | ./helloworld | 16:28 |
DiegoTc | in codeblocks go to to BUILD | 16:28 |
DiegoTc | and click on run | 16:28 |
DiegoTc | and a small terminal will appear with | 16:28 |
DiegoTc | Hello World | 16:28 |
DiegoTc | did everyone got the hello world? | 16:29 |
DiegoTc | Okay guys | 16:30 |
DiegoTc | give 1 min | 16:30 |
DiegoTc | okay lets continue | 16:31 |
DiegoTc | each time you begin to program on c++ | 16:32 |
DiegoTc | you have to have the main(){CODE} | 16:32 |
DiegoTc | the cout<< it is for showing the message | 16:33 |
DiegoTc | it is equivalent to the print of python or c | 16:33 |
DiegoTc | each time you are going to print a string you must include "" | 16:34 |
DiegoTc | and here each end of line ends in ; | 16:34 |
DiegoTc | did everyone got that | 16:34 |
DiegoTc | now | 16:34 |
DiegoTc | if we want to read from the keyboard | 16:34 |
DiegoTc | we have to use cin>> | 16:35 |
DiegoTc | c++ has different types of variables | 16:35 |
DiegoTc | for numbers = int, double, float | 16:36 |
DiegoTc | char | 16:36 |
DiegoTc | bool | 16:36 |
DiegoTc | string | 16:36 |
DiegoTc | so lets make this small exercise | 16:36 |
DiegoTc | fibolinux, yuo have to use "\n" | 16:38 |
DiegoTc | okay copy this example http://paste.ubuntu.com/500395/ | 16:40 |
DiegoTc | let me know when you have it | 16:41 |
DiegoTc | okay if you copy it build it and run it :) | 16:42 |
DiegoTc | did everyone got it? | 16:43 |
DiegoTc | okay i suppose that is a yes | 16:44 |
DiegoTc | with c++ | 16:44 |
DiegoTc | you can use it for math operations | 16:44 |
DiegoTc | examples | 16:44 |
DiegoTc | 5+5=10 | 16:44 |
DiegoTc | we declare a variable int ans | 16:46 |
DiegoTc | ans=5+5; | 16:46 |
DiegoTc | and that will give us 10 | 16:46 |
ClassBot | opensorcerer asked: How do i use \n while printing num ? | 16:46 |
DiegoTc | opensorcerer, could you be a little more specific | 16:47 |
DiegoTc | i don't get you | 16:47 |
DiegoTc | okay opensorcerer | 16:48 |
DiegoTc | easy | 16:48 |
DiegoTc | cout<<"The number is: "<<number<<"\n"; | 16:49 |
DiegoTc | opensorcerer, i thinks you referred to that | 16:50 |
DiegoTc | i forgot this detail | 16:50 |
DiegoTc | on the cout you can print everything in the same line | 16:50 |
DiegoTc | using << | 16:50 |
DiegoTc | on the last example we did this | 16:51 |
DiegoTc | cout<<"The number is: "; | 16:51 |
DiegoTc | cout<< number; | 16:51 |
DiegoTc | but we can do it his way also | 16:51 |
DiegoTc | cout<<"The number is: "<<number; | 16:51 |
DiegoTc | so know that we know some basic concepts lets make another exercise together | 16:52 |
DiegoTc | lets make a small program that the user enters 2 numbers | 16:52 |
DiegoTc | and we have show the sum of this 2 numbers | 16:52 |
DiegoTc | so lets begins what w have to do first¿? | 16:53 |
DiegoTc | if anyone ones to do it alone write and if it works copy it on http://paste.ubuntu.com this way everyone could see it | 16:54 |
DiegoTc | okay first of all we need to write this | 16:55 |
DiegoTc | #include <iostream> | 16:55 |
DiegoTc | using namespace std; | 16:55 |
DiegoTc | main() | 16:55 |
DiegoTc | { | 16:55 |
DiegoTc | wehave to declare 3 variables of (int) type | 16:56 |
DiegoTc | it could be double or floats if we are going to add decimals | 16:56 |
DiegoTc | Carroarmato0, very good | 16:57 |
DiegoTc | http://paste.ubuntu.com/500403/ | 16:57 |
DiegoTc | he declare his 2 variables | 16:58 |
DiegoTc | int var1, var2; | 16:58 |
DiegoTc | he use the cout for telling the user what the program wants | 16:58 |
DiegoTc | cout << "Give me 1st number: "; | 16:58 |
DiegoTc | the cin for reading the number | 16:59 |
DiegoTc | cin >> var1; | 16:59 |
DiegoTc | and in the same cout he gave the answer cout << "The sum is: " << var1 + var2; | 16:59 |
DiegoTc | there are many ways of doing it | 17:00 |
DiegoTc | http://paste.ubuntu.com/500411/ | 17:00 |
DiegoTc | in this one i declared a third variable for giving the answer | 17:00 |
ClassBot | pedro3005 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 || | ||
DiegoTc | pedro you are referring how to convert an int to a string= | 17:01 |
pedro3005 | DiegoTc, and float to int, etc | 17:01 |
DiegoTc | pedro3005, you have to make typecasting | 17:02 |
DiegoTc | http://www.cplusplus.com/doc/tutorial/typecasting/ | 17:02 |
pedro3005 | thanks for the lesson DiegoTc | 17:03 |
bihari_ | i have problem on C language ! | 17:03 |
pedro3005 | bihari_, what is it? | 17:03 |
DiegoTc | np pedro3005 | 17:03 |
bihari_ | write a programme to print greatest and smallest number inside 1d array | 17:04 |
bihari_ | use codepad.org | 17:04 |
bihari_ | pedro3005, write a programme to print greatest and smallest number inside 1d array | 17:05 |
pedro3005 | bihari_, 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 |
pedro3005 | then just print max_num, obviously | 17:08 |
bihari_ | humm can you wright that code? | 17:11 |
pedro3005 | sorry, not going to do homework for you | 17:11 |
opensorcerer | bihari_: Otherwise just sort the numbers in the array and take the largest and smallest, since you need both. | 17:12 |
pedro3005 | oh yeah, he needs the smallest too | 17:12 |
pedro3005 | opensorcerer, but as C has no standard function to sort an array, that might be too much work | 17:12 |
pedro3005 | just have two vars, max_num and min_num | 17:13 |
opensorcerer | pedro3005: He can use the usual nested loop sorting technique. Its really easy. :) | 17:13 |
bihari_ | yes | 17:13 |
pedro3005 | opensorcerer, you mean bubble sort? | 17:13 |
bihari_ | it will be short out in the nested loop | 17:14 |
bihari_ | yes what is buuble sort | 17:14 |
bihari_ | ? | 17:14 |
opensorcerer | pedro3005: I guess so. | 17:14 |
bihari_ | i wants to know | 17:14 |
pedro3005 | http://en.wikipedia.org/wiki/Bubble_sort | 17:14 |
opensorcerer | bihari_: http://en.wikipedia.org/wiki/Bubble_sort | 17:15 |
pedro3005 | opensorcerer, yeah, but that algorithm spends a great amount of computational steps which are simply not needed. | 17:15 |
opensorcerer | pedro3005: :) | 17:15 |
opensorcerer | pedro3005: Ok. | 17:15 |
pedro3005 | I'd rather expend the memory to store two ints than sort an array, specially through an inefficient algorithm | 17:15 |
pedro3005 | just my opinion | 17:15 |
opensorcerer | pedro3005: I go with your opinion, it seems more efficient. :) | 17:16 |
pedro3005 | opensorcerer, since we're talking about C here, both will be so fast that the difference is not notable. But let's stick to theory :P | 17:17 |
opensorcerer | pedro3005: Yes, for the sake of larger programs that he/we may do in future. :) | 17:17 |
pedro3005 | if it was python, though | 17:17 |
opensorcerer | pedro3005: So now we have to variables max_num and min_num. How exactly will we porceed? | 17:18 |
opensorcerer | proceed* | 17:18 |
opensorcerer | pedro3005: You have inbuilt sorting methods in python. right? | 17:18 |
pedro3005 | opensorcerer, well, i'll write some pseudo-code (so we avoid ctrl + c methods :D) | 17:18 |
opensorcerer | pedro3005: we compare once to get max_num and then again to get the min_num? | 17:18 |
pedro3005 | opensorcerer, yeah, hold on | 17:19 |
opensorcerer | pedro3005: Sure. I'm also trying it. :) | 17:21 |
pedro3005 | opensorcerer, with python, we have two standard functions: max() and min() | 17:21 |
pedro3005 | :) | 17:21 |
opensorcerer | pedro3005: Python is super-awesome. :) | 17:23 |
pedro3005 | opensorcerer, this pseudo-code would work http://paste.pocoo.org/show/267209/ | 17:24 |
opensorcerer | pedro3005: See my pm. | 17:26 |
bostikforever | pedro3005: you are awesome | 17:26 |
pedro3005 | bostikforever, ..thank you | 17:27 |
bostikforever | Do you use Netbeans? | 17:27 |
pedro3005 | nope, too slow | 17:27 |
pedro3005 | Geany | 17:27 |
bostikforever | Okay | 17:28 |
bostikforever | I'm having some link ish's with my Netbeans | 17:28 |
bostikforever | could 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 | ||
saji89 | bihari_: how's is your program going? | 17:51 |
bihari_ | saji89, its going good | 17:52 |
bihari_ | how about you saji89 what you doing? | 17:52 |
saji89 | bihari_: 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_ | ok | 17: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 | ||
pedro3005 | Alright | 18:01 |
pedro3005 | First, 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 |
pedro3005 | Well, it was quite complex. I mean, three commands?!? not something you grasp over an afternoon | 18:02 |
pedro3005 | But let us go on | 18:02 |
pedro3005 | We'll begin with a revision of what we saw last class | 18:03 |
pedro3005 | We learned what is the python shell and we learned it can evaluate commands | 18:03 |
pedro3005 | That is great for testing purposes, but at some time you must be able to create a file with your program in it | 18:04 |
pedro3005 | So that's what we're going to do now | 18:04 |
pedro3005 | First, open your favorite editor | 18:05 |
ClassBot | albel12051 asked: do this has the slides | 18:06 |
pedro3005 | No. the instructor is too lazy (damn that guy, huh) | 18:06 |
pedro3005 | Well, in your editor, let's begin with the old classic we also learned last class | 18:07 |
pedro3005 | print "Hello, world!" | 18:07 |
pedro3005 | After you write that, save your file as hello.py | 18:07 |
pedro3005 | You can now execute your file by running the command 'python hello.py' | 18:07 |
pedro3005 | Now, we can notice some important distinctions in python | 18:08 |
pedro3005 | for instance | 18:08 |
pedro3005 | Go to your file and write: | 18:08 |
pedro3005 | print 1+1 | 18:08 |
pedro3005 | now run that file with the above method and you'll see it outputs 2 | 18:08 |
pedro3005 | Go back to the file and change that to simply | 18:09 |
pedro3005 | 1+1 | 18:09 |
pedro3005 | Now run it. You will notice nothing happens | 18:09 |
pedro3005 | The expression was evaluated, but since there was nothing to do with it, it was thrown away | 18:09 |
pedro3005 | Guys | 18:11 |
pedro3005 | I 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 foreseen | 18:11 |
pedro3005 | hope you will excuse me | 18:11 |
pedro3005 | I'll be back as soon as I can, should take just some minutes | 18:12 |
=== guest_ is now known as Scio | ||
=== Scio is now known as Guest64706 | ||
=== fred_ is now known as Guest24596 | ||
pedro3005 | I AM BACK | 18:20 |
pedro3005 | well, it took 8 minutes. I'm not the fastest runner | 18:20 |
pedro3005 | sorry | 18:20 |
pedro3005 | ok | 18:21 |
pedro3005 | where were I | 18:21 |
pedro3005 | Oh yeah, files | 18:21 |
pedro3005 | So as I was saying, if you don't tell python to do something with the expression, it is thrown away | 18:21 |
pedro3005 | We also learned about variables, their types and how to declare them | 18:21 |
pedro3005 | you can for instance declare an int variable x that is 2 | 18:22 |
pedro3005 | x = 2 | 18:22 |
pedro3005 | Consider this case: you want to raise x's value by one | 18:22 |
pedro3005 | Python does NOT have ++ like other languages | 18:22 |
pedro3005 | in your file, you could try | 18:22 |
pedro3005 | x + 1 | 18:22 |
pedro3005 | but we all know that won't work | 18:22 |
pedro3005 | it'll be evaluated then thrown away | 18:22 |
pedro3005 | we must do | 18:23 |
pedro3005 | x = x + 1 | 18:23 |
pedro3005 | But wait! | 18:23 |
pedro3005 | There is a short way to say that | 18:23 |
pedro3005 | we can say that: | 18:23 |
pedro3005 | x += 1 | 18:23 |
pedro3005 | that is the exact same thing as x = x + 1 | 18:23 |
pedro3005 | You can also do it with other signs | 18:24 |
pedro3005 | like | 18:24 |
pedro3005 | x -= 1 | 18:24 |
pedro3005 | or x *= 2 | 18:24 |
pedro3005 | and, of course, x /= 2 | 18:24 |
ClassBot | saji89 asked: When we put it in a file don't we put the shebang line at the top? | 18:25 |
pedro3005 | Yeah, I'm sorry. I always forget that line | 18:25 |
pedro3005 | Start your files with: | 18:25 |
pedro3005 | #!/usr/bin/env python | 18:26 |
pedro3005 | with this method, you don't need the python | 18:26 |
pedro3005 | just make the file executable | 18:26 |
pedro3005 | and then do ./file.py | 18:26 |
ClassBot | saji89 asked: IS #!/bin/python | 18:27 |
pedro3005 | I haven't seen such a thing, so I don't know. Presumably it is | 18:27 |
pedro3005 | But, as we were learning about python's arithmetic system, I forgot one operator | 18:27 |
pedro3005 | that's the mod operator, or % | 18:28 |
pedro3005 | it is similar to other languages | 18:28 |
pedro3005 | it gives you the rest of the division | 18:28 |
pedro3005 | for instance, 10 % 3 = 1 | 18:28 |
pedro3005 | it divides 10 by 3, and what is left is 1 | 18:29 |
pedro3005 | What's the usability of this? A common one is checking whether the number is even or odd | 18:29 |
pedro3005 | if the mod of a number by two is greater than zero, this number is odd | 18:29 |
pedro3005 | And if % is mod, how do we get percentage? | 18:30 |
pedro3005 | Well, that's easy | 18:30 |
pedro3005 | if you want 80 percent of a variable, just do: | 18:30 |
pedro3005 | 80/100 * x | 18:30 |
pedro3005 | or 0,8 * x | 18:31 |
pedro3005 | that's basic math | 18:31 |
pedro3005 | Any questions? | 18:31 |
ClassBot | DiegoTc asked: why by x? | 18:31 |
pedro3005 | Well, x is just an example | 18:31 |
ClassBot | saji89 asked: 0,8 * x means? | 18:32 |
pedro3005 | wait | 18:32 |
pedro3005 | I'm wrong | 18:32 |
pedro3005 | sorry all | 18:32 |
pedro3005 | that's | 18:32 |
pedro3005 | 0.8 * x | 18:32 |
ClassBot | HoellP asked: is there an operator for root? | 18:32 |
pedro3005 | Yes, there is | 18:33 |
pedro3005 | you need to import the module math | 18:33 |
pedro3005 | import math | 18:33 |
pedro3005 | then you can do | 18:33 |
pedro3005 | math.sqrt(9) | 18:33 |
pedro3005 | for instance | 18:33 |
pedro3005 | The math module has many other functions such as sine etc. | 18:33 |
pedro3005 | you can learn about them here http://docs.python.org/library/math.html | 18:33 |
pedro3005 | Questions? | 18:34 |
pedro3005 | Alright, let's move on | 18:34 |
pedro3005 | We learned about ifs last class | 18:34 |
pedro3005 | We can get an answer from python to boolean expressions, as we saw | 18:35 |
pedro3005 | and if directs our program flow given these answers | 18:35 |
pedro3005 | for instance, if we have a variable x | 18:35 |
pedro3005 | and we want to see if it's positive or negative | 18:35 |
pedro3005 | if x > 0: | 18:35 |
pedro3005 | print "It is positive" | 18:35 |
pedro3005 | else: | 18:36 |
pedro3005 | print "It is negative OR equal to zero" | 18:36 |
pedro3005 | Well, what if I want to know exactly? We have the keyword elif | 18:36 |
pedro3005 | that means "else if" | 18:36 |
pedro3005 | so we can rewrite our program using that | 18:36 |
pedro3005 | if x > 0: | 18:36 |
pedro3005 | print "It is positive" | 18:37 |
pedro3005 | elif x == 0: | 18:37 |
pedro3005 | print "It is zero" | 18:37 |
pedro3005 | else: | 18:37 |
pedro3005 | print "It is negative" | 18:37 |
pedro3005 | Questions? | 18:37 |
pedro3005 | Lastly, we learned about the raw_input() command | 18:38 |
pedro3005 | that's what we use to get input from the user | 18:38 |
pedro3005 | its return type is a string | 18:38 |
pedro3005 | we can say | 18:39 |
pedro3005 | answer = raw_input("What is your name? ") | 18:39 |
pedro3005 | Well, what if we want to get a number from it? | 18:40 |
pedro3005 | age = raw_input("What is your age? ") | 18:40 |
pedro3005 | yes, but that's still type string | 18:40 |
pedro3005 | we can't do age + 1, because age is a string | 18:40 |
pedro3005 | We need to use type conversion | 18:41 |
pedro3005 | we can do: | 18:41 |
pedro3005 | int(age) | 18:41 |
pedro3005 | but that is insecure | 18:41 |
pedro3005 | what if the user (never doubt the sheer stupidity of a user) decides to enter "bananas" when asked for his age? | 18:41 |
pedro3005 | int("bananas") will make your program crash | 18:41 |
pedro3005 | So, we have a method for strings | 18:42 |
ClassBot | HoellP asked: how do i combine variables and text in a print statement? | 18:42 |
pedro3005 | You have some options | 18:42 |
pedro3005 | you can do | 18:42 |
pedro3005 | print "the variable is:" + x | 18:43 |
pedro3005 | You can also do | 18:43 |
pedro3005 | print "the variable is %s" % x | 18:43 |
pedro3005 | The latter seems harder, but if you need a specific formatting that is your option | 18:43 |
ClassBot | saji89 asked: IF we need to print two/more variables in one print statement. | 18:44 |
pedro3005 | well, you can do | 18:44 |
pedro3005 | print "var one is %s, var two is %s" % (var_one, var_two) | 18:44 |
pedro3005 | the thing in parenthesis is called a tuple | 18:45 |
pedro3005 | we'll be learning about them later | 18:45 |
pedro3005 | But back to our bananas problem | 18:45 |
pedro3005 | strings have a method called isdigit() | 18:45 |
pedro3005 | you can call it by "any string".isdigit() | 18:45 |
pedro3005 | that will return True or False | 18:45 |
pedro3005 | in our case, we can simply do | 18:45 |
pedro3005 | age = raw_input("What is your age? ") | 18:46 |
pedro3005 | if age.isdigit(): | 18:46 |
pedro3005 | print "Your %s years old!" % age | 18:46 |
pedro3005 | else: | 18:46 |
pedro3005 | print "Get off the computer and go back to your crib, kid" | 18:46 |
pedro3005 | there are many more methods like this one | 18:47 |
pedro3005 | you can learn about them at http://docs.python.org/library/stdtypes.html#str.isalnum (go scrolling down for more) | 18:47 |
pedro3005 | Excuse my horrible english | 18:48 |
pedro3005 | it is "you're" | 18:48 |
pedro3005 | thank you saji89 | 18:48 |
pedro3005 | also, that line lacked one space for indentation | 18:49 |
ClassBot | Otto48 asked: is there a function to validate a string as a float number? | 18:50 |
ClassBot | Otto48 asked: or a negative? | 18:50 |
pedro3005 | There are ways of doing that, but not with (standard) functions | 18:50 |
pedro3005 | What you would need is a try statement | 18:51 |
pedro3005 | If you could store that question for a later class | 18:51 |
pedro3005 | I really didn't want to go over try with you guys before some things | 18:51 |
pedro3005 | Thanks for the comprehension | 18:51 |
pedro3005 | but you may notice that int("-4") works just fine :) | 18:52 |
pedro3005 | and so does float("4.5") | 18:52 |
ClassBot | newboon2age asked: QUESTION: Were is the log for this session? | 18:52 |
pedro3005 | nhandler usually takes care of the logs | 18:52 |
pedro3005 | So, basically that is what we learned last class | 18:54 |
pedro3005 | I wanted to go over it again to really get it complete | 18:54 |
pedro3005 | Let's learn a couple things about ifs | 18:54 |
pedro3005 | What if we have to check several expressions? | 18:55 |
pedro3005 | we have things like not, and, or | 18:55 |
pedro3005 | for instance | 18:55 |
pedro3005 | say we have an int x | 18:55 |
pedro3005 | if x < 0 and x != 0: | 18:55 |
pedro3005 | print "x is negative" | 18:55 |
pedro3005 | and another example | 18:56 |
pedro3005 | if x < 0 or x > 0: | 18:57 |
pedro3005 | print "x is not zero" | 18:57 |
pedro3005 | of course, it'd be better to just do | 18:57 |
pedro3005 | if x != 0 | 18:57 |
pedro3005 | but I couldn't think of a better example :) | 18:57 |
ClassBot | Otto48 asked: Is shortcut evaluation used or are both sides evaluated regardless of the result of one side of the expression? | 18:58 |
pedro3005 | Shortcuts are used. if the first expression fails, the other is not checked (if we're dealing with ands, of course) | 18:58 |
pedro3005 | We also have not, as I mentioned | 18:59 |
pedro3005 | if x > 1 and not x > 10: | 19:00 |
pedro3005 | which is equivalent to x < 10, of course (my examples always suck) | 19:00 |
pedro3005 | but not _is_ useful sometimes | 19:00 |
pedro3005 | Questions? | 19:00 |
ClassBot | Otto48 asked: is 1<x<10 valid syntax? | 19:01 |
pedro3005 | It is not | 19:01 |
pedro3005 | unfortunately, for the math-inclined, you'll have to do x > 1 and x < 10 | 19:01 |
pedro3005 | Let me introduce you guys to a new type now | 19:03 |
pedro3005 | In python, we have lists | 19:04 |
pedro3005 | If you are familiar with C and other languages, they're like arrays | 19:04 |
pedro3005 | but only MUCH better | 19:04 |
pedro3005 | A list is a set of elements | 19:04 |
pedro3005 | You can define them with the following syntax: | 19:05 |
pedro3005 | numbers = [1, 2, 3, 4, 5] | 19:05 |
pedro3005 | To get the nth element of a list, we do list[n-1]. That's because the first element is 0 | 19:06 |
pedro3005 | in this case | 19:06 |
pedro3005 | numbers[0] == 1 | 19:06 |
pedro3005 | and so forth | 19:06 |
pedro3005 | Questions? | 19:06 |
pedro3005 | Ok | 19:07 |
pedro3005 | To get the total number of elements in a list you can use the function len() | 19:07 |
pedro3005 | len(numbers) == 5 | 19:07 |
pedro3005 | Lists are a mutable type, meaning you can add and remove elements from it | 19:07 |
pedro3005 | you can use the method append() to add something to a list at the rightmost position | 19:08 |
pedro3005 | numbers.append(6) | 19:08 |
pedro3005 | now we have numbers = [1, 2, 3, 4, 5, 6] | 19:08 |
ClassBot | saji89 asked: can we make a list immutable? | 19:09 |
pedro3005 | Tuples are sort of like immutable lists | 19:09 |
pedro3005 | so if you want something immutable go ahead and use a tuple (I'll go over them shortly) | 19:09 |
pedro3005 | We also have the remove() method for lists | 19:10 |
pedro3005 | numbers.remove(6) will remove the first instance of 6 it finds | 19:10 |
pedro3005 | if we had, for instance, num = [6, 6, 6] and did num.remove(6), we'd end up with [6, 6] | 19:11 |
pedro3005 | But what if I had [1, 2, 3, 1, 4] and I wanted to remove exactly the last 1? | 19:12 |
pedro3005 | well, that is the 3rd element (remember, the first one is 0) | 19:12 |
pedro3005 | so we can do | 19:12 |
pedro3005 | del numbers[3] | 19:12 |
pedro3005 | Or, we can use the pop() method | 19:13 |
pedro3005 | list.pop(i) will remove the ith element and return it | 19:13 |
pedro3005 | pop(), without arguments, defaults to the last element | 19:14 |
pedro3005 | Any questions about that? | 19:14 |
ClassBot | Otto48 asked: Is there a push method too? | 19:16 |
pedro3005 | yes, the method append() | 19:16 |
pedro3005 | (see above) | 19:16 |
ClassBot | saji89 asked: list.pop(i) will remove the ith element and return it. Returns it, as in prints it? | 19:16 |
pedro3005 | Not exactly | 19:16 |
pedro3005 | for instance, consider this case | 19:16 |
pedro3005 | if numbers.pop() > 0: | 19:16 |
pedro3005 | print "the last element of numbers is bigger than zero" | 19:17 |
pedro3005 | but that wouldn't work with del :) | 19:17 |
pedro3005 | Does that explain it? | 19:17 |
pedro3005 | If you want to put an element at some exact position, you have insert() for that | 19:17 |
pedro3005 | list.insert(i, x) will insert x at position i | 19:18 |
ClassBot | HoellP asked: is pop how i would evaluate command line options? | 19:18 |
pedro3005 | No | 19:18 |
pedro3005 | if you want to see command line arguments, import the module sys and access sys.argv | 19:18 |
pedro3005 | For example | 19:19 |
pedro3005 | if we had numbers = [1, 2, 3] | 19:19 |
pedro3005 | we could do | 19:19 |
pedro3005 | numbers.insert(0, 0) | 19:19 |
pedro3005 | that would insert 0 at position 0 | 19:19 |
pedro3005 | But behold! | 19:20 |
pedro3005 | lists can hold elements of different types | 19:21 |
pedro3005 | (take that, C) | 19:21 |
pedro3005 | for instance | 19:21 |
pedro3005 | things = [1, "hi", 4.3] | 19:21 |
pedro3005 | here we have an int, a string and a float | 19:21 |
pedro3005 | Let's go over some more list methods and then go to slicing | 19:22 |
pedro3005 | We have: | 19:22 |
pedro3005 | list.extend(another_list) | 19:22 |
pedro3005 | this simply adds another list to a list | 19:22 |
pedro3005 | a = [1, 2] and b = [3, 4]. a.extend(b) = [1, 2, 3, 4] | 19:23 |
pedro3005 | this is different from a.append(b) | 19:23 |
pedro3005 | a.append(b) would result in | 19:23 |
pedro3005 | [1, 2, [3, 4]] | 19:23 |
pedro3005 | Yes, friends! Lists inside lists! | 19:23 |
pedro3005 | Say, we had, for instance | 19:26 |
pedro3005 | a = [[1, 2], [3, 4]] | 19:26 |
pedro3005 | len(a) == 2 | 19:26 |
pedro3005 | it has two elements, two lists | 19:26 |
pedro3005 | a[0] == [1, 2] | 19:26 |
pedro3005 | suppose we want the first element of a[0] | 19:26 |
pedro3005 | a[0][0] | 19:26 |
pedro3005 | That may look weird but it's perfectly fine | 19:27 |
pedro3005 | any questions? | 19:27 |
pedro3005 | We have some other methods | 19:28 |
pedro3005 | list.count(x) returns how many times x occurs in list | 19:28 |
pedro3005 | for instance | 19:28 |
pedro3005 | a = [1, 1, 2, 3] | 19:28 |
pedro3005 | a.count(1) == 2 | 19:28 |
pedro3005 | We also have the sort() method | 19:29 |
pedro3005 | this sorts a list in order | 19:29 |
pedro3005 | a = [3, 2, 4, 1] | 19:29 |
pedro3005 | a.sort() | 19:29 |
pedro3005 | a == [1, 2, 3, 4] | 19:29 |
pedro3005 | what if there are strings in the list? | 19:29 |
pedro3005 | a = ["b", "c", "a"] | 19:30 |
pedro3005 | a.sort() | 19:30 |
pedro3005 | a == ["a", "b", "c"] | 19:30 |
pedro3005 | Yes, it sorts alphabetically too :) | 19:30 |
ClassBot | Otto48 asked: How does sort() handle different types? EG a = [1, "w", [5, 6]]? | 19:30 |
pedro3005 | you 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 |
pedro3005 | You also have the function sorted(), which is similar to sort() | 19:33 |
pedro3005 | but sort() alters the list in place, and sorted() merely returns a sorted list | 19:34 |
pedro3005 | let me explain that further | 19:34 |
pedro3005 | a = [3, 1, 2] | 19:34 |
pedro3005 | a.sort() | 19:34 |
pedro3005 | now a == [1, 2, 3] | 19:34 |
pedro3005 | but what if we did: | 19:34 |
pedro3005 | a = [3, 1, 2] | 19:34 |
pedro3005 | sorted(a) | 19:34 |
pedro3005 | a STILL is [3, 1, 2] | 19:34 |
pedro3005 | because sorted() does not modify a | 19:34 |
pedro3005 | it only returns the sorted list | 19:34 |
pedro3005 | did you understand the difference? | 19:35 |
pedro3005 | great | 19:36 |
ClassBot | saji89 asked: simply sorted(a) throws away the result. Isn't it? So we use b=sorted(a). Right? | 19:36 |
pedro3005 | Perfect, saji89 ! | 19:37 |
pedro3005 | You're grasping the concept | 19:37 |
pedro3005 | that is correct | 19:37 |
pedro3005 | We also have the list method reverse(), which reverses a list in place | 19:37 |
pedro3005 | a = [1, 2, 3] | 19:37 |
pedro3005 | a.reverse() | 19:37 |
pedro3005 | a = [3, 2, 1] | 19:37 |
pedro3005 | but it does not SORT the list in reverse | 19:37 |
pedro3005 | it merely switches positions | 19:38 |
pedro3005 | to sort it in reverse, do: | 19:38 |
pedro3005 | a.sort(reverse = True) | 19:38 |
pedro3005 | Questions? | 19:38 |
ClassBot | Otto48 asked: does "a.sort(reverse = True)" hint at named parameters? | 19:39 |
pedro3005 | Yes | 19:39 |
pedro3005 | many functions called will have that | 19:39 |
pedro3005 | We'll be learning more about them in the session about functions | 19:40 |
pedro3005 | Let's have a quick look at for loops, and while if we manage | 19:40 |
pedro3005 | A for loop is a method of iterating over a list, or any iterable type | 19:40 |
pedro3005 | in this loop, we have a temporary variable that will be set to each of the values in the list, run the commands with, then changed | 19:41 |
pedro3005 | for instance | 19:41 |
pedro3005 | values = [1, 2, 3, 4, 5] | 19:41 |
pedro3005 | for val in values: | 19:41 |
pedro3005 | print val | 19:41 |
pedro3005 | Go ahead and try that | 19:42 |
pedro3005 | the example is fairly obvious | 19:42 |
pedro3005 | For each val inside values, print it | 19:42 |
=== stalcup is now known as vorian | ||
pedro3005 | We may, however, raise complexity | 19:43 |
pedro3005 | Say I would like to only print the even values | 19:43 |
pedro3005 | you may recall how to check if a number is even | 19:43 |
pedro3005 | we use % | 19:43 |
pedro3005 | so | 19:43 |
pedro3005 | for val in values: | 19:43 |
pedro3005 | if not val % 2: | 19:44 |
pedro3005 | print val | 19:44 |
pedro3005 | Any questions about that? | 19:44 |
pedro3005 | Alright, that's good | 19:45 |
pedro3005 | We have time for while loops then | 19:45 |
pedro3005 | A while loop is a loop that's run as long as a certain expression evaluates to True | 19:46 |
pedro3005 | for instance | 19:46 |
pedro3005 | x = 5 | 19:46 |
pedro3005 | while x > 0: | 19:46 |
pedro3005 | x -= 1 | 19:46 |
pedro3005 | print "x is now %s" % x | 19:46 |
pedro3005 | go ahead and try that | 19:47 |
pedro3005 | Questions? | 19:48 |
ClassBot | Otto48 asked: Does python have other loops? Does it have constructs similar to "Do ...Until" or "for (a=1;a<5;a+=)"? | 19:50 |
pedro3005 | Nope | 19:50 |
pedro3005 | python does not have a C-like for | 19:50 |
pedro3005 | no do while either, IIRC | 19:50 |
pedro3005 | But be not afraid | 19:51 |
pedro3005 | we can do it all with justs fors and whiles :) | 19:51 |
pedro3005 | Loops have two important keywords | 19:51 |
pedro3005 | break and continue | 19:51 |
pedro3005 | break stops a loop immediately | 19:51 |
pedro3005 | even_numbers = [2, 4, 6] | 19:52 |
pedro3005 | for x in even_numbers: | 19:52 |
pedro3005 | if x % 2: # x is ODD | 19:52 |
pedro3005 | break | 19:52 |
pedro3005 | we found a non-even number! | 19:52 |
pedro3005 | That stops the iteration completely | 19:52 |
pedro3005 | Each of the "loops" is called an iteration | 19:52 |
pedro3005 | and looping over is calling iterating | 19:53 |
pedro3005 | the keyword continue stops an iteration and moves on to the next | 19:53 |
pedro3005 | say we only want to print numbers greater than 0 | 19:53 |
pedro3005 | well, scratch that | 19:54 |
pedro3005 | hm.. | 19:54 |
pedro3005 | alright... my examples suck | 19:54 |
pedro3005 | but it's pretty simple | 19:54 |
pedro3005 | a = [1, 2, 3, 4, 5] | 19:54 |
pedro3005 | for x in a: | 19:54 |
pedro3005 | wait | 19:55 |
pedro3005 | <saji89> pedro3005: x%2 = something suld be there, right? | 19:55 |
pedro3005 | I forgot to mention this | 19:55 |
pedro3005 | no, saji89 , we can do without it | 19:55 |
pedro3005 | say | 19:55 |
pedro3005 | x = 0 | 19:55 |
pedro3005 | if x: blabla | 19:55 |
pedro3005 | it will not run | 19:55 |
pedro3005 | if x: is just a way of saying if x == True | 19:56 |
pedro3005 | things that are == False: 0, "", [], | 19:56 |
pedro3005 | empty things | 19:56 |
pedro3005 | Questions? | 19:57 |
pedro3005 | Good | 19:58 |
pedro3005 | in these last minutes, I'll introduce you guys to two great resources for beginners | 19:58 |
pedro3005 | The UF beginner programming challenges - http://ubuntuforums.org/showthread.php?p=5499486 | 19:58 |
pedro3005 | You should be able to do problem 1 | 19:58 |
* pedro3005 checks if stlsaint is around | 19:59 | |
pedro3005 | No? Good | 19:59 |
pedro3005 | We also have another resource | 19:59 |
pedro3005 | http://projecteuler.net :) | 19:59 |
pedro3005 | It is a series of math questions | 19:59 |
pedro3005 | if you like math, they are great | 20:00 |
pedro3005 | You should also be able to solve the first problem from project euler | 20:00 |
pedro3005 | and the second | 20:00 |
pedro3005 | but they are REALLY hard | 20:00 |
pedro3005 | so if you manage, kudos | 20:00 |
pedro3005 | and if you're not managing, don't worry | 20:00 |
pedro3005 | with your amount of knowledge you could solve 1, 2, 6 and 5 :P | 20: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 || | ||
pedro3005 | This concludes the session | 20:01 |
pedro3005 | Next class I will be going over how to solve the 1st UF beginner problem and the 1st Euler problem | 20:02 |
saji89 | pedro3005: COol.... :) | 20:02 |
pedro3005 | I encourage all to try them | 20:02 |
Mohan_chml | Well Done pedro3005 | 20:02 |
saji89 | pedro3005: I'll try the first ones in the two reaosurces. :) | 20:03 |
pedro3005 | saji89, I'll gladly take PMs | 20:04 |
pedro3005 | :) | 20:04 |
apperceptions | thanks pedro! | 20:04 |
pedro3005 | thank you apperceptions , hope you liked it | 20:04 |
saji89 | pedro3005: Cool. | 20:05 |
* etank is starting to think that the times on the classroom calendar are UTC and he is about 4 hours late | 22:15 | |
=== yofel_ is now known as yofel |
Generated by irclog2html.py 2.7 by Marius Gedminas - find it at mg.pov.lt!