├── .ipynb_checkpoints ├── CE103-Figures-checkpoint.drawio ├── CE103-Week00-Info-checkpoint.ipynb ├── CE103-Week01-checkpoint.ipynb ├── CE103-Week02-checkpoint.ipynb ├── CE103-Week03-checkpoint.ipynb ├── CE103-Week04-checkpoint.ipynb ├── CE103-Week05-checkpoint.ipynb ├── CE103-Week06-checkpoint.ipynb ├── CE103-Week07-checkpoint.ipynb ├── CE103-Week08-checkpoint.ipynb ├── CE103-Week09-checkpoint.ipynb ├── CE103-Week10-checkpoint.ipynb ├── CE103-Week11-checkpoint.ipynb ├── CE103-Week12-checkpoint.ipynb ├── CE103-Week13-checkpoint.ipynb ├── CE103-Week14-checkpoint.ipynb └── README-checkpoint.md ├── CE103-Week01-Introduction.ipynb ├── CE103-Week02-Strings.ipynb ├── CE103-Week03-Numpy.ipynb ├── CE103-Week04-Cond&Loops.ipynb ├── CE103-Week05-Matplotlib.ipynb ├── CE103-Week06-Functions.ipynb ├── Figures ├── .ipynb_checkpoints │ ├── algorithm2-checkpoint.png │ ├── grading-checkpoint.png │ ├── slope-int-checkpoint.png │ └── travel-time-checkpoint.png ├── 3d-array.png ├── GTU_LOGO_1200X768_JPG_EN.jpg ├── GTU_LOGO_1200X768_JPG_EN_Small.jpg ├── Z_TravelTimeCurve.mp4 ├── algorithm.png ├── algorithm2.png ├── anaconda.png ├── arithmetic-operators.png ├── array-sum.png ├── array.png ├── array_element_item.png ├── array_shape.jpg ├── array_vs_list.png ├── arrow.png ├── assignmentoparetor.png ├── blackwidow.jpg ├── captainamerica.jpg ├── cat.png ├── ce103_1.jpg ├── ce103_2.jpg ├── ce103_3.jpg ├── class.png ├── clue.png ├── colab.png ├── color.png ├── comparisonoperator.png ├── def_func.png ├── dot-product.png ├── earth-dist.png ├── elif-else-statement.png ├── encode-decode.png ├── encode.png ├── error-messages.pdf ├── figure-exp.png ├── for-loop.png ├── galata.png ├── grading.png ├── grading2.jpg ├── guido.png ├── homework10.png ├── hulk.jpg ├── hw6_screenshot.png ├── hw6_screenshot2.png ├── hw7-ibb.png ├── hw7.png ├── hw_11.png ├── hw_11_2.png ├── ibb.png ├── if-else-statement.png ├── if-statement.png ├── inclass_II.png ├── ironman.png ├── ironman2.jpg ├── jupyter.png ├── keywords.png ├── lin-reg-fig.png ├── lin-reg-formula.png ├── plaka.jpg ├── regression-curve.png ├── rossum.png ├── scipy-submodules.png ├── sensorrecord.png ├── slope-int.png ├── spoon.jpg ├── string-sequence.png ├── subplot.png ├── swinging_arm.png ├── travel-time.png ├── unicode.png └── while-loop.png ├── README.md └── Test_Input ├── Hello.txt ├── Kusma-GyadiBridge_Nepal.png ├── aaa.xlsx ├── civil_abb.txt ├── civil_abb_out.csv ├── civil_abb_out.json ├── class.json ├── eventlist.csv ├── eventlist.json ├── filename ├── merded.txt ├── merged ├── merged.txt ├── narrow.zip ├── sales.xlsx ├── test ├── tuik.csv ├── tuik.xlsx └── tuik_new.csv /.ipynb_checkpoints/CE103-Figures-checkpoint.drawio: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week00-Info-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "![](./Figures/GTU_LOGO_1200X768_JPG_EN_Small.jpg)\n", 8 | "\n", 9 | "## GEBZE TECHNICAL UNIVERSITY - DEPARTMENT OF CIVIL ENGINEERING\n", 10 | "\n", 11 | "### CE 103 Introduction to Computers and Programming \n", 12 | "##### _2019-2020 Spring_\n", 13 | "\n", 14 | "This lecture meant to serve as an introduction to the Python programming for Civil Engineering undergraduate students. It has enriched content with solved examples based on specific Civil Engineering problems.\n", 15 | "\n", 16 | "---\n", 17 | "### Instructor : \n", 18 | "##### Dr. Ahu Kömeç Mutlu --> _ahumutlu@gtu.edu.tr_\n", 19 | "\n", 20 | "---\n", 21 | "### Teaching Assistants :\n", 22 | "##### Fırat Bezir --> _fbezir@gtu.edu.tr_\n", 23 | "\n", 24 | "##### Sezgin Sarak --> _sezginsarak@gtu.edu.tr_\n", 25 | "\n", 26 | "---\n", 27 | "\n", 28 | "### Textbook and Resources :\n", 29 | "_Introduction to Python for Science and Engineering, David J. Pine, CRC Press, 2019._\n", 30 | "\n", 31 | "_https://docs.python.org/3/tutorial/index.html_\n", 32 | "\n", 33 | "_https://docs.python.org/3/reference/index.html_\n", 34 | "\n", 35 | "---\n" 36 | ] 37 | }, 38 | { 39 | "cell_type": "markdown", 40 | "metadata": {}, 41 | "source": [ 42 | "## Grading and Attendance\n", 43 | "![](./Figures/grading2.jpg)" 44 | ] 45 | }, 46 | { 47 | "cell_type": "markdown", 48 | "metadata": {}, 49 | "source": [ 50 | "## Syllabus\n", 51 | "-\n", 52 | "\n", 53 | "\n", 54 | "**Week 1 Theory :** Introduction to Python, launching Python, learning Github, Azure and Algorithm\n", 55 | "\n", 56 | "**Week 1 Lab :** Github - Jupyter settings \n", 57 | "\n", 58 | "---\n", 59 | "**Week 2 Theory :** Strings, Lists, Arrays and Dictionaries\n", 60 | "\n", 61 | "**Week 2 Lab :** _Coding applications_\n", 62 | "\n", 63 | "---\n", 64 | "**Week 3 Theory :** Numerical Routines NumPy\n", 65 | "\n", 66 | "**Wek 3 Lab :** _Coding applications_\n", 67 | "\n", 68 | "---\n", 69 | "**Week 4 Theory :** Conditionals ad Loops\n", 70 | "\n", 71 | "**Week 4 Lab :** _Coding applications_\n", 72 | "\n", 73 | "---\n", 74 | "**Week 5 Theory :** Plotting\n", 75 | "\n", 76 | "**Week 5 Lab :** _Coding applications_\n", 77 | "\n", 78 | "---\n", 79 | "**Week 6 Theory :** Functions\n", 80 | "\n", 81 | "**Week 6 Lab :** _Coding applications_\n", 82 | "\n", 83 | "---\n", 84 | "**Week 7 Theory :** Curve Fitting\n", 85 | "\n", 86 | "**Week 7 Lab :** _Coding applications_\n", 87 | "\n", 88 | "---\n", 89 | "**Week 8 Theory :** Numerical Routines SciPy\n", 90 | "\n", 91 | "**Week 8 Lab :** _Coding applications_\n", 92 | "\n", 93 | "---\n", 94 | "**Week 9 Theory :** File input and output\n", 95 | "\n", 96 | "**Week 9 Lab :** _Coding applications_\n", 97 | "\n", 98 | "---\n", 99 | "**Week 10 Theory :** Data manipulation and Analysis Pandas\n", 100 | "\n", 101 | "**Week 10 Lab :** _Coding applications_\n", 102 | "\n", 103 | "---\n", 104 | "**Week 11 Theory :** Getting Data from the Web\n", 105 | "\n", 106 | "**Week 11 Lab :** _Coding applications_\n", 107 | "\n", 108 | "---\n", 109 | "**Week 12 Theory :** Python Classes\n", 110 | "\n", 111 | "**Week 12 Lab :** _Coding applications_\n", 112 | "\n", 113 | "---\n", 114 | "**Week 13 Theory :** InClass Project - I \n", 115 | "\n", 116 | "**Week 13 Lab :** _Coding applications_\n", 117 | "\n", 118 | "---\n", 119 | "**Week 14 Theory :** InClass Project - II\n", 120 | "\n", 121 | "**Week 14 Lab :** _Coding applications_\n", 122 | "\n", 123 | "---\n", 124 | "**Week 15 Theory :** No class\n", 125 | "\n", 126 | "---\n", 127 | "**Week 16 Theory :** Final Exam\n", 128 | "\n", 129 | "---" 130 | ] 131 | }, 132 | { 133 | "cell_type": "code", 134 | "execution_count": null, 135 | "metadata": {}, 136 | "outputs": [], 137 | "source": [] 138 | } 139 | ], 140 | "metadata": { 141 | "kernelspec": { 142 | "display_name": "Python 3", 143 | "language": "python", 144 | "name": "python3" 145 | }, 146 | "language_info": { 147 | "codemirror_mode": { 148 | "name": "ipython", 149 | "version": 3 150 | }, 151 | "file_extension": ".py", 152 | "mimetype": "text/x-python", 153 | "name": "python", 154 | "nbconvert_exporter": "python", 155 | "pygments_lexer": "ipython3", 156 | "version": "3.6.8" 157 | } 158 | }, 159 | "nbformat": 4, 160 | "nbformat_minor": 4 161 | } 162 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week01-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Programming Algorithm\n", 17 | "- Launching Python, Variable, List\n", 18 | "- Interactive Python as a Calculator\n", 19 | "- Homework #1 assignment" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "---\n", 27 | "## Programming Algorithm\n", 28 | "\n", 29 | "Algorithms tell the programmers how to code the program. It is important editting to a strong algorithm than coding a program. Flowchart represents a diagram that graphically depicts the steps in a program.​An algorithm steps basically be like as below.\n", 30 | "\n", 31 | "![](./Figures/algorithm.png) \n", 32 | "\n", 33 | "Figure 1 : Flowchart diagram of basic programming seps. Ovals are terminal symbols, Parallelograms are input and output symbols, Rectangles are processing symbols, Symbols are connected by arrows that represent the flow of the program, Statements in a program execute in the order that they appear\n", 34 | "\n", 35 | "\n", 36 | "### Typical steps of programming:\n", 37 | "\n", 38 | ">Design the program​\n", 39 | "\n", 40 | ">>Write the code​\n", 41 | "\n", 42 | ">>>Correct syntax errors​\n", 43 | "\n", 44 | ">>>>Test the program​\n", 45 | "\n", 46 | ">>>>>Correct logic errors​" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "---\n", 54 | "## Introduction to Python\n", 55 | "\n", 56 | "Python is a open source general-purpose programming language. It is available across many platforms including Windows, Linux and Mac OS.\n", 57 | "\n", 58 | "The history of the Python programming language dates back to the late 1980s and its implementation was started in December 1989 by Guido van Rossum. \n", 59 | "\n", 60 | "The version of Python (i.e., Python 1.0) was introduced in 1991 and its eveluation has reached up to Version 3.x (till 2019).\n", 61 | "\n", 62 | "![](./Figures/guido.png)\n", 63 | "Figure 2 : Photograph of Guido van Rossum—Creator. (Image courtesy of Wikipedia.org)." 64 | ] 65 | }, 66 | { 67 | "cell_type": "markdown", 68 | "metadata": {}, 69 | "source": [ 70 | "---\n", 71 | "## Scientific Tools of Python\n", 72 | "\n", 73 | "The Python ecosystem consists of these core scientific tools which are essential for analysing and simulating complex systems, these packages are SciPy library,SciPy, NumPy, Sympy and Matplotlib. **SciPy** library is mainly used for numerical integration and optimization. **NumPy** provides N-dimensional array objects which can be used to perform linear algebra, Fourier transform and other mathematical operations. **SymPy** library is used to generate symbolic mathematics. **Matplotlib** is the oldest and most popular plotting library available for Python. These tools are usefull for solving scientific problems.\n" 74 | ] 75 | }, 76 | { 77 | "cell_type": "markdown", 78 | "metadata": {}, 79 | "source": [ 80 | "---\n", 81 | "## Launching Python \n", 82 | "\n" 83 | ] 84 | }, 85 | { 86 | "cell_type": "markdown", 87 | "metadata": {}, 88 | "source": [ 89 | "There are several interfaces that allows us to use Python interactively. IPython, jupyter notebooks, anaconda spyder are some of them. But in this lecture we will use notebook.azure.com.\n", 90 | "\n", 91 | "You do not have to install Python on to your computer, all you need is internet accesss. " 92 | ] 93 | }, 94 | { 95 | "cell_type": "markdown", 96 | "metadata": {}, 97 | "source": [ 98 | "---\n", 99 | "## Installing and Using Jupyter Notebook\n", 100 | "Jupyter Notebook is an open source web application that uses a browserbased interface and it is one of the many possible ways to interact with Python and the scientific libraries. \n", 101 | "\n", 102 | "You can :\n", 103 | "\n", 104 | "• create and share documents,\n", 105 | "\n", 106 | "• write and execute Python code,\n", 107 | "\n", 108 | "• include tables, figures, equation, visualizations, etc. with Jupyter which is fast processor in the scientific computing algorithms." 109 | ] 110 | }, 111 | { 112 | "cell_type": "markdown", 113 | "metadata": {}, 114 | "source": [ 115 | "---\n", 116 | "## Python Programming Fundamentals\n", 117 | "\n", 118 | "Lets discuss the fundamentals of Python Programming language that we need to know to start writing basic Python codes. These are identifiers, keywords, variables and operators.\n", 119 | "\n", 120 | "### Identifiers\n", 121 | "An identifier is a name given to a variable, function, class or module. Here are the rules that we should know to set an identifier.\n", 122 | "\n", 123 | "• Can begin with an alphabet (A – Z and a – z and _) and be a combination of lowercase or uppercase or digits.\n", 124 | "\n", 125 | "• Do not start with a digit but can used in anywhere else such as legend1 can allow but 1legend is invalid.\n", 126 | "\n", 127 | "• Keywords cannot be used as identifiers.\n", 128 | "\n", 129 | "• Do not use spaces and special symbols like !, @, #, $, % etc. as identifiers.\n", 130 | "\n", 131 | "• Identifier can be of any length.\n", 132 | "\n", 133 | "### Keywords\n", 134 | "\n", 135 | "Keywords are a list of reserved words that have predefined meaning. They are special vocabulary and should not be use as an identifier name which will cause an error.\n", 136 | "\n", 137 | "![](./Figures/keywords.png)\n", 138 | "\n", 139 | "### Variables\n", 140 | "\n", 141 | "Variable is a placeholder to hold any type of data.\n", 142 | "\n", 143 | "• Can consist of any number of letters, underscores and digits.\n", 144 | "\n", 145 | "• Should not start with a number.\n", 146 | "\n", 147 | "• Keywords are not assigned as variables\n", 148 | "\n", 149 | "• They are case-sensitive.\n", 150 | "\n", 151 | "***variable_name = expression***\n" 152 | ] 153 | }, 154 | { 155 | "cell_type": "code", 156 | "execution_count": null, 157 | "metadata": {}, 158 | "outputs": [], 159 | "source": [ 160 | "# set a variable name\n", 161 | "number = 3\n", 162 | "road = \"D100\"\n", 163 | "name = 'Python'\n", 164 | "century = \"hundred\"\n", 165 | "\n", 166 | "# print them on to the screen\n", 167 | "number, road, name, century" 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": {}, 173 | "source": [ 174 | "You can assign a single value to several variables simultaneously." 175 | ] 176 | }, 177 | { 178 | "cell_type": "code", 179 | "execution_count": null, 180 | "metadata": {}, 181 | "outputs": [], 182 | "source": [ 183 | "a = b = c = 4\n", 184 | "a, b, c" 185 | ] 186 | }, 187 | { 188 | "cell_type": "markdown", 189 | "metadata": {}, 190 | "source": [ 191 | "---\n", 192 | "### Operators\n", 193 | "\n", 194 | "Operators are symbols, such as +, –, =, >, and <. Python allows you to perform mathematical or logical operations to manipulate data values. and produce a result based on some rules.\n", 195 | "\n", 196 | "Python language supports a wide range of operators. \n", 197 | "\n", 198 | "\n", 199 | "#### 1. Arithmetic Operators\n", 200 | "\n", 201 | "\n", 202 | "Arithmetic operators are used to execute arithmetic operations such as addition, subtraction, division, multiplication etc.\n", 203 | "\n", 204 | "![](./Figures/arithmetic-operators.png) " 205 | ] 206 | }, 207 | { 208 | "cell_type": "code", 209 | "execution_count": null, 210 | "metadata": {}, 211 | "outputs": [], 212 | "source": [ 213 | " 4 + 5" 214 | ] 215 | }, 216 | { 217 | "cell_type": "code", 218 | "execution_count": null, 219 | "metadata": {}, 220 | "outputs": [], 221 | "source": [ 222 | "a = 3\n", 223 | "b = 5 \n", 224 | "c = 7\n", 225 | "\n", 226 | "a + b * c" 227 | ] 228 | }, 229 | { 230 | "cell_type": "code", 231 | "execution_count": null, 232 | "metadata": {}, 233 | "outputs": [], 234 | "source": [ 235 | "# find answer of the equations below\n", 236 | "\n", 237 | "apple = a ** b\n", 238 | "orange = a / b * c + 3\n", 239 | "d = apple // orange\n", 240 | "\n", 241 | "apple, orange, d" 242 | ] 243 | }, 244 | { 245 | "cell_type": "code", 246 | "execution_count": null, 247 | "metadata": {}, 248 | "outputs": [], 249 | "source": [ 250 | "# find the remainder of the \"apple/orange\"\n", 251 | "\n" 252 | ] 253 | }, 254 | { 255 | "cell_type": "markdown", 256 | "metadata": {}, 257 | "source": [ 258 | "#### 2. Assignment Operators\n", 259 | "\n", 260 | "Assignment operators are used for assigning the values to a variable.\n", 261 | "\n" 262 | ] 263 | }, 264 | { 265 | "cell_type": "code", 266 | "execution_count": null, 267 | "metadata": {}, 268 | "outputs": [], 269 | "source": [ 270 | "x = 5\n", 271 | "y = 8\n", 272 | "x + y" 273 | ] 274 | }, 275 | { 276 | "cell_type": "markdown", 277 | "metadata": {}, 278 | "source": [ 279 | "![](./Figures/assignmentoparetor.png)" 280 | ] 281 | }, 282 | { 283 | "cell_type": "code", 284 | "execution_count": null, 285 | "metadata": {}, 286 | "outputs": [], 287 | "source": [ 288 | "# run the operator below and recognize how x changec in each run\n", 289 | "x += 1\n", 290 | "x\n" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "#### 3. Comparison Operators\n", 298 | "\n", 299 | "They are used to compare the values of two operands and output is always a Boolean value, either True or False\n", 300 | "\n", 301 | "![](./Figures/comparisonoperator.png)" 302 | ] 303 | }, 304 | { 305 | "cell_type": "code", 306 | "execution_count": null, 307 | "metadata": {}, 308 | "outputs": [], 309 | "source": [ 310 | "5 == 7" 311 | ] 312 | }, 313 | { 314 | "cell_type": "markdown", 315 | "metadata": {}, 316 | "source": [ 317 | "---\n", 318 | "## Data Types\n", 319 | "\n", 320 | "Definition of the data type classifies and categorize data items. Its a way to understand which kind of value determines what operatoins can be performed. Different types of variable can do different things.\n", 321 | "\n", 322 | " - int (integers) (ex: 5,98,28829382983) \n", 323 | " - float (real numbers) (ex: 3.7)\n", 324 | " - bool (boolean values) (True or False)\n", 325 | " - NoneType (special) (None; has one value)\n", 326 | "\n", 327 | "You can check the type of your ariable by clling \"type\" command. Python allows you to convert your data from one type to another. " 328 | ] 329 | }, 330 | { 331 | "cell_type": "code", 332 | "execution_count": null, 333 | "metadata": {}, 334 | "outputs": [], 335 | "source": [ 336 | "type(3)" 337 | ] 338 | }, 339 | { 340 | "cell_type": "code", 341 | "execution_count": null, 342 | "metadata": {}, 343 | "outputs": [], 344 | "source": [ 345 | "type('hi')" 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": null, 351 | "metadata": {}, 352 | "outputs": [], 353 | "source": [ 354 | "# type conversion\n", 355 | "float(5)" 356 | ] 357 | }, 358 | { 359 | "cell_type": "code", 360 | "execution_count": null, 361 | "metadata": {}, 362 | "outputs": [], 363 | "source": [ 364 | "# type conversion\n", 365 | "int(3.1)" 366 | ] 367 | }, 368 | { 369 | "cell_type": "markdown", 370 | "metadata": {}, 371 | "source": [ 372 | "Lets go in detail with the basic data types below;\n", 373 | "\n", 374 | "### 1. Numbers\n", 375 | "\n", 376 | "Integers, floating point numbers and complex numbers are defined as int, float and complex class in Python. \n", 377 | "\n", 378 | "### 2. Boolean\n", 379 | "\n", 380 | "They are essential to set conditional statements which could be either True or False (treated as reserved words).\n", 381 | "\n", 382 | "### 3. String\n", 383 | "\n", 384 | "A string may consists letters, numbers, and other types of characters with spaces.\n", 385 | "\n", 386 | "You can check the type of your ariable by clling \"type\" command." 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": null, 392 | "metadata": {}, 393 | "outputs": [], 394 | "source": [ 395 | "x = [3.14]\n", 396 | "y = False\n", 397 | "\n", 398 | "print(type(x))\n", 399 | "print(type(y))" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "---\n", 407 | "### 1. Types of numbers\n", 408 | "\n", 409 | "There ara basically three different types of numbers that we can use in Python.\n", 410 | "\n", 411 | "#### 1.1. Integers\n", 412 | "They can be positive or negative numbers which are written without decimals." 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "(5 + 2) * 3 / 4" 422 | ] 423 | }, 424 | { 425 | "cell_type": "code", 426 | "execution_count": null, 427 | "metadata": {}, 428 | "outputs": [], 429 | "source": [ 430 | "13 // 2" 431 | ] 432 | }, 433 | { 434 | "cell_type": "code", 435 | "execution_count": null, 436 | "metadata": {}, 437 | "outputs": [], 438 | "source": [ 439 | "3828374649983 / 298374" 440 | ] 441 | }, 442 | { 443 | "cell_type": "markdown", 444 | "metadata": {}, 445 | "source": [ 446 | "#### 1.2. Floats\n", 447 | "\n", 448 | "They are rational numbers with fractional parts." 449 | ] 450 | }, 451 | { 452 | "cell_type": "code", 453 | "execution_count": null, 454 | "metadata": {}, 455 | "outputs": [], 456 | "source": [ 457 | "8.0 * 4" 458 | ] 459 | }, 460 | { 461 | "cell_type": "code", 462 | "execution_count": null, 463 | "metadata": {}, 464 | "outputs": [], 465 | "source": [ 466 | "19. / 3" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": null, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "13. // 2" 476 | ] 477 | }, 478 | { 479 | "cell_type": "code", 480 | "execution_count": null, 481 | "metadata": {}, 482 | "outputs": [], 483 | "source": [ 484 | "11. % 3" 485 | ] 486 | }, 487 | { 488 | "cell_type": "code", 489 | "execution_count": null, 490 | "metadata": {}, 491 | "outputs": [], 492 | "source": [ 493 | "3.98e32 * 222" 494 | ] 495 | }, 496 | { 497 | "cell_type": "markdown", 498 | "metadata": {}, 499 | "source": [ 500 | "#### 1.3. Complex Numbers\n", 501 | "\n", 502 | "Numbers with sum of real and imaginery parts. Use j instead of i which equals square root of -1. \n" 503 | ] 504 | }, 505 | { 506 | "cell_type": "code", 507 | "execution_count": null, 508 | "metadata": {}, 509 | "outputs": [], 510 | "source": [ 511 | "(3 + 2j) * (4 - 2j)" 512 | ] 513 | }, 514 | { 515 | "cell_type": "markdown", 516 | "metadata": {}, 517 | "source": [ 518 | "---\n", 519 | "## Homework #1\n", 520 | "\n", 521 | "1 - Design an algorithm for question 2 in Homework #1.\n", 522 | "\n", 523 | "2 - Write your first code that calculates your monthly budget for transportation. \n", 524 | " \n", 525 | " a ) Assume you are using Marmaray and paying 2.6 turkish liras for one way. \n", 526 | " \n", 527 | " b ) Assume that you are traveling by your own car and the gasoline costs 0.7 turkish liras per kilometer.\n", 528 | " \n", 529 | " c ) Calculate the differance between traveling by car and by Marmaray.\n", 530 | " \n", 531 | " PS: Please use your home location and GTU Civil Engineering Department building locations for distance calculations." 532 | ] 533 | }, 534 | { 535 | "cell_type": "markdown", 536 | "metadata": {}, 537 | "source": [ 538 | "---" 539 | ] 540 | }, 541 | { 542 | "cell_type": "code", 543 | "execution_count": null, 544 | "metadata": {}, 545 | "outputs": [], 546 | "source": [] 547 | } 548 | ], 549 | "metadata": { 550 | "kernelspec": { 551 | "display_name": "Python 3", 552 | "language": "python", 553 | "name": "python3" 554 | }, 555 | "language_info": { 556 | "codemirror_mode": { 557 | "name": "ipython", 558 | "version": 3 559 | }, 560 | "file_extension": ".py", 561 | "mimetype": "text/x-python", 562 | "name": "python", 563 | "nbconvert_exporter": "python", 564 | "pygments_lexer": "ipython3", 565 | "version": "3.6.8" 566 | } 567 | }, 568 | "nbformat": 4, 569 | "nbformat_minor": 4 570 | } 571 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week02-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Strings\n", 17 | "- Python Collections\n", 18 | " - Lists -----> Ordered and changeable collection which allows duplicate members.\n", 19 | " - Tuples -----> Ordered and unchangeble collection which allows duplicate members.\n", 20 | " - Dictionaries --> Unordered, changeable, indexed and single members collection.\n", 21 | " - Arrays \n", 22 | " " 23 | ] 24 | }, 25 | { 26 | "cell_type": "markdown", 27 | "metadata": {}, 28 | "source": [ 29 | "---" 30 | ] 31 | }, 32 | { 33 | "cell_type": "markdown", 34 | "metadata": {}, 35 | "source": [ 36 | "### Crash entry to programming world of PYTHON\n", 37 | "\n", 38 | "\n", 39 | "Basic reference tools for Python and Notebook\n", 40 | "\n", 41 | "If you prefer to use your local Python, type the coomand below in to your _terminal window_ to run your python code.\n", 42 | "\n", 43 | "$ **python** **example.py**\n", 44 | "\n", 45 | ",or, \n", 46 | "\n", 47 | "if you are using notebook interface,\n", 48 | "\n", 49 | "_**Shift + Enter**_ --> execute cell and jump to the next one\n", 50 | "\n", 51 | "_**Ctrl/Cmd + Enter**_ --> execute cell and don't jump to the next cell\n", 52 | "\n", 53 | "To write outputs in to the screen or plot figures;\n", 54 | "\n", 55 | "**print**(**\"Hello\"**)\n", 56 | "\n", 57 | "---" 58 | ] 59 | }, 60 | { 61 | "cell_type": "markdown", 62 | "metadata": {}, 63 | "source": [ 64 | "# Strings\n", 65 | "\n", 66 | "We have learned last week that Python (such as other programming languages) can be used as a calculater by using math symbols and also Numbers. \n", 67 | "\n", 68 | "#### !!! A string is a sequence of characters !!!\n", 69 | "\n", 70 | "A character is simply a symbol. Actually computers deal with numbers (binary), not with characters. All characters you may see on the screen are stored and manipulated as a combination of zeros (0) and ones (1), internally. Converting the character to a number is called encoding (ASCII, Unicode etc.) and the visa versa is decoding. \n", 71 | "\n", 72 | "A string is a series of Unicode characters." 73 | ] 74 | }, 75 | { 76 | "cell_type": "code", 77 | "execution_count": null, 78 | "metadata": {}, 79 | "outputs": [], 80 | "source": [ 81 | "# Python can also manipulate \"strings\". They can be adressed in single quotes ('..') or in double quotes (\" \").\n", 82 | "\n", 83 | "iron = \"Cause if we can't protect the Earth, \"\n", 84 | "man = 'you can be sure we will avenge it'\n", 85 | "\n", 86 | "ironman = iron + man\n", 87 | "print(iron + man)\n", 88 | "print(\"iron + man\")" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "![](./Figures/ironman2.jpg)" 96 | ] 97 | }, 98 | { 99 | "cell_type": "code", 100 | "execution_count": null, 101 | "metadata": {}, 102 | "outputs": [], 103 | "source": [ 104 | "# Backslash can be used to enclose for special characters in the output string \n", 105 | "\n", 106 | "'I\\'m Iron man'\n", 107 | "\"I am Iron man\"\n", 108 | "\n", 109 | "'Lines can be seperated \\nby backslash with symbol \"n\"'" 110 | ] 111 | }, 112 | { 113 | "cell_type": "code", 114 | "execution_count": null, 115 | "metadata": {}, 116 | "outputs": [], 117 | "source": [ 118 | "#set the rule variable\n", 119 | "rule = 'use backslash with \\n\"print\"\\n command'\n", 120 | "\n", 121 | "print(rule)" 122 | ] 123 | }, 124 | { 125 | "cell_type": "code", 126 | "execution_count": null, 127 | "metadata": {}, 128 | "outputs": [], 129 | "source": [ 130 | "print ('MyData\\notes\\CE103') # \\n means newline" 131 | ] 132 | }, 133 | { 134 | "cell_type": "code", 135 | "execution_count": null, 136 | "metadata": {}, 137 | "outputs": [], 138 | "source": [ 139 | "print (r'MyData\\notes\\CE103') # use r before the quote" 140 | ] 141 | }, 142 | { 143 | "cell_type": "markdown", 144 | "metadata": {}, 145 | "source": [ 146 | "---\n", 147 | "### How to access characters in string?\n", 148 | "\n", 149 | "Method of indexing can be used to access the characters in a string. Indexing could be think as pointing between characters. Index starts from 0. The left edge of the character numbered as O, increases from left to right and the right edge of the character starts with -1, e.g. -1 refers to the last character, -2 refers to the second last character and so on.\n", 150 | "\n", 151 | "![](./Figures/string-sequence.png)\n", 152 | "\n", 153 | "Accessing an index out of the range will cause an IndexError. We can also access individual characters using slicing. \n", 154 | "The index must be an integer, otherwhise, this will result into TypeError.\n" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "MyString = \"\"\"We are the champions my friends,\n", 164 | "we keep on fighting till the end.\n", 165 | "\"\"\"\n", 166 | "print(MyString)" 167 | ] 168 | }, 169 | { 170 | "cell_type": "code", 171 | "execution_count": null, 172 | "metadata": {}, 173 | "outputs": [], 174 | "source": [ 175 | "# Lets assign Steve Job's words as string to a variable\n", 176 | "str = \"Your time is limited, so don\\’t waste it living someone else\\’s life.\"\n", 177 | "\n", 178 | "print('first character = ', str[0]) #first character\n", 179 | "print('last character = ', str[-1]) #last character\n", 180 | "print('slicing 6nd to 9th character = ', str[6:9]) #slicing 6nd to 9th character\n", 181 | "print('slicing 25th to 37nd last character = ', str[25:-37]) #slicing 25th to 37nd last character" 182 | ] 183 | }, 184 | { 185 | "cell_type": "code", 186 | "execution_count": null, 187 | "metadata": {}, 188 | "outputs": [], 189 | "source": [ 190 | "str = 'abcdefg' \n", 191 | "print(str[::2])\n", 192 | "print(str[1::2])\n", 193 | "print(str[::-1])" 194 | ] 195 | }, 196 | { 197 | "cell_type": "code", 198 | "execution_count": null, 199 | "metadata": {}, 200 | "outputs": [], 201 | "source": [ 202 | "st = 'abcdefg' \n", 203 | "st[8] # index must be in range error \n", 204 | "st[1.5] # index must be an integer error" 205 | ] 206 | }, 207 | { 208 | "cell_type": "markdown", 209 | "metadata": {}, 210 | "source": [ 211 | "---\n", 212 | "Elements of a string cannot be changed once it has been assigned. We can assign different strings to the same variable name." 213 | ] 214 | }, 215 | { 216 | "cell_type": "code", 217 | "execution_count": null, 218 | "metadata": {}, 219 | "outputs": [], 220 | "source": [ 221 | "str = 'hulk'\n", 222 | "print(str)" 223 | ] 224 | }, 225 | { 226 | "cell_type": "markdown", 227 | "metadata": {}, 228 | "source": [ 229 | "![](./Figures/hulk.jpg)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": null, 235 | "metadata": {}, 236 | "outputs": [], 237 | "source": [ 238 | "str = '2,4,6'\n", 239 | "print(str)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "---\n", 247 | "String is one of the most used datatypes in Python with its operational features.\n", 248 | "\n", 249 | "A string can be a join of two or more strings which is called concantenation. ' + ' operator can be used joining while ' * ' operator can be used to repeat the string with a given numbers. \n" 250 | ] 251 | }, 252 | { 253 | "cell_type": "code", 254 | "execution_count": null, 255 | "metadata": {}, 256 | "outputs": [], 257 | "source": [ 258 | "str1 = 'to be, '\n", 259 | "str2 ='or, not '\n", 260 | "\n", 261 | "print( str1 + str2 + str1)\n", 262 | "print(str1 * 3,\"that\\'s the problem\")" 263 | ] 264 | }, 265 | { 266 | "cell_type": "markdown", 267 | "metadata": {}, 268 | "source": [ 269 | "---\n", 270 | "The built-in functions such as enumarate(), len() etc. and numerous methods such as lower(), upper(), join(), split(), find(), replace() etc. are very usefull for the work with sequence as well as works with string.\n" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [ 279 | "str = 'winter is coming'\n", 280 | "\n", 281 | "list_enumerate = list(enumerate(str)) #contains all items as pairs\n", 282 | "print(list_enumerate) \n", 283 | "print(len(str)) #count characters" 284 | ] 285 | }, 286 | { 287 | "cell_type": "code", 288 | "execution_count": null, 289 | "metadata": {}, 290 | "outputs": [], 291 | "source": [ 292 | "str2 = 'sUmmEr is ENDing'\n", 293 | "print(str2.upper())\n", 294 | "str2.lower()\n", 295 | "print(str2)\n", 296 | "str2.split()\n", 297 | "str2.find('is')\n", 298 | "str2.replace('is','is not')" 299 | ] 300 | }, 301 | { 302 | "cell_type": "code", 303 | "execution_count": null, 304 | "metadata": {}, 305 | "outputs": [], 306 | "source": [ 307 | "p = \"Why, Mr. Anderson? Why keep fighting?\"\n", 308 | "print(p.find('Why')) \n", 309 | "p.rfind('Why')" 310 | ] 311 | }, 312 | { 313 | "cell_type": "code", 314 | "execution_count": null, 315 | "metadata": {}, 316 | "outputs": [], 317 | "source": [ 318 | "p = \"Why, Mr. Anderson? Why keep fighting?\"\n", 319 | "\n", 320 | "print('Why keep fighting?'.replace('fighting','laughing'))\n", 321 | "print(p.replace('Why','please',2)) " 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "execution_count": null, 327 | "metadata": {}, 328 | "outputs": [], 329 | "source": [ 330 | "print(\"Why, Mr. Anderson? Why keep fighting?\".count('h'))" 331 | ] 332 | }, 333 | { 334 | "cell_type": "markdown", 335 | "metadata": {}, 336 | "source": [ 337 | "---\n", 338 | "# Lists\n", 339 | "\n", 340 | "Python offers a sequence of datatypes in order to group values. Lists are one of the most versatile and frequently used items. They created by comma-separated values (elements) between square brackets [ ] in any number and any different type such as integer, float, string etc." 341 | ] 342 | }, 343 | { 344 | "cell_type": "code", 345 | "execution_count": null, 346 | "metadata": {}, 347 | "outputs": [], 348 | "source": [ 349 | "lst = [] # create empty list" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "lst = [1, 2, 3]# list of integers" 359 | ] 360 | }, 361 | { 362 | "cell_type": "code", 363 | "execution_count": null, 364 | "metadata": {}, 365 | "outputs": [], 366 | "source": [ 367 | "lst = [3.14, \"pi\", (22/7)] # list of different datatypes" 368 | ] 369 | }, 370 | { 371 | "cell_type": "code", 372 | "execution_count": null, 373 | "metadata": {}, 374 | "outputs": [], 375 | "source": [ 376 | "lst = [\"week\",[2,4,6],['tuesday']] # nested list" 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "---\n", 384 | "#### Access items of a list\n", 385 | "\n", 386 | " ##### _***1.Index Operator***_\n", 387 | "\n", 388 | "An index operator ([ ]) can be used to access the elements of a list. Index starts with 0 and must be an integer. If you access the index number out of the range it will cause IndexError; if you use float or other types, it will cause TypeError will occur.\n" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "mylst = ['a','v','e','n','g','e','r','s']\n", 398 | "\n", 399 | "print(mylst[0])\n", 400 | "print(mylst[5])\n", 401 | "print(mylst[-5])" 402 | ] 403 | }, 404 | { 405 | "cell_type": "code", 406 | "execution_count": null, 407 | "metadata": {}, 408 | "outputs": [], 409 | "source": [ 410 | "mylst[1.0] #TypeError ! only integer can be used " 411 | ] 412 | }, 413 | { 414 | "cell_type": "code", 415 | "execution_count": null, 416 | "metadata": {}, 417 | "outputs": [], 418 | "source": [ 419 | "nested = [\"Squares\", [4,9,16,25]] # Nested List\n", 420 | "print(nested[0][5]) \n", 421 | "print(nested[1][2])" 422 | ] 423 | }, 424 | { 425 | "cell_type": "markdown", 426 | "metadata": {}, 427 | "source": [ 428 | "##### _***2.Slice Operator***_\n", 429 | "\n", 430 | "We can also access the elements of a list by slicing operator.\n", 431 | "\n", 432 | "![](./Figures/captainamerica.jpg)" 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": null, 438 | "metadata": {}, 439 | "outputs": [], 440 | "source": [ 441 | "slc = ['C','a','p','t','a','i','n','A','m','e','r','i','c','a']\n", 442 | "\n", 443 | "print(slc[:]) # elements beginning to end\n", 444 | "print(slc[5:11]) # elements 5th to 11th\n", 445 | "print(slc[:-5]) # elements beginning to 4th\n", 446 | "print(slc[4:]) # elements 5th to end\n" 447 | ] 448 | }, 449 | { 450 | "cell_type": "markdown", 451 | "metadata": {}, 452 | "source": [ 453 | "As differs from strings, the elements of lists can be changed which means they are mutable." 454 | ] 455 | }, 456 | { 457 | "cell_type": "code", 458 | "execution_count": null, 459 | "metadata": {}, 460 | "outputs": [], 461 | "source": [ 462 | "sqr = [4, 9, 16, 25, 36, 49]\n", 463 | "\n", 464 | "sqr[0] = ('2x2') # change the 1st item \n", 465 | "sqr[1:-3] = ['3x3', '4x4'] # change 2nd to 3rd items " 466 | ] 467 | }, 468 | { 469 | "cell_type": "code", 470 | "execution_count": null, 471 | "metadata": {}, 472 | "outputs": [], 473 | "source": [ 474 | "sqr.append(7) # adds new item to the end of the list\n", 475 | "sqr.extend([100, 121, 144])\n", 476 | "sqr.insert(1,3)" 477 | ] 478 | }, 479 | { 480 | "cell_type": "code", 481 | "execution_count": null, 482 | "metadata": {}, 483 | "outputs": [], 484 | "source": [ 485 | "# define sqr list again\n", 486 | "sqr = [4, 9, 16, 25, 36, 49]\n", 487 | "\n", 488 | "sqr[3:3] = [64, 91]\n", 489 | "print(sqr.index(49))\n", 490 | "print(sqr.count(4))\n", 491 | "sqr.sort()\n", 492 | "sqr.reverse()\n", 493 | "\n", 494 | "print(sqr)" 495 | ] 496 | }, 497 | { 498 | "cell_type": "code", 499 | "execution_count": null, 500 | "metadata": {}, 501 | "outputs": [], 502 | "source": [ 503 | "# define sqr list again to see the commands for delete values from a list\n", 504 | "sqr = [4, 9, 16, 25, 36, 49]\n", 505 | "\n", 506 | "del sqr[3] # delete one specific item\n", 507 | "del sqr[4:9] # delete multiple items from the list\n", 508 | "del sqr # delete entire list" 509 | ] 510 | }, 511 | { 512 | "cell_type": "code", 513 | "execution_count": null, 514 | "metadata": {}, 515 | "outputs": [], 516 | "source": [ 517 | "sqr = [4, 9, 16, 25, 36, 49]\n", 518 | "sqr.remove(16) # remove the given item\n", 519 | "sqr.clear()\n", 520 | "sqr[2:3] = []" 521 | ] 522 | }, 523 | { 524 | "cell_type": "code", 525 | "execution_count": null, 526 | "metadata": {}, 527 | "outputs": [], 528 | "source": [ 529 | "# if index is not specified in pop() it removes and returns the last item\n", 530 | "sqr = [4, 9, 16, 25, 36, 49]\n", 531 | "\n", 532 | "print(sqr.pop()) # provides first in, last out data structure\n", 533 | "print(sqr.pop(3)) # remove an item at the given index" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": null, 539 | "metadata": {}, 540 | "outputs": [], 541 | "source": [ 542 | "# We can also use ' + ' operator to join two lists. \n", 543 | "# This is also called concatenation as mentoned above.\n", 544 | "print(sqr + [169])\n", 545 | "\n", 546 | "#The * operator repeats a list for the given number of times.\n", 547 | "print(sqr * 2)\n" 548 | ] 549 | }, 550 | { 551 | "cell_type": "code", 552 | "execution_count": null, 553 | "metadata": {}, 554 | "outputs": [], 555 | "source": [ 556 | "# Lets check if an item is in the list or not\n", 557 | "aaa = ['n','e','o']\n", 558 | "print('n' in aaa)\n", 559 | "print('a' in aaa)\n", 560 | "print('k' not in aaa)" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "---\n", 568 | "## Tuple\n", 569 | "\n", 570 | "A tuple is a collection which is similar to a list. The elements of a tuple is ordered and unchangeable while, in a list, elements can be changed. A Tuple is written with round brackets \"( )\", separated by commas. A tuple item may be any type (float, string, integer, list etc.) and have any number.\n" 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "tpl = () # create empty tuple\n", 580 | "tpl = (1, 2, 3) # having integers\n", 581 | "tpl = (3.14, \"pi\", (22/7)) # tuple of different datatypes\n", 582 | "\n", 583 | "tpl = (\"week\",[2,4,6],(1,2,3)) # nested tuple" 584 | ] 585 | }, 586 | { 587 | "cell_type": "code", 588 | "execution_count": null, 589 | "metadata": {}, 590 | "outputs": [], 591 | "source": [ 592 | "#A tuple created without parenthesis called tuple packing.\n", 593 | "\n", 594 | "tpl2 = 3.14, (22/7), \"pi\"" 595 | ] 596 | }, 597 | { 598 | "cell_type": "code", 599 | "execution_count": null, 600 | "metadata": {}, 601 | "outputs": [], 602 | "source": [ 603 | "a, b, c = tpl2 # unpacking tuple\n", 604 | "\n", 605 | "print(a) \n", 606 | "print(b) \n", 607 | "print(c) " 608 | ] 609 | }, 610 | { 611 | "cell_type": "markdown", 612 | "metadata": {}, 613 | "source": [ 614 | "#### Access items of a tuple\n", 615 | "\n", 616 | "##### _***1.Index Operator***_\n", 617 | "\n", 618 | "An index operator ([ ]) can be used to access the elements of a list. Index starts with 0 and must be an integer. If you access the index number out of the range it will cause IndexError; if you use float or other types, it will cause TypeError will occur." 619 | ] 620 | }, 621 | { 622 | "cell_type": "code", 623 | "execution_count": null, 624 | "metadata": {}, 625 | "outputs": [], 626 | "source": [ 627 | "mytpl = ('E','n','g','i','n','e','e','r','i','n','g')\n", 628 | "\n", 629 | "print(mytpl[0])\n", 630 | "print(mytpl[5])\n", 631 | "print(mytpl[-5])" 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "execution_count": null, 637 | "metadata": {}, 638 | "outputs": [], 639 | "source": [ 640 | "mytpl[1.0] #TypeError ! only integer can be used " 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": null, 646 | "metadata": {}, 647 | "outputs": [], 648 | "source": [ 649 | "nested = (\"Squares\", [4,9,16,25]) # Nested List\n", 650 | "print(nested[0][5]) \n", 651 | "print(nested[1][2])" 652 | ] 653 | }, 654 | { 655 | "cell_type": "markdown", 656 | "metadata": {}, 657 | "source": [ 658 | "##### _***2.Slice Operator***_\n", 659 | "\n", 660 | "We can also access the elemts of a list by slicing operator.\n", 661 | "\n", 662 | "![](./Figures/blackwidow.jpg)\n" 663 | ] 664 | }, 665 | { 666 | "cell_type": "code", 667 | "execution_count": null, 668 | "metadata": {}, 669 | "outputs": [], 670 | "source": [ 671 | "mytpl = ('b','l','a','c','k','w','i','d','o','w')\n", 672 | "\n", 673 | "print(mytpl[:]) # elements beginning to end\n", 674 | "print(mytpl[5:11]) # elements 5th to 11th\n", 675 | "print(mytpl[:-5]) # elements beginning to 4th\n", 676 | "print(mytpl[4:]) # elements 5th to end \n" 677 | ] 678 | }, 679 | { 680 | "cell_type": "code", 681 | "execution_count": null, 682 | "metadata": {}, 683 | "outputs": [], 684 | "source": [ 685 | "# A tuple can be assigned to a different item called reassignment.\n", 686 | "mytpl = (4, 9, 16, [64, 81])" 687 | ] 688 | }, 689 | { 690 | "cell_type": "code", 691 | "execution_count": null, 692 | "metadata": {}, 693 | "outputs": [], 694 | "source": [ 695 | "mytpl[1] = 5 # TypeError : 'tuple' object does not support item assignment" 696 | ] 697 | }, 698 | { 699 | "cell_type": "code", 700 | "execution_count": null, 701 | "metadata": {}, 702 | "outputs": [], 703 | "source": [ 704 | "mytpl[2][0] = 1 # mutable element item can be changed" 705 | ] 706 | }, 707 | { 708 | "cell_type": "code", 709 | "execution_count": null, 710 | "metadata": {}, 711 | "outputs": [], 712 | "source": [ 713 | "mytpl = ('s','p','i','d','e','r') # reassigned" 714 | ] 715 | }, 716 | { 717 | "cell_type": "code", 718 | "execution_count": null, 719 | "metadata": {}, 720 | "outputs": [], 721 | "source": [ 722 | "# We can also use ' + ' operator to join two lists. \n", 723 | "# This is also called concatenation as mentoned above.\n", 724 | "print((1, 2, 3) + (4, 5, 6))\n", 725 | "\n", 726 | "# The * operator repeats a list for the given number of times.\n", 727 | "print('money' * 3)" 728 | ] 729 | }, 730 | { 731 | "cell_type": "markdown", 732 | "metadata": {}, 733 | "source": [ 734 | "---\n", 735 | "## Dictionary\n", 736 | "\n", 737 | "A dictionary in the Python is an unordered, changable and indexed collection of items, which are written by curly brackets {}. A dicionary is a composition of key:value pairs. Each key has to retrieve a specific value.\n" 738 | ] 739 | }, 740 | { 741 | "cell_type": "code", 742 | "execution_count": null, 743 | "metadata": {}, 744 | "outputs": [], 745 | "source": [ 746 | "# Create and print a dictionary:\n", 747 | "\n", 748 | "earthquake = {\n", 749 | " \"location\": \"Elazig\",\n", 750 | " \"magnitude\": \"6.8\",\n", 751 | " \"year\": 2020,\n", 752 | " \"month\":\"January\" \n", 753 | "}\n", 754 | "print(earthquake)" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": null, 760 | "metadata": {}, 761 | "outputs": [], 762 | "source": [ 763 | "# to access the items of the dictionary refer its key name.\n", 764 | "\n", 765 | "x = earthquake[\"location\"]\n", 766 | "#,or,\n", 767 | "x = earthquake.get(\"year\")" 768 | ] 769 | }, 770 | { 771 | "cell_type": "code", 772 | "execution_count": null, 773 | "metadata": {}, 774 | "outputs": [], 775 | "source": [ 776 | "#change values of specific item;\n", 777 | "earthquake[\"location\"] = \"Sivrice-Elazig\"" 778 | ] 779 | }, 780 | { 781 | "cell_type": "code", 782 | "execution_count": null, 783 | "metadata": {}, 784 | "outputs": [], 785 | "source": [ 786 | "print(len(earthquake)) # gives number of the items in dictionary" 787 | ] 788 | }, 789 | { 790 | "cell_type": "code", 791 | "execution_count": null, 792 | "metadata": {}, 793 | "outputs": [], 794 | "source": [ 795 | "earthquake[\"depth\"] = 16 # adding new item to the dictionary" 796 | ] 797 | }, 798 | { 799 | "cell_type": "code", 800 | "execution_count": null, 801 | "metadata": {}, 802 | "outputs": [], 803 | "source": [ 804 | "# Ways to remove items from the dictionary;\n", 805 | "\n", 806 | "earthquake.pop(\"location\") # removes the item with the specified key name\n", 807 | "earthquake.popitem() # removes the last inserted item\n", 808 | "del earthquake[\"month\"] # removes the item with the specified key name\n", 809 | "newdict = earthquake.copy() # copies the dictionary\n", 810 | "earthquake.clear() # empties the dictionar" 811 | ] 812 | }, 813 | { 814 | "cell_type": "markdown", 815 | "metadata": {}, 816 | "source": [ 817 | "---\n", 818 | "## Arrays\n", 819 | "\n", 820 | "We defined a dictionary for an earthquake with its specified features or items. Lets assume that we have thousands of earthquakes listed with different values (for example; magnitude ranges between 3 to 7, depth changes between 5km to 20 km. etc.) To access the specified eartquke, or group of earthquakes we can use arrays in Python.\n", 821 | "\n", 822 | "Arrays are used to store multiple values in a single variable. Lists can do, too. List can also a data structure that offers a sequence of dataytpes while arrays can only stores values of same datatype." 823 | ] 824 | }, 825 | { 826 | "cell_type": "code", 827 | "execution_count": null, 828 | "metadata": {}, 829 | "outputs": [], 830 | "source": [ 831 | "#Lets assign different tyoes of magnitudes to a key;\n", 832 | "\n", 833 | "M = [\"Ml\", \"Mw\", \"Mb\"] # single key can hold multiple variable.\n", 834 | "m1 = \"Ml\"\n", 835 | "m2 = \"Mw\"\n", 836 | "m3 = \"Mb\"\n", 837 | "\n", 838 | "a = M[0] # value of the fisrt array item\n", 839 | "M[0] = \"Md\" # reassign first array item\n", 840 | "print(M)" 841 | ] 842 | }, 843 | { 844 | "cell_type": "code", 845 | "execution_count": null, 846 | "metadata": {}, 847 | "outputs": [], 848 | "source": [ 849 | "a = len(M) # number of elements in an array\n", 850 | "print(a)" 851 | ] 852 | }, 853 | { 854 | "cell_type": "code", 855 | "execution_count": null, 856 | "metadata": {}, 857 | "outputs": [], 858 | "source": [ 859 | "M.append('Ml') # add an element to an array" 860 | ] 861 | }, 862 | { 863 | "cell_type": "code", 864 | "execution_count": null, 865 | "metadata": {}, 866 | "outputs": [], 867 | "source": [ 868 | "M.pop(1) # remove an element from the array" 869 | ] 870 | }, 871 | { 872 | "cell_type": "code", 873 | "execution_count": null, 874 | "metadata": {}, 875 | "outputs": [], 876 | "source": [ 877 | "M.remove('Mb') # remove the element that has the value \"Mb\"" 878 | ] 879 | }, 880 | { 881 | "cell_type": "markdown", 882 | "metadata": {}, 883 | "source": [ 884 | "---" 885 | ] 886 | }, 887 | { 888 | "cell_type": "markdown", 889 | "metadata": {}, 890 | "source": [ 891 | "## Homework #2\n", 892 | "\n", 893 | "\n", 894 | "Please write a code that calculates your weighted grade average which belongs to 2019/2020 fall semester (Define two lists, one for grade and one for credits). Assume that your GPA increases 5% at each semester and calculate your graduation credit. \n", 895 | "\n", 896 | "\n", 897 | "\n", 898 | "PS: Do not forget to upload your pdf files to http://derskutusu.gtu.edu.tr/\n", 899 | "\n", 900 | "---" 901 | ] 902 | } 903 | ], 904 | "metadata": { 905 | "kernelspec": { 906 | "display_name": "Python 3", 907 | "language": "python", 908 | "name": "python3" 909 | }, 910 | "language_info": { 911 | "codemirror_mode": { 912 | "name": "ipython", 913 | "version": 3 914 | }, 915 | "file_extension": ".py", 916 | "mimetype": "text/x-python", 917 | "name": "python", 918 | "nbconvert_exporter": "python", 919 | "pygments_lexer": "ipython3", 920 | "version": "3.6.8" 921 | } 922 | }, 923 | "nbformat": 4, 924 | "nbformat_minor": 4 925 | } 926 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week03-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Numerical Routines **_NumPy_**\n", 17 | " - Array\n", 18 | " - Multidimensional Arrays\n", 19 | " - Matrix manipulations" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "#### List VS Array\n", 27 | "\n", 28 | "We learned about both lists and arrays last week, but, what is the difference between them? When to use a Python array or a list? \n", 29 | "\n", 30 | "Python has lots of data structures with different functions and features. Lists, tuples, dictionaries are some of the built-in data structures that you don't have to import. But array is an additional data structure that has to be imported as a module or an extra package. To use an array, it has to be imported from the **_NumPy package_**.\n", 31 | "\n", 32 | "![](./Figures/array_vs_list.png)\n", 33 | "\n", 34 | "So, if you have a relatively short sequence of items and you don't plan to use any mathematical operation, its better to use list, on the other hans, if you have a very long data sequence (in same datatype) **_NumPy package_** will be very useful and highly recommended even if you plan to use mathemetical operations. \n", 35 | "\n", 36 | "## Array in Python \n", 37 | "\n", 38 | "Array is a python module which defines the data structure holds a fix number of items with same datatypes. An array stores items which called \"elements\" and each location of an element described by \"index\". \"Element\" and \"index\" are important terms to understand the concept of array. \n", 39 | "\n", 40 | "To use arrays in Python, we need to import **_NumPy package_**. Because array is not a fundamental data type like strings, integer etc., we need to import the **_NumPy package_** and declare an array. An array referred as _\"ndarray\"_ is shorthand for N-dimensional array which is with any number of dimension.\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "---\n", 48 | "# NumPy\n", 49 | "\n", 50 | "NumPy (Numerical Python) is an open source Python library which is very useful general-purpose array-processing package and a core of the scientific and engineering computing ecosystem of Python. It includes multidimensional array object, broadcasting functions, linear algebra calculations, Fourier transform and more. The NumPy library also contains matrix data structures and can be used to perform mathematical operations on arrays. NumPy can allow a wide variety of datatypes speedly. \n", 51 | "\n", 52 | "If you have Python you can install NumPy as below;\n", 53 | "\n", 54 | "conda install numpy\n", 55 | "\n", 56 | ",or,\n", 57 | "\n", 58 | "pip install numpy\n", 59 | "\n", 60 | "#### **How to import NumPy ?**\n", 61 | "\n", 62 | "We prefer NumPy because it provides an enormous range of fast and efficient mathematical operations. \n", 63 | "Not only for NumPy but also for all packages or libraries in Python, you need to use statement **_\"import\"_** to make them accessible. " 64 | ] 65 | }, 66 | { 67 | "cell_type": "code", 68 | "execution_count": null, 69 | "metadata": {}, 70 | "outputs": [], 71 | "source": [ 72 | "import numpy as np" 73 | ] 74 | }, 75 | { 76 | "cell_type": "markdown", 77 | "metadata": {}, 78 | "source": [ 79 | ": np stands for an abbreviation in order to keep code standardized and make it easier to understand and faster to run. " 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "The command below will bring you to the reference documentation;\n", 87 | "\n", 88 | "help(np.array)" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "---\n", 96 | "\n", 97 | "### **NumPy Array**\n", 98 | "\n", 99 | "The NumPy array can be defined as a grid of elements which can be indexed in various ways. An array in Numpy is a table of element with same type which indexed by tuple of positive integers. Tuple of integers giving the size of the array is the _shape_ of an array. Dimensions of a NumPy array is called _axes_. The number of axes in array called _rank_. A _vector_ is an array with a single column, a _matrix_ is an array with multiple columns." 100 | ] 101 | }, 102 | { 103 | "cell_type": "code", 104 | "execution_count": null, 105 | "metadata": {}, 106 | "outputs": [], 107 | "source": [ 108 | "import numpy as np\n", 109 | "\n", 110 | "a = np.array([1,2,3]) # this is a rank = 1 array\n", 111 | "\n", 112 | "print(type(a)) # print type of the array\n", 113 | "print(a[1]) # indexing elements of array\n", 114 | "print(a.dtype) # print type of elements in array\n", 115 | "print(a.shape) # print shape of array\n", 116 | "print(a.ndim) # print number of dimensions (axes)\n", 117 | "print(a.size) # print size of the array" 118 | ] 119 | }, 120 | { 121 | "cell_type": "code", 122 | "execution_count": null, 123 | "metadata": {}, 124 | "outputs": [], 125 | "source": [ 126 | "import numpy as np\n", 127 | "b = np.array([[1,2,3],[4,5,6]])\n", 128 | "print(b.shape)\n", 129 | "print(type(b[1,1]))" 130 | ] 131 | }, 132 | { 133 | "cell_type": "markdown", 134 | "metadata": {}, 135 | "source": [ 136 | "---\n", 137 | "#### **_Specify a numpy array with fuctions;_**" 138 | ] 139 | }, 140 | { 141 | "cell_type": "code", 142 | "execution_count": null, 143 | "metadata": {}, 144 | "outputs": [], 145 | "source": [ 146 | "import numpy as np\n", 147 | "\n", 148 | "a= np.zeros((3,4)) # create a matrix of zeros\n", 149 | "print(a,'\\n')\n", 150 | "\n", 151 | "b = np.ones((2,3)) # create a matrix of ones\n", 152 | "print(b,'\\n')\n", 153 | "\n", 154 | "c = np.random.random((1,3)) # create a matrix of random numbers\n", 155 | "print(c,'\\n')\n", 156 | "\n", 157 | "d = np.full((3,2),5) # create a constant matrix\n", 158 | "print(d,'\\n')" 159 | ] 160 | }, 161 | { 162 | "cell_type": "code", 163 | "execution_count": null, 164 | "metadata": {}, 165 | "outputs": [], 166 | "source": [ 167 | "e = np.eye(3) # create an identity matrix\n", 168 | "print(e,'\\n')" 169 | ] 170 | }, 171 | { 172 | "cell_type": "code", 173 | "execution_count": null, 174 | "metadata": {}, 175 | "outputs": [], 176 | "source": [ 177 | "f = np.array([[1, 3, 5], [7, 9, 11]], dtype = 'float') # create array from a float type list \n", 178 | "print(f,'\\n')" 179 | ] 180 | }, 181 | { 182 | "cell_type": "code", 183 | "execution_count": null, 184 | "metadata": {}, 185 | "outputs": [], 186 | "source": [ 187 | "gg = np.arange(5)\n", 188 | "print(gg)\n", 189 | "g = np.arange(1, 12, 2) # create an array of integers between 1 to 12 with steps 2 (first #, last #, step size)\n", 190 | "print(g,'\\n')" 191 | ] 192 | }, 193 | { 194 | "cell_type": "code", 195 | "execution_count": null, 196 | "metadata": {}, 197 | "outputs": [], 198 | "source": [ 199 | "h = np.linspace(5, 15, 5) # create an array with 5 items between 5 to 15 \n", 200 | "print(h,'\\n') " 201 | ] 202 | }, 203 | { 204 | "cell_type": "code", 205 | "execution_count": null, 206 | "metadata": {}, 207 | "outputs": [], 208 | "source": [ 209 | "i = np.random.random((4,4))\n", 210 | "print(i,'\\n')\n", 211 | "\n", 212 | "j = i.reshape(2,2,2,2)\n", 213 | "print(j,'\\n') " 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": {}, 219 | "source": [ 220 | "---\n", 221 | "#### **_more array functions;_**\n" 222 | ] 223 | }, 224 | { 225 | "cell_type": "code", 226 | "execution_count": null, 227 | "metadata": {}, 228 | "outputs": [], 229 | "source": [ 230 | "a = np.array([4, 2, 1, 5, 3])\n", 231 | "print(np.append(a, [8,8])) # Appends values to end of array\n", 232 | "print(np.insert(a, 2, 55)) # Inserts values into array before index 2 (array,2,values)\n", 233 | "print(np.delete(a, 3, axis = 0)) # delete row on index 3 of array\n", 234 | "print(np.sort(a))" 235 | ] 236 | }, 237 | { 238 | "cell_type": "code", 239 | "execution_count": null, 240 | "metadata": {}, 241 | "outputs": [], 242 | "source": [ 243 | "# comparison of two different array\n", 244 | "\n", 245 | "a = np.array([1, 2, 3, 4])\n", 246 | "b = np.array([5, 6, 1, 2])\n", 247 | "\n", 248 | "print(a == b)\n", 249 | "print(a > b)\n", 250 | "\n", 251 | "# ,or, array-wise comparison can be used as below;\n", 252 | "print(np.array_equal(a, b))\n" 253 | ] 254 | }, 255 | { 256 | "cell_type": "markdown", 257 | "metadata": {}, 258 | "source": [ 259 | "---\n", 260 | "#### **_shape and resize array;_**\n" 261 | ] 262 | }, 263 | { 264 | "cell_type": "code", 265 | "execution_count": null, 266 | "metadata": {}, 267 | "outputs": [], 268 | "source": [ 269 | "i = np.random.random([4,4])\n", 270 | "print(i)\n", 271 | "\n", 272 | "i.ndim # gives number of dimensions of the array \n", 273 | "print(i.ndim)\n", 274 | "\n", 275 | "print(np.size(i)) # gives total number of elements of the array\n", 276 | "np.shape(i) # gives the shape of the array\n", 277 | "\n" 278 | ] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": null, 283 | "metadata": {}, 284 | "outputs": [], 285 | "source": [ 286 | "a = np.array([1, 2, 3, 4, 5, 6, 7, 8])\n", 287 | "\n", 288 | "#b = a.reshape(3,2) # gives a new shape to an array with same items, both arrays should have same number of elements.\n", 289 | "b = a.reshape(4, 2)\n", 290 | "print(b)" 291 | ] 292 | }, 293 | { 294 | "cell_type": "markdown", 295 | "metadata": {}, 296 | "source": [ 297 | "---\n", 298 | "#### **_Array indexing;_**\n", 299 | "\n", 300 | "Arrays can be multidimensional, we should specify a slice for each dimension, just like in list operations." 301 | ] 302 | }, 303 | { 304 | "cell_type": "code", 305 | "execution_count": null, 306 | "metadata": {}, 307 | "outputs": [], 308 | "source": [ 309 | "a = np.array([[1,3,5],[2,4,6],[7,8,9]])\n", 310 | "print(a,'\\n')\n", 311 | "\n", 312 | "b = a[:2,0:2] # pull out the subarray of first 2 raws and columns 0 to 1\n", 313 | "print(b,'\\n')\n" 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": null, 319 | "metadata": {}, 320 | "outputs": [], 321 | "source": [ 322 | "print(a[1,2]) # element value at 1th row, 2nd column " 323 | ] 324 | }, 325 | { 326 | "cell_type": "code", 327 | "execution_count": null, 328 | "metadata": {}, 329 | "outputs": [], 330 | "source": [ 331 | "b[0,1] = 11\n", 332 | "print(b)" 333 | ] 334 | }, 335 | { 336 | "cell_type": "code", 337 | "execution_count": null, 338 | "metadata": {}, 339 | "outputs": [], 340 | "source": [ 341 | "a = np.array([[1,3,5],[2,4,6],[7,8,9]])\n", 342 | "\n", 343 | "row1 = a[0, :] \n", 344 | "row2 = a[1:2, :]\n", 345 | "print(row1, row2)\n", 346 | "\n", 347 | "col1 = a[:,1]\n", 348 | "col2 = a[:, 1:2]\n", 349 | "print(col1, col2)" 350 | ] 351 | }, 352 | { 353 | "cell_type": "code", 354 | "execution_count": null, 355 | "metadata": {}, 356 | "outputs": [], 357 | "source": [ 358 | "a = np.array([[1,3,5],[2,4,6],[7,8,9]])\n", 359 | "\n", 360 | "print(a[[0, 1, 2], [0, 1, 0]])\n", 361 | "print(np.array([a[0, 0], a[1, 1], a[2, 0]]))\n", 362 | "\n", 363 | "print(a[[0, 0], [1, 1]]) \n", 364 | "print(np.array([a[0, 1], a[0, 1]])) " 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": {}, 371 | "outputs": [], 372 | "source": [ 373 | "a = np.array([[1,3,5],[2,4,6],[7,8,9]])\n", 374 | "\n", 375 | "b = np.array([0, 2, 0, 1]) \n", 376 | "print(a[np.arange(3)]) \n", 377 | "\n", 378 | "a[np.arange(3)] += 10\n", 379 | "print( a )\n" 380 | ] 381 | }, 382 | { 383 | "cell_type": "code", 384 | "execution_count": null, 385 | "metadata": {}, 386 | "outputs": [], 387 | "source": [ 388 | "a[0:2]" 389 | ] 390 | }, 391 | { 392 | "cell_type": "code", 393 | "execution_count": null, 394 | "metadata": {}, 395 | "outputs": [], 396 | "source": [ 397 | "a = np.array([[3,4], [7, 1], [2, 4]])\n", 398 | "bool_idx = (a > 2)\n", 399 | "print(bool_idx) \n", 400 | "\n", 401 | "print(a[bool_idx]) # print the values \"true\" for the condition a > 2\n", 402 | "print(a[a > 2])" 403 | ] 404 | }, 405 | { 406 | "cell_type": "markdown", 407 | "metadata": {}, 408 | "source": [ 409 | "---\n", 410 | "#### **_Datatypes_**\n", 411 | "\n", 412 | "\"dtype' command can be used to identify the type of an array" 413 | ] 414 | }, 415 | { 416 | "cell_type": "code", 417 | "execution_count": null, 418 | "metadata": {}, 419 | "outputs": [], 420 | "source": [ 421 | "a = np.array([1,2])\n", 422 | "print(a.dtype) # Returns type of elements in array" 423 | ] 424 | }, 425 | { 426 | "cell_type": "code", 427 | "execution_count": null, 428 | "metadata": {}, 429 | "outputs": [], 430 | "source": [ 431 | "a = np.array([1.0, 2.0]) \n", 432 | "print(a.dtype) " 433 | ] 434 | }, 435 | { 436 | "cell_type": "code", 437 | "execution_count": null, 438 | "metadata": {}, 439 | "outputs": [], 440 | "source": [ 441 | "a = np.array([1, 2], dtype=np.int64) \n", 442 | "print(a.dtype) " 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": null, 448 | "metadata": {}, 449 | "outputs": [], 450 | "source": [ 451 | "a = np.array([1,2])\n", 452 | "a.tolist() # Convert array to a Python list" 453 | ] 454 | }, 455 | { 456 | "cell_type": "code", 457 | "execution_count": null, 458 | "metadata": {}, 459 | "outputs": [], 460 | "source": [ 461 | "np.info(np.eye) # View documentation for \"ones\" properties" 462 | ] 463 | }, 464 | { 465 | "cell_type": "markdown", 466 | "metadata": {}, 467 | "source": [ 468 | "---\n", 469 | "#### **_Basic array operations_**\n", 470 | "\n", 471 | "_Addition, subtraction, multiplication, division, etc._" 472 | ] 473 | }, 474 | { 475 | "cell_type": "code", 476 | "execution_count": null, 477 | "metadata": {}, 478 | "outputs": [], 479 | "source": [ 480 | "# Lets define two different arrays; \n", 481 | "\n", 482 | "data = np.random.random([1,2])\n", 483 | "ones = np.ones(2)\n", 484 | "\n", 485 | "print(data, ones)\n", 486 | "\n", 487 | "data + ones\n", 488 | "data - ones\n", 489 | "data * ones\n", 490 | "data / data\n", 491 | "\n", 492 | "#Operation between a vector and a scalar is allowed.\n", 493 | "\n", 494 | "data * 2.5" 495 | ] 496 | }, 497 | { 498 | "cell_type": "markdown", 499 | "metadata": {}, 500 | "source": [ 501 | " _Maxinum, minimum, sum, mean, product, standard deviation, etc._" 502 | ] 503 | }, 504 | { 505 | "cell_type": "code", 506 | "execution_count": null, 507 | "metadata": {}, 508 | "outputs": [], 509 | "source": [ 510 | "a = np.random.random([3,])\n", 511 | "print(a)" 512 | ] 513 | }, 514 | { 515 | "cell_type": "code", 516 | "execution_count": null, 517 | "metadata": {}, 518 | "outputs": [], 519 | "source": [ 520 | "b = np.random.rand(2,3) # 2x3 array of random floats between 0–1\n", 521 | "print(b)" 522 | ] 523 | }, 524 | { 525 | "cell_type": "code", 526 | "execution_count": null, 527 | "metadata": {}, 528 | "outputs": [], 529 | "source": [ 530 | "c = np.random.rand(3,1)*100 # 3x1 array of random floats between 0–100\n", 531 | "print(c)" 532 | ] 533 | }, 534 | { 535 | "cell_type": "code", 536 | "execution_count": null, 537 | "metadata": {}, 538 | "outputs": [], 539 | "source": [ 540 | "d = np.random.randint(14,size=(3,5)) # 3x5 array with random ints between 0–4\n", 541 | "print(d)" 542 | ] 543 | }, 544 | { 545 | "cell_type": "code", 546 | "execution_count": null, 547 | "metadata": {}, 548 | "outputs": [], 549 | "source": [ 550 | "print(d.max()) # find the maximum value element in array\n", 551 | "print(d.min()) # fint the minimum value element in array\n", 552 | "print(d.sum()) # find the sum of the elements in array" 553 | ] 554 | }, 555 | { 556 | "cell_type": "code", 557 | "execution_count": null, 558 | "metadata": {}, 559 | "outputs": [], 560 | "source": [ 561 | "# we can use arithmetic operators to do element-wise operation on array;\n", 562 | "\n", 563 | "a = np.array([1, 2, 5, 3]) \n", 564 | " \n", 565 | "print ( a+3) # add a scalar to each element of array \n", 566 | "print (a-3) # subtract a scalar from each element of array \n", 567 | "print (a*2) # multiply each element by 2 \n", 568 | "print (a**2) # calculate square of each element \n", 569 | "\n", 570 | "# we can also multiply array by a scalar as below;\n", 571 | "a *= 2\n", 572 | "print (a) # modify existing array \n", 573 | " " 574 | ] 575 | }, 576 | { 577 | "cell_type": "code", 578 | "execution_count": null, 579 | "metadata": {}, 580 | "outputs": [], 581 | "source": [ 582 | "# we can use arithmetic operators to do operation on array;\n", 583 | " \n", 584 | "a = np.array([[1, 3], [5, 7]]) \n", 585 | "b = np.array([[2, 4], [6, 8]]) \n", 586 | " \n", 587 | "print (a + b) # add arrays with eachother\n", 588 | "print (a * b) # multiply arrays (elementwise multiplication)\n", 589 | "print (a.dot(b)) # matrix multiplication " 590 | ] 591 | }, 592 | { 593 | "cell_type": "code", 594 | "execution_count": null, 595 | "metadata": {}, 596 | "outputs": [], 597 | "source": [ 598 | "#we can also sort a NumPy array by np.sort;\n", 599 | "\n", 600 | "a = np.array([[1, 4, 2], \n", 601 | " [3, 4, 6], \n", 602 | " [0, -1, 5]])\n", 603 | "\n", 604 | "print(np.sort(a, axis = None)) # Array elements in sorted orde\n", 605 | " \n", 606 | "print(np.sort(a, axis = 1)) # sort array row-wise" 607 | ] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "execution_count": null, 612 | "metadata": {}, 613 | "outputs": [], 614 | "source": [ 615 | "# NumPy also provides mathematical functions such as sin, cos, exp, transpose etc.\n", 616 | "\n", 617 | "a = np.array([np.pi/2, np.pi*2, 3.14]) # create an array of sine values \n", 618 | "print (np.cos(a)) \n", 619 | " \n", 620 | "b = np.array([1, 2, 3]) # exponential values \n", 621 | "print (np.exp(b)) \n", 622 | " \n", 623 | "print(np.sqrt(b)) # square root of array values\n", 624 | "print(np.log(b)) # logarithmic values of array elements\n", 625 | "\n", 626 | "b = np.array([[1, 2, 3], [3, 4, 5], [9, 6, 0]]) \n", 627 | "print (\"\\nOriginal array:\\n\", b) \n", 628 | "print (\"Transpose of array:\\n\", b.T) # transpose of array " 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": null, 634 | "metadata": {}, 635 | "outputs": [], 636 | "source": [ 637 | "# we can find unique element in an array\n", 638 | "\n", 639 | "a = np.array([1, 1, 2, 3, 4, 5, 6, 7, 2, 3, 1, 4,12, 8, 9, 0])\n", 640 | "print(np.unique(a))\n" 641 | ] 642 | }, 643 | { 644 | "cell_type": "markdown", 645 | "metadata": {}, 646 | "source": [ 647 | "---\n", 648 | "#### **_Matrix operations;_**" 649 | ] 650 | }, 651 | { 652 | "cell_type": "code", 653 | "execution_count": null, 654 | "metadata": {}, 655 | "outputs": [], 656 | "source": [ 657 | "# Lets define an array which we can manipulate matrices also;\n", 658 | "\n", 659 | "a = np.array([[1,2],[3,4]])\n", 660 | "print(a)\n", 661 | "\n", 662 | "print(a[0,1])\n", 663 | "print(a[1::])\n", 664 | "print(a[:,0])" 665 | ] 666 | }, 667 | { 668 | "cell_type": "code", 669 | "execution_count": null, 670 | "metadata": {}, 671 | "outputs": [], 672 | "source": [ 673 | "print(a.max()) # find the maximum value element in matrix\n", 674 | "print(a.min()) # fint the minimum value element in matrix\n", 675 | "print(a.sum()) # find the sum of the elements in matrix" 676 | ] 677 | }, 678 | { 679 | "cell_type": "markdown", 680 | "metadata": {}, 681 | "source": [ 682 | "![](./Figures/array-sum.png)" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": null, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "print(a.sum(axis=0)) # column sums\n", 692 | "print(a.sum(axis=1)) # row sums" 693 | ] 694 | }, 695 | { 696 | "cell_type": "code", 697 | "execution_count": null, 698 | "metadata": {}, 699 | "outputs": [], 700 | "source": [ 701 | "# find maximum and minimum rows or columns of the matrix\n", 702 | "print(a.max(axis=0))\n", 703 | "print(a.max(axis=1))" 704 | ] 705 | }, 706 | { 707 | "cell_type": "code", 708 | "execution_count": null, 709 | "metadata": {}, 710 | "outputs": [], 711 | "source": [ 712 | "# Two matrices at same size can be multiply or add with each other\n", 713 | "\n", 714 | "data = np.random.random([2,3])\n", 715 | "ones = np.ones([2,3])\n", 716 | "\n", 717 | "print(data)\n", 718 | "print(ones)\n", 719 | "\n", 720 | "a = data + ones\n", 721 | "print(a)\n", 722 | "\n", 723 | "# Arithmetic operations on matrices with different size con do if only one matrix thas only one column or row\n", 724 | "\n", 725 | "b = data + np.ones([1,3])\n" 726 | ] 727 | }, 728 | { 729 | "cell_type": "markdown", 730 | "metadata": {}, 731 | "source": [ 732 | "For matrix multiplications use the **_dot_** function to compute inner product of vector instead of **' * '** that unlike MATLAB. **_dot_** product can also be used for a vector by a matrix or matrix by matrix multiplications." 733 | ] 734 | }, 735 | { 736 | "cell_type": "code", 737 | "execution_count": null, 738 | "metadata": {}, 739 | "outputs": [], 740 | "source": [ 741 | "x = np.array([[1,2],[3,4]])\n", 742 | "y = np.array([[5,6],[7,8]])\n", 743 | "\n", 744 | "v = np.array([2,4])\n", 745 | "w = np.array([1,2])\n", 746 | "\n", 747 | "print(v.dot(w)) # Inner product of vectors\n", 748 | "print(np.dot(v, w))\n", 749 | "\n", 750 | "print(x.dot(v)) # Matrix / vector product\n", 751 | "print(np.dot(x, v))\n", 752 | "\n", 753 | "print(x.dot(y)) # Matrix / matrix product\n", 754 | "print(np.dot(x, y))" 755 | ] 756 | }, 757 | { 758 | "cell_type": "code", 759 | "execution_count": null, 760 | "metadata": {}, 761 | "outputs": [], 762 | "source": [ 763 | "# to transpose a matrix, use the T attribute of an array object:\n", 764 | "\n", 765 | "a = np.array([[1,2], [3,4]])" 766 | ] 767 | }, 768 | { 769 | "cell_type": "code", 770 | "execution_count": null, 771 | "metadata": {}, 772 | "outputs": [], 773 | "source": [ 774 | "print(a, '\\n') " 775 | ] 776 | }, 777 | { 778 | "cell_type": "code", 779 | "execution_count": null, 780 | "metadata": {}, 781 | "outputs": [], 782 | "source": [ 783 | "print(a.T,'\\n') " 784 | ] 785 | }, 786 | { 787 | "cell_type": "code", 788 | "execution_count": null, 789 | "metadata": {}, 790 | "outputs": [], 791 | "source": [ 792 | "a = np.arange(25).reshape(5,5)\n", 793 | "print(a)\n", 794 | "print(a.diagonal())\n", 795 | "print(a.diagonal(1))\n", 796 | "print(a.diagonal(-1))\n", 797 | "print(a.diagonal(-2))" 798 | ] 799 | }, 800 | { 801 | "cell_type": "code", 802 | "execution_count": null, 803 | "metadata": {}, 804 | "outputs": [], 805 | "source": [ 806 | "a = np.arange(15).reshape(3,5)\n", 807 | "\n", 808 | "print(a)\n" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": null, 814 | "metadata": {}, 815 | "outputs": [], 816 | "source": [ 817 | "a = np.array([[1,2], [3,4]])\n", 818 | "\n", 819 | "print(np.reshape(a,[4,1])) # shape refers to the array size or dimension size\n", 820 | "np.transpose(a)" 821 | ] 822 | }, 823 | { 824 | "cell_type": "markdown", 825 | "metadata": {}, 826 | "source": [ 827 | "---\n", 828 | "#### **_N-deminsional arrays_**\n", 829 | "\n", 830 | "![](./Figures/3d-array.png)\n", 831 | "\n", 832 | "Not only 3D, it may be in 4 or more dimensional array. N-dimensional arrays are widely used in Machine Learning algorithms. Here is an example of 3D NumPy array defined below;" 833 | ] 834 | }, 835 | { 836 | "cell_type": "code", 837 | "execution_count": null, 838 | "metadata": {}, 839 | "outputs": [], 840 | "source": [ 841 | "a = np.random.random((4,3,2)) # refers 3D array with 3 values as 4,3,2.\n", 842 | "\n", 843 | "print(a)" 844 | ] 845 | }, 846 | { 847 | "cell_type": "code", 848 | "execution_count": null, 849 | "metadata": {}, 850 | "outputs": [], 851 | "source": [ 852 | "a.std() # calculates the standart deviation of elemnts in array" 853 | ] 854 | }, 855 | { 856 | "cell_type": "code", 857 | "execution_count": null, 858 | "metadata": {}, 859 | "outputs": [], 860 | "source": [ 861 | "a.var() # calculates variance of the array" 862 | ] 863 | }, 864 | { 865 | "cell_type": "code", 866 | "execution_count": null, 867 | "metadata": {}, 868 | "outputs": [], 869 | "source": [ 870 | "a.mean() # calculates the average value of array" 871 | ] 872 | }, 873 | { 874 | "cell_type": "code", 875 | "execution_count": null, 876 | "metadata": {}, 877 | "outputs": [], 878 | "source": [ 879 | "a.cumsum() # calculates the cumulative summation of elements in the array" 880 | ] 881 | }, 882 | { 883 | "cell_type": "code", 884 | "execution_count": null, 885 | "metadata": {}, 886 | "outputs": [], 887 | "source": [ 888 | "a = np.array([[2,3,4],[1,4,3],[2,5,6]])\n", 889 | "print(a)" 890 | ] 891 | }, 892 | { 893 | "cell_type": "code", 894 | "execution_count": null, 895 | "metadata": {}, 896 | "outputs": [], 897 | "source": [ 898 | "a.conj() # gives the conjugate of elements of the array" 899 | ] 900 | }, 901 | { 902 | "cell_type": "code", 903 | "execution_count": null, 904 | "metadata": {}, 905 | "outputs": [], 906 | "source": [ 907 | "a.trace(-2) # perform a summation of each diagonal." 908 | ] 909 | }, 910 | { 911 | "cell_type": "code", 912 | "execution_count": null, 913 | "metadata": {}, 914 | "outputs": [], 915 | "source": [ 916 | "a = np.array([3.2, 2, 1])\n", 917 | "np.roots(a)" 918 | ] 919 | }, 920 | { 921 | "cell_type": "markdown", 922 | "metadata": {}, 923 | "source": [ 924 | "---" 925 | ] 926 | }, 927 | { 928 | "cell_type": "markdown", 929 | "metadata": {}, 930 | "source": [ 931 | "## Homework #3\n", 932 | "---\n", 933 | "Please solve the deterministic linear equation ( A x = B ) given below by using NumPy. You can represent the problem as matrices and apply matrix algebra to solve it.\n", 934 | "\n", 935 | "_2p + 3q + r = 9_\n", 936 | "\n", 937 | "_p + 5q + 2r = 11_\n", 938 | "\n", 939 | "_p = 6_\n", 940 | "\n", 941 | "Formulize the problem with matrices and solve it for the vector x.\n", 942 | "\n", 943 | "PS: Do not forget to upload your pdf files to http://derskutusu.gtu.edu.tr/\n", 944 | "\n", 945 | "---" 946 | ] 947 | }, 948 | { 949 | "cell_type": "code", 950 | "execution_count": null, 951 | "metadata": {}, 952 | "outputs": [], 953 | "source": [] 954 | } 955 | ], 956 | "metadata": { 957 | "kernelspec": { 958 | "display_name": "Python 3", 959 | "language": "python", 960 | "name": "python3" 961 | }, 962 | "language_info": { 963 | "codemirror_mode": { 964 | "name": "ipython", 965 | "version": 3 966 | }, 967 | "file_extension": ".py", 968 | "mimetype": "text/x-python", 969 | "name": "python", 970 | "nbconvert_exporter": "python", 971 | "pygments_lexer": "ipython3", 972 | "version": "3.6.8" 973 | } 974 | }, 975 | "nbformat": 4, 976 | "nbformat_minor": 4 977 | } 978 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week04-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Conditionals and Loops\n", 17 | "\n", 18 | " - The **_\" if \"_** statement\n", 19 | " - The **_\" if else / elif\"_** statement\n", 20 | " - The **_\" for \"_** loop\n", 21 | " - **_\" break \"_**, **_\" continue \"_**, **_\" pass \"_** statements\n", 22 | " - The **_\" while \"_** statement\n", 23 | " - Nested Loops" 24 | ] 25 | }, 26 | { 27 | "cell_type": "markdown", 28 | "metadata": {}, 29 | "source": [ 30 | "---\n", 31 | "\n", 32 | "In programming world, suc as in real world, sometimes we need to make decisions to move with next step. In Python, when we make a decision, so we can execute the next step of the code. This week we will learn to use decision makeing statements in Python programming language. \n", 33 | "\n", 34 | "In the code we use _conditionals_ to execute different lines of the code, as a way to repeat something, especially to say if its True or not. If the conditional is True, the loop will be infine, otherwise, if the code starts with False, means it will never run.\n", 35 | "\n", 36 | "\n", 37 | "### The **_\" if \"_** statement\n", 38 | "\n", 39 | "\" if \" statement is one of the most simple decision making statement. It is used to decide whether a certain statement is True or False. \n", 40 | "\n", 41 | "![](./Figures/if-statement.png)\n", 42 | "\n", 43 | "---\n", 44 | "\n", 45 | "Lets remember that Python supports the logical conditions listed below;\n", 46 | "\n", 47 | "\n", 48 | " Equal: a == b\n", 49 | " Not Equal: a != b\n", 50 | " Less than: a < b\n", 51 | " Less than or equal to: a <= b\n", 52 | " Greater than: a > b\n", 53 | " Greater than or equal to: a >= b\n", 54 | "\n" 55 | ] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "execution_count": null, 60 | "metadata": {}, 61 | "outputs": [], 62 | "source": [ 63 | "# write your first \"if\" statement\n", 64 | "# The colon (:) is required to seprarate the condition from the body. The line after the colon is also required and it stands for four spaces for indenting.\n", 65 | "\n", 66 | "a = 5\n", 67 | "b = 15\n", 68 | "if b > a:\n", 69 | " print('I\\'m bigger than you')" 70 | ] 71 | }, 72 | { 73 | "cell_type": "code", 74 | "execution_count": null, 75 | "metadata": {}, 76 | "outputs": [], 77 | "source": [ 78 | "# using TABS and spaces make differences, such as:\n", 79 | "\n", 80 | "condition = False\n", 81 | "if condition:\n", 82 | " print('condition is True')\n", 83 | " print('condition is False') # this code will skip both print lines as long as acondition is False.\n", 84 | " \n", 85 | " \n", 86 | "condition = False\n", 87 | "if condition:\n", 88 | " print('condition is True')\n", 89 | "print('condition is False') # will print 'condition is False' regardless of whether condition is True or False\n", 90 | "\n", 91 | "condition = False\n", 92 | "if condition:\n", 93 | " print('condition is True')\n", 94 | " print('condition is False') # will appear \"IndentationError: unexpected indent\" error message" 95 | ] 96 | }, 97 | { 98 | "cell_type": "code", 99 | "execution_count": null, 100 | "metadata": {}, 101 | "outputs": [], 102 | "source": [ 103 | "# lets try it with boolean expression\n", 104 | "\n", 105 | "action = 'jump'\n", 106 | "\n", 107 | "if action == 'jump':\n", 108 | " print('I would like to ', 5 * (action + '! '))" 109 | ] 110 | }, 111 | { 112 | "cell_type": "code", 113 | "execution_count": null, 114 | "metadata": {}, 115 | "outputs": [], 116 | "source": [ 117 | "# make a decision about a number if its positive or not,\n", 118 | "\n", 119 | "A = 5\n", 120 | "if A > 0:\n", 121 | " print(A, \"is a positive number.\")\n", 122 | "\n", 123 | "A = -3\n", 124 | "if A < 0:\n", 125 | " print(A, \"is a negative number.\")" 126 | ] 127 | }, 128 | { 129 | "cell_type": "markdown", 130 | "metadata": {}, 131 | "source": [ 132 | "---\n", 133 | "\n", 134 | "### The **_\" if else / elif \"_** statement\n", 135 | "\n", 136 | "\" if else\" statement is stands for a decision with two possible conclusion. If the statement is True follow 1st path, else follow the 2nd path. \n", 137 | "\n", 138 | "![](./Figures/if-else-statement.png)" 139 | ] 140 | }, 141 | { 142 | "cell_type": "code", 143 | "execution_count": null, 144 | "metadata": {}, 145 | "outputs": [], 146 | "source": [ 147 | "# Lets write a very basic if else statement;\n", 148 | "\n", 149 | "a = 15\n", 150 | "b = 5\n", 151 | "if b > a:\n", 152 | " print('I\\'m bigger than you')\n", 153 | "else:\n", 154 | " print('No you are NOT !')" 155 | ] 156 | }, 157 | { 158 | "cell_type": "code", 159 | "execution_count": null, 160 | "metadata": {}, 161 | "outputs": [], 162 | "source": [ 163 | "# lets try it with boolean expression\n", 164 | "\n", 165 | "#action = 'jump'\n", 166 | "action = 'walk'\n", 167 | "\n", 168 | "if action == 'jump':\n", 169 | " print('I would like to ', 5 * (action + '! '))\n", 170 | "else:\n", 171 | " print('Don\\'t want to', action, 'any more :(')" 172 | ] 173 | }, 174 | { 175 | "cell_type": "code", 176 | "execution_count": null, 177 | "metadata": {}, 178 | "outputs": [], 179 | "source": [ 180 | "# Lets simplify the \" if \" statement above by using \"if else\" statement\n", 181 | "\n", 182 | "A = 5\n", 183 | "\n", 184 | "if A >= 0:\n", 185 | " print(A, \"is a positive number.\")\n", 186 | "else:\n", 187 | " print(A, \"is a negative number.\")" 188 | ] 189 | }, 190 | { 191 | "cell_type": "markdown", 192 | "metadata": {}, 193 | "source": [ 194 | "---\n", 195 | "In order to chain conditions to each other to simplify/solve more complex logics \"elif\" can be used as a concentration version of else and if. \n", 196 | "If the _\" if \"_ statement condition is False, then the code will check the _\" elif \"_ condition on the next block, and so on. Even all of them are False, _\" else \"_ will be executed.\n", 197 | "\n", 198 | "![](./Figures/elif-else-statement.png)\n" 199 | ] 200 | }, 201 | { 202 | "cell_type": "code", 203 | "execution_count": null, 204 | "metadata": {}, 205 | "outputs": [], 206 | "source": [ 207 | "A = 5\n", 208 | "B = 8\n", 209 | "\n", 210 | "if A > 7:\n", 211 | " print(A, \"is greater than 7\")\n", 212 | "elif B > 7:\n", 213 | " print(B, \"is greater than 7.\")\n", 214 | "else:\n", 215 | " print('what is going on!!')" 216 | ] 217 | }, 218 | { 219 | "cell_type": "markdown", 220 | "metadata": {}, 221 | "source": [ 222 | "Or, we can set more complex chain;" 223 | ] 224 | }, 225 | { 226 | "cell_type": "code", 227 | "execution_count": null, 228 | "metadata": {}, 229 | "outputs": [], 230 | "source": [ 231 | "A = 4\n", 232 | "B = 8\n", 233 | "\n", 234 | "if A > 7 and B > 7 :\n", 235 | " print(\"Everything is awsome\")\n", 236 | "elif B > 7 or A < 5 :\n", 237 | " print(\"Confusing\")\n", 238 | "else:\n", 239 | " print('Nothing to do')" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": {}, 245 | "source": [ 246 | "---\n", 247 | "#### _Short hand statements;_" 248 | ] 249 | }, 250 | { 251 | "cell_type": "code", 252 | "execution_count": null, 253 | "metadata": {}, 254 | "outputs": [], 255 | "source": [ 256 | "if a > b: print(\"a is greater than b\")" 257 | ] 258 | }, 259 | { 260 | "cell_type": "code", 261 | "execution_count": null, 262 | "metadata": {}, 263 | "outputs": [], 264 | "source": [ 265 | "a = 3\n", 266 | "b = 5\n", 267 | "print(\"bigger a\") if a > b else print(\"smaller a\") " 268 | ] 269 | }, 270 | { 271 | "cell_type": "code", 272 | "execution_count": null, 273 | "metadata": {}, 274 | "outputs": [], 275 | "source": [ 276 | "a = 5\n", 277 | "b = 5\n", 278 | "print(\"bigger a\") if a > b else print(\"a = b\") if a == b else print(\"smaller a\") " 279 | ] 280 | }, 281 | { 282 | "cell_type": "code", 283 | "execution_count": null, 284 | "metadata": {}, 285 | "outputs": [], 286 | "source": [ 287 | "a = 6\n", 288 | "b = 5\n", 289 | "c = 8\n", 290 | "if a > b and c > a:\n", 291 | " print(\"c > a > b\")" 292 | ] 293 | }, 294 | { 295 | "cell_type": "code", 296 | "execution_count": null, 297 | "metadata": {}, 298 | "outputs": [], 299 | "source": [ 300 | "a = 5\n", 301 | "b = 3\n", 302 | "c = 8\n", 303 | "if a > b or a > c:\n", 304 | " print(\"At least one of the conditions is True\")" 305 | ] 306 | }, 307 | { 308 | "cell_type": "markdown", 309 | "metadata": {}, 310 | "source": [ 311 | "---\n", 312 | "### The **_\" for \"_** loop\n", 313 | "\n", 314 | "Loops are another type of flow control structure in programming language. By using loop conditions, a code can be repeated multiple times under specific conditions.\n", 315 | "\n", 316 | "![](./Figures/for-loop.png)\n" 317 | ] 318 | }, 319 | { 320 | "cell_type": "code", 321 | "execution_count": null, 322 | "metadata": {}, 323 | "outputs": [], 324 | "source": [ 325 | "# define a list of values and iterate each of the values\n", 326 | "\n", 327 | "spring_semester = ['CE 102', 'CE 103', 'CE 105']\n", 328 | "\n", 329 | "for lectures in spring_semester: # searches for \"lecture\" variable in the list assigned to \"spring_semester\"\n", 330 | " print(lectures)" 331 | ] 332 | }, 333 | { 334 | "cell_type": "code", 335 | "execution_count": null, 336 | "metadata": {}, 337 | "outputs": [], 338 | "source": [ 339 | "# Find the summation of the numbers in a list\n", 340 | "\n", 341 | "sum = 0 # what if I dit not define it?? \n", 342 | "\n", 343 | "digit = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # create the list\n", 344 | "\n", 345 | "for i in digit:\n", 346 | " sum = sum + i\n", 347 | "\n", 348 | "print(\"Sum = \", sum)" 349 | ] 350 | }, 351 | { 352 | "cell_type": "code", 353 | "execution_count": null, 354 | "metadata": {}, 355 | "outputs": [], 356 | "source": [ 357 | "# for loop with else statement \n", 358 | "\n", 359 | "digits = [1, 3, 5, 7, 9, 11, 13]\n", 360 | "\n", 361 | "for i in digits:\n", 362 | " print(i)\n", 363 | "else:\n", 364 | " print(\"Listed them all\")" 365 | ] 366 | }, 367 | { 368 | "cell_type": "code", 369 | "execution_count": null, 370 | "metadata": {}, 371 | "outputs": [], 372 | "source": [ 373 | "# create two column list output with for loop\n", 374 | "\n", 375 | "for x in range(8): # Generate numbers 0 to 8\n", 376 | " print(x, '\\t', x**2) # ('\\t') character makes the output align with tab between values " 377 | ] 378 | }, 379 | { 380 | "cell_type": "markdown", 381 | "metadata": {}, 382 | "source": [ 383 | "---\n", 384 | "#### _**.keys( )**_ function" 385 | ] 386 | }, 387 | { 388 | "cell_type": "code", 389 | "execution_count": null, 390 | "metadata": {}, 391 | "outputs": [], 392 | "source": [ 393 | "# do the same iteration in a dictionary using \".keys()\" function, which goes through each item in the dictionary.\n", 394 | "\n", 395 | "credits = {'CE 102': 2, 'CE 103': 3, 'CE 105': 3}\n", 396 | "\n", 397 | "for x in credits.keys(): # allows to iterate over each entry\n", 398 | " print(credits[x])" 399 | ] 400 | }, 401 | { 402 | "cell_type": "markdown", 403 | "metadata": {}, 404 | "source": [ 405 | "---\n", 406 | "#### _**range( )**_ function" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": null, 412 | "metadata": {}, 413 | "outputs": [], 414 | "source": [ 415 | "# range() function can be used to create basic loop as below;\n", 416 | "\n", 417 | "print(range(8))\n", 418 | "\n", 419 | "for i in range(8):\n", 420 | " print('count', i)" 421 | ] 422 | }, 423 | { 424 | "cell_type": "code", 425 | "execution_count": null, 426 | "metadata": {}, 427 | "outputs": [], 428 | "source": [ 429 | "spring_semester = ['CE 102', 'CE 103', 'CE 105']\n", 430 | "\n", 431 | "for lectures in range(len(spring_semester)): # combine range() and len() functions, tries the range() function to the length of a list\n", 432 | " print(lectures)" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": {}, 438 | "source": [ 439 | "---\n", 440 | "#### _**enumerate( )**_ function" 441 | ] 442 | }, 443 | { 444 | "cell_type": "code", 445 | "execution_count": null, 446 | "metadata": {}, 447 | "outputs": [], 448 | "source": [ 449 | "# enumerate() function is another way to access a a list and returns both item and index\n", 450 | "\n", 451 | "spring_semester = ['CE 102', 'CE 103', 'CE 105']\n", 452 | "\n", 453 | "for i, lectures in enumerate(spring_semester): # takes in a list and returns both the item and its index\n", 454 | " print(lectures + 'is a spring semester lecture' + str(i))" 455 | ] 456 | }, 457 | { 458 | "cell_type": "markdown", 459 | "metadata": {}, 460 | "source": [ 461 | "---\n", 462 | "### The **_\" while \"_** statement\n", 463 | "\n", 464 | "A while loop statement is used to repeatedly iterate a block of target statement as long as the given expression is True. If the condition becomes False, the code will pass that line and will follow the loop. The while loop generally prefers for the conditions that we don't know the number of times to iterate.\n", 465 | "\n", 466 | "![](./Figures/while-loop.png)" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": null, 472 | "metadata": {}, 473 | "outputs": [], 474 | "source": [ 475 | "a = 0\n", 476 | "while (a < 5): \n", 477 | " a = a + 1\n", 478 | " print(\"pisi\") # will print \"pisi\" as long as a < 5" 479 | ] 480 | }, 481 | { 482 | "cell_type": "code", 483 | "execution_count": null, 484 | "metadata": {}, 485 | "outputs": [], 486 | "source": [ 487 | "a = [1, 3, 5, 7] \n", 488 | "while a: \n", 489 | " print(a.pop()) # will print items in a, till they exist" 490 | ] 491 | }, 492 | { 493 | "cell_type": "code", 494 | "execution_count": null, 495 | "metadata": {}, 496 | "outputs": [], 497 | "source": [ 498 | "# Lets use \"else\" with \"while\" loop\n", 499 | "\n", 500 | "a = 0\n", 501 | "while a < 3:\n", 502 | " print(a, \" is less than 3\")\n", 503 | " a = a + 1\n", 504 | "else:\n", 505 | " print(a, \" is not less than 3\") " 506 | ] 507 | }, 508 | { 509 | "cell_type": "code", 510 | "execution_count": null, 511 | "metadata": {}, 512 | "outputs": [], 513 | "source": [ 514 | "digit = 10\n", 515 | "\n", 516 | "sum = 0 # set sum as summation variable\n", 517 | "i = 1 # set \" i \" as a counter\n", 518 | " \n", 519 | "while i <= digit:\n", 520 | " sum = sum + i\n", 521 | " i = i+1 # increase counter\n", 522 | "\n", 523 | "print(\"The sum is\", sum) # calculate the summation of all numbers between 0 and 'digit', till 'digit' = 10. " 524 | ] 525 | }, 526 | { 527 | "cell_type": "markdown", 528 | "metadata": {}, 529 | "source": [ 530 | "---\n", 531 | "### The **_\" break \"_** loop\n", 532 | "\n", 533 | "This statement terminates the current loop and proceed with next statement. It can be used with both for and while loops. \n" 534 | ] 535 | }, 536 | { 537 | "cell_type": "code", 538 | "execution_count": null, 539 | "metadata": {}, 540 | "outputs": [], 541 | "source": [ 542 | "for numbers in '1234567890': \n", 543 | " if numbers == '5':\n", 544 | " break\n", 545 | " print('Current number is :', numbers) # will print out all the numbers till its equal 5" 546 | ] 547 | }, 548 | { 549 | "cell_type": "code", 550 | "execution_count": null, 551 | "metadata": {}, 552 | "outputs": [], 553 | "source": [ 554 | "a = 6\n", 555 | "while a > 0: \n", 556 | " print('Current variable value :', a)\n", 557 | " a = a -1\n", 558 | " if a == 5:\n", 559 | " break\n", 560 | "print(\"That\\'s all!\") # will break the code when the condition \"a = 5\"" 561 | ] 562 | }, 563 | { 564 | "cell_type": "markdown", 565 | "metadata": {}, 566 | "source": [ 567 | "---\n", 568 | "### The **_\" continue \"_** loop\n", 569 | "\n", 570 | "This statement returns the control to the beginning of the loop and skip all the remaining statements. And, control moves to the beginning of the loop. It can be used with both for and while loops. " 571 | ] 572 | }, 573 | { 574 | "cell_type": "code", 575 | "execution_count": null, 576 | "metadata": {}, 577 | "outputs": [], 578 | "source": [ 579 | "for numbers in '1234567890': \n", 580 | " if numbers == '5':\n", 581 | " continue\n", 582 | " print('Current number is :', numbers) # will not print out the '5'" 583 | ] 584 | }, 585 | { 586 | "cell_type": "code", 587 | "execution_count": null, 588 | "metadata": {}, 589 | "outputs": [], 590 | "source": [ 591 | "a = 6\n", 592 | "while a > 0: \n", 593 | " print('Current variable value :', a)\n", 594 | " a = a -1\n", 595 | " if a == 5:\n", 596 | " continue\n", 597 | "print(\"That\\'s all!\") # will keep doing the condition again as long as \"a > 0\"" 598 | ] 599 | }, 600 | { 601 | "cell_type": "markdown", 602 | "metadata": {}, 603 | "source": [ 604 | "---\n", 605 | "### The **_\" pass \"_** loop\n", 606 | "\n", 607 | "The pass statement is used to write empty statements, functions or classes. It is a \"null\" opretion which means nothing happens when its executed. " 608 | ] 609 | }, 610 | { 611 | "cell_type": "code", 612 | "execution_count": null, 613 | "metadata": {}, 614 | "outputs": [], 615 | "source": [ 616 | "for numbers in '1234567890': \n", 617 | " if numbers == '5':\n", 618 | " pass\n", 619 | " print('5 is my favorite number')\n", 620 | " print('Current number is :', numbers) # will not print out the '5'" 621 | ] 622 | }, 623 | { 624 | "cell_type": "markdown", 625 | "metadata": {}, 626 | "source": [ 627 | "---\n", 628 | "### The Nested Loops\n", 629 | "\n", 630 | "A loop inside another loop is called _\"Nested Loop\"_, which is allowed in Python. If you have while/for loop inside any loop, this is also allowed." 631 | ] 632 | }, 633 | { 634 | "cell_type": "markdown", 635 | "metadata": {}, 636 | "source": [ 637 | "---\n", 638 | "#### _Nested If_\n", 639 | "\n", 640 | "If you have if statement inside if statement, this is called nested if statements.\n" 641 | ] 642 | }, 643 | { 644 | "cell_type": "code", 645 | "execution_count": null, 646 | "metadata": {}, 647 | "outputs": [], 648 | "source": [ 649 | "a = 300\n", 650 | "\n", 651 | "if a > 10:\n", 652 | " print(\"Above ten,\")\n", 653 | " if a > 50:\n", 654 | " print(\"and also above 100!\")\n", 655 | " else:\n", 656 | " print(\"but not above 50.\") " 657 | ] 658 | }, 659 | { 660 | "cell_type": "code", 661 | "execution_count": null, 662 | "metadata": {}, 663 | "outputs": [], 664 | "source": [ 665 | "a = -5\n", 666 | "if a >= 0:\n", 667 | " if a == 0:\n", 668 | " print(\"Zero\")\n", 669 | " else:\n", 670 | " print(\"Positive number\")\n", 671 | "else:\n", 672 | " print(\"Negative number\")" 673 | ] 674 | }, 675 | { 676 | "cell_type": "markdown", 677 | "metadata": {}, 678 | "source": [ 679 | "---\n", 680 | "#### _Nested while/for_\n", 681 | "\n", 682 | "\n" 683 | ] 684 | }, 685 | { 686 | "cell_type": "code", 687 | "execution_count": null, 688 | "metadata": {}, 689 | "outputs": [], 690 | "source": [ 691 | "# Lets find prime numbers between 1 to 50\n", 692 | "i = 1\n", 693 | "while(i < 50):\n", 694 | " j = 2\n", 695 | " while(j <= (i/j)):\n", 696 | " if not(i%j): \n", 697 | " break\n", 698 | " j = j + 1\n", 699 | " if (j > i/j) : \n", 700 | " print(i, \" is prime\")\n", 701 | " i = i + 1" 702 | ] 703 | }, 704 | { 705 | "cell_type": "code", 706 | "execution_count": null, 707 | "metadata": {}, 708 | "outputs": [], 709 | "source": [] 710 | }, 711 | { 712 | "cell_type": "markdown", 713 | "metadata": {}, 714 | "source": [ 715 | "## Homework #4\n", 716 | "---\n", 717 | "\n", 718 | "Will be announced later :)\n", 719 | "\n", 720 | "PS: Do not forget to upload your pdf files to http://derskutusu.gtu.edu.tr/\n", 721 | "\n", 722 | "---" 723 | ] 724 | }, 725 | { 726 | "cell_type": "code", 727 | "execution_count": null, 728 | "metadata": {}, 729 | "outputs": [], 730 | "source": [] 731 | } 732 | ], 733 | "metadata": { 734 | "kernelspec": { 735 | "display_name": "Python 3", 736 | "language": "python", 737 | "name": "python3" 738 | }, 739 | "language_info": { 740 | "codemirror_mode": { 741 | "name": "ipython", 742 | "version": 3 743 | }, 744 | "file_extension": ".py", 745 | "mimetype": "text/x-python", 746 | "name": "python", 747 | "nbconvert_exporter": "python", 748 | "pygments_lexer": "ipython3", 749 | "version": "3.6.8" 750 | } 751 | }, 752 | "nbformat": 4, 753 | "nbformat_minor": 4 754 | } 755 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week06-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Functions\n", 17 | " \n", 18 | "A function is a code block written to carry out a specified task, which similar with modules. Functions define in the code body, no need to import. They are generally not predefined modules. \n", 19 | "\n", 20 | "You can install a new module you need by simply using \"pip\" and call it by using \"import\" command. But you may need more complicated or specific problem to solve, than you may need your own module. \n", 21 | "\n", 22 | "With the help of **\"Functions\"**, a program break into smaller codes. So, as your program becomes longer, \"Functions\" help you to manage and organize a readable code. You can all a function more than once.\n", 23 | "\n", 24 | "![](./Figures/def_func.png)" 25 | ] 26 | }, 27 | { 28 | "cell_type": "code", 29 | "execution_count": 1, 30 | "metadata": {}, 31 | "outputs": [], 32 | "source": [ 33 | "## SYNTAX ##\n", 34 | "\n", 35 | "def function_name( arguments ):\n", 36 | " \"function_docstring\"\n", 37 | " function_body\n", 38 | " return [expression]" 39 | ] 40 | }, 41 | { 42 | "cell_type": "markdown", 43 | "metadata": {}, 44 | "source": [ 45 | "## _How Functions work in Python?_\n", 46 | "\n", 47 | "There are three types of functions in Python;\n", 48 | "\n", 49 | "**1. Built-in Functions :** \n", 50 | " These functions are always available. No need to import any module. Such as: abs(), bool(), eval(), max(), print(), help(), sum(), type().\n", 51 | "For the full list of Built-in functions, visit --> https://docs.python.org/3/library/functions.html" 52 | ] 53 | }, 54 | { 55 | "cell_type": "code", 56 | "execution_count": 2, 57 | "metadata": {}, 58 | "outputs": [ 59 | { 60 | "data": { 61 | "text/plain": [ 62 | "10" 63 | ] 64 | }, 65 | "execution_count": 2, 66 | "metadata": {}, 67 | "output_type": "execute_result" 68 | } 69 | ], 70 | "source": [ 71 | "sum([4,6])" 72 | ] 73 | }, 74 | { 75 | "cell_type": "markdown", 76 | "metadata": {}, 77 | "source": [ 78 | "**2. Anonymous Functions :**\n", 79 | " They are also called \"lambda\" functions, they do not use \"def\" keyword. It has no name.\n", 80 | " " 81 | ] 82 | }, 83 | { 84 | "cell_type": "code", 85 | "execution_count": 3, 86 | "metadata": {}, 87 | "outputs": [ 88 | { 89 | "name": "stdout", 90 | "output_type": "stream", 91 | "text": [ 92 | "Value of total : 8\n" 93 | ] 94 | } 95 | ], 96 | "source": [ 97 | "# Syntax --> lambda [arg1 [,arg2,.....argn]]:expression\n", 98 | "\n", 99 | "sum = lambda arg1, arg2: arg1 + arg2;\n", 100 | "\n", 101 | "print(\"Value of total : \", sum( 3, 5 )) # call sum as a function" 102 | ] 103 | }, 104 | { 105 | "cell_type": "code", 106 | "execution_count": 4, 107 | "metadata": {}, 108 | "outputs": [ 109 | { 110 | "data": { 111 | "text/plain": [ 112 | "81" 113 | ] 114 | }, 115 | "execution_count": 4, 116 | "metadata": {}, 117 | "output_type": "execute_result" 118 | } 119 | ], 120 | "source": [ 121 | "square = lambda x:x**2 # x is argument, x**2 is expression\n", 122 | "\n", 123 | "square(9)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": {}, 129 | "source": [ 130 | "**3. Used-Defined Functions (UDFs) :**\n", 131 | " These are functions that users create by using **_\" def \"_** command. Functions name should be unique. A Function starts with **_\" def \"_** keyword and a colon **_\" (:) \"_** marks the end of header line. They might or might not need multiple inputs, which are called Parameters or Arguments: information that are passed into a function. A parameter is the variable listed inside the paranthesis and Argument is the value that sent to the function. A function should execute the statement defined by user. It ends with **_\" return \"_** statement if it should output something, otherwise, it will return an object None. The **_\" return \"_** statement is also used to exit a function. " 132 | ] 133 | }, 134 | { 135 | "cell_type": "code", 136 | "execution_count": 5, 137 | "metadata": {}, 138 | "outputs": [], 139 | "source": [ 140 | "def function_name(parameters): \n", 141 | " statement(s) # is called the body of the function\n", 142 | " return" 143 | ] 144 | }, 145 | { 146 | "cell_type": "code", 147 | "execution_count": 6, 148 | "metadata": {}, 149 | "outputs": [], 150 | "source": [ 151 | "# Lets define a square function with UDFs to see the difference between the \"lambda function\".\n", 152 | "\n", 153 | "def square(x):\n", 154 | " return x**2" 155 | ] 156 | }, 157 | { 158 | "cell_type": "markdown", 159 | "metadata": {}, 160 | "source": [ 161 | "---\n", 162 | "## _Define a Function_\n", 163 | "\n", 164 | "*** _In this lecture, from know on, all the words called \"Functions\", refers to the \"used-defined functions (UDFs)\"._" 165 | ] 166 | }, 167 | { 168 | "cell_type": "code", 169 | "execution_count": 7, 170 | "metadata": {}, 171 | "outputs": [], 172 | "source": [ 173 | "def welcome(name):\n", 174 | " print(\"Welcome \" + name + \" !\")\n", 175 | " return" 176 | ] 177 | }, 178 | { 179 | "cell_type": "code", 180 | "execution_count": 8, 181 | "metadata": {}, 182 | "outputs": [], 183 | "source": [ 184 | "# A function could be more complex: for example; contain for loops\n", 185 | "\n", 186 | "def order():\n", 187 | " name = str(input(\"What would you like to eat as dessert?: \"))\n", 188 | " if name:\n", 189 | " print (\"You ordered: \" + str(name))\n", 190 | " else:\n", 191 | " print(\"St Sebastian Cheese-Cake\") \n", 192 | " return # return command is not necessery here." 193 | ] 194 | }, 195 | { 196 | "cell_type": "markdown", 197 | "metadata": {}, 198 | "source": [ 199 | "---\n", 200 | "## _Call a Function_\n", 201 | "\n", 202 | "Use the function name and parenthesis as shown in the example below, to call a function." 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": {}, 208 | "source": [ 209 | "### function_name([arguments])" 210 | ] 211 | }, 212 | { 213 | "cell_type": "code", 214 | "execution_count": 9, 215 | "metadata": {}, 216 | "outputs": [ 217 | { 218 | "data": { 219 | "text/plain": [ 220 | "25" 221 | ] 222 | }, 223 | "execution_count": 9, 224 | "metadata": {}, 225 | "output_type": "execute_result" 226 | } 227 | ], 228 | "source": [ 229 | "square(5)" 230 | ] 231 | }, 232 | { 233 | "cell_type": "code", 234 | "execution_count": 10, 235 | "metadata": {}, 236 | "outputs": [ 237 | { 238 | "name": "stdout", 239 | "output_type": "stream", 240 | "text": [ 241 | "Welcome Home !\n" 242 | ] 243 | } 244 | ], 245 | "source": [ 246 | "welcome(\"Home\")" 247 | ] 248 | }, 249 | { 250 | "cell_type": "code", 251 | "execution_count": 11, 252 | "metadata": {}, 253 | "outputs": [ 254 | { 255 | "name": "stdin", 256 | "output_type": "stream", 257 | "text": [ 258 | "What would you like to eat as dessert?: \n" 259 | ] 260 | }, 261 | { 262 | "name": "stdout", 263 | "output_type": "stream", 264 | "text": [ 265 | "St Sebastian Cheese-Cake\n" 266 | ] 267 | } 268 | ], 269 | "source": [ 270 | "order()" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": 12, 276 | "metadata": {}, 277 | "outputs": [ 278 | { 279 | "name": "stdin", 280 | "output_type": "stream", 281 | "text": [ 282 | "Write a number: -5\n" 283 | ] 284 | }, 285 | { 286 | "name": "stdout", 287 | "output_type": "stream", 288 | "text": [ 289 | "The number you entered is NEGATIVE\n" 290 | ] 291 | } 292 | ], 293 | "source": [ 294 | "# Lets define a function that checks even a number is positive or negative\n", 295 | "\n", 296 | "def test():\n", 297 | " num = int(input(\"Write a number: \"))\n", 298 | " if (num >= 0):\n", 299 | " print(\"The number you entered is POSITIVE\")\n", 300 | " else:\n", 301 | " print(\"The number you entered is NEGATIVE\")\n", 302 | " return\n", 303 | "test()" 304 | ] 305 | }, 306 | { 307 | "cell_type": "markdown", 308 | "metadata": {}, 309 | "source": [ 310 | "---\n", 311 | "## _Arguments_\n", 312 | "\n", 313 | "We already learned what is \"Argument\" stands for above. Its important to call function with correct number of arguments. Meaning that; the number of the arguments that function expects should be equal to the number of arguments that you call in function. " 314 | ] 315 | }, 316 | { 317 | "cell_type": "code", 318 | "execution_count": 13, 319 | "metadata": {}, 320 | "outputs": [ 321 | { 322 | "name": "stdout", 323 | "output_type": "stream", 324 | "text": [ 325 | "Jelly+fish = Jellyfish\n" 326 | ] 327 | } 328 | ], 329 | "source": [ 330 | "# Lets define a function expects 2 arguments and gets 2 arguments;\n", 331 | "\n", 332 | "def compound(first,second):\n", 333 | " print(first + '+' + second + \" = \" + first + \"\" + second)\n", 334 | "\n", 335 | "compound(\"Jelly\",\"fish\")\n" 336 | ] 337 | }, 338 | { 339 | "cell_type": "markdown", 340 | "metadata": {}, 341 | "source": [ 342 | "#### There are four types of Arguments;\n", 343 | "**1. Default Arguments**\n", 344 | " Assumes a default value if a value is not provided in the function.\n", 345 | " " 346 | ] 347 | }, 348 | { 349 | "cell_type": "code", 350 | "execution_count": 14, 351 | "metadata": {}, 352 | "outputs": [ 353 | { 354 | "data": { 355 | "text/plain": [ 356 | "8" 357 | ] 358 | }, 359 | "execution_count": 14, 360 | "metadata": {}, 361 | "output_type": "execute_result" 362 | } 363 | ], 364 | "source": [ 365 | "def defarg(x, y=5): # two argument is given one of them is \"default\", function asks for only one \n", 366 | " return (x * y)\n", 367 | "\n", 368 | "defarg(4, y=2) " 369 | ] 370 | }, 371 | { 372 | "cell_type": "markdown", 373 | "metadata": {}, 374 | "source": [ 375 | "**2. Required Arguments** (also called \"Positional Arguments\")\n", 376 | " the number of arguments in the function has to be in correct positional order and should match with the function definition." 377 | ] 378 | }, 379 | { 380 | "cell_type": "code", 381 | "execution_count": 15, 382 | "metadata": {}, 383 | "outputs": [ 384 | { 385 | "data": { 386 | "text/plain": [ 387 | "3.0" 388 | ] 389 | }, 390 | "execution_count": 15, 391 | "metadata": {}, 392 | "output_type": "execute_result" 393 | } 394 | ], 395 | "source": [ 396 | "def div(x,y): \n", 397 | " return x / y # change in order will effect the result\n", 398 | "\n", 399 | "div(9,3)" 400 | ] 401 | }, 402 | { 403 | "cell_type": "markdown", 404 | "metadata": {}, 405 | "source": [ 406 | "**3. Keyword Arguments**\n", 407 | " If you don't want to keep the right order of parameters in your mind, its easy way to identify arguments by using parameter names." 408 | ] 409 | }, 410 | { 411 | "cell_type": "code", 412 | "execution_count": 16, 413 | "metadata": {}, 414 | "outputs": [ 415 | { 416 | "data": { 417 | "text/plain": [ 418 | "2.5" 419 | ] 420 | }, 421 | "execution_count": 16, 422 | "metadata": {}, 423 | "output_type": "execute_result" 424 | } 425 | ], 426 | "source": [ 427 | "# lets call the \"div()\" function again\n", 428 | "\n", 429 | "div(x=5, y=2) # Call function with keyword arguments" 430 | ] 431 | }, 432 | { 433 | "cell_type": "code", 434 | "execution_count": 17, 435 | "metadata": {}, 436 | "outputs": [ 437 | { 438 | "data": { 439 | "text/plain": [ 440 | "2.5" 441 | ] 442 | }, 443 | "execution_count": 17, 444 | "metadata": {}, 445 | "output_type": "execute_result" 446 | } 447 | ], 448 | "source": [ 449 | "div(y=2, x=5) # with keyword arguments, parameters do not have t be in ordered" 450 | ] 451 | }, 452 | { 453 | "cell_type": "code", 454 | "execution_count": 18, 455 | "metadata": {}, 456 | "outputs": [ 457 | { 458 | "name": "stdout", 459 | "output_type": "stream", 460 | "text": [ 461 | "The first mounth of summer is June\n" 462 | ] 463 | } 464 | ], 465 | "source": [ 466 | "# if you send the arguments with the key = value syntax, the order of the arguments doesn't matter.\n", 467 | "\n", 468 | "def summer(second, third, first):\n", 469 | " print(\"The first mounth of summer is \" + first)\n", 470 | "\n", 471 | "summer(first = \"June\", second = \"July\", third = \"August\") " 472 | ] 473 | }, 474 | { 475 | "cell_type": "markdown", 476 | "metadata": {}, 477 | "source": [ 478 | "**4. Arbitrary Arguments**\n", 479 | " If you don't have any idea about the exact number of arguments to call the function, then you can use **\" *args \"** syntax." 480 | ] 481 | }, 482 | { 483 | "cell_type": "code", 484 | "execution_count": 19, 485 | "metadata": {}, 486 | "outputs": [ 487 | { 488 | "name": "stdout", 489 | "output_type": "stream", 490 | "text": [ 491 | "Keep\n", 492 | " your\n", 493 | " social\n", 494 | " distance\n", 495 | " !\n" 496 | ] 497 | } 498 | ], 499 | "source": [ 500 | "def message(*argv): \n", 501 | " for arg in argv: \n", 502 | " print(arg) \n", 503 | "\n", 504 | "message(\"Keep\",\" your\",\" social\",\" distance\",\" !\")" 505 | ] 506 | }, 507 | { 508 | "cell_type": "markdown", 509 | "metadata": {}, 510 | "source": [ 511 | "---\n", 512 | "## _The \"return\" Statement_\n", 513 | "\n", 514 | "Simply it prints the result to the console. But, if you want to assign the result to a variable, the return statement sends a result back to the caller and exist the function." 515 | ] 516 | }, 517 | { 518 | "cell_type": "code", 519 | "execution_count": 20, 520 | "metadata": {}, 521 | "outputs": [ 522 | { 523 | "name": "stdout", 524 | "output_type": "stream", 525 | "text": [ 526 | "15\n" 527 | ] 528 | } 529 | ], 530 | "source": [ 531 | "# lets define a total function which calculates the summation of numbers between two specific numbers\n", 532 | "\n", 533 | "def total(num1, num2):\n", 534 | " sum = 0\n", 535 | " for i in range(num1, num2 + 1):\n", 536 | " sum += i\n", 537 | " return sum\n", 538 | "\n", 539 | "t = total(1, 5)\n", 540 | "print(t)\n" 541 | ] 542 | }, 543 | { 544 | "cell_type": "markdown", 545 | "metadata": {}, 546 | "source": [ 547 | "---\n", 548 | "## _Recursive Function_" 549 | ] 550 | }, 551 | { 552 | "cell_type": "markdown", 553 | "metadata": {}, 554 | "source": [ 555 | "A function can call other functions or can call itself either. These type of functions are called _recursive functions_.\n", 556 | "\n", 557 | "These functions make the code look elegant but its hard to follow the logic behind." 558 | ] 559 | }, 560 | { 561 | "cell_type": "code", 562 | "execution_count": 25, 563 | "metadata": {}, 564 | "outputs": [ 565 | { 566 | "name": "stdout", 567 | "output_type": "stream", 568 | "text": [ 569 | "5 * 4 * 3 * 2 * n = 1\n" 570 | ] 571 | }, 572 | { 573 | "data": { 574 | "text/plain": [ 575 | "120" 576 | ] 577 | }, 578 | "execution_count": 25, 579 | "metadata": {}, 580 | "output_type": "execute_result" 581 | } 582 | ], 583 | "source": [ 584 | "def factorial(n): # define recursive function\n", 585 | " if n == 1: \n", 586 | " print(\"n = \", n)\n", 587 | " return n \n", 588 | " else:\n", 589 | " print (n,'*', end = \" \") \n", 590 | " return n * factorial(n-1)\n", 591 | "\n", 592 | "factorial(5)" 593 | ] 594 | }, 595 | { 596 | "cell_type": "code", 597 | "execution_count": 22, 598 | "metadata": {}, 599 | "outputs": [ 600 | { 601 | "name": "stdout", 602 | "output_type": "stream", 603 | "text": [ 604 | "12 10 8 6 4 2 2 4 6 8 10 12 " 605 | ] 606 | } 607 | ], 608 | "source": [ 609 | "def mirror(x):\n", 610 | " if (x < 1):\n", 611 | " return\n", 612 | " else:\n", 613 | " print( x,end = \" \")\n", 614 | " mirror(x-2)\n", 615 | " print(x,end = \" \")\n", 616 | " return\n", 617 | "x = 12\n", 618 | "mirror(x)" 619 | ] 620 | }, 621 | { 622 | "cell_type": "markdown", 623 | "metadata": {}, 624 | "source": [ 625 | "---\n", 626 | "## _Global and Local Variables_\n", 627 | "\n", 628 | "A variable declared inside a function is called **_local variable_**, while a variable not bound to any function but can be accessed outside the function is called **_global variable_**." 629 | ] 630 | }, 631 | { 632 | "cell_type": "code", 633 | "execution_count": 23, 634 | "metadata": {}, 635 | "outputs": [ 636 | { 637 | "name": "stdout", 638 | "output_type": "stream", 639 | "text": [ 640 | "15\n" 641 | ] 642 | } 643 | ], 644 | "source": [ 645 | "global_var = 3\n", 646 | "\n", 647 | "def cool():\n", 648 | " local_var = 5 # local variable\n", 649 | " print(global_var * local_var) # both variables are accessible from inside\n", 650 | "\n", 651 | "cool() \n", 652 | " # when function ends, local_var is destroyed\n", 653 | "#print(local_var) # but you can't access local_var outside the function" 654 | ] 655 | }, 656 | { 657 | "cell_type": "markdown", 658 | "metadata": {}, 659 | "source": [ 660 | "---\n", 661 | "## _BONUS_ function\n", 662 | "\n", 663 | "#### **map() function**" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "execution_count": 24, 669 | "metadata": {}, 670 | "outputs": [ 671 | { 672 | "data": { 673 | "text/plain": [ 674 | "[1.0, 2.0, 3.0, 4.0, 5.0]" 675 | ] 676 | }, 677 | "execution_count": 24, 678 | "metadata": {}, 679 | "output_type": "execute_result" 680 | } 681 | ], 682 | "source": [ 683 | "# map() function; applies a function to every item in the array, if you have \"list\" of values\n", 684 | "# Lets define \"div()\" function again\n", 685 | "\n", 686 | "def div(x,y=2): \n", 687 | " return x / y\n", 688 | "\n", 689 | "data = [2, 4, 6, 8, 10] # declare an array of numbers\n", 690 | "list(map(div,data)) # no need to write a loop " 691 | ] 692 | }, 693 | { 694 | "cell_type": "markdown", 695 | "metadata": {}, 696 | "source": [ 697 | "---\n", 698 | "## Homework #6\n", 699 | "\n", 700 | "Please visit https://covid19.saglik.gov.tr/ web page and get the information you need for your assignment. By using this data, lets make some statistics about the COVID-19 spreading speed in Turkey. Please follow the instructions below;\n", 701 | "\n", 702 | "1. Define a function which calculates \"percentage\".\n", 703 | "2. Get the daily based numbers for covid-19 cases, deaths, recovered people and covid-tests. \n", 704 | "3. Calculate percentages between case-death in daily bases and draw a graph. \n", 705 | "\n", 706 | "You are free to use any type of visualization, use your imagination. The shortest code will get extra point. \n", 707 | "\n", 708 | "PS: Do not forget to upload your pdf files to Microsoft Teams-CE103 Class.\n", 709 | "\n", 710 | "The attached screenshots will guide you to get the numeric data you need from the https://covid19.saglik.gov.tr/ web page. \n", 711 | "\n", 712 | "![](./Figures/hw6_screenshot.png)\n", 713 | "![](./Figures/hw6_screenshot2.png)\n", 714 | "---" 715 | ] 716 | }, 717 | { 718 | "cell_type": "code", 719 | "execution_count": null, 720 | "metadata": {}, 721 | "outputs": [], 722 | "source": [] 723 | } 724 | ], 725 | "metadata": { 726 | "kernelspec": { 727 | "display_name": "Python 3", 728 | "language": "python", 729 | "name": "python3" 730 | }, 731 | "language_info": { 732 | "codemirror_mode": { 733 | "name": "ipython", 734 | "version": 3 735 | }, 736 | "file_extension": ".py", 737 | "mimetype": "text/x-python", 738 | "name": "python", 739 | "nbconvert_exporter": "python", 740 | "pygments_lexer": "ipython3", 741 | "version": "3.6.8" 742 | } 743 | }, 744 | "nbformat": 4, 745 | "nbformat_minor": 4 746 | } 747 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week12-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Object Oriented Programming\n", 17 | " - CLASSES & OBJECTS\n", 18 | "\n", 19 | "\n", 20 | "Python is an object oriented programming language. **_Object Oriented Programming_** can be thought of as simply adapting real life to programming. For example, let's imagine that we prepared building stock software. For this, \"reinforced concrete\", \"steel\" and \"masonry\" must be created as **_objects_**. In order to realize such a system by programming, we actually need to define the structure of each object and then produce objects from these structures. When we look around, we see that each item is actually an object. These objects have different properties (**_attributes_**) and functions (**_methods_**).\n", 21 | "\n", 22 | "In order to create our own data types and to produce objects from these data types, we first need to define the structure in which we will produce the objects. This structure is called **_\"class\"_**. Classes are a structure where we define the properties and methods of objects while creating our **_objects_**. The property of \"Reinforced Concrete\" object can be exemplified as strength and its function can be exemplified as modulus of elasticity.\n", 23 | "\n", 24 | "Classes are used to define groups or sets with common properties. For example, all cars have features such as a brand, model, price, color.\n", 25 | "\n", 26 | "### _\" CLASS \"_ a code template for creating objects.\n", 27 | "\n", 28 | "### _\" OBJECT \"_ the instance of the class" 29 | ] 30 | }, 31 | { 32 | "cell_type": "markdown", 33 | "metadata": {}, 34 | "source": [ 35 | "---" 36 | ] 37 | }, 38 | { 39 | "cell_type": "code", 40 | "execution_count": 85, 41 | "metadata": {}, 42 | "outputs": [ 43 | { 44 | "name": "stdout", 45 | "output_type": "stream", 46 | "text": [ 47 | "<__main__.MyClass_Name object at 0x7f9dc84320f0>\n" 48 | ] 49 | } 50 | ], 51 | "source": [ 52 | "# Python uses word \"class\" to define an empty class with no functionalities\n", 53 | "\n", 54 | "class MyClass_Name:\n", 55 | " '''This is a docstring line for description'''\n", 56 | " pass\n", 57 | "\n", 58 | "instanse = MyClass_Name() # object is created using the constructor of class\n", 59 | "print(instanse)" 60 | ] 61 | }, 62 | { 63 | "cell_type": "markdown", 64 | "metadata": {}, 65 | "source": [ 66 | "# Class definition" 67 | ] 68 | }, 69 | { 70 | "cell_type": "code", 71 | "execution_count": 86, 72 | "metadata": {}, 73 | "outputs": [], 74 | "source": [ 75 | "class firstclass():\n", 76 | " 'Lest define a class'\n", 77 | " pass\n" 78 | ] 79 | }, 80 | { 81 | "cell_type": "markdown", 82 | "metadata": {}, 83 | "source": [ 84 | "# Attributes and Methods in class" 85 | ] 86 | }, 87 | { 88 | "cell_type": "code", 89 | "execution_count": 92, 90 | "metadata": {}, 91 | "outputs": [], 92 | "source": [ 93 | "# Lets add some attributes or properties to this class\n", 94 | "\n", 95 | "class Lecture:\n", 96 | " Department = \"\" # class attributes\n", 97 | " Teacher = \"\" # class attributes \n", 98 | " Level = \"\" # class attributes\n", 99 | "\n", 100 | "# Lets define an object and its properties\n", 101 | "\n", 102 | "CE_103 = Lecture() # CE_103 is an instance (example) of the Class \"Lecture\"\n", 103 | "CE_103.Department = \"Civil Engineering\"\n", 104 | "CE_103.Name = \"Python Programming\"\n", 105 | "CE_103.Level = \"UnderGraduate\"\n", 106 | "\n", 107 | "# Lets define another object to same class\n", 108 | "\n", 109 | "DYB_582 = Lecture()\n", 110 | "DYB_582.Department = \"Civil Engineering\"\n", 111 | "DYB_582.Name = \"Engineering Seismology\"\n", 112 | "DYB_582.Level = \"Graduate\"\n" 113 | ] 114 | }, 115 | { 116 | "cell_type": "code", 117 | "execution_count": 93, 118 | "metadata": {}, 119 | "outputs": [ 120 | { 121 | "name": "stdout", 122 | "output_type": "stream", 123 | "text": [ 124 | "-------CE_103-----\n", 125 | "Department: Civil Engineering \n", 126 | "Name :Python Programming\n", 127 | "Level :UnderGraduate\n", 128 | "-------DYB_582------\n", 129 | "Department: Civil Engineering \n", 130 | "Name :Engineering Seismology\n", 131 | "Level :Graduate\n" 132 | ] 133 | } 134 | ], 135 | "source": [ 136 | "print(\"-------CE_103-----\")\n", 137 | "print(\"Department: {} \\nName :{}\\nLevel :{}\".format(CE_103.Department,CE_103.Name,CE_103.Level))\n", 138 | " \n", 139 | "print(\"-------DYB_582------\")\n", 140 | "print(\"Department: {} \\nName :{}\\nLevel :{}\".format(DYB_582.Department,DYB_582.Name,DYB_582.Level))\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": {}, 146 | "source": [ 147 | "---\n", 148 | "In object oriented programming languages, the most effective tool is; when you write it once you can call it more than ones." 149 | ] 150 | }, 151 | { 152 | "cell_type": "code", 153 | "execution_count": 96, 154 | "metadata": {}, 155 | "outputs": [], 156 | "source": [ 157 | "# lets add some function or methods to our object\n", 158 | "\n", 159 | "def lab_credit(self): # first function in Class always starts with keyword \"self\"\n", 160 | " self.lab += 1\n", 161 | " return self.lab" 162 | ] 163 | }, 164 | { 165 | "cell_type": "code", 166 | "execution_count": 97, 167 | "metadata": {}, 168 | "outputs": [], 169 | "source": [ 170 | "# lets write function into the class\n", 171 | "\n", 172 | "class Lecture:\n", 173 | " Department = \"\"\n", 174 | " Teacher = \"\"\n", 175 | " Level = \"\"\n", 176 | " lab = 0\n", 177 | " \n", 178 | " def lab_credit(self): # first function in Class always starts with keyword \"self\"\n", 179 | " self.lab += 1\n", 180 | " return self.lab\n", 181 | "\n", 182 | " # Lets define an object ant its properties\n", 183 | "\n", 184 | "CE_103 = Lecture()\n", 185 | "CE_103.Department = \"Civil Engineering\"\n", 186 | "CE_103.Name = \"Python Programming\"\n", 187 | "CE_103.Level = \"UnderGraduate\"\n" 188 | ] 189 | }, 190 | { 191 | "cell_type": "code", 192 | "execution_count": 98, 193 | "metadata": {}, 194 | "outputs": [ 195 | { 196 | "data": { 197 | "text/plain": [ 198 | "1" 199 | ] 200 | }, 201 | "execution_count": 98, 202 | "metadata": {}, 203 | "output_type": "execute_result" 204 | } 205 | ], 206 | "source": [ 207 | "CE_103.lab_credit() # call the function" 208 | ] 209 | }, 210 | { 211 | "cell_type": "code", 212 | "execution_count": 99, 213 | "metadata": {}, 214 | "outputs": [ 215 | { 216 | "name": "stdout", 217 | "output_type": "stream", 218 | "text": [ 219 | "-------CE_103-----\n", 220 | "Department: Civil Engineering \n", 221 | "Name :Python Programming\n", 222 | "Level :UnderGraduate\n", 223 | "Lab credit : 2\n" 224 | ] 225 | } 226 | ], 227 | "source": [ 228 | "print(\"-------CE_103-----\")\n", 229 | "print(\"Department: {} \\nName :{}\\nLevel :{}\".format(CE_103.Department,CE_103.Name,CE_103.Level))\n", 230 | "\n", 231 | "CE_103.lab_credit()\n", 232 | "print(\"Lab credit :\", CE_103.lab)" 233 | ] 234 | }, 235 | { 236 | "cell_type": "markdown", 237 | "metadata": {}, 238 | "source": [ 239 | "---\n", 240 | "Think that a class as blueprint of specific objects. These objects has to be created in the class. Assume a \"house\" as a class. Class variable is a variable that is shared by all different objects of class. Such as \"roof\", \"column\", \"foundation\". Instance variables are unique to each instance of a class. Such as \"garage\", \"doghouse\", \"dining room\". Functions, or methods, describes the behaviour of an object. In our example, this could be the location of the house. \n", 241 | "\n", 242 | "![alt text here](./Figures/class.png)\n", 243 | "\n" 244 | ] 245 | }, 246 | { 247 | "cell_type": "markdown", 248 | "metadata": {}, 249 | "source": [ 250 | "---\n", 251 | "While creating the object we may also define its properties, such as;" 252 | ] 253 | }, 254 | { 255 | "cell_type": "code", 256 | "execution_count": 41, 257 | "metadata": {}, 258 | "outputs": [ 259 | { 260 | "name": "stdout", 261 | "output_type": "stream", 262 | "text": [ 263 | "-------EQE_576------\n", 264 | "Department: Civil Engineering \n", 265 | "Level :Graduate\n", 266 | "-------DYB_523------\n", 267 | "Department: Civil Engineering \n", 268 | "Level :Graduate\n" 269 | ] 270 | } 271 | ], 272 | "source": [ 273 | "class Lecture:\n", 274 | " Department = \"Civil Engineering\"\n", 275 | " Level = \"Graduate\"\n", 276 | " \n", 277 | "EQE_576 = Lecture() # object creating\n", 278 | "DYB_523 = Lecture()\n", 279 | "\n", 280 | "print(\"-------EQE_576------\")\n", 281 | "print(\"Department: {} \\nLevel :{}\".format(EQE_576.Department,EQE_576.Level))\n", 282 | "\n", 283 | "print(\"-------DYB_523------\")\n", 284 | "print(\"Department: {} \\nLevel :{}\".format(DYB_523.Department,DYB_523.Level))\n" 285 | ] 286 | }, 287 | { 288 | "cell_type": "markdown", 289 | "metadata": {}, 290 | "source": [ 291 | "---\n", 292 | "BUT, in this situation, both objects have same properties! What if I would like to define different properties for each object ??\n", 293 | "\n", 294 | "# **__ init __()** function\n", 295 | "\n", 296 | "We need \"__ init __()\" and \"self\" to exemplified the properties of an object. The first parameter of this function must always be \"self\"." 297 | ] 298 | }, 299 | { 300 | "cell_type": "code", 301 | "execution_count": 5, 302 | "metadata": {}, 303 | "outputs": [ 304 | { 305 | "name": "stdout", 306 | "output_type": "stream", 307 | "text": [ 308 | "Kuzey.Guney@2005gtu.edu.tr\n", 309 | "Cinar.Selvi@2011gtu.edu.tr\n" 310 | ] 311 | } 312 | ], 313 | "source": [ 314 | "# Up to know we learned class attribute, but there also object attributes. \n", 315 | "# In order to define object attributes, I need __init__() function and self.\n", 316 | "\n", 317 | "class student:\n", 318 | " def __init__(self, first, last, enroll): # set instance variables \"self, first, last, enroll\"\n", 319 | " self.fname=first\n", 320 | " self.lname=last\n", 321 | " self.enroll_year=enroll\n", 322 | " self.email=first + '.' + last + '@' + enroll + 'gtu.edu.tr'\n", 323 | "\n", 324 | "undgra1 = student('Kuzey', 'Guney', '2005')\n", 325 | "undgra2 = student('Cinar', 'Selvi', '2011')\n", 326 | "\n", 327 | "print(undgra1.email)\n", 328 | "print(undgra2.email)" 329 | ] 330 | }, 331 | { 332 | "cell_type": "code", 333 | "execution_count": 1, 334 | "metadata": {}, 335 | "outputs": [ 336 | { 337 | "name": "stdout", 338 | "output_type": "stream", 339 | "text": [ 340 | "KuzeyGuney\n", 341 | "CinarSelvi\n" 342 | ] 343 | } 344 | ], 345 | "source": [ 346 | "# Instances of student class have created. Now,let's add some action, to do so, we will add a method to this class.\n", 347 | "# Asuume that we would like to display full name of student.\n", 348 | "\n", 349 | "class student:\n", 350 | " def __init__(self, first, last, enroll): # set instance variables \"self, first, last, enroll\"\n", 351 | " self.fname=first\n", 352 | " self.lname=last\n", 353 | " self.enroll_year=enroll\n", 354 | " self.email=first + '.' + last + '@' + enroll + 'gtu.edu.tr'\n", 355 | " \n", 356 | " def fullname(self):\n", 357 | " return '{}{}'.format(self.fname, self.lname)\n", 358 | "\n", 359 | "undgra1 = student('Kuzey', 'Guney', '2005')\n", 360 | "undgra2 = student('Cinar', 'Selvi', '2011') \n", 361 | "\n", 362 | "print(undgra1.fullname())\n", 363 | "print(undgra2.fullname())" 364 | ] 365 | }, 366 | { 367 | "cell_type": "code", 368 | "execution_count": 8, 369 | "metadata": {}, 370 | "outputs": [], 371 | "source": [ 372 | "Kemal = student('Kemal','Durmaz','2009')" 373 | ] 374 | }, 375 | { 376 | "cell_type": "code", 377 | "execution_count": 9, 378 | "metadata": {}, 379 | "outputs": [ 380 | { 381 | "data": { 382 | "text/plain": [ 383 | "<__main__.student at 0x7fdab81df8d0>" 384 | ] 385 | }, 386 | "execution_count": 9, 387 | "metadata": {}, 388 | "output_type": "execute_result" 389 | } 390 | ], 391 | "source": [ 392 | "Kemal" 393 | ] 394 | }, 395 | { 396 | "cell_type": "code", 397 | "execution_count": 10, 398 | "metadata": {}, 399 | "outputs": [ 400 | { 401 | "data": { 402 | "text/plain": [ 403 | "['__class__',\n", 404 | " '__delattr__',\n", 405 | " '__dict__',\n", 406 | " '__dir__',\n", 407 | " '__doc__',\n", 408 | " '__eq__',\n", 409 | " '__format__',\n", 410 | " '__ge__',\n", 411 | " '__getattribute__',\n", 412 | " '__gt__',\n", 413 | " '__hash__',\n", 414 | " '__init__',\n", 415 | " '__init_subclass__',\n", 416 | " '__le__',\n", 417 | " '__lt__',\n", 418 | " '__module__',\n", 419 | " '__ne__',\n", 420 | " '__new__',\n", 421 | " '__reduce__',\n", 422 | " '__reduce_ex__',\n", 423 | " '__repr__',\n", 424 | " '__setattr__',\n", 425 | " '__sizeof__',\n", 426 | " '__str__',\n", 427 | " '__subclasshook__',\n", 428 | " '__weakref__',\n", 429 | " 'email',\n", 430 | " 'enroll_year',\n", 431 | " 'fname',\n", 432 | " 'fullname',\n", 433 | " 'lname']" 434 | ] 435 | }, 436 | "execution_count": 10, 437 | "metadata": {}, 438 | "output_type": "execute_result" 439 | } 440 | ], 441 | "source": [ 442 | "dir(Kemal)" 443 | ] 444 | }, 445 | { 446 | "cell_type": "code", 447 | "execution_count": 11, 448 | "metadata": {}, 449 | "outputs": [ 450 | { 451 | "data": { 452 | "text/plain": [ 453 | "'Kemal.Durmaz@2009gtu.edu.tr'" 454 | ] 455 | }, 456 | "execution_count": 11, 457 | "metadata": {}, 458 | "output_type": "execute_result" 459 | } 460 | ], 461 | "source": [ 462 | "kemal.email" 463 | ] 464 | }, 465 | { 466 | "cell_type": "markdown", 467 | "metadata": {}, 468 | "source": [ 469 | "---\n", 470 | "## Homework #12\n", 471 | "\n", 472 | "Create your own python module by defining a class for Mechanical properties of truss.\n", 473 | "\n", 474 | "These properties will be:\n", 475 | "\n", 476 | " 1. Area\n", 477 | " 2. Perimeter\n", 478 | " 3. location of center of gravity\n", 479 | " 4. Moment of Inertia\n", 480 | " 5. Moment of Inertia with respect to axes\n", 481 | " 6. Moment of inertia on principal axes\n", 482 | "\n", 483 | "PS : Do not forget to upload your answer sheets to CE_103 Class on MS Teams.\n" 484 | ] 485 | } 486 | ], 487 | "metadata": { 488 | "kernelspec": { 489 | "display_name": "Python 3", 490 | "language": "python", 491 | "name": "python3" 492 | }, 493 | "language_info": { 494 | "codemirror_mode": { 495 | "name": "ipython", 496 | "version": 3 497 | }, 498 | "file_extension": ".py", 499 | "mimetype": "text/x-python", 500 | "name": "python", 501 | "nbconvert_exporter": "python", 502 | "pygments_lexer": "ipython3", 503 | "version": "3.6.8" 504 | } 505 | }, 506 | "nbformat": 4, 507 | "nbformat_minor": 4 508 | } 509 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week13-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Week 13 _ In Class Assigment I\n", 15 | "\n", 16 | "\n", 17 | "Dear students, \n", 18 | "\n", 19 | "You have a week to complete your first In Class assignment. You will be working as teams, please be sure that all of you as a team member spend same effort for this assignment. \n", 20 | "\n", 21 | "Please follow the intructions below and do not hesitate to ask question, if any, during your process. \n", 22 | "\n", 23 | "1 - Download the \"Sensor Record\" application to your Android cell phones.\n", 24 | "\n", 25 | "![](./Figures/sensorrecord.png)\n", 26 | "\n", 27 | "\n", 28 | "2 - Run the application and select 'Accelerometer' to record data. This application will record your data and will save it as file in \".csv\" format. \n", 29 | "\n", 30 | "\n", 31 | "3 - Stand up and hold your phone tight in your hand. Withput rotating your hand, slowly make a pendulum action (minimum 3 seconds period) with arm by moving back and forth. Do repeat this movement at least for 30 seconds. \n", 32 | "\n", 33 | "![](./Figures/swinging_arm.png)\n", 34 | "\n", 35 | "\n", 36 | "4 - Transfer the movement record data to your PC. Analyse the data and plot time series figure. Calculate and plot the magnitude of your pendulum movement. \n", 37 | "\n", 38 | "**_Hints:_** \n", 39 | "\n", 40 | "a. Remember the theoritical information from CE105 course.\n", 41 | "\n", 42 | "b. You may want to read the reference paper below to get help in detail : \n", 43 | "\n", 44 | "\n", 45 | "https://www.researchgate.net/publication/313164057_Step_Counting_Using_Smartphone_Accelerometer_and_Fast_Fourier_Trransform\n", 46 | "\n" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "---" 54 | ] 55 | } 56 | ], 57 | "metadata": { 58 | "kernelspec": { 59 | "display_name": "Python 3", 60 | "language": "python", 61 | "name": "python3" 62 | }, 63 | "language_info": { 64 | "codemirror_mode": { 65 | "name": "ipython", 66 | "version": 3 67 | }, 68 | "file_extension": ".py", 69 | "mimetype": "text/x-python", 70 | "name": "python", 71 | "nbconvert_exporter": "python", 72 | "pygments_lexer": "ipython3", 73 | "version": "3.6.8" 74 | } 75 | }, 76 | "nbformat": 4, 77 | "nbformat_minor": 4 78 | } 79 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/CE103-Week14-checkpoint.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "# Week 14 _ In Class Assigment II\n", 15 | "\n", 16 | "\n", 17 | "Dear students, \n", 18 | "\n", 19 | "You have a week to complete your second In Class assignment. You will be working as teams, please be sure that all of you as a team member spend same effort for this assignment. \n", 20 | "\n", 21 | "\n", 22 | "**Title of the task: \"Create your own step counter (pedometer)\"**\n", 23 | "\n", 24 | "Please follow the intructions below and do not hesitate to ask question, if any, during your process. \n", 25 | "\n", 26 | "\n", 27 | "1 - Use the \"Sensor Record\" application that you used for the previous project.\n", 28 | "\n", 29 | "![](./Figures/sensorrecord.png)\n", 30 | "\n", 31 | "\n", 32 | "2 - Run the application and select 'Accelerometer' to record data and save it as file in \".csv\" format. \n", 33 | "\n", 34 | "\n", 35 | "3 - Stand up and start walking while holding your phone tight in your hand. If you start walking, your arms will move forth and backward similar to a pendulum. Interestingly, when you step your left foot forward, your right arm will be on forward, too. Next, your right foot will move with your left arm. The movement of your arms will eventually create a pendulum motion and also lateral translation. Do walk more then 2 minutes and count your steps. \n", 36 | "\n", 37 | "![](./Figures/swinging_arm.png)\n", 38 | "\n", 39 | "\n", 40 | "4 - Transfer the movement record data to your PC. Analyse the data and plot time series figure. Creeate your own step counting module. Conclude your assignment by comparing the steps you counted with the results from your data analysis.\n" 41 | ] 42 | }, 43 | { 44 | "cell_type": "markdown", 45 | "metadata": {}, 46 | "source": [ 47 | "---" 48 | ] 49 | } 50 | ], 51 | "metadata": { 52 | "kernelspec": { 53 | "display_name": "Python 3", 54 | "language": "python", 55 | "name": "python3" 56 | }, 57 | "language_info": { 58 | "codemirror_mode": { 59 | "name": "ipython", 60 | "version": 3 61 | }, 62 | "file_extension": ".py", 63 | "mimetype": "text/x-python", 64 | "name": "python", 65 | "nbconvert_exporter": "python", 66 | "pygments_lexer": "ipython3", 67 | "version": "3.6.8" 68 | } 69 | }, 70 | "nbformat": 4, 71 | "nbformat_minor": 4 72 | } 73 | -------------------------------------------------------------------------------- /.ipynb_checkpoints/README-checkpoint.md: -------------------------------------------------------------------------------- 1 |

