variables are made using the = sign
example:
message = "Hello Python World!"
print(message)
strings are made using '_' or "_"
changing the case of the words in a string:
name = "ada lovelace"
print(name.title())
should give output of ada lovelace
can change string to capslock or lapslock like this:
name = ada lovelace
print(name.upper()) or print(name.lower())
will display ADA LOVELACE or ada lovelace
using variables in a string
first_name = "ada"
last_name = "lovelace"
full_name = f"{first_name} {last_name}"
print(full_name)
output should be ada lovelace again
adding whitespace w/ tabs/newlines
use \t to add a tab to your text
print("Python")
Python
print("\tPython")
(idk how tabs work in java imagine there's a tab) Python
use \n to add a newline to your text
print("Languages:\nPython\nC\nJavaScript")
Languages:
Python
C
JavaScript
stripping whitespace
favorite_language = 'python'
favorite_language
'python '
favorite_language.rstrip()
'python'
favorite_language
'python'
to remove whitespace permanently:
favorite_language = 'python'
favorite_language = favorite_language.rstrip
favorite_language
'python'
can remove from left/right/both sides with lstrip, rstrip, and just strip respectively
removing prefixes
nostarch_url = "https://nostarch.com
nostarch_url.removeprefix('https://')
'nostarch.com'
keep new value with removed prefix:
simple_url = nostarch_url.removeprefix('https://')
managing integers
you can add (+) subtract (-) multiply (*) and divide (/)
2 + 3
5
3 - 2
1
2 * 3
6
3 / 2
1.5
two multiplication symbols to represent exponents
3 ** 2
9
3 ** 3
27
10 ** 6
1000000
supports order of operations
2 + 3*4
14
(2 + 3) * 4
20
decimal numbers are called "floats"
0.1 + 0.1
0.2
0.2 + 0.2
0.4
2 * 0.1
0.2
2 * 0.2
0.4
may give arbitrary decimal places in number sometimes
0.2 + 0.1
0.3000000000000000000004
3 * 0.1
0.3000000000000000000004
not a cause for concern (happens in all languages)
division will always result in a float:
4/2
2.0
mix of integer & float will also give float
1 + 2.0
3.0
2 * 3.0
6.0
3.0 ** 2
9.0
underscores can group digits to make large numbers more readable
universe_age: 14_000_000_000
will still print only the digits
14000000000
values can be assigned to variables in one line of code:
x, y, z = 0, 0, 0
makes programs easier to read
constants are variables that stay the same throughout a program's life
MAX_CONNECTIONS = 5000
that's a constant - all caps are an indication of one
comments are extremely useful, very helpful to leave notes for yourself
#, or the hashtag mark, indicates a comment
# Say hello to everyone.
print("Hello Python people!")
square [] brackets indicate a list
list elements are separated by comma
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
print(bicycles)