четверг, 8 октября 2015 г.

№2 Строки

Escaping characters
There are some characters that cause problems. For example:
'There's a snake in my boot!'
This code breaks because Python thinks the apostrophe in 'There's'ends the string. We can use the backslash to fix the problem, like this:

'There\'s a snake in my boot!'        

# The string below is broken. Fix it using the escape backslash
'This isn\'t flying, this is falling with style!'  

"""
The string "PYTHON" has six characters,
numbered 0 to 5, as shown below:

+---+---+---+---+---+---+
| P | Y | T | H | O | N |
+---+---+---+---+---+---+
  0   1   2   3   4   5

So if you wanted "Y", you could just type
"PYTHON"[1] (always start counting from 0!)
"""
fifth_letter = "MONTY"[5]
print fifth_letter     

String methods
Great work! Now that we know how to store strings, let's see how we can change them using string methods.
String methods let you perform specific tasks for strings.
We'll focus on four string methods:
  1. len()
  2. lower()
  3. upper()
  4. str()
Let's start with len(), which gets the length (the number of characters) of a string!                                      
parrot = "Norwegian Blue"
print len(parrot)

>14

Now let's look at str(), which is a little less straightforward. The str()method turns non-strings into strings! For example would turn 2 into "2"."""Declare and assign your variable on line 4,
then call your method on line 5!"""

pi = 3.14
print str(pi)
3.14

Dot Notation
Let's take a closer look at why you uselen(string) and str(object), but dot notation (such as "String".upper()) for the rest.
lion = "roar"
len(lion)
lion.upper()
Methods that use dot notation only work with strings.
On the other hand, len() and str()can work on other data types.
ministry = "The Ministry of Silly Walks"
print len(ministry)
print ministry.upper()
>27 >THE MINISTRY OF SILLY WALKS
The area where we've been writing our code is called the editor.
The console (the window in the upper right) is where the results of your code is shown.
print simply displays your code in the console.
String Concatenation
You know about strings, and you know about arithmetic operators. Now let's combine the two!
print "Life " + "of " + "Brian"
This will print out the phrase Life of Brian.
The + operator between strings will 'add' them together, one after the other. Notice that there are spaces inside the quotation marks after Lifeand of so that we can make the combined string look like 3 words.
Combining strings together like this is called concatenation. Let's try concatenating a few strings together now!
Make sure you include the spaces after"Spam " and "and ".

# Print the concatenation of "Spam and eggs" on line 3!


print "spam " + "and " + "eggs"
String Formatting with %, 
Remember, we used the % operator to replace the %s placeholders with the variables in parentheses ( круглая скобка или парентеза, вводное слово) Remember, we used the % operator to replace the %s placeholders with the variables in parentheses.
name = "Mike"
print "Hello %s" % (name)
You need the same number of %sterms in a string as the number of variables in parentheses:
print "The %s who %s %s!" % ("Knights", "say", "Ni")
# This will print "The Knights who say
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is  %s, " \
"and your favorite color is %s." % (name, quest, color)




And Now, For Something Completely Familiar
Great job! You've learned a lot in this unit, including:
Three ways to create strings
'Alpha'
"Bravo"
str(3)
String methods
len("Charlie")
"Delta".upper()
"Echo".lower()
Printing a string
print "Foxtrot"
Advanced printing techniques
g = "Golf"
h = "Hotel"
print "%s, %s" % (g, h)
# Write your code below, starting on line 3! my_string = "Buuuuchneva" print len(my_string) print my_string.upper()
 

четверг, 1 октября 2015 г.

пост №1

в теле функции обязательно отбивать табом команды. таб == 4 пробела. Блять.
Что-то R мне нравится больше.

#comments

""" commentscommentscommentscommentscommentscommentscommentscommentscommentscommentscommentscommentscommentscommentscomments
"""

Now let's work with exponents.
eight = 2 ** 3
In the above example, we create a new variable called eight and set it to 8, or the result of 2 to the power to 3 (2^3).
Notice that we use ** instead of * or the multiplication operator.

Our final operator is moduloModulo returns the remainder from a division. So, if you type 3 % 2, it will return 1, because 2 goes into 3 evenly once, with 1 left over.

# comment's linemonty = Truepython = 1.234monty_python = python ** 2


Примитивнейшие задачки: 
1) 
You've finished eating at a restaurant, and received this bill:
  • Cost of meal: $44.50
  • Restaurant tax: 6.75%
  • Tip: 15%
You'll apply the tip to the overall cost of the meal (including tax).

# Assign the variable meal the value 44.50 on line 3!meal =  44.50tip = 44.50 * 0.15cost =  meal + tip
# Assign the variable total on line 8!

meal = 44.50
tax = 0.0675
tip = 0.15

meal = meal + meal * tax
total = meal + meal * tip

print("%.2f" % total)