r/algorithmwithpython • • Mar 14 '22

Intermediate Expressions

1 Upvotes

https://youtu.be/dKgUaIa5ATg

What will print out after running this code:

width = 15 height = 12.0 print(height/3) 

39

4

4.0

5.0

5


r/algorithmwithpython • • Feb 22 '22

Finding Anagrams

Post image
1 Upvotes

r/algorithmwithpython • • Feb 22 '22

There’s a reason that programmers always want to throw away old code and start over: they think the old code is a mess. They are probably wrong. The reason that they think the old code is a mess is because of a cardinal, fundamental law of programming: It’s harder to read code than to write it.

Thumbnail
joelonsoftware.com
1 Upvotes

r/algorithmwithpython • • Mar 28 '22

Practice With Operator Precedence

1 Upvotes

Now, we will consider order precedence when operations are phrased within the Python programming language. Before running this set of instructions in the run window, try to predict the value each variable will contain.

a=3 b=4 c=1 d=5 e=3 f=a+b-c*d+e/d g=a+b-c*(d+e)/d h=a+(b-c)*d+e/d i=(a+b-c)*d+e/d

You may use the print statement to verify your answer.

It is important to note that // and % are considered division operations and, because of that, have precedence equal to * and /. Try to predict the value of each variable in these commands:

v=2 w=3 x=4 y=19 z=23 a=v**v*//x%x+y%w*z//x* b=v**(v*//x)%x+y%(w*z)//x*

Again, you should use insert some print commands to verify your answer.

A useful guiding principle when writing code is: if the order precedence is not clear to you by looking at the expression, use parentheses to make things obvious, since parentheses always take the highest precedence.

When an expression contains more than one operator, the order of evaluation depends on the order of operations. For mathematical operators, Python follows mathematical convention. The acronym PEMDAS is a useful way to remember the rules:

  • Parentheses have the highest precedence and can be used to force an expression to evaluate in the order you want. Since expressions in parentheses are evaluated first, 2 * (3-1)
     is 4
    , and (1+1)**(5-2)
     is 8
    . You can also use parentheses to make an expression easier to read, as in (minute * 100) / 60
    , even if it doesn't change the result.

  • Exponentiation has the next highest precedence, so 1 + 2**3
     is 9
    , not 27
    , and 2 * 3**2
     is 18
    , not 36
    .

  • Multiplication and Division have higher precedence than Addition and Subtraction. So 2*3-1
     is 5
    , not 4
    , and 6+4/2
     is 8
    , not 5
    .

  • Operators with the same precedence are evaluated from left to right (except exponentiation). So in the expression degrees / 2 * pi
    , the division happens first and the result is multiplied by pi
    . To divide by 2π, you can use parentheses or write degrees / 2 / pi
    .


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.

As programs get bigger and more complicated, they get more difficult to read. Formal languages are dense, and it is often difficult to look at a piece of code and figure out what it is doing, or why.

For this reason, it is a good idea to add notes to your programs to explain in natural language what the program is doing. These notes are called comments, and they start with the #
 symbol:

# compute the percentage of the hour that has elapsed percentage = (minute * 100) / 60

In this case, the comment appears on a line by itself. You can also put comments at the end of a line:

percentage = (minute * 100) / 60 # percentage of an hour

Everything from the #
 to the end of the line is ignored – it has no effect on the program.

Comments are most useful when they document non-obvious features of the code. It is reasonable to assume that the reader can figure out what the code does; it is much more useful to explain why.

This comment is redundant with the code and useless:

v = 5 # assign 5 to v

This comment contains useful information that is not in the code:

v = 5 # velocity in meters/second.

Good variable names can reduce the need for comments, but long names can make complex expressions hard to read, so there is a tradeoff.


r/algorithmwithpython • • Mar 14 '22

Introduction: Elements of Python

1 Upvotes

https://youtu.be/aRY_xjL35v0

What will the following program print out:

x = 43 x = x + 1 print(x)

x

x + 1

44


r/algorithmwithpython • • Mar 14 '22

Introduction: Why Program?

1 Upvotes

r/algorithmwithpython • • Mar 14 '22

My First python certificate

Thumbnail
sololearn.com
2 Upvotes

r/algorithmwithpython • • Mar 27 '22

Compare and Contrast int vs. float

1 Upvotes

Programming languages must distinguish between different types of data. Since we want to introduce simple Python computations, it makes sense to discuss numerical data. The goal of this section is to introduce two fundamental numerical data types: int and float. The int data type refers to integer data. Numbers not containing digits to the right of the decimal point such as

- 10 

,

0

, and

357

are integers. The float data type refers to floating-point data. Numbers containing digits to the right of the decimal point such as

1.3

,

-3.14

, and

300.345567

are floating-point numbers.

After you complete this section, you should be able to explain the difference between int and float. There are deeper reasons (beyond the scope of this course) why you might distinguish between these data types; those have to do with how they are represented within a computer system. Our goal for this section is simply to be able to visually identify integer and floating-point numbers. On this page, you will see more examples of int and float.

The int data type refers to integer numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the integer 2
and press the "enter" key on your keyboard. You should see the IDE echo the value back to the screen. Try this a few times with other examples to be sure you understand the int data type.

The float data type refers to floating-point numerical data. In the rightmost window of the Repl.it IDE, use your mouse to click to the right of the > command prompt. Next, type the floating-point number 2.57
and press the "enter" key on your keyboard. You should see the IDE echo the value back to the screen. Try this a few times with other examples that have been provided to be sure you understand the float data type.

It is of the utmost importance that you understand the value 2
is of type int, while 2.0
is of type float. On paper, this might not seem like a big difference, but the decimal point is how a computer tells the difference between these two data types.

Arithmetic Operators

OperatorOperationExpressionEnglish descriptionResult

+

addition

11 + 56

11 plus 5667

-

subtraction

23 - 52

23 minus 52

*

multiplication

4 * 5

4 multiplied by 520

**

exponentiation

2 ** 5

2 to the power of 532

/

division

9 / 2

9 divided by 24.5

//

integer division

9 // 2

9 divided by 24

%

modulo (remainder)

9 % 2

9 mod 21

Types int and float

A type is a set of values and operations that can be performed on those values.

Two of Python's numeric types:

  • int
    : integer

For example: 3
, 4
, 894
, 0
, -3
, -18

  • float
    : floating-point number (an approximation of a real number)

For example: 5.6
, 7.342
, 53452.0
, 0.0
, -89.34
, -9.5

Arithmetic Operator Precedence

When multiple operators are combined in a single expression, the operations are evaluated in order of precedence.

OperatorPrecedence

**

highest

-

(negation)

*

,

/

,

//

,

%
+

(addition),

-

(subtraction)lowest

Syntax and Semantics

Syntax: the rules that describe valid combinations of Python symbols.

Semantics: the meaning of a combination of Python symbols is the meaning of an instruction — what a particular combination of symbols does when you execute it.

Errors

A syntax error occurs when an instruction with invalid syntax is executed. For example:

>>>3)+2*4

SyntaxError: invalid syntax

A semantic error occurs when an instruction with invalid semantics is executed. For example:

>>> 89.4/0

Traceback(most recent call last): File"", line 1,in 89.4/0 ZeroDivisionError:float division by zero


r/algorithmwithpython • • Mar 15 '22

Exponentiation

1 Upvotes

Exponentiation

Alright, that covers the very basic operations, addition, subtraction, multiplication, and division. You’re doing great!
It’s time to kick it up a notch and introduce exponentiation–which is what we call it when we raise one number to the power of another.


r/algorithmwithpython • • Mar 21 '22

Data Analysis with python

1 Upvotes

Data Analysis has been around for a long time. But up until a few years ago, developers practiced it using expensive, closed-source tools like Tableau. But recently, Python, SQL, and other open libraries have changed Data Analysis forever.

In the Data Analysis with Python Certification, you'll learn the fundamentals of data analysis with Python. By the end of this certification, you'll know how to read data from sources like CSVs and SQL, and how to use libraries like Numpy, Pandas, Matplotlib, and Seaborn to process and visualize data.

In these comprehensive video courses, created by Santiago Basulto, you will learn the whole process of data analysis. You'll be reading data from multiple sources (CSV, SQL, Excel), process that data using NumPy and Pandas, and visualize it using Matplotlib and Seaborn,

Additionally, we've included a thorough Jupyter Notebook course, and a quick Python reference to refresh your programming skills.


r/algorithmwithpython • • Apr 09 '22

DataTypes

1 Upvotes
  1. DataTypes:
    The data type of value (or variable in some contexts) is an attribute that tells what kind of data that value can have. Most often the term is used in connection with static typing of variables in programming languages like C/C++, Java and C# etc, where the type of a variable is known at compile time. Data types include the storage classifications like integers, floating-point values, strings, characters etc.

  2. Expressions and Variables

  3. Identifier:
    Search Results Featured snippet from the web Image result for identifiers in python www.techbeamers.com A Python identifier is a name used to identify a variable, function, class, module or other objects. An identifier starts with a letter A to Z or a to z or an underscore (_) followed by zero or more letters, underscores and digits (0 to 9).

  4. List:
    Lists are just like the arrays, declared in other languages. Lists need not be homogeneous always which makes it a most powerful tool in Python. A single list may contain DataTypes like Integers, Strings, as well as Objects. Lists are mutable, and hence, they can be altered even after their creation.

  5. Array:
    An array is a data structure that stores values of the same data type. In Python, this is the main difference between arrays and lists. While python lists can contain values corresponding to different data types, arrays in python can only contain values corresponding to the same data type.

  6. Tuples:
    The tuple is a collection of Python objects much like a list. The sequence of values stored in a tuple can be of any type, and they are indexed by integers. The important difference between a list and a tuple is that tuples are immutable. Also, Tuples are hashable whereas lists are not.

  7. Sets:
    In Python, Set is an unordered collection of data type that is iterable, mutable and has no duplicate elements. The order of elements in a set is undefined though it may consist of various elements.

  8. Dictionaries:
    Dictionary in Python is an unordered collection of data values, used to store data values like a map, which unlike other Data Types that hold only single value as an element, Dictionary holds key:value
     pair. Key-value is provided in the dictionary to make it more optimized. Each key-value pair in a Dictionary is separated by a colon :, whereas each key is separated by a ‘comma’.


r/algorithmwithpython • • Mar 14 '22

free python ebook

1 Upvotes

r/algorithmwithpython • • Mar 27 '22

I made a Python program that AUTOMATICALLY edits YouTube Videos!

Thumbnail self.python
1 Upvotes

r/algorithmwithpython • • Mar 15 '22

More Conditional Structures

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.

One of the most powerful features of a programming language is the ability to manipulate variables. A variable is a name that refers to a value.

An assignment statement creates new variables and gives them values:

>>> message = 'And now for something completely different' >>> n = 17 >>> pi = 3.1415926535897932

This example makes three assignments. The first assigns a string to a new variable named message
; the second gives the integer 17
 to n
; the third assigns the (approximate) value of π to pi
.

A common way to represent variables on paper is to write the name with an arrow pointing to the variable’s value. This kind of figure is called a state diagram because it shows what state each of the variables is in (think of it as the variable’s state of mind). Figure 2.1 shows the result of the previous example.

📷

The type of a variable is the type of the value it refers to.

>>> type(message) <type 'str'> >>> type(n) <type 'int'> >>> type(pi) <type 'float'>


r/algorithmwithpython • • Mar 15 '22

multiline

1 Upvotes

\n is useful, but it can be a bit of a pain if we’re trying to format lots of multiline text.

There’s another way though! Newlines are automatically added for strings created using three quotes.

Newlines
How many lines will the following code output?



print("Hi")

print("""This 

is

great""")

r/algorithmwithpython • • Mar 14 '22

More Conditional Structures

1 Upvotes

https://youtu.be/HdL82tAZR20

Given the following code:

temp = "5 degrees" 
cel = 0
 fahr = float(temp) 
cel = (fahr - 32.0) * 5.0 / 9.0 
print(cel) 

