天天看点

jupyter notebook for python (二)

(一)

字符串(string):

A String is a common type of data, used in programming, consisting of a sequence of 1 or more characters.

  • A String is a sequence of characters (aka: a string of characters)
    • Character examples: 

      A, B, C, a, b, c, 1, 2, 3, !, &

  • A String in python is contained in quotes, single(') or double(")
    • String examples: "ABC", 'Joana Dias', 'I have 2 pet cats.', "item #3 cost $3.29 USD"
      Note: the quotes containing a string must come in matched pairs
      • "Hello"

         or 

        'Hello'

         are correctly formatted strings
      • "Hello'

         is incorrectly formatted starting with double " and ending with single ' python needs a matching quote to know where the string ends

示例图: 

jupyter notebook for python (二)

(二)

整数型(Integers):

#A integers is another type:

  • whole numbers such as 

    -2, -1, 0, 1, 2, 3

     are Integers
  • Integers are not placed in quotes

A String can contain any character, this includes letters, spaces, punctuation, number digits and formatting. In a string with digits (

"123"

) the digits are text images rather than representations of numeric values.

remember a line of python code starting with the pound or hash symbol (#*) indicates a *comment

comments are for humans to read and are not run by computers

示例图:

jupyter notebook for python (二)

(三) 

赋值:

Variables & Storing Strings in Variables:

jupyter notebook for python (二)

单词翻译:

jupyter notebook for python (二)
jupyter notebook for python (二)
jupyter notebook for python (二)

总结:我就把它当成一个变量,然后被赋值,打印输出的结果一样。 

详情:

Variables are named containers

Computer programs often create storage for things that are used in repeated processing like item_price, student_name, stock_symbol. In python a variable is a type of object that can be addressed by name to access the assigned contents.

Name a variable staring with a letter or underscore "_"

There are different styles for creating variables this course uses lowercase descriptive names connected by underscores:

  • item_price
  • student_name
  • stock_symbol

descriptive names reduce the need for comments in the code and make it easier to share code or read code written long ago

Variables can hold strings

once a variable is assigned:

current_msg = "I am a string"

a python program can refer to the variable,   

current_msg

,   in code

so that  

current_msg

, can be used like the string value  (

"I am a string"

)   which it stores

Variable reassignment

We can reassign a variable, changing the contents. 

current_msg = "new current message"

Variables can be used in place of literals

If we initialize 

current_msg = "new current message"

 then

the literal string 

"new current message"

 and the variable 

current_msg

 are the same when used in code

(四) 

(变量可以重复使用,并被覆盖)

Data types in variables:

实例图:

(图中的任务只是改写例子的名字就ok了)

jupyter notebook for python (二)

详情:

variables can be initialized with different data types. Below we see item_price is a decimal(aka - float) and student_name is string

item_price = 10.25 #item_price initialized as a numeric value (no quotes)
student_name ="Dias, Joana" #student_name initialized as a string
license_plate = "123A" #license_plate initialized as a string
           

Variables can start initialized as an Integer number data type

x = 22
           
here the Integer 

22

  value was assigned to the variable  

x

The type of data a variable holds can be changed,

Python variables can change the type of data a variable holds

x = 22
x = "I am a string"
           

x

  changed type from Integer  

x = 22

  to string  

x = "I am a string"

Best Practice: Friendly Names
Friendly name examples, (

item_price, student_name,license_plate

), were used above.

Variables help us to write code that can be used repeatedly

A personalized letter can be sent to every student with individual names by using a name in a variable. Let's start with printing a name based on string variable.

Create a variable, 

name

, at the top of the next cell. Assign a string value to   

name

.

Remember to use the quotes for string values

example:

name = "Joana Dias"
print(name)
           

翻译: 

jupyter notebook for python (二)
jupyter notebook for python (二)