0% found this document useful (0 votes)
4 views3 pages

Python Basics: Print, Input, Variables

1) The document provides tips on how to use basic commands in Python such as print, input, variables, and data types. 2) It is explained that the print command is used to display messages to the user and input is used to receive data from the user. 3) The main data types in Python are presented: integers, floats, booleans, and strings, as well as how to declare variables.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Python Basics: Print, Input, Variables

1) The document provides tips on how to use basic commands in Python such as print, input, variables, and data types. 2) It is explained that the print command is used to display messages to the user and input is used to receive data from the user. 3) The main data types in Python are presented: integers, floats, booleans, and strings, as well as how to declare variables.

Translated by

ScribdTranslations
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

PYTHON-

In this way, the variable message receives (=) the


TIP
To save a file with Python code, one must Hello world.
save the file with '.py' at the end of its name. We can use the print command to show to
45user the variables.
Example:
print (message)
Hello world
The print command is used to show something to
PRINT-------------------------------- INPUT
user, be it messages, numbers, etc. In another
50
In a way, the print means write. It must be written. The input command provides interactivity with the
in the following way: user, thus being able to collect information that
Example: will be provided by the user themselves. Otherwise
message to the user Form, the input means read. It should be written in the
Resultado:mensagem ao usuário 55following way:
Example: - OBSERVATION - Example:
print(7+4) Everything that will be shown to name = input("Enter your name: ")
Resultado:11 user must be between Resultado:Digite seu nome: (nessa parte o usuário
parentheses. If it is a
Example: message, it should be you will have to type the name). Example Natalia. Then
print(“7”+”4”) in single or double quotes. 60thus, the name Natália will be stored in the variable
Resultado:74 name.
To combine two pieces of information, like in the third one. TYPES
The four most basic primitive types that
for example, we can use + (plus) or , (comma)
PRIMITIVES------------------
they exist in Python:
but it is not in any case. To join a
65int:Números inteiros;
message with a number, we should use the comma.
Ex: 7 ; -4 ;
To combine two messages, we must use the plus.
float: Real numbers or numbers
Example:
floating point;
print("12"+"3")
Ex: 4.5 ; -15.223 ;
Resultado:123
70bool: Logical or boolean values;
Example:
Ex: True ; False ;
print("Hello world", "3")
Characters or strings.
Resultado:Olá mundo 3
Hello; "7.5"; "" (call of empty string, because
Example:
there is nothing inside).
print(“Olá mundo”+3)
In this example there is an error, because the + cannot be
75
- OBSERVATION -
usado para unir mensagens e números. In Python, to write a number with a comma instead of
instead of using a comma, we will use a period.

VARIÁVEIS---------------------------
They are spaces in the computer's memory, where - OBSERVATION -
There are two ways to place the variables within the command
One can store information. Every variable, in print.
Python language is an object. They can receive
values. When this happens, one should use the
equal symbol (=). In the first form, the variable is placed after the text in
question. Already in the second, {} is used and the structure
Example: .format(variable). Remembering that both will have the same
mensagem = “Olá mundo” result.
OPERATORS
- OBSERVATION -
a)ARITHMETIC---
5 + 2 == 7
To display the type of a variable, just use the Addition: + 5 - 2 == 3
command type before the variable.
b) Subtraction: - 5 * 2 == 10
Example:
5 / 2 == 2.5
c) Multiplication: * 5 squared equals 25
d) Division: / 5 // 2 == 2
Result: 5 % 2 == 1
e) Power or exponentiation: **
To use the '=' with
(the power can also be made) the function of equalizing,
Example:
using the pow(a,b) function, on- two must be used
of "a" represents the base and "b" “==”.

Result: the index) ORDER


DE
f) Integer division: //
- PRECEDENCE -
g) Modulus or remainder of the division: %
()
Example: **
80Example:
As an example of everything that has been said so far, * / // %
5 + 3 * 2 == 11 +-
we will have the following case:
3 * 5 + 4 ** 2 == 31 If in an expression
Calculate the sum between two values that will be appear operators
3 * (5+4) ** 2 == 243
informed by the user and then show it in the what they have a
pow(4,3) = 64 same order of
screen, informing which numbers were entered. precedence, they
81**(1/2) = 9
Code: will be resolved at
oioioi order what
to appear.

TIP
We can show information to the user in several ways.
ways, like after the information is shown 10 spaces
Result: they will be released before another piece of information is presented.
For this we will use ":10". In this way, the information will occupy
10 spaces and everything that is not a character will be added
spaces.
Example:

OBSERVATION
In the resolution, the two forms that were presented
we can fit the variables into the print command.
It is also noticeable that the results were exactly In this way, o name Gustavo plus the spaces
equals. a total of 20 spaces were accounted for and then it was presented
TIP the next information was the “!”.
Below is a table with several codes that We can use left alignments - using "<"
help verify if what was reported to the computer is after ":"Example::<20} or alignment to the right -
something like a number. What does the computer using ">" after ":". To paracentralize, just use "^" after
will return to the user True or False. :. To replace the blank spaces with something, just
Code Function put what you want after the “:”.
.isnumeric() If it is a number
Example:In this way o name will be
.isalpha() Is it literate?
centered and around it will have "=".
.isalnum() If it is alphanumeric
.isupper() If they are uppercase letters
.islower() If they are lowercase letters
.isspace() It is a space - TIP -
.isttle( ) It is capitalized To define the number of decimal places, just use the
Example: following expression:

In this way, using {:.3f} the number will have 3 decimal places.
TIP
So that there is no line break between
print commands, just use 'end=' ' ' at the end of
command.

In this way, the results of the print commands will be


displayed on the same line.

For there to be a line break in the same


print command, just use "\n" in place where you
wants to change the line.

.
In this way, the results of the print commands will be
displayed on different lines.

MODULES--------------------------
--add a module in Python, one must
To
use the command 'import' followed by the name of the

desired package. If you want to import only


an element of the desired package, use 'from' +
the package name + 'import' + the element
desired.
There are several packages in Python, such as 'math',

it contains several libraries that facilitate the


mathematical calculations, such as:

ceil: rounds numbers to


summit;
floor: rounds numbers to
low
trunc:remove everything that is after the
comma;
pow:calcula potência;
sqrt:calcula raiz quadrada;
factorial:calcula o fatorial de um número.
Example:

In this example, the user will be asked for a number,


that will have its square root informed.
If you want to know which packages exist for
your version of Python, just go to the browser and
access [Link].
TIP
To use emojis, follow these steps:

You might also like