answersLogoWhite

0

Python Programming

Python was first released in 1991, it is an high-level programming language. It was created by Python Software Foundation. The language is influenced by a number of programming language such as C, Lisp, Perl, Java and others.

155 Questions

What is the meaning of print and input in idle python?

"print" will output a value onto the screen for a user to see.

"input" or "raw_input" gets a user's input.

Example of semantic errors?

scores = [51, 47, 53, 97, 27]

summation = 0.0

for score in scores:

summation *= score # Semantic Error.

average = summation / len(scores)

print average # Isn't average. average == 0

Does Python allow programmers to break a statement into multiple lines?

Apparently it does. From an online style guide:

"The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation. Make sure to indent the continued line appropriately. The preferred place to break around a binary operator is after the operator, not before it. "

Source: http://www.python.org/dev/peps/pep-0008/#maximum-line-length

What is floor division in Python?

Floor division is division where the answer is rounded down. For example, 5/2 in floor division is not 2.5, but 2. In Python 2, floor division is the default. In Python 3, the floor division operator is //.

Python 2:

>>> 5/2

2

>>> 5.0/2

2.5

Python 3:

>>> 5/2

2.5

>>> 5//2

2

Which python expression finds the value?

It depends... "if" will check to see if a value is equal to something; but there are operators such as "+", "*", "-", "/", etc.

How do you define Y as 1 and N as 0 in Python?

I suspect that you mean that you want to define constants, which is not possible in Python. Any variable can be rebound to something else at any time.

What is the keyword for function(python)?

There are two related concepts, both called "keyword arguments". On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which were not mentioned at all. The other concept is on the function definition side: You can define a function that takes parameters by name -- and you don't even have to specify what those names are. These are pure keyword arguments, and can't be passed positionally. The syntax is def my_function(arg1, arg2, **kwargs) Any keyword arguments you pass into this function will be placed into a dictionary named kwargs. You can examine the keys of this dictionary at run-time, like this: def my_function(**kwargs): print str(kwargs) my_function(a=12, b="abc") {'a': 12, 'b': 'abc'}

What must happen before the while loop will stop iterating?

The while loop will stop iterating if its condition is no longer true when it reaches the end of an iteration, or if it encounters a break statement.

What does the print command do in Python?

The print command is a way to display output to the console. The hello world program, for example, can be written in python as simply

print("Hello world")

Other values can also be used in a print statement:

a = 4

print(a) #will print the number 4

How do you Print all the first characters in the string in python programming?

Let's say your string is a variable called "string"

To print out all the characters in order, you would do:

for i in string:

print(string[i])

If you wanted to print out characters up to a point (n = maximum characters):

for i in range(n):

print(string[i])

hope this helps!

How do you decrement on python?

-=

Example:

variable = 5

variable -= 2

will out put 5

Is it faster to read and write a csv or sqlite file?

The answer to this question really depends on the data base engine (that is the server software) being used to store the information. If, for instance, you are using MS SQL as the engine, it will naturally write all it's information into a SQL file, or the engine will be used to write into a stand-alone SQLLite file (where no actual server software is running).

In any case, most database engines will write to their own native files to help minimize read/write times. This is because CSV files are both a blessing and a curse. They are universal ASCII Comma Separated Value files that can be read by anything - even Notepad. This makes them easily transferrable from any system to another. However, they pretty much have to be read AND written to in SEQUENTIAL order. In a database containing a few thousand records, that could be a long Update execution time.

In summary, it is extremely faster for a database engine to use it's native file format. However, CSV's are mainly for import/export purposes and transferring data from one system to another.

How do you print 1-10 odd numbers in python?

This can be easily done using the modulo operation (represented by the '%' symbol). This operation gives the remainder of the division of the two numbers used in it.

e.g. 4 % 2 = 0, 5 % 2 = 1, 10 % 3 = 1, 10 % 2 = 0

In a modulo operation, any even number divided by 2 will always have a remainder of 0. Therefore, any number divided by 2 with a remainder of more than 0 must be odd. This can be done in Python for a number range like so:

current_number = 1 # variable to store the current number being checked

while current_number <= 10: # loop to go through the range of numbers up until 10

if current_number % 2 > 0: # check if the current number has a remainder of more than 0

print(current_number) # if it does, then print it out

current_number += 1 # move to the next number

This will print out all the odd numbers up until 10, each on a separate line.