Skip to content

Instantly share code, notes, and snippets.

@G-Goldstein
Last active May 5, 2016 10:39
Show Gist options
  • Save G-Goldstein/11bb02469461fbf054eeffae810aad0f to your computer and use it in GitHub Desktop.
Save G-Goldstein/11bb02469461fbf054eeffae810aad0f to your computer and use it in GitHub Desktop.
# The print function
print('Hello') # Hello
print(3) # 3
# Variables
greeting = 'Hello'
print(greeting) # Hello
other_greeting = "Hi"
print(other_greeting) # Hi
number = 5
print(number) # 5
number = number + 1
print(number) # 6
# Comments
# This is a comment line. A # on its own begins a comment, with anything to the right of it being ignored when the script runs.
# I've used comments to indicate the output for each print line. You wouldn't normally do this.
# String concatenation
greeting = 'Hello ' + 'world'
print(greeting) # Hello world
greeting_again = greeting + ' again!'
print(greeting_again) # Hello world again!
# If the result is a string, we can put that straight into the print function
print('Hello ' + 'World!') # Hello World!
# Other things can be printed too. Everything has a printable representation.
print(5 + 3) # 8
# Functions
def add_one(num):
return num + 1
next_num = add_one(3)
print(next_num) # 4
# Challenge one: What does this function do? How do you use it?
def welcome(name):
return 'Hello ' + name
# Solution:
# This function takes in a string and returns a new string that is the original string with the prefix of 'Hello '
# e.g.: welcome('Graham') returns 'Hello Graham'
person = 'Graham'
greeting = welcome(person)
print(greeting) # Hello Graham
# or:
print(welcome('Graham')) # Hello Graham
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment