Learn Python Quickly Flashcards
learn python 3.8 quickly
exit()
exited with the quit command or depending on the version you are running quit()
help()
all the various commands
> > >
The primary prompt. Python is waiting for a command when you see this.
…
Python is waiting for your input, but this means it’s the second line of input
IDE
Most programming done in the IDE (integrated development environment). They auto complete commands, phrases, and functions, track bugs, highlight syntax errors and overall make programming and seeing the output of your programs easier.
Book recommends which IDEs?
PyCharm, Spyder, PyDev, Idle, and Wing.
writing comments in python
use # at the start of a line or
Triple single quotes at the start and end of your multi-line statement:
‘’’
comment 1
2
3
‘’’
variables: how to assign string variables?
use “
stuff = “cloudy”
Python Operators
- Operators are symbols that perform functions and are used to manipulate data in different ways
Example operators
Operators:
- assignment is the = for assigning variables
- R = 12
- S = 5
- T = S
- ^^ assigning variable to a variable
- Addition: +
- R + S = 17
- Subtraction: -
- R - S = 7
- Multiplication: *
R * S = 60 - Division: /
- R / S = 2.4
- Floor division: //
- This divides and then rounds down to the nearest whole number
- R // S = 2
- Modulus (mod): %
- Modulus operator produces the remainder of the division operation
- R % S = 2
- Exponent: **
- R ** S is R to the S power
- R ** S = 248,832
R = 12
S = 5
- Floor division:
//
- This divides and then rounds down to the nearest whole number
- R // S = 2
R = 12
S = 5
- Modulus (mod):
%
- Modulus operator produces the remainder of the division operation
- R % S = 2
R = 12
S = 5
- Exponent:
**
- R ** S is R to the S power
- R ** S = 248,832
R = 12
S = 5
Can you combine operators?
Yes. Specifically this way:
Short handing R = R * 2
shrinks to R * = 2
Same for the other operators
Variables are stored as what primary Data Types in Python?
Primary Data types:
- integers
- floats
- strings
What are the the different data structures used for the primary data type variables?
- lists
- tuples
- dictionaries
Integers
- same as math class
- whole numbers
- positive or negative
- assigns normally to a variable
floats
- Numbers that contain decimal parts
- positive or negative
- assigns normally to a variable
strings
- text data
- assign them using “ “ or ‘ ‘
- string_1 = “string”
What order should nested quotes be used inside strings?
- Use “ “ first, then ‘ ‘ inside the quoted space
How are numbers treated in a quote do when assigned to a variable?
A = “12”
Python sees them as a string.
String Manipulation
- you can combine them the same way we combine numbers:
str_1 = “Words “
str_2= “and “
str_3= “more words.”
str_4 = str_1 + str_2 + str_3
print(str_4)
Running this =
Words and more words.
changing letter case using built in functions. Explain upper and lower case usage with string variables.
Using the following functions will change the letter case to upper or lower:
my_up_string = “all MY letters”.upper()
my_dn_string2 = “ all MY letters”.lower()
print(my_up_string + my_dn_string2)
running this =
ALL MY LETTERS all my letters
String formatting example using %
Using mod operator % on strings allows you to specify values / variables you would like to insert into a string.
string = “With the mod op, you can add %s, integers like %d, or even floats like %2.1f.”%(“strings”, 25, 12.34)
print (string)
running this =
With the mod op, you can add strings, integers like 25, or even floats like 12.3.
where every % gets its variable / value piped in from the entries at the end of the variable assignment line. special notes here:
- %s is a string
- %d is an integer (digit?)
- %f is a float, but in this instance we used %2.1f
- %2.1f took the 12.34 and read it as needing 2 digits before the decimal place and one digit after the decimal place resulting in 12.3
Automatic formatting example using {}
There’s more on this later in the chapter, but using the format function for strings instead of using the modulus operator.
- Inside the brackets goes:
- data type tag
- position of the value in the collection of values you want in that spot
- it is possible to leave the brackets blank. Python will automatically fill them in from left to right with the values listed left to right.
“The string you want to format {} “.format(values you want to insert)
- curly braces denote the location where you want to insert the value.
- to insert multiple values, you need to create multiple braces then separate the values with commas
string = “with the mod operator, you can add {0:s}, integers like {1:d}, or even floats like {2:2.2f}”
print (string.format(“strings”, 25, 12.34))
will print =
with the mod operator, you can add strings, integers like 25, or even floats like 12.3
Python starts with 0 or 1?
Unlike some other programming languages, python is a zero-based system when it comes to positions. First position is ZERO, second is ONE.