The Noobs of Python: Ep.1.0 - The Introduction

Nice! Thanks for the advice this makes more sense to me now.

2 Likes

Glad you are enjoying this, it's the goal we wanted to accomplish.

@Miguel_Sensacion @Eden Thanks a lot! It's really exciting :D I've made the same mistake as @Ehwin. Had my ubuntu shell run 2.7 interpret or instead of 3.x and constantly getting errors. Now works great.

Here is my homework:

#!/usr/bin/env python3

from time import sleep
print("Hi!")
sleep(1)

#ask for name and sleep for 1 sec
name = input("What's your name? ")
sleep(1)

#print welcome message and sleep for 1 sec
print("Hi " + name + ", it's nice to meet you.")
sleep(1)

#print BMI message and sleep for 2 sec
print("Les's calculate your BMI. I will need your height and weight.")
sleep(2)

#ask for height input and sleep for 1 sec
height1 = input("Please enter your height in meters: ")
sleep(1)

#ask for weight input and sleep for 2 sec
weight1 = input("Please enter your weight in kilograms: ")
sleep(2)

#Calculate BMI
bim = float(weight1) / float(height1) **2

#Print calculatet BMI rounded to 2 decimal places
print("Your BMI is ", round(bim, 2))

What do you think about it?

2 Likes

You can execute the code from terminal by using the command (without need of #!/usr/bin/env python3 ):

$ python3 code.py

1 Like

Yea I've discovered it once i realised i was using wrong python version. Thanks! :)

1 Like

Cheers mate!

@Pobs @Maras This is an ongoing series, so keep an eye out for more threads. No particular order, but we hope to cover a bunch of content !

1 Like

Nice and clearly written :)

1 Like

or python3.5
On Fedora i got that option.

1 Like

Are you following a style or naming convention, if so, which?

I was looking at PEP 8 and Google Python Style Guide, don't really know whether or not they differ in some way, though.


https://google.github.io/styleguide/pyguide.html

1 Like

PEP 8

I recommend you get Flake8 as well.

Ok so i thought it would be good to take step back and go to basics even more. Noobs like me who never had any previous programming experience need any help they can get and explanation on every step.
As we can see in the previous examples Variable is a character sensitive name assigned to a specific data/value.
Now we have many different types of data, some of the basic ones are: integers, floats, strings, lists and booleans

Lest start with integers. Integer is a whole number, not a fraction, for instance 2, 3, 57, 481, -9.
We can assign numbers to a variables and using function type() check what type that variable is.

#define a
a = 1
#print type of a
print(type(a))

output will be: int

#define b
b = 9
#print type of b
print(type(b))

Again output will be: int

We can perform basic calculations on these variables.

a = 1
b = 8
c = a + b
print(c)

Floats are numbers with fractions: 2.1, 3.8, 203.89. You can perform mathematical operations on them just like on integers.

a = 2.5
b = 3.7
print(a + b)

and our output would be: 6.2


Next are strings. String is a sequence of characters. It can be a name, number or any combination of characters. You can assign a string to a variable with single or double quotations: 'string1' "string2"

#define var first_name
first_name = "John"

#define var last_name
last_name = "Smith"

#print var first_name and last_name
print(first_name, last_name)

That program will print output: John Smith. Each data type behaves a bit differently with different operations. For instance if i would print above variables like this:

print(first_name + last_name)

The output would be: JohnSmith

Strings can be also numbers like: python random_number_string = '39762''
so when i call type() function it should return that variable random_number_string is a string

#define var random_number_string
random_number_string = '39762'

#print type of random_number_string
print(type(random_number_string))

And the output will be: str


Now we will get to lists and other data typed in later episodes but i would like you to pay attention to the input() function and how to change one data type to another.

When you assign variable to user input, that variable is a string unless you convert it.
For instance:

# provide fists input
input_var1 = input("Please enter data for var1: ")

# provide secund input
input_var2 = input("Please enter data for var2: ")

# print message + type of input_var1
print("input_var1 is a ", type(input_var1))

# print message + type of input_var2
print("input_var2 is a ", type(input_var2))

And here is out output:

pj@LT440S:~$ python3 input_test.py
Please enter data for var1: test1
Please enter data for var2: 123
input_var1 is a  <class 'str'>
input_var2 is a  <class 'str'>

Now lest say i want to perform a basic addition of two inputs.

>>> var1 = input("Please type a number: ")
Please type a number: 20
>>> var2 = input("Please type a number: ")
Please type a number: 10
>>> print(var1 + var2)
2010

As you can see instead of addition python just printed those 2 provided inputs next to each other like in our previous example with first and last name strings.
If you wan to add them together you need to convert var1 and var2 to integers or floats like this:

int(var1)
int(var2)
float(var1)
float(var2)

If you call a type function on above you will see that type has changed

>>> type(int(var1))
<class 'int'>
>>> type(int(var2))
<class 'int'>

You can also assign that conversion to new variable:

>>> var3 = int(var1)
>>> var4 = int(var2)
>>> type(var3)
<class 'int'>
>>> type(var4)
<class 'int'>

Now we can do calculations on them:

# First input
var1 = input("Please type a number: ")
# Secund input
var2 = input("Please type a number: ")

# Var3 = integer of var1
var3 = int(var1)
# Var4 = integer of var2
var4 = int(var2)

print(var3 + var4)
# Or you can print the result like this
print(int(var1) + int(var2))

And the output:

pj@LT440S:~$ python3 input_test2.py
Please type a number: 20
Please type a number: 10
30
30

Once again i would like to state i'm a uber noob of Python and learned the above through various other tutorials, practicing, experimenting and trying to create more and more complicated programs. The above examples were for me like magic so after i finally understood it i thought i would break it down and spoon feed other noobs like me who struggle to understand programming. Of course if something is still a bit unclear ask and I''ll try to answer or I'm sure @Miguel_Sensacion and many other more advanced people on this forum will be able to answer.

3 Likes

This is called type casting.

1 Like

Thanks! Will remember this. Not good with terminology yet ;)

2 Likes

That's the point of this thread though, isn't it? ;D

1 Like

Amazing post ! This was my intention as I stated above, incase anyone missed it.

Thank you for the help and contribution

1 Like

Working on Ep2.1 and while practicing already have some addition for upcoming Ep.3. Just don't want to post all together not to confuse people :D

1 Like

Just another quick update.

To make your code and and colorful you have to type three of these ` (its like single quotation but tilted) then name of the language, in our case python, then your code and then another three of these tilted quotation characters. Hopefully this will bring a lot more clarity to the reader. Thanks @Eden

So typing this:

```python
sample_input = input("Enter sample message that will be printer back you you: ")
print("You typed in the following message: ", sample_input)
```

Will give us:

sample_input = input("Enter sample message that will be printer back you you: ")
print("You typed in the following message: ", sample_input)
3 Likes

Hello fellow coders,
let me ask for help. I wanted to follow your tutorial and got so far as installing Python 3.5, SublimeText3 with Python linters, copied the first example code, hit Build.

The program runs without errors inside SublimeText3, writes Hello, asks for my name, but then Im not able to input my response. I can write it, but hitting enter does not do anything.

Google suggests to install konsole, which is no good for me since Im doing this on Windows 7.

Also Im quite confused with SublimeText autocompletion function....when I write "tim" it suggests "time", but when I hit tab to confirm and add "." it does not bring any further suggestions of functions available. Again, I have to type at least "time.s" for "sleep" to show up. I guess its level1 noob mistake, but how do I fix this?

Thanks for help.

Sounds like you have a couple issues. Let's start with the Python 3.5 problem first. Here is a video on Intalling Python 3.x on Windows.

for running your code in the mean time you can use idle3.5 which comes packaged with python on install.
Simply open it and, go to : File > New File , here you should be able to paste your code and whe you are ready you can run it as well. to run your code click on Run > Run Module or F5 and it will run your code in the shell.

You can use the cmd prompt to run your python programs, like a Terminal.

As for sublime... That is a bit tricky because it depends on what you want/need to get your workflow going. There are many things you can do with it including having a console integrated... Once you get your code going, post your issue with sublime [ screenshot if possible ] and we'll see what we can do.