Python Documentation
Introduction
Python is a high-level, interpreted, general-purpose programming language created by Guido van Rossum and first released in 1991. Known for its clear syntax, Python is popular among beginners and experienced developers. It supports multiple programming paradigms including procedural, object-oriented, and functional programming. Python's extensive standard library simplifies many tasks, reducing the need to write code from scratch.
Hello World
To get started with writing python codes, the first program you should make is to print 'Hello World', here is how you do it:
print('Hello World')
The output of this program would be:
Hello World
The same way that is written on the print.
Data Types
- Int: 1, 2, 3;
- Float: 1.2, 7.2;
- String: "String", "s";
- Boolean: True, False;
- Lists: [1, 2, 3] ["Python", "Word"];
- Tuple: (1, 2, 3) ("Python", "Word");
- Dicionary: {1, 2, 3}, {"Python", "Word"};
Input
In Python, input() is used to capture user input from the console. It pauses program execution, waits for the user to type something.
name = str(input('Type your name: '))
In this case, the variable 'name' will recieve and store a string input from the keyboard.
If Else
In Python, the if else statement is used to execute code based on conditions. The if statement checks a condition; if it's true, the code block under it runs. If false, the else block executes instead. Python also supports elif (short for "else if") to handle multiple conditions. This allows for complex decision-making and control flow in programs, enabling different code paths depending on various conditions.
age = 18
if age >= 18:
print('You are an adult!')
else:
print('You are not an adult yet!')
The output of this program would be:
You are an adult!
Loops
In Python, a for loop is used to iterate over a sequence of elements, such as a list, tuple, or range. For example:
for i in range(0, 5):
print(i)
In this case, the loop would be through numbers 0 to 4 and those numbers in order are the output of the program.
In Python, functions are blocks of reusable code that perform a specific task. Defined using the def keyword, they can take parameters and return values. Functions help organize code, promote reusability, and improve readability. They encapsulate logic, allowing for modular and maintainable programming.
def sum(a, b):
soma = a+b
print(soma)
#codigo principal
Sum(3, 4)
The output of this program would be:
7