permalink | layout | language |
---|---|---|
/CodiMD-QAs-cleaned-toc/ |
base |
EN |
This is cleaned Q&A note taken in CodiMD for "Carpentry@UiO Plotting and Programming in Python workshop" on 2021 Mar 2-3.
- Please complete The Carpentries Post-workshop survey as well!
- Carpentry@UiO Plotting and Programming in Python Q&A note
- Table of Contents
- Day 1
- Day 2
Table of contents generated with markdown-toc
(Example of questions and answers)
- Q. How to run the code?
- A. Press
shift+Enter
- A. Press
- Q. What is the shortcut for inserting a new line?
- A. Press
a
to insert a line above,b
- A. Press
(End of example of questions and answers)
-
Q. Can you say a bit about the characters that are not on the keyboard?
- A: this depends on the operating system. What operating system are you using and what keys are you wondering about?
- A2: On Windows, the special character
~
is found betweenenter
andå
on a Norwegian keyboard.
-
Q. I do no access my desktop folder
-
A: On Mac and Linux's terminal,
cd ~/Desktop
for the absolute path or move through your folder usingpwd
to know where you are (your path) and usecd my_path_to_my_folder
-
A2: It depends on your OS. Are you on Linux, Mac or Windows? -> windows
- So your problem is to changed folder then? -> yes.
- Do you manage to open a terminal? -> yes I have anaconda open and my folder extracted in the desktop
-
A3: If you type the command
pwd
you will see which folder you are currently in. What does it say? ->'pwd' is not recognized as an internal or external command, operable program or batch file- Im in C:\
- A: I'm sorry. I was mixing windows and linux command (I'm on linux). Try
cd
which should be the equivalent for you. The command$ cd Desktop
should change your directory.
- A: I'm sorry. I was mixing windows and linux command (I'm on linux). Try
- Im in C:\
-
A4: For all the cd error, make sure that you are in your terminal not a in the python console
-
A5: If you're on a computer managed by UiO, try to run the command "cd M:\pc\Desktop". This only works in Anaconda PowerShell Prompt, not the other Anaconda Prompt.
- "cd M:/pc/Desktop" (regular slashes) works fine on git bash as well (just checked it)
-
A6: If you get an error on that, try to find where your desktop is: Open (double-click) the
python-novice-gapminder-data
folder, ordata
if you have that, and Click on the address bar. Copy that text and paste it after "cd". If you only haddata
, remove\data
off the end, to go to the desktop. Use that location, e.g. for me it is:cd C:\Users\(username)\Desktop\python-novice-gapminder-data
-
A7: One problem we had was with navigating to the desktop on a UiO laptop. It turns out that the apparent desktop area is actually under onedrive. I don't know exactly how that works because I'm not on a UiO laptop right now. Anyway the solution was to right-click-then-copy the unzipped python-... folder, then navigate to M:/pc/Desktop and paste it there. After that one can cd to the folder and move on. I guess the point for the instruction is that "copy the folder to your desktop" is not going to work for people with this setup because what they see as desktop is neither ~/Desktop nor M:\pc\Desktop
-
[NB! the comment above is moved from original place to more relevant placeoriginal place]
-
-
-
Q. running windows, get error messages for all the cd desktop, skrivebord command
- A: for now, it does not matter too much where you start Jupyter Lab, we can fix this in first exercise session
-
Q. My jupyter notebook keeps becoming unresponsive for a few moments. I think it may have to do with git refreshing or saving, but I am not sure. It gets rather annoying as it interrupts typing a lot. Any ideas?
- A: Is the kernel maybe dying?
- Doesn't really look like it. Python is idle during the freezes, and comes back online after a few moments, no problems there so far.
- A2: One thing to try is to save the notebook, and choose 'Kernel' from the menus and then 'Restart'
- trying that now... not much change, still freezes occasionally (with "git: refreshing" at the bottom)
- A3: next thing to try is saving, closing the JupyterLab, interrupting what is going in the terminal, and starting JupyterLab again
- trying that now... not much change here either. I'll just grin and bear it, it's OK.
- A: Is the kernel maybe dying?
-
Q: in the last exercise why does "2.0 * second" gives an error?
- A:
second
is a string, and you can do2 * second
(try this!) but Python will not allow you to multiply a float with a string
- A:
-
Q: Is there a difference between using ''or " " for defining a string?
- A: no, but you canot mix them!
-
Q: slicing in Python is confusing to me. E.g., atom_name[1:3] results in 2 letters only, the second and the third. Can you try and explain this so it makes more sense please? :-)
- A: maybe this image can help:
(from https://www.geeksforgeeks.org/python-list/). This kind of image was what helped me understand it originally. One way to think about this is that the index points to the boundary before the item and not the item itself. If you look at the image (index numbers above the string) you will see that between 1 and 3 there are indeed two letters.
- A: that is because slicing works from the first index, to, but not including the second index, so from 1 to 3 (not including). This also means the length of the slice is the difference between the indexes (3 - 1 = 2).
- A: By writing
print(atom_name[1:])
you would print from the second index (the latter a) to the end of the string
- A: maybe this image can help:
-
Q: What is the difference between single quotes and double quotes, please? I think, print() works with both.
- A: both are fine but you cannot mix them!
- A: if you want a single quote to appear in the output, use double quotes around it:
print("do 'it' like this")
(or the other way around)
- Q: How is the max function working?
- A: It chooses the largest value. If it is a letter, it chooses the highest letter value.
- A2: See here for a Stack-answer
- Q: I was a bit slow in the last exercise and can't get the Jigsaw Puzzle code to work. I get an error for the line there after I start using the
random.randrange
line saying I have an invalid literal for int() with base 10- A: Do you get an error on ___ = len(bases) ?
- I already get an error for ___ = random.randrange(n_bases)
- This indicates that n_bases is not an integer.
- You have to change the orders of the lines, so you can define n_bases as len(bases). Then you can use it in the line with random.randrange. Does that make sense?
- yes, thanks! with the n_bases = len line first i make the string to an integer and allow the second line to randomize that, correct?
- The first line gets the length of the string as an integer only, it doesn't change the string itself.
- Great, yes that makes sense! Thanks!
- The first line gets the length of the string as an integer only, it doesn't change the string itself.
- yes, thanks! with the n_bases = len line first i make the string to an integer and allow the second line to randomize that, correct?
- I already get an error for ___ = random.randrange(n_bases)
- A: Do you get an error on ___ = len(bases) ?
-
Q: How could I see the sidebar in Jupyter? I really don't see it. Just using !ls to check.
- A: (Sorry to write on top) Click on the Folder icon on the left. Does that work?
- I dont even see this icon. I started it in the terminal just with jupyter-notebook
- A2: Jupyter notebook is different from jupyter lab. Notebook doesn't have the sidebar. It's good you found an alternative, !ls.
- Just installed jupyter-lab in my conda env. Sorry. Thanks for the help :-)
- A: (Sorry to write on top) Click on the Folder icon on the left. Does that work?
-
Q: Is there any similar to ? in R or help in STATA for python?
- A: You can always use
help(something)
in python -- But Jupyter actually has ?something
, so that works! For example?print
.- Thanks!
- A: You can always use
-
Q: my pandas package won't load (import). I've tried to pip install it again, but it still doesn't load
- A: You are in Jupyter note, installed Anaconda3, right? I believe that mixing up pip and conda is causing a problem.. If you type in the jupyter note
conda list
, and if you cannot findpandas
, please write back here.- The pandas package is on the conda list
- A2: I just had the same problem. I installed in a terminal pandas into the conda env, and re-executed the cell with import pandas as pd and it worked.
- Thanks for this advice. This may work better?
- tried to conda install, the package is there, but it does not load in jupyter. the conda install also require some permissions to access the anaconda folder
- I cannot answer the permission question, but I did 'conda install pandas'. In worst case, save your notebook. Restart jupyter-lab after installing pandas? <- this is the way:)
- ok, i'll try to restart (but later:)
- I cannot answer the permission question, but I did 'conda install pandas'. In worst case, save your notebook. Restart jupyter-lab after installing pandas? <- this is the way:)
- tried to conda install, the package is there, but it does not load in jupyter. the conda install also require some permissions to access the anaconda folder
- Thanks for this advice. This may work better?
- A3: When you start Jupyter Lab, it doesn't use the current conda environment by default, it uses the
base
environment. Have you created a new conda environment?- no, how do I do that?
- It's easier if you haven't done that, so never mind.
- no, how do I do that?
- A: You are in Jupyter note, installed Anaconda3, right? I believe that mixing up pip and conda is causing a problem.. If you type in the jupyter note
-
Q: In Jupyter, there are no tab that helps guesssing the function or variable as far as I know. Is that correct?
- A: It only works for some things. If you have already defined a variable and run a cell, then it will auto-complete the name in another cell. It can also auto-complete file names. And functions. -> 👍
-
Q: What is the difference between Jupyter lab and jupyter notebook?
- A: Jupyter Lab is a newer project, made to replace jupyter notebook. Jupyter lab can have multiple noteboks open, and other items like scripts and a file browser. Jupyter notebook can only show a single notebook at a time. Both work similarly in the notebook, and you can use the one you prefer.
-
Q: how did you make this page, is there something similar to Rmarkdown in phyton ??
- A: The Carpentries organisation has set up a server that runs CodiMD: https://github.com/hackmdio/codimd . That's the one we're using.
- sound good! I was refering to this one http://swcarpentry.github.io/python-novice-gapminder/index.html
is the same code? Thanks
- This is set up using Github Pages. It requires some script to run and make html pages from the code that's in the github repository. It uses a "static site generator".
- thanks
- This is set up using Github Pages. It requires some script to run and make html pages from the code that's in the github repository. It uses a "static site generator".
- sound good! I was refering to this one http://swcarpentry.github.io/python-novice-gapminder/index.html
is the same code? Thanks
- A: The Carpentries organisation has set up a server that runs CodiMD: https://github.com/hackmdio/codimd . That's the one we're using.
-
Q: I wasnt able to run the help command as recommends in jupyter.
- -A: Have you initalised the variable or the library?
- I just include pandas and the other commands
import pandas as pd
help(pd)
- it works! thanks
- Nice! Thank you for the feedback!
- it works! thanks
- I just include pandas and the other commands
- -A: Have you initalised the variable or the library?
-
Advice to organizers: We had problems with Jupiter Lab and few had it installed and the solution was just to use a notebook or a python interpreter. A windows tutorial for installing Jupiter Lab with conda would be nice addition to the online material
- Thanks for the feedback. Did you see the setup instructions on the course website? https://uio-carpentry.github.io/2021-03-02-uio-python-online/ That just shows anaconda, but by default Anaconda will also install Jupyter Lab and all the necessary Python packages. We will keep in mind that using notebook directly, or python interpreter can help, thanks.
- The people on Windows could not get their Jupyter lab running and I do believe that they have problems running a conda environment or lack an installed Jupyter Lab. Jupyter notebook was installed with conda it seems. In group 9, only the helper had the Lab running.
- Excellent point. We should make sure the setup instructions include Jupyter Lab as a separate item. Some people may have older Anaconda without Jupyter lab. 👍
- I've added an issue on Github (carpentries/workshop-template#731). It's a bit complicated because there are two Python lessons in Software Carpentry, but only one set of instructions
- The people on Windows could not get their Jupyter lab running and I do believe that they have problems running a conda environment or lack an installed Jupyter Lab. Jupyter notebook was installed with conda it seems. In group 9, only the helper had the Lab running.
- Thanks for the feedback. Did you see the setup instructions on the course website? https://uio-carpentry.github.io/2021-03-02-uio-python-online/ That just shows anaconda, but by default Anaconda will also install Jupyter Lab and all the necessary Python packages. We will keep in mind that using notebook directly, or python interpreter can help, thanks.
What do you think is the best about Day1 of this workshop?
- That Jupyter notebook tells where the error is.+1
- Easy to follow exercises.
- Introduction to software and language
- Interesting discussion/questions
- Very well organized
- Well structured
- Great to learn something new from very competent instructors/helpers
- Small group thing
- Lex
- the link you provided in the end was very useful
- lots of content Good answers to questions -Friendly environment
Where do you want to go first when we can go abroad without any quarantine?
- Home country +9
- In a warm place +
- visit my parents +3
- To visit friends and family! +2
- Go hiking at Madagascar!
- Mars +1
- Visit friends and family in the UK, US
- Go to the Netherlands +1
- USA
- Lars Lerin's gallery in Sweden
Please write questions below
-
Q: Yes, I wonder something. Exercise:
element[-1:3]
This confused me. -1 is the last element.- A: If you try element[-1:3:-1] this is a valid index sequence so you get what you expect (i.e., elements -1, -2, and -3: 'mui'); there is no index between -1 and 3, so you get nothing.
- What does the 3 mean in your sequence
element[-1:3:-1]
, please?- 3 is the position right before the 3rd element (index-3 element, actually the 4th by regular counting); -1 is the position right before the last element; and the third argument (+1 by default) is the stepping ; reposting the graph here:
(from https://www.geeksforgeeks.org/python-list/)
- 3 is the position right before the 3rd element (index-3 element, actually the 4th by regular counting); -1 is the position right before the last element; and the third argument (+1 by default) is the stepping ; reposting the graph here:
- I was not aware I could mix negative and positive indices.
- Sure you can. Try element[-4:4] (it gives you 'h'). These indices are just positions in the list or string, it does not matter if they are specified with reference to the beginning or the end. Once you have two points, then they are stepped through by the third argument and then you get the content of the result. (I should probably add here that it gets tricky with negative step, so you should be careful; a lot of the gory details are shown for example here:
- element[-4:4] this is also confusing for me, because in both directions, h is the fourth letter. Upps, wrong, sorry.
- element is 'lithium' 0 is before 'l', 1 is before 'i', 2 is before 't', 3 is before 'h', 4 is before 'i' (hence after 'h'); -1 is before 'm', -2 is before 'u', -3 is before 'i', -4 is before 'h'. So [-4:4] (implied [-4:4:1]) means "From before 'h' to after 'h' with a step of 1" so you get 'h'
- Just to clarify, right of -1 it starts with 0, 1, 2 etc.?
- Sure you can. Try element[-4:4] (it gives you 'h'). These indices are just positions in the list or string, it does not matter if they are specified with reference to the beginning or the end. Once you have two points, then they are stepped through by the third argument and then you get the content of the result. (I should probably add here that it gets tricky with negative step, so you should be careful; a lot of the gory details are shown for example here:
- A better example is
element[-6:5]
, you take the elementlen(element)-6=1
which iselement[1]='i'
up toelement[5]='u'
.The final result iselement[-6:5]='ithi'
- For info, to take the reverse of an array use
::-1
see the difference betweenpressures[::-1]
andpressure[::1]
.element[-6:5]
--> Understood! I start with negative index and finish with a postive index.
- What does the 3 mean in your sequence
- A: If you try element[-1:3:-1] this is a valid index sequence so you get what you expect (i.e., elements -1, -2, and -3: 'mui'); there is no index between -1 and 3, so you get nothing.
-
Q: can one slice every second element?
- A: yes.
elements[start:stop:step]
. For exampleelements[1:5:2]
will take every second element - A2:
elements[::2]
also works for the entire string
- A: yes.
-
Q: can we get the figure in vector format
- A:
.eps
and.svg
work. Hope that helps. More complex answers here.
- A:
-
Q: I had in my room the question what happens when I import only matplotlib and not matplotlib.pyplot. This means I would import more functions? And I would need to write something like pyplot.plot is used as new plot command?
- A: yes, you would import more (not sure what, tough) and have to use
matplotlib.pyplot.plot(x,y)
instead ofplt.plot(x,y)
- Thank you!
- A: yes, you would import more (not sure what, tough) and have to use
Please write questions below
- Q: sorry, I missed it. Could you have several "else", or you need to use "elif", and have only one "else"?
- A: One else and multiple elif.
If you need more time, please write here!
10 more minutes please?
- Room 7 are doing fine. Some discussions and finally we did not finish Exe. 4.
- Room 11: We managed the first 3 exercises together and I lost one participant due to comuter problems.
-
The answer for the last "bonus" exercise is a bit difficult. Solution is
fewest = float('Inf')
!! But you could have usedfewest = 999999
. -
Q: What is total += number short for?
- A: It's the same as
total = total + number
- A2: Short hand for lazy programmers ;)
- A: It's the same as
-
Just a comment: I like to add a print statement into the loops to check what they are doing :-)
- that is a very good practice and helps with debugging your code!
- You can also use the Python Tutor to see what your code is doing: http://pythontutor.com
-
Q: Why not use else?
- A: It would give the same result, but I think "else" would be clearer. So, good idea!
-
Q: can you state the difference in functions and
-
the question stopped here
-
- I have a question- we do not manage to get the minimum value returned, instead we get a mix of characters and numbers
- Which breakout room are you in?
- 8
- Coming!
-
Q: Could you kindly explain again what the implication of "return None" is for the example we did with date_result = print_date(x , y , z)? From group (6)
- A: In python every function returns something, so in the example, the print_date function did not have a return in the body of the function there but when calling it, it would return None.
- Thank you :)
- A: In python every function returns something, so in the example, the print_date function did not have a return in the body of the function there but when calling it, it would return None.
-
Q: Was there a link to the content of the course that we can later refer to?
-
Q: Do you know if we can use codimd for our classes?
- A: You can use hackmd.io as exactly the same interface.
- A2: etherpad as well: https://etherpad.wikimedia.org for an 'open' one
- A3: overleaf for LaTeX
-
Q: How do you deal with matrices in Python?
- A: the Numpy package has functionality for working with matrices (and arrays/vectors)