First steps with Python3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.7 KiB

1 year ago
  1. # 1. Introduction to Python
  2. Python is a dynamic, interpreted (bytecode-compiled) language. There are no type declarations of variables, parameters, functions, or methods in source code. This makes the code short and flexible, and you lose the compile-time type checking of the source code. Python tracks the types of all values at runtime and flags code that does not make sense as it runs[^1].
  3. Actually, Python is widely used by engineers, scientists, statistics and data-science. Why should I learn Python?
  4. - Well, is a free and open source,
  5. - is versatile (multiparadign)
  6. - is portable and usable in other platforms
  7. - There are several modules to work with
  8. # Installing python
  9. **Note**: this guide only focus on GNU/Linux Ubuntu and Unix based (MacOS) operate systems (OS).
  10. ## Ubuntu
  11. To install the latest Python version, above 3.8, use the next commands in the terminal to let the *APT* package manager to install it:
  12. ```
  13. sudo apt update
  14. sudo apt install python3
  15. ```
  16. we are going to use the Integrated Development Environment (IDLE), thus, please run the next commands to install it:
  17. ```
  18. sudo apt-get update
  19. sudo apt-get install idle3
  20. ```
  21. ## Unix-based
  22. In the MacOS system there is recommended package manager call *Homebrew*, to install it run the next command in the terminal:
  23. ```
  24. /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  25. ```
  26. then, you will have access to the `brew` command to install and manage your packages. Next, you can install Python by using:
  27. ```
  28. brew install python3 python-tk
  29. ```
  30. these both packages allow us to run the IDLE for Python.
  31. # Testing Python
  32. ## Interactive
  33. ```
  34. $ python ## Run the Python interpreter
  35. Python 3.X.X (XXX, XXX XX XXXX, 03:41:42) [XXX] on XXX
  36. Type "help", "copyright", "credits" or "license" for more information.
  37. >>> a = 6 ## set a variable in this interpreter session
  38. >>> a ## entering an expression prints its value
  39. 6
  40. >>> a + 2
  41. 8
  42. >>> a = 'hi' ## 'a' can hold a string just as well
  43. >>> a
  44. 'hi'
  45. >>> len(a) ## call the len() function on a string
  46. 2
  47. >>> a + len(a) ## try something that doesn't work
  48. Traceback (most recent call last):
  49. File "", line 1, in
  50. TypeError: can only concatenate str (not "int") to str
  51. >>> a + str(len(a)) ## probably what you really wanted
  52. 'hi2'
  53. >>> foo ## try something else that doesn't work
  54. Traceback (most recent call last):
  55. File "", line 1, in
  56. NameError: name 'foo' is not defined
  57. >>> ^D ## type CTRL-d to exit (CTRL-z in Windows/DOS terminal)
  58. ```
  59. ## module files
  60. Python source files use the ".py" extension and are called "modules." With a Python module hello.py, the easiest way to run it is with the shell command "python hello.py Alice" which calls the Python interpreter to execute the code in hello.py, passing it the command line argument "Alice"
  61. ```
  62. #!/usr/bin/env python
  63. # import modules used here -- sys is a very standard one
  64. import sys
  65. # Gather our code in a main() function
  66. def main():
  67. print('Hello there', sys.argv[1])
  68. # Command line args are in sys.argv[1], sys.argv[2] ...
  69. # sys.argv[0] is the script name itself and can be ignored
  70. # Standard boilerplate to call the main() function to begin
  71. # the program.
  72. if __name__ == '__main__':
  73. main()
  74. ```
  75. to run the code use
  76. ```
  77. $ python hello.py Guido
  78. Hello there Guido
  79. $ ./hello.py Alice ## without needing 'python' first (Unix)
  80. Hello there Alice
  81. ```
  82. # `if`, `for` and plot
  83. ```
  84. x = int(input("Please enter an integer: "))
  85. Please enter an integer: 42
  86. >>> if x < 0:
  87. ... x = 0
  88. ... print('Negative changed to zero')
  89. ... elif x == 0:
  90. ... print('Zero')
  91. ... elif x == 1:
  92. ... print('Single')
  93. ... else:
  94. ... print('More')
  95. ...
  96. More
  97. ```
  98. # References
  99. [^1]: https://developers.google.com/edu/python/introduction