Skip to content

Latest commit

 

History

History
165 lines (115 loc) · 4.36 KB

unit-1.md

File metadata and controls

165 lines (115 loc) · 4.36 KB

Unit 01

Back to Syllabus

Table of Contents

Comments

# this is a comment

Comments are one of the most useful tools in programming. Comments can be used to:

organize code

# 10/07/2020
# ------------------ #
print('Welcome to Python!')

# --- say hello! --- #
print('Hello!')

explain code

# express an opinion about spam
print("I don't like spam!")

exclude certain lines of code while testing

print("Hello friends!")
# print("Hello nobody!") # this line is excluded using a comment

Back to top

The print() function

print("Hello! Welcome to my really cool app!")

print() is a built-in function in Python. Functions can be used to perform a variety of tasks. The parentheses are required in order for the function to work properly.

Data can be passed into the print() function's parentheses and that data will be displayed in the terminal. If no data is privided to print(), it displays a blank line.

print('Hello world!') # Hello world!
print() # prints a blank line
print(4 + 4) # 8

Back to top

Datatype: Strings

There are many different datatypes in Python. The first one we will explore are Strings. String literals represent textual data.

# Single quotes:
'allows embedded "double" quotes'

# Double quotes:
"allows embedded 'single' quotes".

#Triple quoted:
'''Three single quotes''', """Three double quotes"""

Triple quoted strings may span multiple lines. All associated whitespace will be included in the string literal.

print("""
PDX Code Guild
Programming 101
Unit 1
""")

Output

PDX Code Guild
Programming 101
Unit 1

Some examples of Strings:

# String: Hello, friends!
print("Hello, friends!")

# String: Welcome to PDX Code Guild!
print("Welcome to PDX Code Guild!")

Back to top

String Concatenation

To concatenate, or combine, two strings you can use the + operator.

Strings will be printed exactly as they're written.

print("PDX" + "Code" + "Guild") # PDXCodeGuild

If we want spaces between our strings, we have to include them ourselves.

# spaces inside another string
print("PDX" + " Code " + "Guild") # PDX Code Guild

# spaces inside their own strings
print("PDX" + " " + "Code" + " " + "Guild") # PDX Code Guild

Methods

Methods are functions that manipulate the piece of data to which they're attached. In Python, many datatypes have methods, including strings.

Find a full list of string methods here.

Common string methods:

method description example result
capitalize() Converts the first letter to uppercase "hello world".capitalize() "Hello world"
lower() Converts a string to lowercase "Hello World".lower() "hello world"
strip() Removes whitespace from the beginning and end of a string " Hello World ".strip() "Hello World"
title() Converts the first character of each word to uppercase "hello world".title() "Hello World"
upper() Converts a string to uppercase "hello world".upper() "HELLO WORLD"

Escape Characters

Escape characters allow us to use special characters inside a string. They also allow us to use 'illegal' characters in a string, such as a double quote withing a string surrounded by double quotes. An escape character is a \ followed by the character you want to insert.

code result
\' Single Quote
\" Double Quote
\\ Backslash
\n New Line
\t Tab

To practice everything you learned in Unit 1, please complete the following lab:

Lab 01: Hello

Back to top