Skip to content

Learning Python: Day 3

Chapter 2: (Continued)

Adding whitespace to strings with tabs or newlines

  • whitespace – any nonprinting characters, such as spaces, tabs, and end-of-line symbols, can organize output to make it easier to read
  • add a new tab with \t

>>> print(“Python”)

Python

>>> print(“\tPython”)

Python

  • Add a newline with \n

>>> print(“Languages:\nPython\nC\nJavaScript”)

Languages:

Python

C

JavaScript

  • Combine tabs and newlines with “\n\t” to tell Python to move to a new line and start the next line with a tab

>>> print(“Languages:\n\tPython\n\tC\n\tJavaScript”)

Languages:

Python

C

JavaScript

Stripping whitespace

  • rstrip() method – ensures no whitespace exists at the right side of a string

>>> favorite_language = ‘Python ‘

>>> favorite_language

‘Python ‘

>>> favorite_language.rstrip()

‘Python’

>>> favorite_language

‘Python ‘

  • temporarily removes space

>>> favorite_language = ‘Python ‘

>>> favorite_language = favorite_language.rstrip()

>>> favorite_language

‘Python’

  • lstrip() method – ensures no whitespace exists at the left side
  • strip() method – ensures no whitespace exists from both sides

Removing prefixes

  • when working with strings, it’s common to remove prefixes
  • for example, a URL with https://

>>> nostarch_url = ‘https://nostarch.com’

…nostarch_url.removeprefix(‘https://’)

‘nostarch.com’

Avoiding syntax errors with strings

  • use double quotes instead of single quotes when apostrophes are involved to avoid errors
  • syntax error – occurs when Python doesn’t recognize a section of your program

message = “One of Python’s strengths is its diverse community.”

print(message)

One of Python’s strengths is its diverse community.

  • singles quotes would result in a syntax error

Numbers

  • used in programming to keep score in games, represent data in visualizations, store information in web applications

Integers

  • can add (+), subtract (-), multiply (*), and divide (/) integers

>>> 3-2

1

>>> 2*3

6

  • use two multiplication symbols to represent exponents

>>> 3**2

9

>>> 3**3

27

  • Python supports order of operations

>>> 2+3*4

14

>>> (2+3)*4

20

Floats

  • float – any number with a decimal point

>>> 0.1+0.1

0.2

>>> 0.2+0.2

0.4

>>> 2*0.1

0.2

  • can sometimes get an arbitrary number of decimal places

>>> 0.2+0.1

0.30000000000000004

  • ignore for now

Integers and floats

  • when dividing any two numbers, even integers that result in a whole number, you’ll always get a float

>>> 4/2

2.0

  • if you mix an integer and a float in any operation, you’ll get a float as well:

>>> 1+2.0

3.0

>>> 3.0**2

9.0

Underscores in numbers

  • make large numbers more readable with underscores

>>> universe_age = 14_000_000_000

>>> print(universe_age)

14000000000

Multiple assignment

  • can assign values to more than one variable using a single line of code as long as you separate variables and values with commas

>>> x,y,z = 0,0,0

Constants

  • constant – variable whose value stays the same throughout the life of the program, use all capital letters for the variable

MAX_CONNECTIONS = 5000

Comments

  • comment – allows you to write notes within your programs

How do you write comments?

  • hash mark (#) indicates a comment, ignored by interpreter

# Say hello to everyone.

print(“Hello Python people!”)

Hello Python people!

What kinds of comments should you write?

  • comments should explain what your code should do
  • summarize your project throughout, whether working solo or with a team

The Zen of Python

  • Python’s community philosophy is contained in “The Zen of Python” by Tim Peters

>>> import this

The Zen of Python, by Tim Peters

Beautiful is better than ugly.

Simple is better than complex.

Complex is better than complicated.

Readability counts.

There should be one– and preferably only one– obvious way to do it.

No is better than never.

Summary

  • learned how to work with variables, use descriptive variable names, resolve name and syntax errors
  • learned what strings are and how to display them using lowercase, uppercase, and title case
  • started using whitespace and removing unneeded elements from a string
  • started working with integers, floats, and numerical data
  • learned to write comments to make code easier to understand
  • read about the philosophy of keeping your code simple whenever possible
Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *