Pdfjoiner
Pdfjoiner
print
Python
display text
Write text characters that should be displayed on the screen
between single or double quotes.
print("Hello world")
>> Hello world
display numbers
The number can stand alone or be the result of a math
expression. We don't use quotation marks in these cases.
print(150 + 50)
>> 200
TOTAL
strings
Python
concatenation
Union of text strings:
print("Hello" + " " + "world")
>> Hello world
special characters
We can tell the console that the character after the \ symbol
should be treated as a special character.
input
Python
Imagine the situation: Your best friend has started a brewery and has everything ready. His
product is fantastic. It has body, good flavor, good color, and just the right level of foam. But it
lacks an identity. He can't think of a name for his beer that would give it a unique and original
identity.
So, you come to him and say: “Don’t worry. I'm going to create a program that will ask you
two questions and then tell you what to name your beer. It's as simple as that.”
I know that in the real world, we wouldn't need to develop a software just to ask it two
questions. But until we learn a bit more functionality, well, our programs are going to have to
stay in the realm of simplicity. Yet if you're just starting out, this is going to be quite a challenge.
You're going to create Python code that asks your friend to answer two questions that
require a single word each, and then displays those combined words on the screen to
form a creative brand.
You can use any questions you want. The idea is for the outcome to be original,
creative, and even funny. And if you want to add some difficulty to your challenge, I
suggest you try to have the name of the beer printed in quotation marks on the screen.
Remember that there are different ways for the print function to show the quotes without
interrupting the stream, and it's also possible to print out text in at least two lines using line
breaks within the code.
Well, try to do it on your own, and if it gets complicated, don't worry, we will solve it together
in the next lecture.
data types
Python
variables
Python
some examples
country = "Thailand"
num1 = 55
num2 = 45
print(num1 + num2)
>> 100
TOTAL
naming Python
variables
There are conventions and best practices associated with
naming variables in Python. They are aimed towards
readability and maintainability of the code.
rules
1. Readable: variable name should be relevant to its content
2. Unit: there are no spaces (instead, use underscores)
3. Plain: omit certain language-specific signs, such as ñ, è, ç
4. Numbers: Variable names must not start with numbers
(although we can have them at the end of the variable
name)
5. Symbols: Must not include : " ' , < > / ? | \ ( ) ! @ # $ % ^ & * ~
-+
6. Keywords: we do not use Python reserved words
TOTAL
There are two basic numeric data types in Python: int and
float. Like any variable in Python, its type is defined the
moment we assign a value to a variable. You can get the data
type of a variable with the type() function.
int
An integer, positive or negative, without decimals, of
indeterminate length.
num1 = 7
print(type(num1))
>> <class 'int'>
float
Number that can be positive or negative, which in turn
contains one or more decimal places.
num2 = 7.525587
print(type(num2))
>> <class 'float'>
TOTAL
type conversions
Python
int(var)
>> <class 'int'> Setting the data type as integer
float(var)
Setting the data type as float
>> <class 'float'>
TOTAL
formatting strings
Python
We have two main tools to mix static text and variables into
strings:
arithmetic
Python
operators
Arithmetic operators are used with numeric values to perform
common mathematical operations:
Addition: +
Subtraction: -
Multiplication: *
Division: /
Floor division: //
useful for detecting
Modulus: % even values :)
Exponentiation: **
just a special case
Square root: **0.5 of exponentiation
TOTAL
round
Python
round(number,ndigits)
number to be number of
rounded decimals to use
(default is 0 = int)
examples
print(round(100/3))
>> 33
print(round(12/7,2))
>> 1.71
Day 2 Python Challenge
Congratulations: You've finished learning all the content of Day 2, and now you're able to
create a slightly more complex program.
You already know how to do things like mathematical operations, data type conversions, string
formatting and rounding. Let’s celebrate with a new challenge.
The situation is this: You work in a company where salespeople receive 13%
commissions on their total sales, and your boss wants you to help the salespeople
calculate their commissions by creating a program that asks them their name and
how much they've sold this month.
Your program will answer them with a sentence that includes their name and the
amount of commissions they are entitled to.
Now, this is not a complex program, but that's okay, you're still learning. Even though what
you have learned so far is very simple, putting it all together in one program can make your new
skills get a little bit messy.
A couple of guidelines:
This program should start by asking the user for things. So, you're going to need to use
input to receive user input, and you should use variables to store that input. Remember
that user inputs are stored as strings, so you should convert one of those inputs to float
to be able to do operations on it.
And what operation do you need to do? Well, calculate 13% of the number that the user
has entered. That is, multiply the number by 13, then divide by 100.
It would be good to print the result on the screen, so make sure this information has no
more than two decimals, so it is going to be easy to read. Then, organize all that in a
string that you would format. Remember, we learned two ways to do it, and any of them
is valid.
All right, go on. Try to solve it. And if it gets complicated, Federico will do it with you in the next
lesson.