Python 1 Flashcards
What are the 2 methods to start Python on your PC?
-IDLE- A cross-platform Python development enviornment, called Intehrated Development Enviorment or simply IDE
-Python Shell- running ‘python’ from the Command Line
How to start Python from the command line.
-Type ‘cmd’ at the Window search bar
-Type ‘python’ at the command line. Python Shell appears.
Starting Python with IDLE.
-Open console or command-line interface by typing ‘cmd’ at the Window search
-Type ‘idle’ at the command line. The IDLE Shell appears.
How does IDLE help you program in Python by:
-Coloring-coding your program
-Debugging
-Auta-indent
-Interactive shell
How to make a comment?
-Add a hashtag before
What colors are comments, string, and outputs.
-Comments are red
-Strings are green
-Output is blue
What is a syntax?
The set of legal structures and
commands that can be used in a particular
programming language.
What is a code or source code?
The sequence of instructions in a program
What is output?
The messages printed to the user by a program
What is a console?
The textbox onto which output is printed
What is compiling?
Translates your program into a form that a machine understands.
What is interpreting?
Directly interpreted into machine instructions
What is a variable?
A named piece of memory that can store a value.
How to make a variable
name=value
ex:
- x = 10
- name = “Sarah”
Rules for naming variables
Legal variable names:
-Can only contain letters, numbers, and underscores.
-Cannot start with a number
Good variable names:
-Choose descriptive names
-Be consistent
-Keep the length in check
Illegal names:
-If it starts with a number
-If it is one of pythons keywords
What happens if you get an illegal name
You get a syntax error
ex:
“SyntaxError: invalid syntax”
What does print do?
Produces text output on the console
What do you input to get the output Hello, world!?
(‘Hello, world!”)
How to prevent it from making a new line?
Specify that it should end with a blank
ex:
print(‘a’, end=’ ‘)
print(‘b’, end=’ ‘)
print(‘c’)
Output: a b c
What are the 2 number types?
-Integers (e.g. -3,-2, 0)
-Floating point (e.g. -23,2,3.14)
What are operators and operands?
-Operators are special symbol that represent computations like addition and multiplication
eg: “ + - X”
-Operators are the values the operators are applied to
eg “1 + 2”
1 and 2 are the operands
+ is the operators
What do these signs mean and their order of evalutation?
- +
- -
- *
- /
- %
- **
-//
- +, Addition, Left to right, 5+3=8
- ”-“ Minus, Left to right, 5-3=2
-
, Multiply, Left to right, 53=15
Division, Left to right, 15/3=15 - %. Modulus, Left to right, 5 % 2=1
- . Square, Right to left, 52=25
- //, Integer division and only the whole number is returned, right to left, 5//2=2
What are the keywords in Python?
-and
-as
-assert
-break
-class
-continue
-def
-del
-elif
-else
-except
-exec
-finally
-for
-from
-global
-if
-import
-in
-is
-lambda
-not
-or
-pass
-print
-raise
-return
-try
-while
-with
-yield
Explain the order of operations of python
-Do what is in parentheses first
-Then ** Right to left
-Then *, /, % left to right
-Then +, - left to right
What does the division operator produce?
and answer these and explain why.
-15.0/2.0=
-15 / 2 =
-1 / 2.0 =
The division operator always produces a real number
-7.5
-7.5
-0.5
What does the integer division operator do? and answer these questions
»> minute = 59
»> minute//60
=?
»> minute = 59
»> minute //60.0
=?
Performs floor division on the result of division
-0, as both numbers dont have a decimal
-0.0 as one of the numbers is a floating point number so the result is a float