21 days Python from introduction to time Day1

Posted by NathanLedet on Sun, 14 Jun 2020 11:23:44 +0200

Chapter 2 variables and simple data types

  1. variable
    Variable names can only contain letters, numbers and underscores. They can start with letters and underscores, but not numbers
    Variable names cannot contain spaces. You can use underscores to separate words
    python keywords and function names cannot be used as variable names
    Variable names should be short and descriptive
    Be careful with lowercase l and capital O (easy to think of as numbers 1 and 0)
    Therefore, you should avoid naming mistakes when using variable names
  2. character string
    Display each word in uppercase, i.e. change the initial of each word to uppercase title()
    Change string to all uppercase upper()
    Change string to all lowercase lower()
    Python uses the plus sign (+) to merge strings
    Tabs \ t
    Line break \ n
    You can include tabs and line breaks in the same string
    Remove the blank string () at both ends of the string
    Remove the blank lstrip() at the beginning of the string
    Remove the blank rstrip() at the end of the string
    Note: this deletion is temporary. If you need to permanently delete, you need to reassign the deleted string to the variable
    Using strings to avoid syntax errors
    Using str() to avoid numeric type errors

Give it a try (see code NO1.py `)

  1. Store a message in a variable and print it out
  2. Store a message in the variable and print it out; then change the value of the variable to a new message and print it out.
  3. Save the user name in a variable and display a message to the user
  4. Case names in 3
  5. Find a famous saying and print it out (including the name and the famous saying of the person)
  6. Repeat 5, store the name in a variable, the message in a variable, and then print out the famous sentence
  7. Remove whitespace from the name of the person (use at least one combination of characters \ t \ Nall)
  8. Write 4 expressions, using addition, subtraction, multiplication and division respectively, but the results are all 8
  9. Store a number in a variable, then use the variable to create a message and print
"""
1.	Store a message in a variable and print it out
2.	Store a message in the variable and print it out; then change the value of the variable to a new message and print it out.
3.	Save the user name in a variable and display a message to the user
4.	Case names in 3
5.	Find a famous saying and print it out (including the name and the famous saying of the person)
6.	Repeat 5, store the name in a variable, the message in a variable, and then print out the famous sentence
7.	Remove whitespace from person name (use at least combination of characters\t \n Once each)
8.	Write 4 expressions, using addition, subtraction, multiplication and division respectively, but the results are all 8
9.	Store a number in a variable, then use the variable to create a message and print
"""

message = 'Hello World!'
print(message)
#Hello World!

message = 'Good Bye!'
print(message)
#Hello World!
#Good Bye!

people = 'TuTu'
print(people + ' is a genious girl.')
print(people.title() + ' is a nice girl')
print(people.upper() + ' is a beautiful girl')
print(people.lower() + ' is a rich girl')

celebrity = 'Oska'
print(celebrity +' said:"We are all in the gutter,but some of us are looking at the stars."')

celebrity = 'Oska'
sentence = 'We are all in the gutter,but some of us are looking at the stars.'
print(celebrity + ' said:"' + sentence +'"' )

celebrity = ' Oska '
sentence = ' We are all in the gutter,but some of us are looking at the stars. '
print(celebrity.strip() + sentence.lstrip())
print(celebrity.rstrip() + sentence.lstrip())
print(sentence)
print('\t We are all in the gutter, \t but some of us are looking at the stars.')
print('\n\t We are all in the gutter, \n but some of us are looking at the stars.')

print(3+5)
print(int(3.2)+int(5.9))
print(int(float(3.2)+float(4.8)))
print(int(str(8)))

num = 17
print('My favorite number is ' + str(num))

Summary:
In this chapter, you will learn how to use variables; how to create descriptive variable names and how to eliminate name errors and syntax errors; what strings are and how to display them in case and initial capital; how to display neat output using white space and how to eliminate redundant white space in strings; how to use integers and floating-point numbers and note when using numerical data An unexpected act of meaning; a comment code.

Topics: Python