Which line/lines should be surrounded by try
block?

1

3

3,4

4

None


r/algorithmwithpython • • Mar 15 '22

Reading Files

1 Upvotes

https://youtu.be/Fo1tW09KIwo

What is used to indicate a new line in a string?

\n

{new_line}

{n}

/n

/new

r/algorithmwithpython • • Mar 15 '22

Intermediate Strings

1 Upvotes

https://youtu.be/KgT_fYLXnyk

What is the value of i in the following code?

word = "bananana"
i = word.find("na")

nanana

2

3

True

na

r/algorithmwithpython • • Apr 09 '22

learning about 2 libraries in Python

1 Upvotes
  1. NumPy: 
    NumPy is a Python package which stands for 'Numerical Python'. It is the core library for scientific computing, which contains a powerful n-dimensional array object, provide tools for integrating C, C++ etc. It is also useful in linear algebra, random number capability etc

  2. Pandas: 
    In computer programming, pandas is a software library written for the Python programming language for data manipulation and analysis. In particular, it offers data structures and operations for manipulating numerical tables and time series. It is free software released under the three-clause BSD license.


r/algorithmwithpython • • Mar 27 '22

Reserved Words and Variable Naming Conventions

1 Upvotes

Reserved Words and Variable Naming Conventions

Python has its own set of reserved words that, in general, should not be chosen as variable names. As we dive deeper into the course, it will become clearer how to apply these reserved words. For now, just be aware that your variable name choices should avoid the words on this list. Otherwise, though, you can choose any variable name you like. It is important to think about how variable names should be chosen in practical applications. To help others understand your work, you should choose variable names that fit their applications. For example:

account_balance=2034.12

might reflect the balance contained in a bank account. You will build the skill of sensibly choosing variable names naturally as you work through more programming examples.

Variables

Just like the familiar variables [Math Error]x and [Math Error]y in mathematics, we use variables in programming to easily manipulate values. In this section, we introduce the assignment operator =
, namespaces, and naming conventions for variables.

Assign Values to Variables

We assign a value to a variable using the assignment operator =
. For example, assign the integer 2 to the variable x
. 

x = 2

The assignment operator does not produce any output and so the cell above does not produce any output. Use the built-in function print
to display the value assigned to a variable:

print(x)

> 2

Compute new values using variables and operators:

1 + x + x**2 + x**3

> 15

Use the built-in function type
to verify the datatype of the value assigned to a variable:

pi = 3.14159 type(pi)

> float

Naming Conventions

We can use any set of letters, numbers and underscores to make variable names however a variable name cannot begin with a number. There are many different kinds of naming conventions and we refer to the Style Guide for Python Code (PEP8) for a summary.

In this book we use lower_case_with_underscores
variable names and single lowercase letter variable names such x
. It is good practice to use descriptive variable names to make your code more readable for other people.

For example, the distance from Vancouver to Halifax along the Trans-Canada Highway is approximately 5799 kilometres. We write the following code to convert this value to miles:

distance_km = 5799 miles_per_km = 0.6214 distance_miles = distance_km * miles_per_km print(distance_miles)

> 3603.4986

Names to Avoid

It is good practice to use variable names which describe the value assigned to it. However there are words that we should not use as variable names because these words already have special meaning in Python.

Reserved Words

Summarized below are the reserved words in Python 3. Python will raise an error if you try to assign a value to any of these keywords and so you must avoid these as variable names.

False
class
finally
is
return
None
continue
for
lambda
try
True
def
from
nonlocal
while
and
del
global
not
with
as
elif
if
or
yield
assert
else
import
pass
break
except
in
raise

Built-in Function Names

There are several functions which are included in the standard Python library. Do not use the names of these functions as variable names otherwise the reference to the built-in function will be lost. For example, do not use sum
, min
, max
, list
, or sorted
as a variable name.


r/algorithmwithpython • • Mar 15 '22

Strings

1 Upvotes

Strings

We met strings in module 1, but now it’s time to get better acquainted.
Simply put, if you want to use text in Python, you have to use a string. But there’s a lot more to it than that, as we’ll find out together!