Skip to content

Instantly share code, notes, and snippets.

@claraj
Created August 9, 2020 13:51
Show Gist options
  • Save claraj/0c03ad70653703c885de9662eebc58b5 to your computer and use it in GitHub Desktop.
Save claraj/0c03ad70653703c885de9662eebc58b5 to your computer and use it in GitHub Desktop.
Python basics
# Python basics
# No need to specify type of variables
zebras = 4
school = 'MCTC'
GPA = 3.4
languages = ['Python', 'JavaScript', 'Swift']
mobile_development_languages = { 'iOS': 'Swift', 'Android': 'Java' }
# For loops - repeat once for every item in an iterable
for language in languages:
print(language)
for platform in mobile_development_languages: # Loops over keys in dictionary by default
print(platform)
# if, elif, else statements. GPA is just a number.
if GPA == 4:
print('Wow!')
elif GPA > 3:
print('Awesome!')
else:
print('Cool!')
if mobile_development_languages['Android'] == 'Java':
print('Time to upgrade to Kotlin!')
# Various list operation, such as...
languages.append('C#')
number_of_languages = len(languages)
languages.sort()
print(languages)
# Slicing iterables - lists, strings
first_two = languages[0:2] # ['C#', 'JavaScript']
print(first_two)
last_language = languages[-1:] # ['Swift']
print(last_language)
# String operations, for example... Also replace, upper, lower ...
message = 'Welcome to the Capstone class'
words = message.split() # split by spaces ['Welcome', 'to', 'the', 'Capstone', 'class']
parts = message.split('t') # or by any other character ['Welcome ', 'o ', 'he Caps', 'one class']
characters = len(message) # 29
# While loops. Python has 'truthy' and 'falsy' values.
# An empty list is considered false in an if-statement. A list with any elements is true
# An empty string is falsy, so is 0 and None
while languages:
print(languages.pop())
name = ''
while not name:
name = input('Please enter your name: ')
# Formatting with f-strings
print(f'Nice to meet you, {name}!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment