Professional Documents
Culture Documents
Joel Lee
1754 articles
python databases
crate.io
OPEN
>>>age = 28
>>>title = "{} the {} of {} years".format(name, job, age)
>>>title
>"Joel the Programmer of 28 years"
python databases
crate.io Taste Test: Energy Drinks (Wha
nosql database for python open source V?) | TGTS S3E4
data store as backend
OPEN
>>>availability = ["Monday", "Wednesday",
PC & MOBILE "Friday", "Saturday
LIFESTYLE
>>>result = " - ".join(availability)
>>>result
>'Monday - Wednesday - Friday - Saturday' Latest Deals
Pay What You $
The de ned string is the separator that goes between each Want: Adobe CC
list item, and the separator is only inserted between two A-Z Lifetime
items (so you won’t have an extraneous one at the end).
Using the join method is much faster than doing it by hand.
The Ultimate $
Data & Analytics
Conditionals Bundle:
Boolean Values
StackSkills $
Unlimited:
Like in all other programming languages, comparison
Lifetime Access
operators evaluate to a boolean result: either True or False.
Here are all the comparison operators in Python:
The ==, !=, and <> operators above are used to compare the
values of two variables. If you want to check if two
variables point to the same exact object, then you’ll need to
use the is operator:
PC & MOBILE LIFESTYLE
>>>a = [1,2,3]
>>>b = [1,2,3]
>>>c = a
>>>print(a == b) # True
>>>print(a is b) # False
>>>print(a is c) # True
>>>a = [1,2,3]
>>>b = [1,2,3]
>>>if a is not b:
>>> # Do something here
>>>x = False
>>>if not x:
>>> # Do something here
The in Operator
Complex Conditionals
>>>legs = 8
>>>habitat = "Land"
>>>if legs == 8 and habitat == "Land":
>>> species = "Spider"
>>>weather = "Sunny"
>>>if weather == "Rain" or weather == "Snow":
>>> umbrella = True
>>>else:
>>> umbrella = False
>>>weather = "Sunny"
>>>umbrella = weather == "Rain" or weather == "Snow"
>>>umbrella
>False
Loops PC & MOBILE LIFESTYLE
>>>i = 0
>>>while i < 10:
>>> print(i)
>>> i = i + 1
>>>i = 0
>>>while True:
>>> print(i)
>>> if i >= 10:
>>> break
Enumerations
>Monday is weekday 0
>Tuesday is weekday 1
>Wednesday is weekday 2
>Thursday is weekday 3
>Friday is weekday 4
>>>i = 0
>>>for day in weekdays:
>>> print("{} is weekday {}".format(day, i))
>>> i = i + 1
Dictionaries
Dictionaries (or dicts) are the most important data type to
know in Python. You’re going to be using them all the time.
They’re fast, they’re easy to use, and they will keep your
code clean and readable. Mastery of dicts is half the battle
in learning Python.
READ MORE
>>>d = {}
>>>d = {}
>>>d["one_key"] = 10
>>>d["two_key"] = 25
>>>d["another_key"] = "Whatever you want"
The nice thing about a dict is that you can mix and match
variable types. It doesn’t matter what you put in there. To
make initialization of a dict easier, you can use this syntax:
>>>d = {
>>> "one_key": 10,
>>> "two_key": 25,
>>> "another_key": "Whatever you want"
>>>}
>>>d["one_key"]
>10
>>>d["another_key"]
>"Whatever you want"
>>>d["one_key"] + d["two_key"]
>35
>>>for key in d:
>>> print(key)
And if you want to remove an item from a dict, use the del
operator:
>>>del d["one_key"]
>>>capitals = {
>>> "Alabama": "Montgomery",
>>> "Alaska": "Juneau",
>>> "Arizona": "Phoenix",
>>> ...
>>>}
>>>state = "Pennsylvania"
>>>capitals[state]
>"Harrisburg"