#WSQ13

--Originally published at Python fundamentals

File input/output is our access to permanent stores of data that last beyond the run time of our application.

Write a program that opens and reads the file 93cars.dat.txt and produces the following data:

  • average gas mileage in city (City MPG)
  • average gas mileage on highway (Highway MPG)
  • average midrange price of the vehicles in the set.

 

Screenshot 2017-05-02 11.29.07.png


Creation and use of dictionaries in Python #22

--Originally published at Learning With Python LWP

Each key is separated from its value by a colon (:), the items are separated by commas, and the whole thing is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this: {}.

Keys are unique within a dictionary while values may not be. The values of a dictionary can be of any type, but the keys must be of an immutable data type such as strings, numbers, or tuples.

Accessing Values in Dictionary:

To access dictionary elements, you can use the familiar square brackets along with the key to obtain its value. Following is a simple example −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Name']: ", dict['Name']
print "dict['Age']: ", dict['Age']

When the above code is executed, it produces the following result −

dict['Name']:  Zara
dict['Age']:  7

If we attempt to access a data item with a key, which is not part of the dictionary, we get an error as follows −

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'}

print "dict['Alice']: ", dict['Alice']

When the above code is executed, it produces the following result


Validated user input (ensure correct/expected data entry) #20 #TC1014

--Originally published at Learning With Python LWP

Reading and Writing Files in Python

Overview

When you’re working with Python, you don’t need to import a library in order to read and write files. It’s handled natively in the language, albeit in a unique manner.

The first thing you’ll need to do is use Python’s built-in open function to get a file object.

The open function opens a file. It’s simple.

When you use the open function, it returns something called a file object. File objects contain methods and attributes that can be used to collect information about the file you opened. They can also be used to manipulate said file.

For example, the mode attribute of a file object tells you which mode a file was opened in. And the name attribute tells you the name of the file that the file object has opened.

You must understand that a file and file object are two wholly separate – yet related – things.


#WSQ12 finding “e”

--Originally published at Python fundamentals

Instructions

In this assignment you will estimate the mathematical constant e. You should create a function called calculuate_e which receives one parameter called precision that should specify the number of decimal points of accuracy.

You will want to use the infinite series to calculate the value, stopping when the accuracy is reached (previous and current calculation are the same at the specified accuracy).

Screenshot 2017-05-02 11.10.49

Screenshot 2017-05-02 11.08.27


Women in computing

--Originally published at Python fundamentals

 

Introduction

On the past Wednesday i was attending an event with my little sister called “Women in computing”, the idea behind this was to bring women engineers working on the tech industry to share their experiences and encourage other women to work on technology.

The truth is that even though I wasn’t part of the target audience I really enjoy it and learn a lot.

The event

The first exponent was Elizabeth López an Oracle engineer that told us about the serious problem of the lack of women in our industry, among other things.

Now, as someone that its working on get a degree in engineer I know that this it’s a sadly truth.

Then after a few minutes she came up with reasons about why we don’t have to many women in tech.

  1. The first one it’s that too many women are interested in science.
  2. Second, the small part that its interested in science are not interested in IT

Finally, she made a very good work trying to motivate and encourage to other women to study not just IT but to reach their potential and made it clear that IT it’s not just about programming.

The last and second speaker was Mireya, she was a former employee at CISCO Systems and now it’s an employee at Oracle with a managerial position.

Her talk was interesting, she told us about her life and the experiences that gave shape to her career.

The story that I enjoy it the most was when she told us that gave up a promotion because she wanted to spend more time with her childs and if she took it she would be transferred, at the end she concluded.

“Even though I gave up to the promotion i handle it to get the opportunity to

Continue reading "Women in computing"

#WSQ11

--Originally published at Python fundamentals

To find out how many times the word banana its found it on a text file, we firstly will create our own file. Once we do that we will create another function that will help us to finish the main program, this function is going to use the string module to transform each character of a string into a lower case letter. If the character can’t be transform it will omit it and just don’t add it into a new variable.

Then it will return a new string which will be a copy of the original one, but this time without characters that are not lower case letters. This function will be called “Just_lowercases”.

Then we are going to create our main program “find_bananas”, this program will use a counter and a for loop that will iterate on each line of the text and on each line will call the “Just_lowercases” function that will convert the text and use the “count” method to count how many times the word “banana” its found it.

I read that on windows if we don’t use the close() function once we open a file using python, the file will have troubles later, i forget to do it the first time but nothig happened. Nevertheless just in case we will do it to avoid posibles errors.

 

wsq11

 

sources:

https://therealgiovasblog.wordpress.com/2017/04/05/wsq11/


#Quizz3

--Originally published at Python fundamentals

Instructions

For this quiz I want you to (in class) create a program with two functions:

  • def square_root(x):  // returns the square root of x (float)
  • def cube_root(x): // returns the cube root of x (float)

Procedure

I used the math module, in the square_root I just use the function “sqrt” and on the cube_root i used a matemathical equivalent using 1/3.

quizz3.png


I will give you a FREE BOOK

--Originally published at Python fundamentals

4235156633_8b155f5339_m

image by David Feng

This its a book called Coders at Work, this its a very interesting book about reporter that made an enterview to ones fo the best coders on the planet.

We have Jamie Zawinski an early programmer at Netscape that drop out the university at Carnegie Mellon.

Douglas Crockford the inventor of JSON

L Peter Deutsch a fellow ACM member that started in the programming in the 50’s by the age of 11.

And many other interesting personalities more.

 

Now, the book has two conditions:

The first one its that once you finish to read it, you will have to give the book to another person and the next person should do the same.

The second one its that you will have to wait untill may 31… because i didn’t fish it yet jaja.

So do you want it?

If the answer its yes just let me know in the comments or contact me and i will give it to you.

 


Creation and use of ranges in Python

--Originally published at Learning With Python LWP

What is Python’s range() Function?

As an experienced Python developer, or even a beginner, you’ve likely heard of the Python range() function. But what does it do? In a nutshell, it generates a list of numbers, which is generally used to iterate over with for loops. There’s many use cases. Often you will want to use this when you want to perform an action X number of times, where you may or may not care about the index. Other times you may want to iterate over a list (or another iterable object), while being able to have the index available.

The range() function works a little bit differently between Python 2.x and 3.x under the hood, however the concept is the same. We’ll get to that a bit later, however.

Python’s range() Parameters

The range() function has two sets of parameters, as follows:

range(stop)

  • stop: Number of integers (whole numbers) to generate, starting from zero. eg. range(3) == [0, 1, 2].

range([start], stop[, step])

  • start: Starting number of the sequence.
  • stop: Generate numbers up to, but not including this number.
  • step: Difference between each number in the sequence.