Gebze Technical University | Civil Engineering Department | CE_103 Introduction to computers and programming for civil engineers

2 | 3 | drawing 4 | -------------------------------------------------------------------------------- /CE103-Week01-Introduction.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "cells": [ 3 | { 4 | "cell_type": "markdown", 5 | "metadata": {}, 6 | "source": [ 7 | "### CE 103- INTRODUCTION TO COMPUTERS and PROGRAMMING" 8 | ] 9 | }, 10 | { 11 | "cell_type": "markdown", 12 | "metadata": {}, 13 | "source": [ 14 | "### _The topics of the week !_\n", 15 | "\n", 16 | "- Programming Algorithm\n", 17 | "- Launching Python, Variable, List\n", 18 | "- Interactive Python as a Calculator\n", 19 | "- Homework #1 assignment" 20 | ] 21 | }, 22 | { 23 | "cell_type": "markdown", 24 | "metadata": {}, 25 | "source": [ 26 | "---\n", 27 | "## Programming Algorithm\n", 28 | "\n", 29 | "Algorithms tell the programmers how to code the program. It is important editting to a strong algorithm than coding a program. Flowchart represents a diagram that graphically depicts the steps in a program.​An algorithm steps basically be like as below.\n", 30 | "\n", 31 | "![](../Figures/algorithm.png) \n", 32 | "\n", 33 | "Figure 1 : Flowchart diagram of basic programming seps. Ovals are terminal symbols, Parallelograms are input and output symbols, Rectangles are processing symbols, Symbols are connected by arrows that represent the flow of the program, Statements in a program execute in the order that they appear\n", 34 | "\n", 35 | "\n", 36 | "### Typical steps of programming:\n", 37 | "\n", 38 | ">Design the program​\n", 39 | "\n", 40 | ">>Write the code​\n", 41 | "\n", 42 | ">>>Correct syntax errors​\n", 43 | "\n", 44 | ">>>>Test the program​\n", 45 | "\n", 46 | ">>>>>Correct logic errors​" 47 | ] 48 | }, 49 | { 50 | "cell_type": "markdown", 51 | "metadata": {}, 52 | "source": [ 53 | "---\n", 54 | "## Introduction to Python\n", 55 | "\n", 56 | "Python is a open source general-purpose programming language. It is available across many platforms including Windows, Linux and Mac OS.\n", 57 | "\n", 58 | "The history of the Python programming language dates back to the late 1980s and its implementation was started in December 1989 by Guido van Rossum. \n", 59 | "\n", 60 | "The version of Python (i.e., Python 1.0) was introduced in 1991 and its eveluation has reached up to Version 3.x (till 2019).\n", 61 | "\n", 62 | "\n", 63 | "

\n", 64 | "\n", 65 | "Figure 2 : Photograph of Guido van Rossum—Creator. (Image courtesy of Wikipedia.org).\n", 66 | "\n", 67 | "https://www.linkedin.com/in/guido-van-rossum-4a0756/\n", 68 | "\n", 69 | "

\n" 70 | ] 71 | }, 72 | { 73 | "cell_type": "markdown", 74 | "metadata": {}, 75 | "source": [ 76 | "---\n", 77 | "### Scientific Tools of Python\n", 78 | "\n", 79 | "The Python ecosystem consists of these core scientific tools which are essential for analysing and simulating complex systems, these packages are SciPy library,SciPy, NumPy, Sympy and Matplotlib. **SciPy** library is mainly used for numerical integration and optimization. **NumPy** provides N-dimensional array objects which can be used to perform linear algebra, Fourier transform and other mathematical operations. **SymPy** library is used to generate symbolic mathematics. **Matplotlib** is the oldest and most popular plotting library available for Python. These tools are usefull for solving scientific problems.\n" 80 | ] 81 | }, 82 | { 83 | "cell_type": "markdown", 84 | "metadata": {}, 85 | "source": [ 86 | "---\n", 87 | "## Launching Python \n", 88 | "\n" 89 | ] 90 | }, 91 | { 92 | "cell_type": "markdown", 93 | "metadata": {}, 94 | "source": [ 95 | "There are several interfaces that allows us to use Python interactively. \n", 96 | "\n", 97 | "\n", 98 | "1. Anaconda Navigator - SPYDER\n", 99 | " (www.anaconda.com -> “Products” -> “Individual Edition” -> \"Downloads\") select your OS.\n", 100 | " \n", 101 | "

\n", 102 | "\n", 103 | "2. IPython (Terminal)\n", 104 | "\n", 105 | "\n", 106 | "3. Jupyter Lab or Jupyter Notebook (https://jupyter.org/)\n", 107 | "\n", 108 | "

\n", 109 | "\n", 110 | "4. Google COLAB (https://colab.research.google.com/?utm_source=scs-index)\n", 111 | "\n", 112 | "

\n", 113 | "\n" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": {}, 119 | "source": [ 120 | "---\n", 121 | "## Installing and Using Jupyter Notebook\n", 122 | "Jupyter Notebook is an open source web application that uses a browserbased interface and it is one of the many possible ways to interact with Python and the scientific libraries. \n", 123 | "\n", 124 | "You can :\n", 125 | "\n", 126 | "• create and share documents,\n", 127 | "\n", 128 | "• write and execute Python code,\n", 129 | "\n", 130 | "• include tables, figures, equation, visualizations, etc. with Jupyter which is fast processor in the scientific computing algorithms." 131 | ] 132 | }, 133 | { 134 | "cell_type": "markdown", 135 | "metadata": {}, 136 | "source": [ 137 | "---" 138 | ] 139 | }, 140 | { 141 | "cell_type": "markdown", 142 | "metadata": {}, 143 | "source": [ 144 | "### Crash entry to programming world of PYTHON\n", 145 | "\n", 146 | "\n", 147 | "Basic reference tools for Python and Notebook\n", 148 | "\n", 149 | "If you prefer to use your local Python, type the coomand below in to your _terminal window_ to run your python code.\n", 150 | "\n", 151 | "$ **python** **example.py**\n", 152 | "\n", 153 | ",or, \n", 154 | "\n", 155 | "if you are using notebook interface,\n", 156 | "\n", 157 | "_**Shift + Enter**_ --> execute cell and jump to the next one\n", 158 | "\n", 159 | "_**Ctrl/Cmd + Enter**_ --> execute cell and don't jump to the next cell\n", 160 | "\n", 161 | "To write outputs in to the screen or plot figures;\n", 162 | "\n", 163 | "**print**(**\"Hello\"**)" 164 | ] 165 | }, 166 | { 167 | "cell_type": "markdown", 168 | "metadata": {}, 169 | "source": [ 170 | "---\n", 171 | "## Python Programming Fundamentals\n", 172 | "\n", 173 | "Lets discuss the fundamentals of Python Programming language that we need to know to start writing basic Python codes. These are identifiers, keywords, variables and operators.\n", 174 | "\n", 175 | "### Identifiers\n", 176 | "An identifier is a name given to a variable, function, class or module. Here are the rules that we should know to set an identifier.\n", 177 | "\n", 178 | "• Can begin with an alphabet (A – Z and a – z and _) and be a combination of lowercase or uppercase or digits.\n", 179 | "\n", 180 | "• Do not start with a digit but can used in anywhere else such as legend1 can allow but 1legend is invalid.\n", 181 | "\n", 182 | "• Keywords cannot be used as identifiers.\n", 183 | "\n", 184 | "• Do not use spaces and special symbols like !, @, #, $, % etc. as identifiers.\n", 185 | "\n", 186 | "• Identifier can be of any length.\n", 187 | "\n", 188 | "### Keywords\n", 189 | "\n", 190 | "Keywords are a list of reserved words that have predefined meaning. They are special vocabulary and should not be use as an identifier name which will cause an error. (__del, and, def, False, break__ etc.)\n", 191 | "\n", 192 | "![](../Figures/keywords.png)\n", 193 | "\n", 194 | "### Variables\n", 195 | "\n", 196 | "Variable is a placeholder to hold any type of data.\n", 197 | "\n", 198 | "• Can consist of any number of letters, underscores and digits.\n", 199 | "\n", 200 | "• Should not start with a number.\n", 201 | "\n", 202 | "• Keywords are not assigned as variables\n", 203 | "\n", 204 | "• They are case-sensitive.\n", 205 | "\n", 206 | "***variable_name = expression***\n" 207 | ] 208 | }, 209 | { 210 | "cell_type": "code", 211 | "execution_count": 11, 212 | "metadata": {}, 213 | "outputs": [ 214 | { 215 | "data": { 216 | "text/plain": [ 217 | "(3, 'D100', 'Python', 'hundred')" 218 | ] 219 | }, 220 | "execution_count": 11, 221 | "metadata": {}, 222 | "output_type": "execute_result" 223 | } 224 | ], 225 | "source": [ 226 | "# set a variable name\n", 227 | "number = 3\n", 228 | "road = \"D100\"\n", 229 | "name = 'Python'\n", 230 | "century = \"hundred\"\n", 231 | "\n", 232 | "# print them on to the screen\n", 233 | "number, road, name, century" 234 | ] 235 | }, 236 | { 237 | "cell_type": "code", 238 | "execution_count": 13, 239 | "metadata": {}, 240 | "outputs": [ 241 | { 242 | "name": "stdout", 243 | "output_type": "stream", 244 | "text": [ 245 | "3\n" 246 | ] 247 | } 248 | ], 249 | "source": [ 250 | "print(number)" 251 | ] 252 | }, 253 | { 254 | "cell_type": "code", 255 | "execution_count": 15, 256 | "metadata": {}, 257 | "outputs": [ 258 | { 259 | "data": { 260 | "text/plain": [ 261 | "('hundred', 'D100')" 262 | ] 263 | }, 264 | "execution_count": 15, 265 | "metadata": {}, 266 | "output_type": "execute_result" 267 | } 268 | ], 269 | "source": [ 270 | "century, road" 271 | ] 272 | }, 273 | { 274 | "cell_type": "code", 275 | "execution_count": null, 276 | "metadata": {}, 277 | "outputs": [], 278 | "source": [] 279 | }, 280 | { 281 | "cell_type": "code", 282 | "execution_count": 17, 283 | "metadata": {}, 284 | "outputs": [ 285 | { 286 | "data": { 287 | "text/plain": [ 288 | "5" 289 | ] 290 | }, 291 | "execution_count": 17, 292 | "metadata": {}, 293 | "output_type": "execute_result" 294 | } 295 | ], 296 | "source": [ 297 | "__d = 5\n", 298 | "__d" 299 | ] 300 | }, 301 | { 302 | "cell_type": "markdown", 303 | "metadata": {}, 304 | "source": [ 305 | "You can assign a single value to several variables simultaneously." 306 | ] 307 | }, 308 | { 309 | "cell_type": "code", 310 | "execution_count": 2, 311 | "metadata": {}, 312 | "outputs": [ 313 | { 314 | "data": { 315 | "text/plain": [ 316 | "(4, 4, 4)" 317 | ] 318 | }, 319 | "execution_count": 2, 320 | "metadata": {}, 321 | "output_type": "execute_result" 322 | } 323 | ], 324 | "source": [ 325 | "a = b = c = 4\n", 326 | "a, b, c" 327 | ] 328 | }, 329 | { 330 | "cell_type": "markdown", 331 | "metadata": {}, 332 | "source": [ 333 | "---\n", 334 | "### Operators\n", 335 | "\n", 336 | "Operators are symbols, such as +, –, =, >, and <. Python allows you to perform mathematical or logical operations to manipulate data values and produce a result based on some rules.\n", 337 | "\n", 338 | "Python language supports a wide range of operators. \n", 339 | "\n", 340 | "\n", 341 | "#### 1. Arithmetic Operators\n", 342 | "\n", 343 | "\n", 344 | "Arithmetic operators are used to execute arithmetic operations such as addition, subtraction, division, multiplication etc.\n", 345 | "\n", 346 | "![](Figures/arithmetic-operators.png) " 347 | ] 348 | }, 349 | { 350 | "cell_type": "code", 351 | "execution_count": 18, 352 | "metadata": {}, 353 | "outputs": [ 354 | { 355 | "data": { 356 | "text/plain": [ 357 | "9" 358 | ] 359 | }, 360 | "execution_count": 18, 361 | "metadata": {}, 362 | "output_type": "execute_result" 363 | } 364 | ], 365 | "source": [ 366 | " 4 + 5" 367 | ] 368 | }, 369 | { 370 | "cell_type": "code", 371 | "execution_count": 10, 372 | "metadata": {}, 373 | "outputs": [ 374 | { 375 | "data": { 376 | "text/plain": [ 377 | "20.5" 378 | ] 379 | }, 380 | "execution_count": 10, 381 | "metadata": {}, 382 | "output_type": "execute_result" 383 | } 384 | ], 385 | "source": [ 386 | "3 + 5 * 7 / 2" 387 | ] 388 | }, 389 | { 390 | "cell_type": "code", 391 | "execution_count": 19, 392 | "metadata": {}, 393 | "outputs": [ 394 | { 395 | "data": { 396 | "text/plain": [ 397 | "100" 398 | ] 399 | }, 400 | "execution_count": 19, 401 | "metadata": {}, 402 | "output_type": "execute_result" 403 | } 404 | ], 405 | "source": [ 406 | "10 ** 2" 407 | ] 408 | }, 409 | { 410 | "cell_type": "code", 411 | "execution_count": 6, 412 | "metadata": {}, 413 | "outputs": [ 414 | { 415 | "data": { 416 | "text/plain": [ 417 | "3.3333333333333335" 418 | ] 419 | }, 420 | "execution_count": 6, 421 | "metadata": {}, 422 | "output_type": "execute_result" 423 | } 424 | ], 425 | "source": [ 426 | "10 / 3" 427 | ] 428 | }, 429 | { 430 | "cell_type": "code", 431 | "execution_count": 7, 432 | "metadata": {}, 433 | "outputs": [ 434 | { 435 | "data": { 436 | "text/plain": [ 437 | "3" 438 | ] 439 | }, 440 | "execution_count": 7, 441 | "metadata": {}, 442 | "output_type": "execute_result" 443 | } 444 | ], 445 | "source": [ 446 | "10 // 3" 447 | ] 448 | }, 449 | { 450 | "cell_type": "code", 451 | "execution_count": 8, 452 | "metadata": {}, 453 | "outputs": [ 454 | { 455 | "data": { 456 | "text/plain": [ 457 | "1" 458 | ] 459 | }, 460 | "execution_count": 8, 461 | "metadata": {}, 462 | "output_type": "execute_result" 463 | } 464 | ], 465 | "source": [ 466 | "10 % 3" 467 | ] 468 | }, 469 | { 470 | "cell_type": "code", 471 | "execution_count": 20, 472 | "metadata": {}, 473 | "outputs": [ 474 | { 475 | "data": { 476 | "text/plain": [ 477 | "(16, 9.0, 1.0)" 478 | ] 479 | }, 480 | "execution_count": 20, 481 | "metadata": {}, 482 | "output_type": "execute_result" 483 | } 484 | ], 485 | "source": [ 486 | "# find answer of the equations below\n", 487 | "\n", 488 | "a = 4\n", 489 | "b = 2\n", 490 | "c = 3\n", 491 | "\n", 492 | "apple = a ** b\n", 493 | "orange = a / b * c + 3\n", 494 | "d = apple // orange\n", 495 | "\n", 496 | "apple, orange, d" 497 | ] 498 | }, 499 | { 500 | "cell_type": "code", 501 | "execution_count": 6, 502 | "metadata": {}, 503 | "outputs": [ 504 | { 505 | "data": { 506 | "text/plain": [ 507 | "5.399999999999994" 508 | ] 509 | }, 510 | "execution_count": 6, 511 | "metadata": {}, 512 | "output_type": "execute_result" 513 | } 514 | ], 515 | "source": [ 516 | "# find the remainder of the \"apple/orange\"\n", 517 | "\n", 518 | "apple%orange" 519 | ] 520 | }, 521 | { 522 | "cell_type": "markdown", 523 | "metadata": {}, 524 | "source": [ 525 | "#### 2. Assignment Operators\n", 526 | "\n", 527 | "Assignment operators are used for assigning the values to a variable.\n", 528 | "\n" 529 | ] 530 | }, 531 | { 532 | "cell_type": "code", 533 | "execution_count": 7, 534 | "metadata": {}, 535 | "outputs": [ 536 | { 537 | "data": { 538 | "text/plain": [ 539 | "13" 540 | ] 541 | }, 542 | "execution_count": 7, 543 | "metadata": {}, 544 | "output_type": "execute_result" 545 | } 546 | ], 547 | "source": [ 548 | "x = 5\n", 549 | "y = 8\n", 550 | "x + y" 551 | ] 552 | }, 553 | { 554 | "cell_type": "markdown", 555 | "metadata": {}, 556 | "source": [ 557 | "---\n", 558 | "![](../Figures/assignmentoparetor.png)" 559 | ] 560 | }, 561 | { 562 | "cell_type": "code", 563 | "execution_count": 22, 564 | "metadata": {}, 565 | "outputs": [ 566 | { 567 | "data": { 568 | "text/plain": [ 569 | "8" 570 | ] 571 | }, 572 | "execution_count": 22, 573 | "metadata": {}, 574 | "output_type": "execute_result" 575 | } 576 | ], 577 | "source": [ 578 | "# run the operator below and recognize how x changed in each run\n", 579 | "\n", 580 | "x = 4\n", 581 | "x += 4\n", 582 | "x\n" 583 | ] 584 | }, 585 | { 586 | "cell_type": "markdown", 587 | "metadata": {}, 588 | "source": [ 589 | "#### 3. Comparison Operators\n", 590 | "\n", 591 | "They are used to compare the values of two operands and output is always a Boolean value, either True or False\n", 592 | "\n", 593 | "![](Figures/comparisonoperator.png)" 594 | ] 595 | }, 596 | { 597 | "cell_type": "code", 598 | "execution_count": 23, 599 | "metadata": {}, 600 | "outputs": [ 601 | { 602 | "data": { 603 | "text/plain": [ 604 | "True" 605 | ] 606 | }, 607 | "execution_count": 23, 608 | "metadata": {}, 609 | "output_type": "execute_result" 610 | } 611 | ], 612 | "source": [ 613 | "5 == 5" 614 | ] 615 | }, 616 | { 617 | "cell_type": "code", 618 | "execution_count": 24, 619 | "metadata": {}, 620 | "outputs": [ 621 | { 622 | "data": { 623 | "text/plain": [ 624 | "True" 625 | ] 626 | }, 627 | "execution_count": 24, 628 | "metadata": {}, 629 | "output_type": "execute_result" 630 | } 631 | ], 632 | "source": [ 633 | "5 <= 7" 634 | ] 635 | }, 636 | { 637 | "cell_type": "markdown", 638 | "metadata": {}, 639 | "source": [ 640 | "---\n", 641 | "## Data Types\n", 642 | "\n", 643 | "Definition of the data type classifies and categorize data items. Its a way to understand which kind of value determines what operatoins can be performed. Different types of variable can do different things.\n", 644 | "\n", 645 | " - int (integers) (ex: 5,98,28829382983) \n", 646 | " - float (real numbers) (ex: 3.7)\n", 647 | " - bool (boolean values) (True or False)\n", 648 | " - NoneType (special) (None; has one value)\n", 649 | "\n", 650 | "You can check the type of your ariable by clling \"type\" command. Python allows you to convert your data from one type to another. " 651 | ] 652 | }, 653 | { 654 | "cell_type": "code", 655 | "execution_count": 26, 656 | "metadata": {}, 657 | "outputs": [ 658 | { 659 | "data": { 660 | "text/plain": [ 661 | "float" 662 | ] 663 | }, 664 | "execution_count": 26, 665 | "metadata": {}, 666 | "output_type": "execute_result" 667 | } 668 | ], 669 | "source": [ 670 | "type(6.6)" 671 | ] 672 | }, 673 | { 674 | "cell_type": "code", 675 | "execution_count": 27, 676 | "metadata": {}, 677 | "outputs": [ 678 | { 679 | "data": { 680 | "text/plain": [ 681 | "str" 682 | ] 683 | }, 684 | "execution_count": 27, 685 | "metadata": {}, 686 | "output_type": "execute_result" 687 | } 688 | ], 689 | "source": [ 690 | "type('hello')" 691 | ] 692 | }, 693 | { 694 | "cell_type": "code", 695 | "execution_count": 29, 696 | "metadata": {}, 697 | "outputs": [ 698 | { 699 | "data": { 700 | "text/plain": [ 701 | "5" 702 | ] 703 | }, 704 | "execution_count": 29, 705 | "metadata": {}, 706 | "output_type": "execute_result" 707 | } 708 | ], 709 | "source": [ 710 | "# type conversion\n", 711 | "int(5.98374)" 712 | ] 713 | }, 714 | { 715 | "cell_type": "code", 716 | "execution_count": 14, 717 | "metadata": {}, 718 | "outputs": [ 719 | { 720 | "data": { 721 | "text/plain": [ 722 | "3" 723 | ] 724 | }, 725 | "execution_count": 14, 726 | "metadata": {}, 727 | "output_type": "execute_result" 728 | } 729 | ], 730 | "source": [ 731 | "# type conversion\n", 732 | "int(3.1)" 733 | ] 734 | }, 735 | { 736 | "cell_type": "markdown", 737 | "metadata": {}, 738 | "source": [ 739 | "Lets go in detail with the basic data types below;\n", 740 | "\n", 741 | "### 1. Numbers\n", 742 | "\n", 743 | "Integers, floating point numbers and complex numbers are defined as int, float and complex class in Python. \n", 744 | "\n", 745 | "### 2. Boolean\n", 746 | "\n", 747 | "They are essential to set conditional statements which could be either True or False (treated as reserved words).\n", 748 | "\n", 749 | "### 3. String\n", 750 | "\n", 751 | "A string may consists letters, numbers, and other types of characters with spaces.\n", 752 | "\n", 753 | "You can check the type of your ariable by clling \"type\" command." 754 | ] 755 | }, 756 | { 757 | "cell_type": "code", 758 | "execution_count": 33, 759 | "metadata": {}, 760 | "outputs": [ 761 | { 762 | "name": "stdout", 763 | "output_type": "stream", 764 | "text": [ 765 | "\n", 766 | "\n" 767 | ] 768 | } 769 | ], 770 | "source": [ 771 | "x = [3.14]\n", 772 | "y = False\n", 773 | "\n", 774 | "print(type(x))\n", 775 | "print(type(y))" 776 | ] 777 | }, 778 | { 779 | "cell_type": "markdown", 780 | "metadata": {}, 781 | "source": [ 782 | "---\n", 783 | "### 1. Types of numbers\n", 784 | "\n", 785 | "There ara basically three different types of numbers that we can use in Python.\n", 786 | "\n", 787 | "#### 1.1. Integers\n", 788 | "They can be positive or negative numbers which are written without decimals." 789 | ] 790 | }, 791 | { 792 | "cell_type": "code", 793 | "execution_count": 34, 794 | "metadata": {}, 795 | "outputs": [ 796 | { 797 | "data": { 798 | "text/plain": [ 799 | "-5.25" 800 | ] 801 | }, 802 | "execution_count": 34, 803 | "metadata": {}, 804 | "output_type": "execute_result" 805 | } 806 | ], 807 | "source": [ 808 | "(5 + 2) * 3 / (-4)" 809 | ] 810 | }, 811 | { 812 | "cell_type": "code", 813 | "execution_count": 17, 814 | "metadata": {}, 815 | "outputs": [ 816 | { 817 | "data": { 818 | "text/plain": [ 819 | "6" 820 | ] 821 | }, 822 | "execution_count": 17, 823 | "metadata": {}, 824 | "output_type": "execute_result" 825 | } 826 | ], 827 | "source": [ 828 | "13 // 2" 829 | ] 830 | }, 831 | { 832 | "cell_type": "code", 833 | "execution_count": 18, 834 | "metadata": {}, 835 | "outputs": [ 836 | { 837 | "data": { 838 | "text/plain": [ 839 | "12830791.72442304" 840 | ] 841 | }, 842 | "execution_count": 18, 843 | "metadata": {}, 844 | "output_type": "execute_result" 845 | } 846 | ], 847 | "source": [ 848 | "3828374649983 / 298374" 849 | ] 850 | }, 851 | { 852 | "cell_type": "markdown", 853 | "metadata": {}, 854 | "source": [ 855 | "#### 1.2. Floats\n", 856 | "\n", 857 | "They are rational numbers with fractional parts." 858 | ] 859 | }, 860 | { 861 | "cell_type": "code", 862 | "execution_count": 19, 863 | "metadata": {}, 864 | "outputs": [ 865 | { 866 | "data": { 867 | "text/plain": [ 868 | "32.0" 869 | ] 870 | }, 871 | "execution_count": 19, 872 | "metadata": {}, 873 | "output_type": "execute_result" 874 | } 875 | ], 876 | "source": [ 877 | "8.0 * 4" 878 | ] 879 | }, 880 | { 881 | "cell_type": "code", 882 | "execution_count": 20, 883 | "metadata": {}, 884 | "outputs": [ 885 | { 886 | "data": { 887 | "text/plain": [ 888 | "6.333333333333333" 889 | ] 890 | }, 891 | "execution_count": 20, 892 | "metadata": {}, 893 | "output_type": "execute_result" 894 | } 895 | ], 896 | "source": [ 897 | "19. / 3" 898 | ] 899 | }, 900 | { 901 | "cell_type": "code", 902 | "execution_count": 21, 903 | "metadata": {}, 904 | "outputs": [ 905 | { 906 | "data": { 907 | "text/plain": [ 908 | "6.0" 909 | ] 910 | }, 911 | "execution_count": 21, 912 | "metadata": {}, 913 | "output_type": "execute_result" 914 | } 915 | ], 916 | "source": [ 917 | "13. // 2" 918 | ] 919 | }, 920 | { 921 | "cell_type": "code", 922 | "execution_count": 22, 923 | "metadata": {}, 924 | "outputs": [ 925 | { 926 | "data": { 927 | "text/plain": [ 928 | "2.0" 929 | ] 930 | }, 931 | "execution_count": 22, 932 | "metadata": {}, 933 | "output_type": "execute_result" 934 | } 935 | ], 936 | "source": [ 937 | "11. % 3" 938 | ] 939 | }, 940 | { 941 | "cell_type": "code", 942 | "execution_count": 23, 943 | "metadata": {}, 944 | "outputs": [ 945 | { 946 | "data": { 947 | "text/plain": [ 948 | "8.835600000000001e+34" 949 | ] 950 | }, 951 | "execution_count": 23, 952 | "metadata": {}, 953 | "output_type": "execute_result" 954 | } 955 | ], 956 | "source": [ 957 | "3.98e32 * 222" 958 | ] 959 | }, 960 | { 961 | "cell_type": "markdown", 962 | "metadata": {}, 963 | "source": [ 964 | "#### 1.3. Complex Numbers\n", 965 | "\n", 966 | "Numbers with sum of real and imaginery parts. Use j instead of i which equals square root of -1. \n" 967 | ] 968 | }, 969 | { 970 | "cell_type": "code", 971 | "execution_count": 24, 972 | "metadata": {}, 973 | "outputs": [ 974 | { 975 | "data": { 976 | "text/plain": [ 977 | "(16+2j)" 978 | ] 979 | }, 980 | "execution_count": 24, 981 | "metadata": {}, 982 | "output_type": "execute_result" 983 | } 984 | ], 985 | "source": [ 986 | "(3 + 2j) * (4 - 2j)" 987 | ] 988 | }, 989 | { 990 | "cell_type": "markdown", 991 | "metadata": {}, 992 | "source": [] 993 | } 994 | ], 995 | "metadata": { 996 | "kernelspec": { 997 | "display_name": "Python 3", 998 | "language": "python", 999 | "name": "python3" 1000 | }, 1001 | "language_info": { 1002 | "codemirror_mode": { 1003 | "name": "ipython", 1004 | "version": 3 1005 | }, 1006 | "file_extension": ".py", 1007 | "mimetype": "text/x-python", 1008 | "name": "python", 1009 | "nbconvert_exporter": "python", 1010 | "pygments_lexer": "ipython3", 1011 | "version": "3.6.7" 1012 | } 1013 | }, 1014 | "nbformat": 4, 1015 | "nbformat_minor": 4 1016 | } 1017 | -------------------------------------------------------------------------------- /Figures/.ipynb_checkpoints/algorithm2-checkpoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/.ipynb_checkpoints/algorithm2-checkpoint.png -------------------------------------------------------------------------------- /Figures/.ipynb_checkpoints/grading-checkpoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/.ipynb_checkpoints/grading-checkpoint.png -------------------------------------------------------------------------------- /Figures/.ipynb_checkpoints/slope-int-checkpoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/.ipynb_checkpoints/slope-int-checkpoint.png -------------------------------------------------------------------------------- /Figures/.ipynb_checkpoints/travel-time-checkpoint.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/.ipynb_checkpoints/travel-time-checkpoint.png -------------------------------------------------------------------------------- /Figures/3d-array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/3d-array.png -------------------------------------------------------------------------------- /Figures/GTU_LOGO_1200X768_JPG_EN.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/GTU_LOGO_1200X768_JPG_EN.jpg -------------------------------------------------------------------------------- /Figures/GTU_LOGO_1200X768_JPG_EN_Small.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/GTU_LOGO_1200X768_JPG_EN_Small.jpg -------------------------------------------------------------------------------- /Figures/Z_TravelTimeCurve.mp4: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/Z_TravelTimeCurve.mp4 -------------------------------------------------------------------------------- /Figures/algorithm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/algorithm.png -------------------------------------------------------------------------------- /Figures/algorithm2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/algorithm2.png -------------------------------------------------------------------------------- /Figures/anaconda.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/anaconda.png -------------------------------------------------------------------------------- /Figures/arithmetic-operators.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/arithmetic-operators.png -------------------------------------------------------------------------------- /Figures/array-sum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/array-sum.png -------------------------------------------------------------------------------- /Figures/array.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/array.png -------------------------------------------------------------------------------- /Figures/array_element_item.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/array_element_item.png -------------------------------------------------------------------------------- /Figures/array_shape.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/array_shape.jpg -------------------------------------------------------------------------------- /Figures/array_vs_list.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/array_vs_list.png -------------------------------------------------------------------------------- /Figures/arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/arrow.png -------------------------------------------------------------------------------- /Figures/assignmentoparetor.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/assignmentoparetor.png -------------------------------------------------------------------------------- /Figures/blackwidow.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/blackwidow.jpg -------------------------------------------------------------------------------- /Figures/captainamerica.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/captainamerica.jpg -------------------------------------------------------------------------------- /Figures/cat.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/cat.png -------------------------------------------------------------------------------- /Figures/ce103_1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ce103_1.jpg -------------------------------------------------------------------------------- /Figures/ce103_2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ce103_2.jpg -------------------------------------------------------------------------------- /Figures/ce103_3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ce103_3.jpg -------------------------------------------------------------------------------- /Figures/class.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/class.png -------------------------------------------------------------------------------- /Figures/clue.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/clue.png -------------------------------------------------------------------------------- /Figures/colab.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/colab.png -------------------------------------------------------------------------------- /Figures/color.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/color.png -------------------------------------------------------------------------------- /Figures/comparisonoperator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/comparisonoperator.png -------------------------------------------------------------------------------- /Figures/def_func.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/def_func.png -------------------------------------------------------------------------------- /Figures/dot-product.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/dot-product.png -------------------------------------------------------------------------------- /Figures/earth-dist.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/earth-dist.png -------------------------------------------------------------------------------- /Figures/elif-else-statement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/elif-else-statement.png -------------------------------------------------------------------------------- /Figures/encode-decode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/encode-decode.png -------------------------------------------------------------------------------- /Figures/encode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/encode.png -------------------------------------------------------------------------------- /Figures/error-messages.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/error-messages.pdf -------------------------------------------------------------------------------- /Figures/figure-exp.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/figure-exp.png -------------------------------------------------------------------------------- /Figures/for-loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/for-loop.png -------------------------------------------------------------------------------- /Figures/galata.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/galata.png -------------------------------------------------------------------------------- /Figures/grading.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/grading.png -------------------------------------------------------------------------------- /Figures/grading2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/grading2.jpg -------------------------------------------------------------------------------- /Figures/guido.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/guido.png -------------------------------------------------------------------------------- /Figures/homework10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/homework10.png -------------------------------------------------------------------------------- /Figures/hulk.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hulk.jpg -------------------------------------------------------------------------------- /Figures/hw6_screenshot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw6_screenshot.png -------------------------------------------------------------------------------- /Figures/hw6_screenshot2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw6_screenshot2.png -------------------------------------------------------------------------------- /Figures/hw7-ibb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw7-ibb.png -------------------------------------------------------------------------------- /Figures/hw7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw7.png -------------------------------------------------------------------------------- /Figures/hw_11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw_11.png -------------------------------------------------------------------------------- /Figures/hw_11_2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/hw_11_2.png -------------------------------------------------------------------------------- /Figures/ibb.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ibb.png -------------------------------------------------------------------------------- /Figures/if-else-statement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/if-else-statement.png -------------------------------------------------------------------------------- /Figures/if-statement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/if-statement.png -------------------------------------------------------------------------------- /Figures/inclass_II.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/inclass_II.png -------------------------------------------------------------------------------- /Figures/ironman.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ironman.png -------------------------------------------------------------------------------- /Figures/ironman2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/ironman2.jpg -------------------------------------------------------------------------------- /Figures/jupyter.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/jupyter.png -------------------------------------------------------------------------------- /Figures/keywords.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/keywords.png -------------------------------------------------------------------------------- /Figures/lin-reg-fig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/lin-reg-fig.png -------------------------------------------------------------------------------- /Figures/lin-reg-formula.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/lin-reg-formula.png -------------------------------------------------------------------------------- /Figures/plaka.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/plaka.jpg -------------------------------------------------------------------------------- /Figures/regression-curve.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/regression-curve.png -------------------------------------------------------------------------------- /Figures/rossum.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/rossum.png -------------------------------------------------------------------------------- /Figures/scipy-submodules.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/scipy-submodules.png -------------------------------------------------------------------------------- /Figures/sensorrecord.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/sensorrecord.png -------------------------------------------------------------------------------- /Figures/slope-int.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/slope-int.png -------------------------------------------------------------------------------- /Figures/spoon.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/spoon.jpg -------------------------------------------------------------------------------- /Figures/string-sequence.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/string-sequence.png -------------------------------------------------------------------------------- /Figures/subplot.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/subplot.png -------------------------------------------------------------------------------- /Figures/swinging_arm.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/swinging_arm.png -------------------------------------------------------------------------------- /Figures/travel-time.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/travel-time.png -------------------------------------------------------------------------------- /Figures/unicode.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/unicode.png -------------------------------------------------------------------------------- /Figures/while-loop.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Figures/while-loop.png -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | ## GEBZE TECHNICAL UNIVERSITY - DEPARTMENT OF CIVIL ENGINEERING 4 | 5 | ### CE 103 Introduction to Computers and Programming 6 | # _2021-2022 Spring_ 7 | 8 | This lecture meant to serve as an introduction to the Python programming for Civil Engineering undergraduate students. It has enriched content with solved examples based on specific Civil Engineering problems. 9 | 10 | --- 11 | ### Instructor : 12 | ##### Dr. Ahu Kömeç Mutlu --> _ahumutlu@gtu.edu.tr_ 13 | 14 | --- 15 | ### Teaching Assistants : 16 | ##### Oğuz Dal --> _odal@gtu.edu.tr_ 17 | ##### Nurullah Açıkgöz --> _nacikgoz@gtu.edu.tr_ 18 | 19 | --- 20 | 21 | ### Textbook and Resources : 22 | _Introduction to Python for Science and Engineering, David J. Pine, CRC Press, 2019._ 23 | 24 | _https://docs.python.org/3/tutorial/index.html_ 25 | 26 | _https://docs.python.org/3/reference/index.html_ 27 | 28 | --- 29 | 30 | ## Grading and Attendance 31 |

32 | 33 | ## Syllabus 34 | - 35 | 36 | 37 | **Week 1 Theory :** Introduction to Python, launching Python, learning Github, Azure and Algorithm 38 | 39 | **Week 1 Lab :** Github - Jupyter settings 40 | 41 | --- 42 | **Week 2 Theory :** Strings, Lists, Arrays and Dictionaries 43 | 44 | **Week 2 Lab :** _Coding applications_ 45 | 46 | --- 47 | **Week 3 Theory :** Numerical Routines NumPy 48 | 49 | **Wek 3 Lab :** _Coding applications_ 50 | 51 | --- 52 | **Week 4 Theory :** Conditionals ad Loops 53 | 54 | **Week 4 Lab :** _Coding applications_ 55 | 56 | --- 57 | **Week 5 Theory :** Plotting 58 | 59 | **Week 5 Lab :** _Coding applications_ 60 | 61 | --- 62 | **Week 6 Theory :** Functions 63 | 64 | **Week 6 Lab :** _Coding applications_ 65 | 66 | --- 67 | **Week 7 Theory :** Curve Fitting 68 | 69 | **Week 7 Lab :** _Coding applications_ 70 | 71 | --- 72 | **Week 8 Theory :** Numerical Routines SciPy 73 | 74 | **Week 8 Lab :** _Coding applications_ 75 | 76 | --- 77 | **Week 9 Theory :** File input and output 78 | 79 | **Week 9 Lab :** _Coding applications_ 80 | 81 | --- 82 | **Week 10 Theory :** Data manipulation and Analysis Pandas 83 | 84 | **Week 10 Lab :** _Coding applications_ 85 | 86 | --- 87 | **Week 11 Theory :** Getting Data from the Web 88 | 89 | **Week 11 Lab :** _Coding applications_ 90 | 91 | --- 92 | **Week 12 Theory :** Python Classes 93 | 94 | **Week 12 Lab :** _Coding applications_ 95 | 96 | --- 97 | **Week 13 Theory :** InClass Project - I 98 | 99 | **Week 13 Lab :** _Coding applications_ 100 | 101 | --- 102 | **Week 14 Theory :** InClass Project - II 103 | 104 | **Week 14 Lab :** _Coding applications_ 105 | 106 | --- 107 | **Week 15 Theory :** No class 108 | 109 | --- 110 | **Week 16 Theory :** Final Exam 111 | 112 | --- 113 | -------------------------------------------------------------------------------- /Test_Input/Hello.txt: -------------------------------------------------------------------------------- 1 | Hello from the other side 2 | I must've called a thousand times 3 | -------------------------------------------------------------------------------- /Test_Input/Kusma-GyadiBridge_Nepal.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Test_Input/Kusma-GyadiBridge_Nepal.png -------------------------------------------------------------------------------- /Test_Input/aaa.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Test_Input/aaa.xlsx -------------------------------------------------------------------------------- /Test_Input/civil_abb.txt: -------------------------------------------------------------------------------- 1 | A.A.S.H.T.O - American Association of State Highway Transport 2 | A.B - Anchor Bolt Or Asbestos Board 3 | A.C.I - American concrete institute. 4 | A.R.E.A - American Railway Engineering Association. 5 | A.S.C - Allowable stress of concrete. 6 | A.S.T.M - American Society for Testing Materials 7 | AC - Asphalt Concrete 8 | AE - Assistant Engineer 9 | APM - Assistant Project Manager 10 | B.M - Benchmark 11 | B.M - Bending moment. 12 | B.O.F - Bottom Of Foundation 13 | BHK - Bedroom, Hall, Kitchen 14 | BLK - Block Work -------------------------------------------------------------------------------- /Test_Input/civil_abb_out.csv: -------------------------------------------------------------------------------- 1 | A.A.S.H.T.O , American Association of State Highway Transport 2 | A.B , Anchor Bolt Or Asbestos Board 3 | A.C.I , American concrete institute. 4 | A.R.E.A , American Railway Engineering Association. 5 | A.S.C , Allowable stress of concrete. 6 | A.S.T.M , American Society for Testing Materials 7 | AC , Asphalt Concrete 8 | AE , Assistant Engineer 9 | APM , Assistant Project Manager 10 | B.M , Benchmark 11 | B.M , Bending moment. 12 | B.O.F , Bottom Of Foundation 13 | BHK ," Bedroom, Hall, Kitchen" 14 | BLK , Block Work 15 | -------------------------------------------------------------------------------- /Test_Input/civil_abb_out.json: -------------------------------------------------------------------------------- 1 | {"A.A.S.H.T.O": "American Association of State Highway Transport", "A.B": "Anchor Bolt Or Asbestos Board", "A.C.I": "American concrete institute.", "A.R.E.A": "American Railway Engineering Association.", "A.S.C": "Allowable stress of concrete.", "A.S.T.M": "American Society for Testing Materials", "AC": "Asphalt Concrete", "AE": "Assistant Engineer", "APM": "Assistant Project Manager", "B.M": "Bending moment.", "B.O.F": "Bottom Of Foundation", "BHK": "Bedroom, Hall, Kitchen", "BLK": "Block Work"} -------------------------------------------------------------------------------- /Test_Input/class.json: -------------------------------------------------------------------------------- 1 | { 2 | "LectureCodes": [ 3 | "GTU_CE103", 4 | "GTU_CE105", 5 | "GTU_CE247" 6 | ], 7 | "Avg_StudentPerLecture": 75, 8 | "NumberOfClass": 12, 9 | "Students": [ 10 | { 11 | "Name/Surname": "Braced Frame", 12 | "age": 19 13 | }, 14 | { 15 | "Name/Surname": "Axial Load", 16 | "age": 18 17 | } 18 | ] 19 | } -------------------------------------------------------------------------------- /Test_Input/eventlist.csv: -------------------------------------------------------------------------------- 1 | STATION,STATION_NAME,ELEVATION,LATITUDE,LONGITUDE,DATE,HPCP,Measurement Flag,Quality Flag 2 | COOP:310301,ASHEVILLE NC US,682.1,35.5954,-82.5568,20100101 00:00,99999,], 3 | COOP:310301,ASHEVILLE NC US,682.1,35.5954,-82.5568,20100101 01:00,0,g, 4 | COOP:310301,ASHEVILLE NC US,682.1,35.5954,-82.5568,20100102 06:00,1, , 5 | -------------------------------------------------------------------------------- /Test_Input/eventlist.json: -------------------------------------------------------------------------------- 1 | { 2 | "quiz": { 3 | "sport": { 4 | "q1": { 5 | "question": "Which one is correct team name in NBA?", 6 | "options": [ 7 | "New York Bulls", 8 | "Los Angeles Kings", 9 | "Golden State Warriros", 10 | "Huston Rocket" 11 | ], 12 | "answer": "Huston Rocket" 13 | } 14 | }, 15 | "maths": { 16 | "q1": { 17 | "question": "5 + 7 = ?", 18 | "options": [ 19 | "10", 20 | "11", 21 | "12", 22 | "13" 23 | ], 24 | "answer": "12" 25 | }, 26 | "q2": { 27 | "question": "12 - 8 = ?", 28 | "options": [ 29 | "1", 30 | "2", 31 | "3", 32 | "4" 33 | ], 34 | "answer": "4" 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /Test_Input/filename: -------------------------------------------------------------------------------- 1 | Lets write some words in the FIRST line of a file. 2 | This could be the SECOND line. Am I right?. 3 | 3rd Line 4 | 4th Line 5 | 5th Line 6 | -------------------------------------------------------------------------------- /Test_Input/merded.txt: -------------------------------------------------------------------------------- 1 | Hello from the other side 2 | I must've called a thousand times 3 | -------------------------------------------------------------------------------- /Test_Input/merged: -------------------------------------------------------------------------------- 1 | Hello from the other side 2 | I must've called a thousand times 3 | lets write something for line 1. 4 | lets write something for line 2. 5 | third line. 6 | 4th line. 7 | -------------------------------------------------------------------------------- /Test_Input/merged.txt: -------------------------------------------------------------------------------- 1 | Hello from the other side 2 | I must've called a thousand times 3 | lets write something for line 1. 4 | lets write something for line 2. 5 | third line. 6 | 4th line. 7 | -------------------------------------------------------------------------------- /Test_Input/narrow.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Test_Input/narrow.zip -------------------------------------------------------------------------------- /Test_Input/sales.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Test_Input/sales.xlsx -------------------------------------------------------------------------------- /Test_Input/test: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Test_Input/tuik.csv: -------------------------------------------------------------------------------- 1 | Year|01-Ocak|02-Şubat|03-Mart|04-Nisan|05-Mayıs|06-Haziran|07-Temmuz|08-Ağustos|09-Eylül|10-Ekim|11-Kasım|12-Aralık 2 | 2012|-4.25|-2.19|-4.24|8.31|1.14|1.58|-5.35|0.02|-0.18|-0.98|-4.06|-2.51 3 | 2013|1.24|2.89|6.72|-3.98|-2.69|-1.36|2.59|0.29|1.69|-3.96|3.59|-1.79 4 | 2014|1.67|-2.32|-1.75|-1.42|-1.13|0.74|-0.54|-0.03|1.12|1.13|2.4|-3.67 5 | 2015|1.13|2.75|-0.65|-2.27|2.51|-1.86|-1.67|1.64|-1.93|0.06|1.01|2.93 6 | 2016|-0.91|-2.58|7.33|1.78|-10.87|-1.94|9.08|-5.38|5.9|-5.58|-3.13|-4.26 7 | 2017|-0.99|2.85|5.95|2.36|6.07|-0.79|-2.51|5.02|-8.0|1.77|-0.04|-0.2 8 | 2018|2.07|0.68|-4.58|-3.26|-5.66|-5.17|1.6|-6.81|-18.34|1.73|-4.14|-0.04 9 | 2019|-8.57|10.04|7.99|-8.37|-3.7|-3.53|6.6|5.11|7.52|8.46|-0.54|11.17 10 | -------------------------------------------------------------------------------- /Test_Input/tuik.xlsx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/komec/CE103_Lecture/589f8fc02dfe8f9331986d930031a9faee36fe4e/Test_Input/tuik.xlsx -------------------------------------------------------------------------------- /Test_Input/tuik_new.csv: -------------------------------------------------------------------------------- 1 | ,YEAR,Unnamed: 4,Unnamed: 5,Unnamed: 6 2 | 3,2011.0,-1.94,-1.41,-1.3 3 | 4,2012.0,-2.19,-4.24,8.31 4 | 5,2013.0,2.89,6.72,-3.98 5 | 6,2014.0,-2.32,-1.75,-1.42 6 | 7,2015.0,2.75,-0.65,-2.27 7 | 8,2016.0,-2.58,7.33,1.78 8 | 9,2017.0,2.85,5.95,2.36 9 | 10,2018.0,0.68,-4.58,-3.26 10 | 11,2019.0,10.04,7.99,-8.37 11 | 12,2020.0,6.61,-3.45,-34.07 12 | 13,2011.0,-0.91,-8.45,7.78 13 | 14,2012.0,-3.03,2.99,2.04 14 | 15,2013.0,1.11,6.56,-4.04 15 | 16,2014.0,-8.04,-3.81,1.62 16 | 17,2015.0,2.78,-1.39,-2.31 17 | 18,2016.0,-1.31,-1.67,0.14 18 | 19,2017.0,2.12,12.31,-0.18 19 | 20,2018.0,-3.69,-5.92,-0.08 20 | 21,2019.0,-8.74,4.43,-0.27 21 | 22,2020.0,-5.68,3.72,-42.19 22 | --------------------------------------------------------------------------------