Presentación Python

Page 1

HOUR OF CODE /* PYTHON BASICS */

The content of this presentation was written by Coralys Cubero Rivera for #Include<Girls> from the University of Puerto Rico Rio Piedras Campus Chapter. The same is shared under the Creative Commons Attribution-ShareAlike 4.0 International license, according to the original (https://creativecommons.org/licenses/by-sa/4.0/). We promote the use, minor modification and distribution of this material, as long as it is for educational, non-profit purposes and the work is attributed to the aforementioned author.


What is Python? ● Python is a high-level programming language. ○ Applications include web programming, scripting, scientific computing, and artificial intelligence. ● Python is processed at runtime by the interpreter. There is no need to compile your program before executing it. ● It is very popular and used by organizations such as Google, NASA, the CIA, and Disney. © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


For our tutorial: Go to your browser and search Repl.it Python Online IDE

You can also download Python to your computer at https://www.python.org/downloads/ © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #1: Simple Operations +

- * / ** // %

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Simple Operations ● Python has the ability to carry out a variety of calculations: ○ Addition: 3 + 4 = 7

○ Modulo: 7 % 5 = 2

○ Subtraction: 7 - 3 = 4

○ Floor Division: 7 // 2 = 3

○ Multiplication: 4 * 3 = 12 ○ Division: 12 / 4 = 3 ○ Exponentiation: 2 ** 2 = 4

■ It gives you the quotient of a division- the integer result of the division taking out the remainder

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Let’s try it with the interpreter! What’s the result of

What’s the result of :

37 // 7 43 // 9 15 // 4

37 % 7 43 % 9 15 % 4

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #2: Basic Data Types integers

floats

strings

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Basic Data Types ● Integers ○ 1 ○ 4 ○ 156 ● Floats ○ 23.45 ○ 98.7 ○ 89.8

● Strings ○ Words ○ Phrases ○ Sentences ■ Strings need to be enclosed by double quotes (“ ”) or single quotes(‘ ’) ■ Escape character: \

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


String Operations ● Concatenation + ○ Glues two words or phrases, together ■ “Computer ” + “Science” = Computer Science ■ “Computer” + “Science” + “Week” = ComputerScienceWeek

● Multiplication * ○ Produces a repeated version of the original string. ○ It works only with integers. Be mindful of blank ■ “a” * 3 = aaa spaces! They need to be included in the ■ “cs ” * 5 = cs cs cs cs cs string too!

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


String Operations ● The length of a string is determined using the string function len(). ○ Example: len(“Code”) -----> 4

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Type Casting ● Data types can change among themselves ○ A string can become an integer and vice versa ■ int(“7”) = 7 ■ str(7) = “7” ○ An integer can become a float and vice versa ■ float(7) = 7.0 ■ int(7.1) = 7 ○ A float can become a string and vice versa ■ str(7.4) = “7.4” ■ float(“7.4”) = 7.4 © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #3:

Displaying and receiving information print() input() © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Displaying information ● To show a sentence, word, number - anything - Python has the print() function. ○ You write whatever you want to display inside the parenthesis, and the interpreter will take care of it. ■ print(4) ----> 4 ○ When displaying strings, it’s important to include the quotes around it. ■ print(“Hour of Code”) ----> Hour of Code © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Receiving information ● To receive information from the user, Python has the input() function. ○ You write the prompt to the user inside the parenthesis, and wait for them to reply. ■ input(“What’s your name? ”)

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #4: Variables hour_of_code = “Python Basics”

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Variables ● A variable allows you to store a value by assigning it a name, which can be used to refer to the value in the program. To assign a variable, use one equal sign.

● The only characters that are allowed are letters, numbers, and underscores. Be mindful though, variable names cannot be started with numbers.

○ university = “University of Puerto Rico Rio Piedras Campus” © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Variables ● A variable can be reassigned any time needed. Just remember it will lose the previous content it stored. ● Trying to use a variable that has not been defined will cause an error. ● To delete the existence of a variable, we can use the del command. © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Let’s try it with the interpreter! Write a Python program that asks the user for their name and greets them using it.

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Booleans and Comparisons ==

