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 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.

Convert 16 digit hex key to 64 bit binary in python programming?

>>> hex_val = "ab3795d1c49ef1bb"

>>> decimal_val = int(hex_val,16)

>>> decimal_val

12337494432384217531L

>>> binary_val = bin(decimal_val)

>>> binary_val

'0b1010101100110111100101011101000111000100100111101111000110111011'

#note that the string that represent the binary number starts with '0b'.

# you can remove it if you want like this:

>>> binary_val = binary_val[2:]

>>> binary_val

'1010101100110111100101011101000111000100100111101111000110111011'

What does asterisk do in python?

An asterisk is also known as "times by", or "x"

What variable keeps a running total?

The answer is "an accumulator."

"A running total is a sum of numbers that ACCUMULATES with each iteration of a loop. The variable used to keep the running total is called an accumulator."

It can be any variable you want! It could be total, tot, etc. Here is an example:

total = 0

randoms = [random.randRange(0, 99999)] * 100 # Creates a list of 100 random numbers

for number in randoms:

total += number

print(total) # Py3k print statement. Sample output: 35460

What is the use of colon in python after loop and condition?

it starts a new code block. all the indented code after the colon will be considered as one block.

for example, in loops it means that all the indented code after the colon will be run on each iteration.

example:

for i in range(4):

# this code will run 4 times

# this code will be ran once.

What is the difference between a weak reference and a strong reference in python?

The same difference as in other languages. A weak reference is one that won't stop the garbage collector from eliminating an object.

How do you code a program to find kaprekar number?

import java.io.*;

class kaprekar

{

public static void main(String args[])throws IOException

{

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter any number");

int n=Integer.parseInt(br.readLine());

int t=n,count=0;

while(n!=0)

{

n=n/10;

count++;

}

int s=t*t;

int p=(int)Math.pow(10,count);

int ld=s%p;

int minus=s-ld;

int fd=minus/p;

int sum=fd+ld;

if(sum==t)

{

System.out.println("Kaprekar Number");

}

else

{

System.out.println("Not a Kaprekar number");

}

}

}

How you write customer care program in python language?

a=input("Enter your customer call number\t:")

print("***********************************************************************************************************************************************************************")

print("\t\t\t\t\t\t\t\t\tWELCOME TO OUR COUSTOMER CARE")

def cust():

print("***********************************************************************************************************************************************************************")

print("\nPlease choose your language options\t:\n***********************************************************************************************************************************************************************\n\t\t\t\t\t\t\t\tPlease choose your language options:\n\t\t\t\t\t\t\t\t\t:-Press 1 for Telugu\n\t\t\t\t\t\t\t\t\t:-Press 2 for English\n\t\t\t\t\t\t\t\t\t:-Press 3 for Hindi\n\t\t\t\t\t\t\t\t\t:-Press 4 for exit\n***********************************************************************************************************************************************************************")

b=input("Please choose your language\t:")

def menu():

if(b==1):

print ("\n************************************************************************************************************************************************************************\n\t\t\t\t\t\t\t:-Press 1 for balance enquiring\n\t\t\t\t\t\t\t:-Press 2 for recharge\n\t\t\t\t\t\t\t:-Press 3 for dailer tunes\n\t\t\t\t\t\t\t:-Press 4 for to know your cell details\n\t\t\t\t\t\t\t:-Press 0 for go to previous menu\n************************************************************************************************************************************************************************")

c=input("Please Enter your option\t:")

if(c==1):

a=input("Please enter the balance enquiry number\t:")

print"\nYour balance is 35.59 Rupees\nThank you."

d=input("Do You want Main menu again press Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(c==2):

print("\n**********************************************************************************************************************************************************************\n\t\t\t\t\t\t\t:-Press 1 for your own accout\n\t\t\t\t\t\t\t:-Press 2 for any other account\n************************************************************************************************************************************************************************")

d=input("Please enter your option\t:")

if(d==1):

print"\nEnter *123* and then enter your voucher numeber and finally press # and dail.\nMessage:-Successfully recharge:\t"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(d==2):

e=input("Please Enter the phone number\t:")

f=input("How much of amount you recharge\t:")

f=open("customer.txt","a")

f.write("\n")

f.write("your given amount Successfully recharged.")

f.close()

print "\nSuccess fully recharged."

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

else:

print "Please give correct option."

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(c==3):

print("\n:-Press 1 for New songs\n:-Press 2 for old songs\n:-Press 3 for classical songs\t:")

g=input("Please Enter your option\t:")

if(g==1):

new=raw_input("Enter which new song you want\t:")

print new,"tone is selected to your mobile.\nThank you"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(g==2):

old=raw_input("Enter which old song you want\t:")

print old,"tone is selected to your mobile.\nThank you"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(g==3):

cla=raw_input("Enter which classical song you want\t:")

print cla,"tone is selected to your mobile.\nThank you"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

else:

print"Please choose correct option"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

elif(c==4):

h=input("Please tell your mobile number\t:")

print"Your mobile number details\t:::\n\tName:RajaMohan\n\tAddress:Door N0. 2-13, peddaveedhim Penturu,Nandigam (mdl & po)\n\tDistrict:Srikakulam"

d=input("Do You want Main menu again press -1 Other Wise U will Exit:\t");

if(d==-1):

return menu();

menu()

cust();

#BY RajaMoahn @RGUKT Nuzvid 2010

Is python a case sensitive language?

Yes.

For instance, you can write:

x=2

X='a string'

If you now print each of these variables you will find that they have retained the values that you assigned to them.

Very often programmers create classes with uppercase names then use the lowercase version of the same name for instances of the class. This is possible because Python is case sensitive.

How do you make a simple Python RPG game?

I actually am working on one right now. what I'm doing is I'm using pygame. pygame lets you open a separate window and have objects with it. you can look through the pygame documentation and it will teach you how to use it.

How do you make a menu system in python?

What do you mean a Menu System... do you mean the GUI

What is used in python programming for space in output?

I interpreted your question as this:

What is used in Python for adding a blank line to the output?

If you are looking for this, a simple print statement will do:

print #in python 2.x

or

print() #in python 3.x

If that wasn't what you were looking for, change the question to be more clear.

How do you cache a webpage in Python 2.7?

I don't know what you mean by cache. Given the URL, you can save it to a file, with the following code. Mind you, it's not always entirely smooth sailing. Web developers have various ways of blocking attempts to grab the contents of their pages.

from urllib import urlopen

file ( 'myfile.htm', 'w' ) . write ( urlopen ( URL ) . read ( ) )

What is the purpose of the flag variable in a bubble sort?

to eliminate unnecessary swaps to eliminate unnecessary comparisons to stop as soon as the list is sorted to sort an array of unknown size