Python Notes (1) - Syntax and Strings

转载请注明出处: http://blog.csdn.net/cxsydjn/article/details/71302732

The note introduces basic Python syntax and strings.

Python notes of open courses @Codecademy.

Brief Introduction

Python is a high level scripting language with object oriented features.

Python programs can be written using any text editor and should have the extension .py.

You may click to Python Basics for more details.

Python Syntax

  • Variables and Data Types
    • Var_name = X. X could be int 5 / float 1.23 / boolean True (or False)
  • Whitespace
    • Important. Have to be careful with how to use it.
    • Should indent your code with correct (usually four) spaces.
  • Comments
    • Single Line: #
    • Multi-Line: """ bla…bla… """
  • Operators and Maths

    • Add, subtract, multiply, divide numbers like +, -, *, /
    • Exponentiation: **
    • Modulo (which returns the remainder from a division): %

      
      # Examples for `/`
      
      5 / 2
      
      # 2
      
      
      5.0 / 2
      
      # 2.5
      
      
      float(5) / 2
      
      # 2.5
      
    • When you divide an integer by another integer, the result is always an integer (rounded down, if needed).

    • When you divide a float by an integer, the result is always a float.
    • To divide two integers and end up with a float, you must first use float() to convert one of the integers to a float.
  • External Resources

Strings

  • Strings
    • String: can contain letters, numbers, and symbols. A string could created by 'String', "String", or str(3.14).
    • Escaping Characters: 'There's' should be written as 'There\'s'
    • Access by Index: In Python, we start counting the index from zero instead of one. E.g., c = "cats"[0].
  • String Methods
    • len(): gets the length (the number of characters) of a string. E.g., len(str_name) or len("String")
    • lower(): gets rid of all the capitalization in strings. E.g., str_name.lower()
    • upper(): makes a string completely upper case. E.g., str_name.upper()
    • str(): turns non-strings into strings. E.g., str(str_name)
    • Dot Notation: Methods that use dot notation only work with strings. Additionally, len() and str() can work on other data types.
  • Print

    • print: simply displays your code in the console.
    • String Concatenation: The + operator between strings will ‘add’ them together, one after the other. E.g., print "I " + "love " + "python", you might get I love python
    • Explicit String Conversion: combines a string with something that isn’t a string using str(). E.g., print "I love python " + str(3.0), you might got I love python 3.0
    • String Formatting with %: The % operator after a string is used to combine a string with variables. The % operator will replace a %s in the string with the string variable that comes after it. E.g.,

      
      # Example 1
      
      string_1 = "you"
      string_2 = "me"
      
      print "I love %s! Do you love %s?" % (string_1, string_2)
      
      
      # Example 2  
      
      name = raw_input("Question1")
      quest = raw_input("Question2")
      color = raw_input("Question3")
      
      print "Answer to Q1 is %s, answer to Q2 is %s, " \
      "and answer to Q3 is %s." % (A1, A2, A3)
  • Date and Time

    • datetime: keeps track of time when something happened.
    • datetime.now(): to retrieve the current date and time.
    • Extracting Information: year, month, day, hour, minute, and second from datetime.now().
    • Print as yyyy-mm-dd, mm/dd/yyyy and hh:mm:ss: see examples below

      
      # Using the first row to import the **datetime library**.
      
      from datetime import datetime
      
      now = datetime.now()
      
      
      # Extracting Information
      
      current_year = now.year
      current_month = now.month
      current_day = now.day
      
      
      # print date as `yyyy-mm-dd` and `mm/dd/yyyy`
      
      print '%s-%s-%s' % (now.year, now.month, now.day)
      print '%s/%s/%s' % ( now.month, now.day, now.year)
      
      
      # print time as `hh:mm:ss`
      
      print '%s:%s:%s' % ( now.hour, now.minute, now.second)

External Resources

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值