GoConqr has a new look! Let us know what you think.

Notes

Python

Nagashayan reddy Information Technology English 1 page

Content of the notes (1)

Page 1

You might also find use for integer division, denoted by //. It divides one integer by another, but rather than giving the exact answer it rounds the answer down to an integer. (Note: it rounds down even if the answer is negative.)

print(15 // 4) print(16 // 4) print(-5//4)

2. There is another operator that is sometimes mistaken for the exponentiation operator, the caret: ^. bitwise xor.

There are two different Python versions. Both Python 2 and Python 3 are used throughout the globe. The most significant difference between the two is how you write a print statement. In Python 3, print has parentheses.

Strings

When printing things in Python, we are supplying a text block that we want to be printed. Text in Python is considered a specific type of data called a string. A string, so named because they're a series of letters, numbers, or symbols connected in order — as if threaded together by string. Strings can be defined in different ways:

print "This is a good string" print 'You can use single quotes or double quotes for a string'

Above we printed two things that are strings and then attempted to print two things that are not strings. While double-quotes (") and single-quotes (') are both acceptable ways to define a string, a string needs to be opened and closed by the same type of quote mark.

We can combine multiple strings using +, like so:

print "This is " + "a good string"

This code will print out "This is a good string".

Most of the time, code should be written in such a way that it is easy to understand on its own. However, if you want to include a piece of information to explain a part of your code, you can use the # sign. A line of text preceded by a # is called a comment. The machine does not run this code — it is only for humans to read. When you look back at your code later, comments may help you figure out what it was intended to do.

# this variable counts how many rows of the spreadsheet we have: row_count = 13

Numbers

Variables can also hold numeric values. The simplest kind of number in Python is the integer, which is a whole number with no decimal point:

int1 = 1 int2 = 10 int3 = -5

A number with a decimal point is called a float. You can define floats with numbers after the decimal point or by just including a decimal point at the end:

float1 = 1.0 float2 = 10. float3 = -5.5

You can also define a float using scientific notation, with e indicating the power of 10:

# this evaluates to 150: float4 = 1.5e2

Multi-line Strings

We have seen how to define a string with single quotes and with double quotes. If we want a string to span multiple lines, we can also use triple quotes:

address_string = """136 Whowho Rd Apt 7 Whosville, WZ 44494"""

This address spans multiple lines, and is still contained in one variable, address_string.

When a string like this is not assigned to a variable, it works as a multi-line comment. This can be helpful as your code gets more complex:

"""The following piece of code does the following steps: takes in some input does An Important Calculation returns the modified input and a string that says "Success!" or "Failure..." """ ... a complicated piece of code here...

Booleans

Sometimes we have a need for variables that are either true or false. This datatype, which can only ever take one of two values, is called a boolean. In Python, we define booleans using the keywords True and False:

a = True b = False

A boolean is actually a special case of an integer. A value of True corresponds to an integer value of 1, and will behave the same. A value of False corresponds to an integer value of 0.

ValueError

Python automatically assigns a variable the appropriate datatype based on the value it is given. A variable with the value 7 is an integer, 7. is a float, "7" is a string. Sometimes we will want to convert variables to different datatypes. For example, if we wanted to print out an integer as part of a string, we would want to convert that integer to a string first. We can do that using str():

age = 13 print "I am " + str(age) + " years old!"

This would print:

>>> "I am 13 years old!"

Similarly, if we have a string like "7" and we want to perform arithmetic operations on it, we must convert it to a numeric datatype. We can do this using int():

number1 = "100" number2 = "10" string_addition = number1 + number2 #string_addition now has a value of "10010" int_addition = int(number1) + int(number2) #int_addition has a value of 110

If you use int() on a floating point number, it will round the number down. To preserve the decimal, you can use float():

string_num = "7.5" print int(string_num) print float(string_num)

>>> 7 >>> 7.5

Information

Description
Information Technology Note on Python, created by Nagashayan reddy on 21/06/2018.
Views
1
Rating

Feedback

What is it about?

What do you think of the new look?

How do you like it?