Chapter1-3 Flashcards

1
Q

Checking python version?

A

ust type python on command line , u will see the version

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

How to start python on terminal?

A

start terminal
ii. Type python to start python
Write ur program

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

best way to name python variable ?

A
  1. The Python variables you’re using at this point should be lowercase. You won’t get errors if you use uppercase letters, but it’s a good idea to avoid using them for now.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is python traceback?

A

A traceback is a record of where the interpreter ran into trouble when trying to execute your code. e.g Traceback (most recent call last):
u File “hello_world.py”, line 2, in v print(mesage)
wNameError: name ‘mesage’ is not defined

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

What does name error means?

A

A name error usually means we either forgot to set a variable’s value before using it, or we made a spelling mistake when entering the variable’s name.

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

A string

A

you can use single or double quotes around strings

This exibility allows you to use quotes and apostrophes within your strings:

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

whet is title() function?

A

title() function convert first letter if each word in string to capital

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

how to strip whitespaces?

A

u strip whitespce using rstrip() e.g avorite_language = ‘python ‘ . favorite_language.rstrip() remove the whitespace after n

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

lstrip()?

A

strip whitespace from the left side of a strin

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

how to strip whitespace from both sides?

A

use strip()

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

What is syntax error?

A

A syntax error: A syntax error occurs when Python doesn’t recognize a section of your pro- gram as valid Python code. For example, if you use an apostrophe within single quotes, you’ll produce an error.

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

print in python 2 and 3 ? what is relation?

A

In python 2 , printing is not necessary u can type print “isa Musa”. In python 3 , some code u will see print with parenthesis some without parenthesis

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

does python concatenate numbers with string?

A

Pyhon don’t concatinate numbers with string unless u change the numbers to string
error

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

Integers in Python 2 ?

A

result to an integer
Division of integers in Python 2 results in an integer with the remainder truncated. Note that the result is not a rounded integer; the remainder is simply omitted.3/2 in python returen 1 make sure to use float

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

How to comment in python?

A

Comments using #

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

What is the zen of Python ?

A

Experienced Python programmers will encourage you to avoid com- plexity and aim for simplicity whenever possible. The Python community’s philosophy is contained in “The Zen of Python” by Tim Peters

17
Q

how to open zen of python?

A

You can access this brief set of principles for writing good Python code by enter- ing “import this” into your interpreter.

18
Q

what Is a list?

A

A list is a collection of items in a particular order. You can make a list that includes the letters of the alphabet, the digits from 0–9, or the names of all the people in your family. List items don’t have to be related in any order. Because a list usually contains more than one element, it’s a good idea to make the name of your list plural, such as letters, digits, or names.

19
Q

How to create a list?

A

In Python, square brackets ([]) indicate a list, and individual elements in the list are separated by commas bicycles = [‘trek’, ‘cannondale’, ‘redline’, ‘specialized’]

20
Q

how to access a List ?

A

Accessing Elements in a List A[0]

21
Q

where index position start in python?

A

Index Positions Start at 0, Not 1

22
Q

Accesing last item in list

A

1) A[-1] , we use -1 to access the last item in a list

23
Q

how negative list acess useful?

A

This syntax is quite useful, because you’ll often want to access the last items in a list without knowing exactly how long the list is. This convention extends to other negative index values as well. The index -2 returns the second item from the end of the list, the index -3 returns the third item from the end, and so forth.

24
Q

How to modify element in list?

A

motorcycles[0] = ‘ducati’

25
Q

How to addd element In a list?

A

The append() method makes it easy to build lists dynamically

26
Q

Example of adding item in list?

A

motorcycles = []

motorcycles. append(‘honda’)
motorcycles. append(‘yamaha’)
motorcycles. append(‘suzuki’

Building lists this way is very common, because you often won’t know the data your users want to store in a program until after the program is running. To put your users in control, start by de ning an empty list that will hold the users’ values. Then append each new value provided to the list you just created.

27
Q

How to Inserting Elements into a List ?

A

1) Insert method() is use to add element in a list
2) motorcycles = [‘honda’, ‘yamaha’, ‘suzuki’]
3) motorcycle.insert(0 , ‘musa’) …. This operation will push every other item to the right
4) result : [‘ducati’, ‘honda’, ‘yamaha’, ‘suzuki’]

28
Q

Removing Elements from a List

A

1) You can delete item from list if you know its position
2) We can use the keyword del to remove the element
3) E.g del motorcycles[0] .. This delete item at the first position

29
Q

How to remove an Item Using the pop() Method

A

Sometime u want use the position of an item u remove. For example u can assign it to inactive members. Pop() operation is useful in that sense

The pop() method removes the last item in a list, but it lets you work with that item after removing it. The term pop comes from thinking of a list as a stack of items and popping one item off the top of the stack .

Example of using pop()

Popped_motorcycle = motorcycle. Pop()

i. U can remove any item from any position using pop() by providing index
1) first_owned = motorcycles.pop(0)….. This removes an element at first position

30
Q

If you’re unsure whether to use the del statement or the pop() method what to do?

A

here’s a simple way to decide: when you want to delete an item from a list and not use that item in any way, use the del statement; if you want to use an item as you remove it, use the pop() method.

31
Q

How to remove item by value?

A

Sometimes you may need to remove an item from the list and you don’t know the position of the item , but you only know the value. You can use remove() methode to remove the item from the list

Example: motorcycles.remove(‘ducati’ ) This will remove ducati from the list motorcycle

The remove method , only removes the first occurance of an item. U need to use loop , to remove all the occurrence of the item u want remove.

32
Q

cars.sort() ?

A

sorting function

33
Q

cars.sort(reverse=True) ?

A

ii. cars.sort(reverse=True) ………..sorting in reverse order

34
Q

What is differ between sort() and sorted()

A

Sort() function sort data permaently. Sorted() function sort temporarily, it does not affect the original list

35
Q

What reverse() does?

A

v. reverse() method is a method that can be use to reverse the element in a list. Th reverse method as well reverse the order of the list permanently , but you can reverse it back , by using reverse() function again on the list

36
Q

What is IndexError:

A

list index out of range