chapter 2 variables and simple data types

Variables

  • You can change the value of a variable in your program at any time, and Python will always keep track of its current value.
  • Naming and Using Variables
    • Variable names can contain only letters, numbers, and underscores. They can start with a letter or an underscore, but not with a number. For instance, you can call a variable message_1 but not 1_message.
    • Spaces are not allowed in variable names, but underscores can be used to separate words in variable names. For example, greeting_message works but greeting message will cause errors.
    • Avoid using Python keywords and function names as variable names. For example, do not use the word print as a variable name; Python has reserved it for a particular programmatic purpose. (See “Python Keywords and Built-in Functions” on page 466.)
    • Variable names should be short but descriptive. For example, name is better than n, student_name is better than s_n, and name_length is better than length_of_persons_name.
    • Be careful when using the lowercase letter l and the uppercase letter O because they could be confused with the numbers 1 and 0.
  • The Python variables you’re using at this point should be lowercase. You won’t get errors if you use uppercase letters, but uppercase letters in variable names have special meanings that we’ll discuss in later chapters
  • Avoiding Name Errors When Using Variables
    • A name error usually means we either forgot to set a variable’s value before using it, or we made a spelling mistake when entering the variable’s name
  • Variables Are Labels
    • It’s much better to think of variables as labels that you can assign to values. You can also say that a variable references a certain value.

Strings

  • A string is a series of characters. Anything inside quotes is considered a string in Python, and you can use single or double quotes around your strings like this: “This is a string.” ‘This is also a string.’
  • This flexibility allows you to use quotes and apostrophes within your strings
  • Changing Case in a String with Methods
    • One of the simplest tasks you can do with strings is change the case of the words in a string.
    • Every method is followed by a set of parentheses, because methods often need additional information to do their work.
    • The title() function doesn’t need any additional information, so its parentheses are empty.
    • The title() method changes each word to title case, where each word begins with a capital letter.
  • Using Variables in Strings
    • use a variable’s value inside a string
    • To insert a variable’s value into a string, place the letter f immediately before the opening quotation mark
    • Put braces around the name or names of any variable you want to use inside the string. Python will replace each variable with its value when the string is displayed.
    • These strings are called f-strings. The f is for format, because Python formats the string by replacing the name of any variable in braces with its value.
  • Adding Whitespace to Strings with Tabs or Newlines
    • In programming, whitespace refers to any nonprinting characters, such as spaces, tabs, and end-of-line symbols. You can use whitespace to organize your output so it’s easier for users to read
    • \n \t
  • Stripping Whitespace
    • rstrip()
    • When the rstrip() method acts on the variable favorite_language 3, this extra space is removed. However, it is only removed temporarily. If you ask for the value of favorite_language again, the string looks the same as when it was entered, including the extra whitespace
    • To remove the whitespace from the string permanently, you have to associate the stripped value with the variable name
    • lstrip()
    • strip()
  • Removing Prefixes
    • simple_url = nostarch_url.removeprefix(‘https://’)
  • Avoiding Syntax Errors with Strings
    • if you use an apostrophe within single quotes, you’ll produce an error.
    • Your editor’s syntax highlighting feature should help you spot some syntax errors quickly as you write your programs. If you see Python code highlighted as if it’s English or English highlighted as if it’s Python code, you probably have a mismatched quotation mark somewhere in your file.

Numbers

  • Integers
    • add (+), subtract (-), multiply (*), and divide (/)
    • Python uses two multiplication symbols to represent exponents
  • Floats
    • Python calls any number with a decimal point a float.
  • Integers and Floats
    • When you divide any two numbers, even if they are integers that result in a whole number, you’ll always get a float
    • Python defaults to a float in any operation that uses a float, even if the output is a whole number.
  • Underscores in Numbers
    • When you’re writing long numbers, you can group digits using underscores to make large numbers more readable
    • When you print a number that was defined using underscores, Python prints only the digits
    • universe_age = 14_000_000_000
  • Multiple Assignment
    • You can assign values to more than one variable using just a single line of code.
    • x, y, z = 0, 0, 0
  • Constants
    • Python programmers use all capital letters to indicate a variable should be treated as a constant and never be changed
    • MAX_CONNECTIONS = 5000

Comments

  • How Do You Write Comments?
    • In Python, the hash mark (#) indicates a comment.
  • What Kinds of Comments Should You Write?
    • Skilled programmers expect to see comments in code, so it’s best to start adding descriptive comments to your programs now. Writing clear, concise comments in your code is one of the most beneficial habits you can form as a new programmer.
    • When you’re deciding whether to write a comment, ask yourself if you had to consider several approaches before coming up with a reasonable way to make something work; if so, write a comment about your solution.

The Zen of Python

  • import this
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值