Python Basics at PyCAR2020

Strings

Create a string and assign it to a variable


In [ ]:
my_string = "We're going to learn Python at #nicar20"

Print your string. The print() command was optional in Python 2.x, but is now required in Python 3.x.


In [ ]:
print(my_string)

Get the type of an object.


In [ ]:
type(my_string)

Make a comment to yourself and then get the length of your string.


In [ ]:
#this is a comment in my code
len(my_string)

Remember that a string is just a series of characters, not a word or a sentence So you can "slice" off pieces of a string based on the index of the characters in it. Try to slice off the last 5 characters in my_string


In [ ]:
my_string[-5:]

Convert it to lowercase.


In [ ]:
my_string.lower()

Convert it to uppercase.


In [ ]:
my_string.upper()

Convert it to titlecase.


In [ ]:
my_string.title()

Concatenate strings.


In [ ]:
my_string + my_string

Split strings.


In [ ]:
my_string.split("learn")

Join a list of strings


In [ ]:
"+++".join([my_string, my_string])

Remove a character.


In [ ]:
my_string.replace(" ", "##")

Strip whitespace.


In [ ]:
"  this string has whitespace  ".strip()

Removes only leading whitespace chars.


In [ ]:
"  this string has whitespace  ".lstrip()

Removes only trailing whitespace chars.


In [ ]:
"  this string has whitespace  ".rstrip()

I need help remembering what methods my variable has available.


In [ ]:
dir(my_string)

I need help on a specific method.


In [ ]:
help(my_string.lower)

integers

Create an integer.


In [ ]:
my_integer = 25

Print your integer.


In [ ]:
print(my_integer)

Get the type of your integer.


In [ ]:
type(my_integer)

Do some addition.


In [ ]:
my_sum = my_integer + 10

print(my_sum)

Do some subtraction.


In [ ]:
my_difference = my_integer - 10
print(my_difference)

Do some multiplication.


In [ ]:
my_product = my_integer * 10
print(my_product)

Do some division.


In [ ]:
my_quotient = my_integer / 10
print(my_quotient)

Floats

Create a float, assign it a value and print it.


In [ ]:
my_float = 25.345
print(my_float)

Divide a float in half.


In [ ]:
23.46/2

Divide an integer in half. If you're used to Python 2.X, this will behave a little bit differently.


In [ ]:
5/2

Lists

Create a list.


In [ ]:
a_list = [1, 2, 3]

Create a list, you can use strings, integers, variables, etc


In [ ]:
my_list = ["We're going to learn Python at #NICAR16", 10, 15, 20]

Print the list.


In [ ]:
print(my_list)

Print its type.


In [ ]:
type(my_list)

Print its length.


In [ ]:
len(my_list)

Create a list of numbers.


In [ ]:
my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Print the first item in the list.


In [ ]:
my_list[0]

Print the last item in the list.


In [ ]:
my_list[-1]

Print all items from 4 through the end. We call this slicing.


In [ ]:
my_list[3:]

Print the items from the start to the penultimate.


In [ ]:
my_list[:-1]

Print the middle 4 items.


In [ ]:
my_list[3:-3]

Add an item to the list and see if it was added.


In [ ]:
my_list.append(11)
my_list

Delete the last item in the list.


In [ ]:
del(my_list[-1])
my_list

Set the last value in the list to a variable


In [ ]:
last = my_list.pop(-1)
last

In [ ]:
my_list

Dictionaries

Create an empty dictionary


In [ ]:
mydict = {}

Add a key called "class_size", whose value is my_product


In [ ]:
mydict['class_size'] = my_product
mydict

Add a dictionary key called "nerds" and a value of "at bar".


In [ ]:
mydict['nerds'] = 'at bar'
mydict

Call the value of a dictionary for the key "nerds".


In [ ]:
mydict['nerds']

Conditionals & Comparisons

Take your list or make a new one, loop through it printing out each value


In [ ]:
my_list = [1, 2, 3, 4, 5, 6]
for x in my_list:
    print(x)

Create two variables, assign values and compare them


In [ ]:
x = 5
y = 15

In [ ]:
x == y

In [ ]:
x != y

In [ ]:
x > y

In [ ]:
x >= y

In [ ]:
x < y

In [ ]:
x <= y

Create two variables, assign one as a number and one as a string and compare them


In [ ]:
z = 10
v = "10"
z == v

If x is greater than y, print my_list. Otherwise, print my_dict.


In [ ]:
if x > y:
    for z in my_list:
        print(z)
else:
    for whatever in my_list[-3:]:
        print(whatever)

This barely skims the surface of what you can do in Python, but hopefully this overview will make you comfortable enough to get started.

Now, exit the program.


In [ ]:
exit()