Python Basics Sample Chapters
Python Basics Sample Chapters
import requests
resp = requests.get("https://realpython.com")
html = resp.text
print(html[205:294])
int main(void)
{
printf("Hello, world\n");
}
Hello, world
print("Hello, world")
# This is Python code:
print("Hello world!")
$ # This is a terminal command:
$ python hello-world.py
>>>
Cmd+Spacebar
idle Return
>>>
$ python --version
$ python3 --version
$ python3 --version
Python 3.7.2
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic
Release
$ idle3
3.7 idle3
$ idle-python3.7
>>>
Python 3.7.2 (default, Dec 25 2018, 03:50:46)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.
>>>
>>>
1 + 1
Enter
Enter
2
>>> 1 + 1
2
>>>
>>>
>>>
print()
r
print()
print() print
"Hello, world"
"Hello, world"
print()
"Hello,
world"
print("Hello, world")
>>>
hello_-
world.py .py
.py
.py
Hello, world
Hello, world
F5
Ctrl-Click
hello_world.py
print("Hello, world)
"Hello, world"
"Hello, world"
"Hello,
world
hello_world.py
print(Hello, world)
"Hello, world"
Hello, world
NameError
print(Hello, world)
NameError
Hello,
world
Hello world
print
Hello world
hello_world.py
phrase = "Hello, world"
print(phrase)
phrase
"Hello, world" = phrase
print()
=
=
hello_word.py
print()
Hello, world
print()
print("phrase")
phrase
phrase
Phrase
phrase phrase
NameError
Phrase phrase
print()
print() Print()
phrase string1 _a1p4a list_of_names
9lives
myPhrase listOfNames
myPhrase my_phrase
listOfNames list_of_names
print()
print()
print()
Enter my_phrase
'Hello, world'
"Hello, world"
print(my_phrase)
>>> print(my_phrase)
Enter
Hello, world
my_phrase Enter
my_phrase
my_phrase
"Hello, world"
print()
>>> x = 2
>>> x
2
>>> print(x)
2
print()
print()
x = 2 y = "2" print(x)
print(y) x y
>>> x = 2
>>> y = "2"
>>> print(x)
2
>>> print(y)
2
>>> x
2
>>> y
'2'
print()
Enter
print Enter
>>> print
<built-in function print>
hello_world.py
phrase
#
print("#1")
# Print "Hello, world"
print("Hello, world")
'''
"""
''' """
''' """
hello_world.py
"""
This is my first script.
It prints the phrase "Hello, world."
"""
#
SyntaxError
print()
=
#
''' """
string1 = 'Hello, world.'
string2 = "We're #1!"
string3 = "1234"
string4 = 'I said, "Put it over by the llama."'
SyntaxError
"
string2
# string3
"×Pýŧħøŋ×"
\
""" '''
print(paragraph)
>>> print("""This is a
... string that spans across multiple lines
... that also preserves whitespace.""")
This is a
string that spans across multiple lines
that also preserves whitespace.
+
>>> string1 = "abra"
>>> string2 = "cadabra"
>>> magic_string = string1 + string2
>>> magic_string
'abracadabra'
string1 string2 +
magic_string
first_name last_name
first_name " "
last_name
print()
"abc"
"make it so"
len()
>>> len("abc")
3
len()
my_string
len() len() my_-
string
print()
[ ]
( )
[ ]
{ }
'a'
"apple pie" 'p' 'p'
'l'
>>> flavor[0]
'a'
"apple pie"
a p p l e p i e
0 1 2 3 4 5 6 7 8
>>> flavor[13]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
IndexError
"apple pie"
>>> flavor[-1]
'e'
a p p l e p i e
-9 -8 -7 -6 -5 -4 -3 -2 -1
>>> flavor[:-5]
'appl'
>>> flavor[-5:]
'e pie'
>>> flavor[-8:-5]
'ppl'
IndexError
>>> flavor[:14]
'apple pie'
>>> flavor[13:15]
''
flavor
"apple pie"
flavor[13:15]
""
TypeError str
str
"goal" "foal"
"f"
"goal"
"goal" my_string
my_string[1:] "oal" "f"
"foal"
:
len()
print()
"zing"
"bazinga"
.lower()
.lower()
.
lower ()
print() len()
.lower()
.lower() .upper()
len()
loud_voice len()
>>> len(loud_voice)
20
.upper() .lower()
. .lower() .upper()
loud_voice.upper()
.upper()
.rstrip() .lstrip() .strip()
.rstrip()
.rstrip()
"Arthur Dent"
.lstrip() .rstrip()
.strip()
.startswith()
.endswith()
"Enterprise"
.startswith()
"e" "n"
.startswith()
"Enterprise"
"e" "n" .startswith("en")
False
>>> starship.startswith("En")
True
.endswith()
>>> starship.endswith("rise")
True
.startswith() .endswith()
>>> starship.endswith("risE")
False
True False
.upper() .lower()
name.upper() name
name.upper() "ELENA"
name "Elena"
"name"
my_string Enter
>>> my_string.
Ctrl+Space
Tab
my_string.u Tab
my_string.upper
my_string
my_string
my_string
"An-
imals" "Badger" "Honey Bee" "Honeybadger"
.startswith("be")
string1 = "Becomes"
string2 = "becomes"
string3 = "BEAR"
string4 = " bEautiful"
.startswith("be")
True
input()
input()
input()
input()
Enter input()
input()
Enter
user_input
.upper()
"no"
Enter
input()
>>> num = "2"
>>> num + num
'22'
+
"2" + "2" "22" "4"
num * 3 "12"
"121212"
"12" * 3 = "12" + "12" +
"12" n
n
num * 3
>>> 3 * num
'121212'
*
"12" * "3" En-
ter
TypeError
*
>>> "3" + 3
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
TypeError +
"3" + 3 6
"3"
TypeError
input()
num "2" 2
num * 2 "2"
"22"
int() float()
int()
float()
>>> int("12")
12
>>> float("12")
12.0
float()
>>> int("12.0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '12.0'
>>> num_pancakes = 10
>>> "I am going to eat " + num_pancakes + " pancakes."
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can only concatenate str (not "int") to str
num_pancakes
"I'm going to eat"
num_pancakes str()
>>> num_pancakes = 10
>>> "I am going to eat " + str(num_pancakes) + " pancakes."
'I am going to eat 10 pancakes.'
str()
str()
>>> total_pancakes = 10
>>> pancakes_eaten = 5
>>> "Only " + str(total_pancakes - pancakes_eaten) + " pancakes left."
"Only 5 pancakes left."
str()
>>> str(print)
'<built-in function print>'
>>> str(int)
"<class 'int'>"
>>> str(float)
"<class 'float'>"
str()
int()
float()
str()
input()