Last active
December 22, 2015 16:49
-
-
Save mei-li/6501765 to your computer and use it in GitHub Desktop.
python variables, functions, files for beginners @pyladies-berlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################### | |
# Python basics (Learning python the hard way 1-20) | |
# We will talk about: | |
# * variables | |
# * functions | |
# * files | |
# by Mei Li Triantafyllidi | |
##################################################### | |
# This presentation is executable python code | |
# Whatever is after # symbol is a comment | |
# the rest is code | |
# opensource presentation tool: | |
# https://github.com/inglesp/prescons | |
##################################################### | |
# Variables | |
# ... are placeholders for data | |
# ... are symbolic names for values | |
x = 3 | |
x | |
name = "Mary" | |
name | |
##################################################### | |
# Python is strongly typed | |
# ... every variable has a type | |
type(x) | |
type(name) | |
##################################################### | |
# Python does dynamic typing | |
# ... the type of every variable can change | |
x = "a sequence of characters is a *string*" | |
type(x) | |
##################################################### | |
# A variable can be assigned to an expression | |
k = 4 | |
k = 3 + 5 | |
k | |
k = 4*8.3 - 5 | |
k | |
# expressions can include other variables | |
x = 3 | |
z = 2*x + 5 | |
# how much is z? | |
z | |
# what happens if we change x? | |
x = 4 | |
# how much is z? | |
z | |
########################################################## | |
## What if we wanted z variable to change when we change x? | |
## --> Functions | |
## pieces of code that are defined in one place and executed in another | |
# Lets see a function example | |
def kelvin(celsius): | |
kelvin_value = celsius + 273 | |
return kelvin_value | |
## ... then when we need to do the calculation | |
kelvin(54) # calling the function kelvin with parameter 54 | |
kelvin_value = kelvin(21) | |
kelvin_value | |
# Another function example | |
def get_pyladies_link(location): | |
return "http://" + location + ".pyladies.com" | |
get_pyladies_link("nyc") | |
site = get_pyladies_link("berlin") | |
site | |
################################################ | |
### Printing | |
### If you are not using the interactive console | |
### the result of an expresion will not printed | |
### you have to use print python builtin function | |
print "Hello world!!!" | |
print "You can find our website at " + site + " stay in touch" | |
################################################ | |
### ... so far | |
### Variables store values: | |
### - numbers (integers, real) | |
### - strings (sequence of characters, text) | |
### - ... lists, dictionaries, tuples, objects | |
### Variables get assigned to expressions | |
### Expressions can include | |
### * arithmetic operations between: | |
### numbers, strings, variables, function calls | |
### of similar type | |
### ... logic operations | |
intro = "This is pyladies site: " + get_pyladies_link("berlin") | |
intro | |
################################################ | |
### ....lets continue with functions | |
### Functions can get more than one argument | |
### Fuctions do not need to return something | |
def greeting(name, site): | |
print "Welcome " + name + "!!!" | |
print "We are so happy to expand our community in " + site + "." | |
greeting("Helen", "http://best-site.com") | |
greeting("Korina", get_pyladies_link("berlin")) | |
########################################################## | |
## Functions summary | |
## * def is used to define the function | |
## * after is the function name | |
## * after the parameters comma separated in parenthesis (zero or more) | |
## * return statement can used to return the outcome of the function | |
################################################ | |
## Files | |
## Writing to a file | |
filename = "myfirst_created_by_program_file.txt" | |
## function open | |
## http://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files | |
f = open(filename, "w") | |
f.write("I can write in the file?!?!?!?") | |
f.close() | |
################################################ | |
# Altogether | |
# Lets make a function to send greetings to some pyladies | |
def make_greeting_file(name): | |
f = open("Greeting_" + name + ".txt", "w") | |
f.write("Welcome " + name + "!!!\n") | |
f.write("We are so happy to expand our community in pyladies.") | |
f.close() | |
make_greeting_file("Marina") | |
# code reuse | |
make_greeting_file("Klara") | |
################################################ | |
### Files summary | |
### * Files open with *open* function | |
### * to open for writing, pass "w" as second parameter to open | |
### - Always close the file, with close method | |
### - use "write" method to write into a file | |
### | |
### The end .... | |
########################################## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment