Search

Carolina's Blog Site

I use this blog to share my experiences in learning how to program, among other things.

Month

August 2016

Using If & Else to set an alarm.

So, Alex (@hkael.wordpress.com) and I created a program using if and else statements to set an alarm. It took awhile, and at the end, we realized:

a) it doesn’t take into account minutes, which would make it a more complex piece of code, and we like, don’t know how to do it-YET!

b) Ken told us there’s actually already a module that does this.

Lol. It was fun though.

Enjoy the screenshots.

alarm final extra fibal

SO…..Later today I had a life altering realization. I thought the print function would only display strings, so I also converted integer values to strings and displayed all of them using +++. But I just discovered you can totally print stuff that has strings and integers, as long as you separate them with ,,,,. Whoa.

I know this doesn’t seem like much of a difference, but I feel like it’s always better to be zenny about it and write shorter code.

BEFORE:

oldiiii

AFTER:

new

 

 

7 Ways Statements are Completely Underrated.

A statement is an instruction that the Python interpreter can execute. Some kinds of statements are while statements, for statements, if statements, and import statements.

  • An if statement is a conditional statement. It is run once if its condition evaluates to True, and never if it evaluates to False.
  • An else statement follows an if statement and introduces a new block of code that will run only if the condition is false.
  • An elif statement combines else & if. Pretty simple
  • Nesting conditionals basically means creating conditions inside conditions.

 

  • A try/except statement is used to see exceptions if an exception might occur.
  • The finally statement is used to ensure some code runs despite any errors; it’s placed at the bottom of a try/except statement.

 

  • An import statement lets you access the code in another module by the process of importing it.
    • Btw, modules are just files containing Python code. There are 3 main types of modules in Python: those you write yourself, those you install from external sources, and those that are installed already with Python (called the standard library).

 

  • A for statement/loop runs through an iterable Python object, giving you a value until there are no more items left.
  • A while statement/loop is repeatedly executed as long as the condition holds.
    • break: To end a while loop prematurely, use the break statement, which causes the loop to finish immediately.
    • continue: Unlike break, continue jumps back to the top of the loop, rather than stopping it.

Dwight Meme - the below statement is true the above statement is false

source: https://memegenerator.net/instance/24442353

User Input.

The input function allows the user to provide a prompt string. The first thing you need to do is declare a variable where the input will be stored. When the function is evaluated, the prompt is shown. The user of the program can enter anything and press enter. But wait, there’s more…you should know that the input function returns a string value. If you want to do some arithmetic with it, you need to convert that string into an int or a float, using the int or float converter functions.

SCREENSHOT.

Boolean Logic

The logic that computers use is called Boolean logic, so called because it was invented by George Boole way back in the 1800s.

There are two Boolean values: True and False. I know, shocker.

  • They can be created by comparing values, for example using the equal operator ==, the not equal operator !=, the greater than operator, etc.
  • Btw, DO NOT confuse assignment (=) with comparison (==).
  • Python’s Boolean operators are and, or, and not.
    • And: Takes 2 arguments. It evaluates to True if both are True.
    • Or: Takes 2 arguments. It evaluates to True if either or both are True.
    • Not: Takes 1 argument, and inverts it. The result of not True is False, and not False goes to True.

 

Anyway, operator precedence is a very important. Python’s order of operations is the same as that of normal math: Please Excuse My Dear Aunt Sally.

.

I guess he’s just not your type.

In Python, everything is a value (object). Values are classified into types (classes) of data. There are 3 standard types of data:

  • Numeric types
  • Sequence types(Strings, Lists, Tuples)
  • Mapping types (Dictionaries)

Numeric types: The 2 most common types of number are the integer (whole number) and the float (has decimals), but there’s also long (super long integers, written like integers but followed by an L) and complex (includes real and imaginary numbers).

Strings are a collection of characters stringed together that can be used to store like, anything. They’re immutable sequences of Unicode code points. To enter a string, use “” or ”.  Btw, adding strings together is called concatenation. You can refer to a certain part of a string by using the index operator to get one character, e.g. [1], or by slicing it to get groups of characters, e.g. [1:3].

Lists are used to store an indexed list of items. They are created using [] and separating items with ,,,. They are mutable. The certain item in the list can be accessed by using its index in square brackets.

  • To check if an item is in a list, use the in operator, which returns True if the item is in the list, and False if it is not.
  • Append method adds an item to the end of an existing list, e.g. nums.append(4).
  • Len function to get the number of items in a list, e.g. print (len(nums)).
  • The insert method allows you to insert a new item at any position in the list.; the first argument is the index of the element before which to insert.
  • The index method finds the first occurrence of a list item and returns its index.
  • max(list): Returns the list item with the maximum value.
  • min(list): Returns the list item with minimum value.
  • list.count(obj): Returns a count of how many times an item occurs in a list.
  • list.remove(obj): Removes an object from a list.
  • list.reverse(): Reverses objects in a list.
  • There’s a ton more -look’em up!

Tuples: pretty much lists, except they’re immutable, and they use ().

Ranges represent an immutable sequence of numbers, commonly used in for loops.

Dictionaries are the relation between a key and a value given to that key, and the’re enclosed by {}. Basically, they relate values to objects. To retrieve the value of the key, use []. E.g.

>>>my_dct= {‘ramen’: 5, ‘pasta’: 3}

>>>my_dct [‘pasta’]

3

 

SOURCE: https://docs.python.org/3/library/stdtypes.html

Common errors

Syntax error: Syntax refers to the structure of a program and the rules about that structure.

Runtime error: So called because the error does not appear until you run the program, also called exceptions.

Semantic error: If there is a semantic error in your program, it will technically run successfully cause the computer won’t generate any error messages. However, your program will not do the right thing. The meaning of the program (its semantics) is wrong.

Coding Success! | THAT 5,000 LINE CODE RAN SUCCESSFULLY WITHOUT ANY ERROR | image tagged in memes,evil toddler,programming,frustrated programmer,yeah,overjoyed | made w/ Imgflip meme maker

image from: https://imgflip.com/i/vuer9

 

How variables are bringing sexy back.

variable is a name that refers to a value. Their names can be as long as you like. They can contain both letters and numbers, but they can’t begin with a number. Also, they can’t have spaces, and they can’t be Python keywords.

Assignment statements create new variables and also give them values to refer to. They go like this: name= value.

The assignment token, =, should not be confused with equality (==).

 

 

 

 

How to get likes and comments on your python code

 

So basically, it’s important to comment on your code, so you and others can easily undesrtand it.

If you want brief comment (only a single line), just type # blah blah

If you want to make a bigger comment (multiple lines), just type “””blah blah”””

.

comments

got pic from: http://scumbagsteve.com/scumbag-steve-meme/56972-picture

Zen of Python

The Zen of Python is a collection of 20 software principles, only 19 of which were written around June 1999 by Tim Peters. I don’t really get why he only wrote 19, I guess it’s just some Tim Peters inside joke or something. Maybe it’s an opportunity for people to add their own principle, like rule #20: there is no rule 20, or you’re not gonna need it.

The Zen of Python can be found on the official Python website. It’s also included as an Easter egg–which is an intentional inside joke, hidden message, or feature in an interactive work (computer program, video game,etc.) –in Python interpreter. It’s displayed by entering the statement import this. I did it on my computer, and this is the screenshot.

zen

The principles are actually really cool, and they make a lot of sense. This video explains each principle: https://www.youtube.com/watch?v=PHSAVai7yx4.

The Hunger Games guide to Python 3 basics

This website is the bomb: http://www.sololearn.com/Play/Python

I just learned about integers, floats, and strings. Fun stuff.

One does not simply type code in the python shell

There are two ways to use the Python interpreter: shell mode and program mode.

In shell/interactive mode, you type Python expressions into the Python shell, and the interpreter immediately shows the result.

In program/normal/scrpt mode, you type Python code in an editor (such as Notepad ++ or Atom) and save it, and and then run it using the command line, Cygwin, IDLE, etc. This is better cause you can save and share your code!!!!!!

“Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” -Bill Gates

Create a free website or blog at WordPress.com.

Up ↑