TranslateProject/sources/tech/20180204 Python Hello World and String Manipulation.md
2018-02-08 08:51:02 +08:00

4.1 KiB
Raw Blame History

translating---geekpi

Python Hello World and String Manipulation

Before starting, I should mention that the code used in this blog post and in the video below is available on my github.

With that, lets get started! If you get lost, I recommend opening the video below in a separate tab.

Hello World and String Manipulation Video using Python

** Get Started (Prerequisites)

Install Anaconda (Python) on your operating system. You can either download anaconda from the official site and install on your own or you can follow these anaconda installation tutorials below.

Install Anaconda on Windows: Link

Install Anaconda on Mac: Link

Install Anaconda on Ubuntu (Linux): Link

Open a Jupyter Notebook

Open your terminal (Mac) or command line and type the following (see 1:16 in the video to follow along) to open a Jupyter Notebook:

jupyter notebook

Print Statements/Hello World

Type the following into a cell in Jupyter and type shift + enter to execute code.

# This is a one line comment
print('Hello World!')

Output of printing Hello World!

Strings and String Manipulation

Strings are a special type of a python class. As objects, in a class, you can call methods on string objects using the .methodName() notation. The string class is available by default in python, so you do not need an import statement to use the object interface to strings.

# Create a variable
# Variables are used to store information to be referenced
# and manipulated in a computer program.
firstVariable = 'Hello World'
print(firstVariable)

Output of printing the variable firstVariable

# Explore what various string methods
print(firstVariable.lower())
print(firstVariable.upper())
print(firstVariable.title())

Output of using .lower(), .upper() , and title() methods

# Use the split method to convert your string into a list
print(firstVariable.split(' '))

Output of using the split method (in this case, split on space)

# You can add strings together.
a = "Fizz" + "Buzz"
print(a)

string concatenation

Look up what Methods Do

For new programmers, they often ask how you know what each method does. Python provides two ways to do this.

  1. (works in and out of Jupyter Notebook) Use help to lookup what each method does.

Look up what each method does

  1. (Jupyter Notebook exclusive) You can also look up what a method does by having a question mark after a method.
# To look up what each method does in jupyter (doesnt work outside of jupyter)
firstVariable.lower?

Look up what each method does in Jupyter

Closing Remarks

Please let me know if you have any questions either here or in the comments section of the youtube video. The code in the post is also available on my github. Part 2 of the tutorial series is Simple Math.


via: https://www.codementor.io/mgalarny/python-hello-world-and-string-manipulation-gdgwd8ymp

作者:Michael 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出