Chapter 13: Python Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Declaration of variables

A
  • No declaration necessary
  • A comment should be used either way
#Number1: Type INTEGER
#YourName: Type STRING
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Assignment of constants

A
  • identifier=value

* PI=3.14

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Assignment of variables

A

•identifier=expressions

  • A=34
  • B=B+1
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Arithmetic operations

A
    • | - | * | / | ** | // | %

* Rules of precedence should be followed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Outputting information

A
  • print(printlist)

* print(“Hello, ”,YourName,”Your number is: “,Number1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Comments

A

This is a comment

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Data types

A
  • int
  • float
  • str
  • bool
  • datetime
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Boolean expressions

A
  • ==
  • !=
  • >
  • <
  • > =
  • <=
  • and | or | not
  • in
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Selection statements

A
if x<0:
  print(“Negative”)
elif x=0:
  print(“Zero”)
else:
  print(“Positive”)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Count-controlled loops

A

for x in range (5):

print(x)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Pre-condition loops

A

Answer=‘ ‘

while Answer != “Y”:
Answer=input(“Enter Y or N”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Creating 1D arrays

A
•List1=[]
  List1.append(“Fred”)
  List1.append(“Jack”)
  List1.append(“Ali”)
•List2=[0,0,0,0,0,0,0]
•List3=[0 for i in range (100)]
•List4=[“ “]*100
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Accessing 1D arrays

A
  • NList[24]=0

* AList[3]=“D”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Creating 2D arrays

A

•Board=[0 for i in range(7)]
for j in range(6)

•Board=[07]6

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Accessing 2D arrays

A

Board [2][3]=0

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

String manipulation built-in functions

A
  • ThisString[P]
  • chr(i)
  • ord(ch)
  • len(s)
  • s=s1+s2
17
Q

Slicing

A
  • ThisString[2:]: CDEFG
  • ThisString[:2]: AB
  • ThisString[-2:]: FG
  • ThisString[:-2]: ABCDE
18
Q

Truncating numbers

A

int(x)

19
Q

String —> integer

A

int(s)

20
Q

String —> float

A

float(x)

21
Q

Random number generator

A

randint(1,6)

22
Q

Date and time

A

from datetime import * SomeDate=date(2015,3,15)

Today=date.today
print(SomeDate)
print(Today)
SomeDate=SomeDate+TimeDelat(1)

23
Q

Writing on a text file

A

FileHandle=open(“Text.txt”,”w”)
FileHandle.write(LineOfText)
FileHandle.close()

24
Q

Reading from a text file

A

FileHandle=open(“Text.txt”,”r”)
LineOfText=FileHandle.readline()
FileHandle.close

25
Q

Appending to a text file

A

FileHandle=open(“Text.txt”,”a”)
FileHandle.write(LineOfText)
FileHandle.close()

26
Q

EoF marker

A
FileHandle=open(“Text.txt”,”r”)
LineOfText=FileHandle.readline()
while len(LineOfText)>0:
  LineOfText=FileHandle.readline()
  print(LineOfText)
FileHandle.close()