- Python Basics
- Collections
- Control Flow
- Functions
- Classes
- Modules
- Generators and Decorators
- Python Next
Example code based on
LearnXinYminutes
and licensed under
CC Attribution-Share 3
Python Basics
Python is a general-purpose language focused on readability. Unlike most languages, whitespace matters in python, and classes, functions and control structures are marked by being indented. Python is similar to Ruby, though it believes in one way to do things. This module focuses on Python 2.7.
# Single line comments start with a hash.
""" Multiline strings can be written
using three "s, and are often used
as comments
"""
# Everything is an object in Python
type(3) #=>
type(3.0) #=>
type("word") #=>
type(True) #=>
type(None) #=>
Basic Math
# Math is what you would expect
1 + 1 #=> 2
8 - 1 #=> 7
10 * 2 #=> 20
35 / 5 #=> 7
2 ** 5 #=> 32
5 % 3 #=> 2 #mod
# Python automatically floors the results of Integer division (like Java)
5 / 2 #=> 2
# Use floats to fix this:
2.0 # This is a float
5.0 / 2.0 #=> 2.5
# Enforce precedence with parentheses
(1 + 3) * 2 #=> 8
Logic
# Boolean values are capitalized
True
False
# negate with not
not True #=> False
not False #=> True
# Equality and Inequality
1 == 1 #=> True
2 == 1 #=> False
1 != 1 #=> False
2 != 1 #=> True
# Comparisons
1 < 10 #=> True
1 > 10 #=> False
2 <= 2 #=> True
2 >= 2 #=> True
# Comparisons can be chained!
1 < 2 < 3 #=> True
2 < 3 < 2 #=> False
#Logical Operators
3>2 and 2>1 #=> True
2>3 and 2>1 #=> False
2>3 or 2>1 #=> True
# None is an object (like nil or null)
None #=> None
# Don't use the equality "==" symbol to compare objects to None
# Use "is" instead
"etc" is None #=> False
None is None #=> True
# The 'is' operator tests for object identity. This isn't
# very useful when dealing with primitive values, but is
# very useful when dealing with objects.
# None, 0, and empty strings/lists all evaluate to False.
# All other values are True
bool(None) #=> False
bool(0) #=> False
bool("") #=> False
Strings
# Strings are created with single or double-quotes
"This is a string."
'This is also a string.'
# Strings can be added too!
"Hello " + "world!" #=> "Hello world!"
# A string can be treated like a list of characters
"This is a string"[0] #=> 'T'
# % can be used to format strings:
"%s can be %s" % ("strings", "interpolated")
#=> 'strings can be interpolated'
# A newer, preferred way to format strings is the format method.
"{0} can be {1}".format("strings", "formatted")
#=> 'strings can be formatted'
# You can use keywords if you don't want to count.
"{name} wants to eat {food}".format(name="Bob", food="lasagna")
#=> 'Bob wants to eat lasagna'
# Python has a print function, available in versions 2.7 and 3...
print("I'm Python. Nice to meet you!")
# and an older print statement, in all 2.x versions but removed from 3.
print "I'm also Python!"
Variables
# No need to declare variables before assigning to them.
some_var = 5 # Convention is to use lower_case_with_underscores
some_var #=> 5
# You can assign multiple variables in one line
x = y = 10 #=> 10
x #=> 10
y #=> 10
# Variables can be dynamically assigned to different types
thing = 5 #=> 5
thing = "hello" #=> "hello"
thing = True #=> True
# Accessing a previously unassigned variable is an exception.
# See Control Flow to learn more about exception handling.
some_other_var # Raises a name error
Challenge
What will the following code print?
dollars = 23
cents = 34
print("He has ${0}.{1}".format(dollars, cents))
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Challenge
ab is considered powerful if (and only if) both of the following 2 conditions are met:
- ab >= 2 * b2
- ab >= (a*b)2
return True
if ab is powerful and False
otherwise.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
Kavier
Jul 3, 8:54 PMI believe only an if statement will solve this correctly since need to do one or the other, but it doesn't look like if statements are covered in this section :( Minor thing but I had to go find this out somewhere else due to it not being listed here.
Also, the for statement is used in the code but never actually exampled, same for the function use.
Learneroo
Oct 26, 10:53 PM@Kavier, thanks, the challenge has been changed so you don't need an if-statement. (The boilerplate code can be ignored.)
Mandy
Nov 3, 5:38 AMwhy doesn't this work?
if (a**b >= 2 * b**2 and a**b >= (a*b)**2) {
return True;
}
else return False;
Learneroo
Nov 3, 10:08 AMThat's not the if-statement in python. Either peak ahead to control flow or solve the problem without an if-statement.
Ed Mondragon
Dec 9, 11:15 AMThe correct code is:
if (a**b >= 2*b**2) and (a**b >= (a*b)**2):
return True
else:
return False
Sherif Bassiouny
Jun 11, 10:38 AMi think that the problem statement points to decision making , so IF construct is implied , but since it is not yet the time for control flow i used this code and it worked fine :
cond1 = a ** b >= 2 * b * b
cond2 = a ** b >= (a * b) ** 2
return(cond1 and cond2)