!=

>

<=

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Booleans and Comparisons ● Another data type in Python is the boolean type. ○ There are two boolean values: True and False.

● We use this data type when making comparisons in our program. ○ We can check equality == : 2 == 3 is False ○ We can check inequality != : 2 != 3 is True ○ We can check less than and greater than values < > : 4 > 1 is True ○ We can check greater than or equal values >= : 2 >= 2 is True ○ We have less than or equal values <= : 2 <= 1 is False © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #6: Making Decisions if

elif

else

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Making Decisions ● In many programming languages, there exists what’s called an if statement. If statements carry out certain lines of code if a condition is True. Otherwise, the code inside is not executed. ○ Example: if 5 > 6 : print (“Hey”) ○ Python uses indentation to delimit blocks of code. This is mandatory and if you don’t use the indentation, then the program won’t work. © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Making Decisions ● If we want to check a condition inside another condition, we can use nested if statements. ○ Example: if num > 3: print (“five”) if num <= 47: print(num)

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Making Decisions ● If the condition evaluated in the if statement turns out to be False; you can carry out a block of code using an else statement. ○ Important: else statements can’t be alone, they have to be after an if statement. ○ Example: if 5 > 6: print(“You are awesome!”) else: print(“Coding is fun”) © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Making Decisions ● You can chain if and else statements to determine which option in a series of possibilities is True. We do this by using the shortcut elif. A series of if and elif statements can have a final else block, which is called if the if or none of the elif expressions is True. ○ Example: if 5 < 3: print (“Merry Christmas!”) elif 5 > 3: print (“¡Feliz Navidad!”) else: print(“Happy Holidays”) © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


CODING CHALLENGE Create a program that takes two numbers and evaluates if the sum of them is odd or even.

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #8: While Loops while love: print(“happy”)

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


While Loops ● A while statement can be run more than once. The statements inside it are repeatedly executed, as long as the condition holds. Once it evaluates to False, the next section of code is executed. i=0 while 5 > i: print(i) i += 1

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


While Loops ● The code in the body of a while loop is executed repeatedly. This is called iteration. The infinite loop is a special kind of while loop; it never stops running. Its condition always remains True. We want to avoid these! ● To end a while loop prematurely, the break statement can be used. ● When encountered inside a loop, the break statement causes the loop to finish immediately.

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


While Loops i=0 while 1==1: print(i) i=i+1 if i >= 5: print("Breaking") break print("Finished") © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


While Loops ● Using the break statement outside of a loop causes an error. ● Another statement that can be used within loops is continue. ● Unlike break, continue jumps back to the top of the loop, rather than stopping it.

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


CODING CHALLENGE Create a program that calculates the sum of all the values that are between two numbers. Make it skip every 4 numbers, and stop if it gets to a number larger than 187. Let it tell you the sum it comes up with. © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Section #9: For Loops for i in range(“code”): print(“Coding is awesome!”)

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


For Loops ● The range function creates a sequential list of numbers. ● If range is called with one argument, it produces an object with values from 0 to that argument-1. ● If it is called with two arguments, it produces values from the first to the second-1. ● range can have a third argument, which determines the interval of the sequence produced. This third argument must be an integer.

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


For Loops ● The for loop is commonly used to repeat some code a certain number of times. This is done by combining for loops with range objects. for i in range(5): print("hello!")

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


CODING CHALLENGE Write a Python Program that produces the following pattern:

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


For an interactive tutorial check out: https://www.sololearn.com/Play/Python/

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Moving Forward What you should learn next:

● ● ● ● ● ●

Lists Functions Files Error handling Classes And much more!

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


References ●

https://docs.python.org/3/tutorial/

https://www.tutorialspoint.com/python3/index.htm

https://www.sololearn.com/Play/Python/hoc

© 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


THANK YOU © 2019 Coralys Cubero Rivera Shared under the Creative Commons Attribution-ShareAlike 4.0 International license.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.