Python: Basic Concepts
Python Basic Concepts
python
basic-concepts
Python Basic Concepts
1 Overview
📘 Python
Python is a high-level, interpreted programming language known for its simplicity and readability. Created by Guido van Rossum and first released in 1991, Python emphasizes code readability with its use of significant indentation. It supports multiple programming paradigms including procedural, object-oriented, and functional programming. Python is widely used in web development, data science, artificial intelligence, automation, and scientific computing.
2 Main Programming and Python Concepts
3 Displaying text
print() built-in function:
- Writes text to the console (or standard output).
- Takes multiple arguments (between the parentheses) and displays them.
3.1416
Hello world!
- By default,
printadds a new line at the end of the string.
Hello world!
This is a new line.
This is a new line.
- If values separated by commas,
printautomatically adds a space in between.
Hello world! How are you?
5 Number operations
5
2
6
1.5
1
9
1
6 Data types
- Categories of values that share characteristics.
- Dictate how values are stored, represented…
type()built-in function:
[‘int’]
[‘float’]
[‘str’]
6.1 Built-in types
Different types have different properties on how they interact between them:
4
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
6
‘ab’
‘51’
TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’
7 Casting
- Converting one type to another.
- Using built-in functions
int(),float(),str(), etc.
<class ‘str’>
<class ‘int’>
4
<class ‘int’>
<class ‘str’>
<class ‘str’>
abc1
51
51
3
3
3
8 Variables
- Used for storing and manipulating data.
- Naming:
- Should start with a letter a-z or A-Z or an underscore (_).
- They can also contain numbers, as long as a number is not the first character.
- Case-sensitive.
- Assignation
- With
= - No need to specify data type (done automatically by Python).
- Reassignation is allowed at any time.
- With
- Python automatically detects the data type based on the assigned value:
<class ‘str’>
<class ‘int’>
<class ‘bool’>
<class ‘int’>
<class ‘bool’>
- Use the value of the variables anywhere in the program:
John Doe
25
Isaac
- We can operate with variables as if they were the values:
3
-1
Isaac Newton
IsaacNewton
8.1 Updating operation
- For updating the value of a variable, we can do it in the classical way:
6
- Shortcut for updating a variable:
6
4
12
3
8.2 Convention variable names
- Snake Case: Words separated by
_. E.g.,is_student,user_nameorname_and_surname.
- Camel Case:
- Lower Camel Case: First word lowercase, subsequent words start with capital letter. E.g.,
isStudent,userName,nameAndSurname. - Upper Camel Case (Pascal Case): First word also capitalized. E.g.,
IsStudent,UserName,NameAndSurname.
- Lower Camel Case: First word lowercase, subsequent words start with capital letter. E.g.,
9 Keywords
- Python’s reserved words. They have a special purpose and functionality and cannot be used as identifiers (variable names, function names, class names, etc.).
| and | del | from | not | as |
| elif | global | or | assert | else |
| if | pass | async | except | import |
| raise | await | finally | in | return |
| break | for | is | try | class |
| with | lambda | while | continue | yield |
| nonlocal | def | False | None | True |
SyntaxError: invalid syntax
3
10 Built-in functions
- Built-in functions are globally available in any Python program.
- Unlike reserved words, it is possible to use built-in function names as variable names, thus modifying the default behavior (NOT RECOMENDED!).
| abs | all | any | ascii | bin |
| bool | breakpoint | bytearray | bytes | callable |
| chr | classmethod | compile | complex | delattr |
| dict | dir | divmod | enumerate | eval |
| exec | filter | float | format | frozenset |
| getattr | globals | hasattr | hash | help |
| hex | id | input | int | isinstance |
| issubclass | iter | len | list | locals |
| map | max | memoryview | min | next |
| object | oct | open | ord | pow |
| property | range | repr | reversed | |
| round | set | setattr | slice | sorted |
| staticmethod | str | sum | super | tuple |
| type | vars | zip | import |
- Take care!
TypeError: ‘int’ object is not callable
11 User input
- Reads text from the user.
- Returns it as a string.
Enter your name: Isaac
Isaac
Isaac
TypeError: can only concatenate str (not “int”) to str
32
12 Example
Write a program in which a user enters his/her name and age and the system prints a message with that information.
# Use input for processing the user name
user_name = input('Enter your name: ')
# Use input for processing the user age
user_age = int(input('Enter your age: '))
# Remember that, by default, the input function returns a string,
# so we do not need to convert the age to integer for concatenating it
print('Hello! You are ', user_name, ' and you are ', user_age, ' years old.')
Hello! You are Isaac and you are 31 years old.

4 Comments
'''or""")10