Skip to content

Instantly share code, notes, and snippets.

@monteirobrena
Last active May 2, 2018 01:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monteirobrena/83b2bd39ac13ee9677d1b6e50ca2b378 to your computer and use it in GitHub Desktop.
Save monteirobrena/83b2bd39ac13ee9677d1b6e50ca2b378 to your computer and use it in GitHub Desktop.
Introduction to Python Programming
my_var = 500
# Decrease 10%
my_var *= .9
# Increase 5%
my_var *= 1.05
# Return only integer of division
my_var // 3 # 166
my_var / 3 # 166.66666666666666
# Convert to integer
int(my_var)
# Convert to float
float(my_var)
# Convert to string
str(my_var) # '500'
# Type of object
type(my_var)
# Logical operators
and
or
not
3 == 3 and 9 != 0 # True
3 == 3 or 9 != 0 # True
not (3 == 3 and 9 != 0) # False
# String
my_string_1 = "Hello"
my_string_2 = "!"
len(my_string_1) # 5
len(my_var) # TypeError: object of type 'int' has no len()
my_string_1.islower() # False
"brena monteiro".title() # Brena Monteiro
"brena monteiro".count("Brena") # 0
"brena monteiro".count("brena") # 1
"brena monteiro".count("bre") # 1
print(my_string_1 + my_string_2) # Hello!
print(my_string_1 * 3) # HelloHelloHello
my_string_1[0] # 'H'
"0" + "5" # '05'
0 + 5 # 5
"0" + 5 # TypeError: must be str, not int
0 + "5" # TypeError: unsupported operand type(s) for +: 'int' and 'str'
# List
my_list = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
my_list[0] # 'Jan'
my_list[-1] # 'Dec'
my_list[-12] # 'Jan'
my_list[12] # IndexError: list index out of range
my_list[-13] # IndexError: list index out of range
my_list[0:3] # ['Jan', 'Feb', 'Mar']
# my_string.join(list) - Add the string between each element of the list
new_list = "\n".join(["one", "two", "three"])
print(new_list)
# one
# two
# three
# min(list) - Sorted alphabetically to get the greatest element of the list
min(my_list) # 'Apr'
# max(list) - Sorted alphabetically to get the smallest element of the list
max(my_list) # 'Sep'
# len(list) - Return the length of elements in the list
len(my_list) # 12
# sorted(list) - Sorted alphabetically
sorted(my_list) # ['Apr', 'Aug', 'Dec', 'Feb', 'Jan', 'Jul', 'Jun', 'Mar', 'May', 'Nov', 'Oct', 'Sep']
# list.append(element) - Add the element in the final of the list
my_list.append('2018') # ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec', '2018']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment