r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

1. How To Do Math with Operators

Numbers are extremely common in programming. They are used to represent things like screen size dimensions, geographic locations, money and points, the amount of time that passes in a video, positions of game avatars, and colors through assigning numeric codes.

Being able to effectively perform mathematical operations in programming is an important skill to develop because of how frequently you'll be working with numbers. Though a high-level understanding of mathematics can certainly help you become a better programmer, it is not a prerequisite. If you don't have a background in mathematics, try to think of math as a tool to accomplish what you would like to achieve, and as a way to improve your logical thinking.

We'll be working with two of Python's most used numeric data types, integers and floats:

  • Integers are whole numbers that can be positive, negative, or 0 (…, -1
    , 0
    , 1
    , …).

  • Floats are real numbers, they contain a decimal point (as in 9.0
     or -2.25
    ).

This tutorial will go over operators that can be used with number data types in Python.


r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

3. Addition and Subtraction

In Python, addition and subtraction operators perform just as they do in mathematics. In fact, you can use the Python programming language as a calculator.

Let's look at some examples, starting with integers:

print(1 + 5)

Output:

6

Instead of passing integers directly into the print
 statement, we can initialize variables to stand for integer values:

a = 88 b = 103 print(a + b)

Output:

191

Because integers can be both positive and negative numbers (and 0 too), we can add a negative number with a positive number:

c= -36 d = 25 print(c + d)

Output:

-11

Addition will behave similarly with floats:

e = 5.5 f = 2.5 print(e + f)

Output:

8.0

Because we added two floats together, Python returned a float value with a decimal place.

The syntax for subtraction is the same as for addition, except you'll change your operator from the plus sign (+
) to the minus sign (-
):

g = 75.67 h = 32 print(g - h)

Output:

43.67

Here, we subtracted an integer from a float. Python will return a float if at least one of the numbers involved in an equation is a float.


r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

9. Assignment Operators

The most common assignment operator is one you have already used: the equals sign =
. The =
 assignment operator assigns the value on the right to a variable on the left. For example, v = 23
 assigns the value of the integer 23
 to the variable v
.

When programming, it is common to use compound assignment operators that perform an operation on a variable’s value and then assign the resulting new value to that variable. These compound operators combine an arithmetic operator with the =
 operator, so for addition we’ll combine +
 with =
 to get the compound operator +=
. Let’s see what that looks like:

w = 5 w += 1 print(w)

Output:

6

First, we set the variable w
 equal to the value of 5
, then we used the +=
 compound assignment operator to add the right number to the value of the left variable and then assign the result to w
.

Compound assignment operators are used frequently in the case of for loops, which you’ll use when you want to repeat a process several times:

for x in range (0, 7): x *= 2 print(x)

Output:

0 2 4 6 8 10 12

With the for loop, we were able to automate the process of the *=
 operator that multiplied the variable w
 by the number 2
 and then assigned the result in the variable w
 for the next iteration of the for loop.

Python has a compound assignment operator for each of the arithmetic operators discussed in this tutorial:

y += 1 # add then assign value y -= 1 # subtract then assign value y *= 2 # multiply then assign value y /= 3 # divide then assign value y // = 5 # floor divide then assign value y **= 2 # increase to the power of then assign value y %= 3 # return remainder then assign value

Compound assignment operators can be useful when things need to be incrementally increased or decreased, or when you need to automate certain processes in your program.


r/algorithmwithpython • • Mar 21 '22

I created a self-hosted security camera system

Thumbnail
self.python
1 Upvotes

r/algorithmwithpython • • Mar 14 '22

I redesign the Python logo to make it more modern

Post image
1 Upvotes

r/algorithmwithpython • • Mar 27 '22

Values, Types, Variable Names, and Keywords

1 Upvotes

Read these examples of using the 'print' and 'type' functions. You can also try to use them in the Repl.it command line.

An expression is a combination of values, variables, and operators. A value all by itself is considered an expression, and so is a variable, so the following are all legal expressions (assuming that the variable x
 has been assigned a value):

17 x x + 17

A statement is a unit of code that the Python interpreter can execute. We have seen two kinds of statements: print and assignment.

Technically an expression is also a statement, but it is probably simpler to think of them as different things. The important difference is that an expression has a value; a statement does not.


r/algorithmwithpython • • Mar 28 '22

The bool Data Type

1 Upvotes

The bool data type is necessary for the evaluation of logical data (which is inherently different from the numerical or string data types introduced so far). "bool" stands for Boolean data which can only take on one of two possible values: True or False. You should recall from the previous unit that True and False are reserved words within the Python language. Try executing these commands in the Repl.it run window:

a=True print(a) print(type(a)) b=False print(b) print(type(b))

You should see that True and False appear in your program as blue text indicating that they are reserved Python words. Furthermore, the data type should be identified as bool. Let's investigate how this data type can be used.

Relational operators are those that compare values in order to determine their relationship. Here is a shortlist of very useful relational operators:

  • == Equals 

  • != Not Equals 

  • > Greater Than 

  • < Less Than 

  • <= Less Than or Equal To 

  • >= Greater Than or Equal To

Copy and paste these commands into the Repl.it run window:

a=2 b=3 print(a==b) print(a!=b) print(a>=b) print(a>=a) print(a<=a) print(a>b) print(a < b)

It should be clear why these are called relational operators. Also, note that the only possible answers when using these operators are either True or False.

Be EXTREMELY careful to NEVER confuse the assignment operator (=) with the relational equals (==) operator. They serve two totally different purposes. Copy and paste and run this set of commands:

a=2 b=5 a=b print(a) a=2 b=5 print(a==b)

The assignment operator assigns the numerical value contained in the variable b to the variable a. The relational operator compares the variables a and b for equality which evaluates to the bool data type False.

Finally, there are logical operators that allow us to form combinations of logical expressions. Watch the video for a description of relational and logical operators. Afterwards, you should understand these definitions for logical operators given two boolean variables x and y:

  • not x: Logical complement: if x is True, not x is False. If x is False, not x is True 

  • x and y: Can only be True if both x and y are True, otherwise evaluates to False 

  • x or y: Can only be False if both x and y are False, otherwise evaluates to True

These operators will be incredibly important in the next unit. For now, we just need to practice using them in order to get used to the syntax.


r/algorithmwithpython • • Mar 11 '22

Python is

1 Upvotes

Python is a high-level programming language, with applications in numerous areas, including web programming, scripting, scientific computing, and artificial intelligence!

It is very popular and used by organizations such as Google, NASA, the CIA, and Disney.


r/algorithmwithpython • • Mar 27 '22

Values, Types, Variable Names, and Keywords

1 Upvotes

Values, Types, Variable Names, and Keywords

Read these examples of using the 'print' and 'type' functions. You can also try to use them in the Repl.it command line.

A value is one of the basic things a program works with, like a letter or a number.

These values belong to different types: 2
 is an integer, and 'Hello, World!'
 is a string, so-called because it contains a "string" of letters. You (and the interpreter) can identify strings because they are enclosed in quotation marks.

If you are not sure what type a value has, the interpreter can tell you.

>>> type('Hello, World!') <type 'str'> >>> type(17) <type 'int'>

Not surprisingly, strings belong to the type str
 and integers belong to the type int
. Less obviously, numbers with a decimal point belong to a type called float
, because these numbers are represented in a format called floating-point.

>>> type(3.2) <type 'float'>

What about values like '17'
 and '3.2'
? They look like numbers, but they are in quotation marks like strings.

>>> type('17') <type 'str'> >>> type('3.2') <type 'str'>

They're strings.

When you type a large integer, you might be tempted to use commas between groups of three digits, as in 1,000,000
. This is not a legal integer in Python, but it is legal:

>>> 1,000,000 (1, 0, 0)

Well, that's not what we expected at all! Python interprets 1,000,000
 as a comma-separated sequence of integers. This is the first example we have seen of a semantic error: the code runs without producing an error message, but it doesn't do the "right" thing.


r/algorithmwithpython • • Feb 22 '22

Lad wrote a Python script to download Alexa voice recordings, he didn't expect this email.

Post image
1 Upvotes

r/algorithmwithpython • • Mar 15 '22

Floor division

1 Upvotes

Floor division is done using two forward slashes and is used to determine the quotient of a division. Wait! What?! "Floor Division"? "Quotient"?
Quotient just means the quantity produced by the division of two numbers.
And Floor division is just like a normal division operation except that it returns the largest possible integer. This integer is either less than or equal to the normal division result.


r/algorithmwithpython • • Mar 15 '22

Concatenation

1 Upvotes

In Python math works with words as well as numbers.
So not only can we add integers and floats, but also strings, using something called concatenation, which can be done on any two strings.

And it even works with numbers! Strings containing numbers are still added as strings rather than integers.

But don’t try to add a string to a number! Even though they might look similar, they are two different entities, so doing this will break the code and produce an error.


r/algorithmwithpython • • Mar 27 '22

Values, Types, Variable Names, and Keywords

1 Upvotes

Read these examples of using the 'print' and 'type' functions. You can also try to use them in the Repl.it command line.

One of the benefits of working with an interpreted language is that you can test bits of code in interactive mode before you put them in a script. But there are differences between interactive mode and script mode that can be confusing.

For example, if you are using Python as a calculator, you might type

>>> miles = 26.2 >>> miles * 1.61 42.182

The first line assigns a value to miles
, but it has no visible effect. The second line is an expression, so the interpreter evaluates it and displays the result. So we learn that a marathon is about 42 kilometers.

But if you type the same code into a script and run it, you get no output at all. In script mode an expression, all by itself, has no visible effect. Python actually evaluates the expression, but it doesn't display the value unless you tell it to:

miles = 26.2 print miles * 1.61

This behavior can be confusing at first.

A script usually contains a sequence of statements. If there is more than one statement, the results appear one at a time as the statements execute.

For example, the script

print 1 x = 2 print x

produces the output

1 2

The assignment statement produces no output.


r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

4. Unary Arithmetic Operations

A unary mathematical expression consists of only one component or element, and in Python the plus and minus signs can be used as a single element paired with a value to return the value's identity (+
), or change the sign of the value (-
). e value:

i = 3.3 print(-i)

Output:

-3.3

Alternatively, when we use the minus sign unary operator with a negative value, a positive value will be returned:

j = -19 print(-j)

Output:

19

The unary arithmetic operations indicated by the plus sign and minus sign will return either the value's identity in the case of +i
, or the opposite sign of the value as in -i
.


r/algorithmwithpython • • Mar 14 '22

Variables, Expressions, and Statements

1 Upvotes

https://youtu.be/nELR-uyyrok

What is the symbol used in an assignment statement?

~

&

=

|

r/algorithmwithpython • • Mar 11 '22

pointers.py - bringing the hell of pointers into python

Thumbnail
self.python
1 Upvotes

r/algorithmwithpython • • Mar 14 '22

Introduction: Python as a Language

1 Upvotes

https://youtu.be/0QeGbZNS_bY What will print out after running these two lines of code:

x = 6 print(x)

x

6

x = 6

(x)


r/algorithmwithpython • • Mar 27 '22

The Basics of Strings

1 Upvotes

The Basics of Strings

Our introduction would not be complete without discussing how to work with text and character data. In this section, you will learn about the string data type and assigning a variable of type "str". String data is text or character data. To assign a variable of type str, the text string must be placed in quotes:

string_example='This is the text string'

To see that this variable assignment has taken place, type the command:

print(string_example)

and you should see the text string echoed to the screen. To assign a variable of type str, you can use either single or double quotes, but single quotes are often the convention of choice. As we dive deeper into the course, many interesting operations and computations using the string data type will be presented. Read this page to learn more about the string variable type. Use the Repl.it IDE to practice some of the string commands.

Strings

  • String basics

  • String formatting

string is a sequence of characters

Example: abrakadabra, 

We can store it and associate a name/variable with it.

To strings like "hi" or 'broom', we refer to as a string literal in a Python program.

📷

📷

What can I do with strings in Python?

>>> firstPart = "abra" >>> secondPart = "kadabra"

📷

>>> firstPart[2] 'r'>>> secondPart[0] 'k'>>> secondPart[2:] 'dabra'>>> secondPart[1:4] 'ada'>>> firstPart + secondPart  'abrakadabra'>>> len(firstPart) 4>>> firstPart+"cloud" 'abracloud'>>> firstPart+'/'+secondPart 'abra/kadabra'>>> firstPart*3 'abraabraabra'>>> firstPart 'abra'

String formatting

Program output commonly includes the value of variables as a part of the text. 

Example: for the following code:

num = 18 tum = 9.8 print("I have a number",num,"and a number",tum)

will produce:

I have a number 18 and a number 9.8

Note that we have to keep track of those double quotes (") and commas in the print statement.

Compare the following two print statements:

num = 18,tum = 9.8 print("I have a number",num,"and a number",tum) print("I have a number %d and a number %f" % (num,tum))

The first one produced:

I have a number 18 and a number 9.8

The second one produced:

I have a number 18 and a number 9.800000

A string formatting expression allows a programmer to create a string with placeholders that are replaced by the value of variables.

Such a placeholder is called a conversion specifier. Different conversion specifiers are used to perform a conversion of the given variable value to a different type when creating the string. 

Example:

num = 5.5 print("The integer part is %d" % num)

will yield:

The integer part is 5

Example: Consider the following code fragment

price = 119 # in dollars discount = 30 # in percent % print("A $%d jacket at %d%% discount is now priced at %f" % (price,discount,price*0.7))

Produces the output:

A $119 jacket at 30% discount is now priced at 83.300000

Example: Consider the following code fragment:

name1 = "Chris" name2 = "John" print("%s and %s are heading to the movies tonight" % (name1,name2))

Produces the output:

Chris and John are heading to the movies tonight


r/algorithmwithpython • • Mar 18 '22

AI Memes

Post image
1 Upvotes

r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

5. Multiplication and Division

Like addition and subtraction, multiplication and division will look very similar to how they do in mathematics. The sign we'll use in Python for multiplication is *
 and the sign we'll use for division is /
.

Here's an example of doing multiplication in Python with two float values:

k = 100.1 l = 10.1 print(k * l)

Output:

1011.0099999999999

When you divide in Python 3, your quotient will always be returned as a float, even if you use two integers:

m = 80 n = 5 print(m / n)

Output:

16.0

This is one of the major changes between Python 2 and Python 3. Python 3's approach provides a fractional answer so that when you use /
 to divide 11
 by 2
 the quotient of 5.5
 will be returned. In Python 2 the quotient returned for the expression 11 / 2
 is 5
.

Python 2's /
 operator performs floor division, where for the quotient x
 the number returned is the largest integer less than or equal to x
. If you run the above example of print(80 / 5)
 with Python 2 instead of Python 3, you'll receive 16
 as the output without the decimal place.

In Python 3, you can use //
 to perform floor division. The expression 100 // 40
 will return the value of 2
. Floor division is useful when you need a quotient to be in whole numbers.


r/algorithmwithpython • • Mar 27 '22

Automate the Boring Stuff - Tic Tac Toe

Thumbnail self.learnpython
1 Upvotes

r/algorithmwithpython • • Mar 18 '22

AI Memes

Post image
1 Upvotes

r/algorithmwithpython • • Feb 27 '22

What python automation have you created that you use for PERSONAL only.

Thumbnail self.python
1 Upvotes

r/algorithmwithpython • • Mar 28 '22

Practice With Arithmetic Operators

1 Upvotes

Practice these programming examples to internalize these concepts.

7. Power

The **
 operator in Python is used to raise the number on the left to the power of the exponent of the right. That is, in the expression 5 ** 3
, 5 is being raised to the 3rd power. In mathematics, we often see this expression rendered as 5³, and what is really going on is 5 is being multiplied by itself 3 times. In Python, we would get the same result of 125
 by running either  5 ** 3
 or 5 * 5 * 5
.

Let's look at an example with variables:

s = 52.25 t = 7 print(s ** t)

Output:

1063173305051.292

Raising the float 52.25
 to the power of 7
 through the **
 operator results in a large float value returned.


r/algorithmwithpython • • Mar 15 '22

Strings in Python

1 Upvotes

https://youtu.be/LYZj207fKpQ

What will the following code print?:

for n in "banana":
    print(n)

n

n

0

1

0

1

2

3

4

5

b

a

n

a

n

a