Sunteți pe pagina 1din 30

Chapter 1

Welcome to the Python world!

C2 General
Python – an introduction
Pythn is a very powerful programming language, but at the same time, very easy to write
and has an easy-to-understand syntax – one of the most “readable” programming
languages

Main characteristics:

• Object oriented language (OOP)


• Open source
• Interpreted language
• Portable

Areas in which is most widely used:


• Web Development (Django, Flask)
• Machine Learning
• Big data
C2 General
Python – an introduction

There are multiple Python implementations available:


1. Cpython (C written Python) – most widely used; main focus of this course
2. Jpython (Python that runs in the Java Virtual Machine)
3. IronPython (Python that runs in .NET)

C2 General
Python – an introduction
In Python we will encounter multiple types of files:

1) .py – main files to be used when writing code


2) .pyc – files obtained after the interpretation of the .py ones (bytecode)
3) .pyd - Python Software Foundation - PYD files – using same format as the .DLL ones

C2 General
Python install
The main focus of this course will be python3.6

To check if this version is installed on your OS, open a terminal and write:

python36

OR

python

C2 General
Installing Python
For downloading and installing the official Python versions:

https://www.python.org/downloads/ - any of 3.6/3.7/3.8 versions can be used

The PyCharm IDE can be downloaded from:

https://www.jetbrains.com/pycharm/download/ - we are recommending the Community


Edition

For Linux users:

yum search python followed by yum install python”xxx” – for centos/fedora

apt search python followed by apt install python”xxx” - for ubuntu

C2 General
Python 3.6 Windows Install

C2 General
Python 3.6 Windows Install

C2 General
Python 3.6 Windows Install

C2 General
Tools used for writing Python code
In order to familiarize with the Python syntax, we will use REPL (read-eval-print-loop) – the main
prompt

Other IDE’s that can be used:


1) Visual Studio Code
2) Spyder
3) IPython Notebook
4) Anaconda

C2 General
Strings

Concatenation of strings:

>>> "first" "second"


'firstsecond'
>>> "first" + "second"
'firstsecond'
>>>

C2 General
String – basic operations
Upperacasing:

>>> "hello world".upper()


'HELLO WORLD'
>>>

Lowercasing:

>>> "HELLO WORLD".lower()


'hello world'
>>>

C2 General
String – basic operations
Capitalization

>>> "hello world". capitalize()


'Hello world'
>>>

Determining the length of a string


>>> "HELLO WORLD".__len__()
11
>>> len("HELLO WORLD")
11
>>>

C2 General
String – basic operations
Replacing part of a string

>>> "Hello world".replace('world','World')


'Hello World'
>>>

Searching among a string

>>> "Hello world".find('world')


6
>>>

C2 General
String – basic operations
Using join() to concatenate multiple string using a specific string as delimiter

>>> s = '-'
>>> seq = ("a", "b", "c");
>>> s.join( seq )
'a-b-c'
>>>

Converting a number to a string

>>> str(34)
'34'
>>>

C2 General
Numbers, variables, basic data types
Variables

To better understand variables we can name them aliases – they are used to identify
specific memory location, in order to access them. Upon reaching the chapter of objects
we will be able to give a much better definition for them.
Following basic data types can be saved in the memory: int, float, string, None etc.

a = 3 – as specified, “a” will be a name used to point to a certain memory location,


containing an integer value – in this case 3

>>> a = 3
>>> a
>>> 3

C2 General
Integer
int – represents integer values and used in decimal format
>>> 10
10 - decimal in python
>>> 3 * 4
12 - multiplicaton
>>> 3 + 4
7 - adding
>>> 4 - 3
1 - substraction
>>> 4 ** 2
16 – raise to power
>>> 4/2
2 - division
>>>

C2 General
Integer – other numerical bases
>>> 0b10
2 - binary in python
>>> 0o10
8 - octal in python
>>> 0x10
16 - hexadecimal in python
>>>
>>> int(3.5)
3
>>> int(-3.5)
-3
>>> int("496")
496 - string converted to integer

C2 General
Float
Float – floating decimal numbers

>>> 3.125
3.125
>>> 3e2
300.0
>>> 3e1
30.0
>>> float(8)
8.0
>>> float("1.618")
1.618 - string converted to a float value

C2 General
Float
>>> float("nan")
Nan (not a number)
>>> float("inf")
inf
>>> float("-inf")
-inf
>>> 3.0 + 1
4.0
>>>

C2 General
None
• Used for representing the absence of a value

>>> None
>>> a = None
>>> print(a)
None

C2 General
Bool
• Used for representing Boolean values (True/False)

>>> True
True
>>> False
False
>>> bool(0)
False
>>> bool(-1)
True
>>> bool(0.0)
False

C2 General
Bool – evaluating other data types
>>> bool(0.207)
True
>>> bool(-1.117)
True
>>> bool([])
False
>>> bool([1,5,9])
True
>>> bool("")
False
>>> bool("Spam")
True
>>>

C2 General
Bool – evaluating other data types
>>> bool("False")
True
>>> bool(False)
False
>>>

C2 General
Complex values
>>> z = 2 + 3j
>>> z.real
2.0
>>> z.imag
3.0
>>> abs(3 + 4j)
5.0
>>z.conjugate()
>>> pow(3 + 4j, 2)
(-7+24j)
>>>
>>> complex(2,3)
(2+3j)

C2 General
Reading user input data
Capturing user-input as a string from the terminal – using input()
>>> response = input("Please enter your name: ")
Please enter your name: Telacad
>>> response
'Telacad'

C2 General
Reading user input data
>>> name = input("What's your name? ")
What's your name? Telacad
>>> print("Nice to meet you " + name + "!")
Nice to meet you Telacad!
>>> age = input("Your age? ")
Your age? 34
>>> print("So, you are already " + str(age) + " years old, " + name + "!")
So, you are already 34 years old, Telacad!

C2 General
Reading user input password
>>> import getpass
>>> pswd = getpass.getpass('Password:')
Password:
>>> pswd
'parola'
>>>

C2 General
Exercises
1. Read an user-input string and concatenate it with “ Academy teaches the Python”.
Further concatenate with the numerical value of 3 and with the following string: “ because
Python 2 no longer has official support in 2020”. Print the final result. All concatenations
must be performed in separate instructions.

2. Write a program that prompts the user to input an username and a password. The result of
the program must be printed in the following format:
Username: “username_user_input”
Password: “password_user_input

3. Convert a float value to an integer one. What’s difference do you observe in the result?

4. Add a float value with a complex one. Print the real and imaginary parts, followed by the
module of the result

C2 General
5. Having the following string: message = “Telecom Academy is teaching a lot of online
courses” - replace “teaching” with “offering”.
6. Save in a variable the rest of the division of 5 and 2. Print the value from that variable.
7. Havng the following string: colors = ‘blue,red,green’, use the split(“,”) method and print the
result.

C2 General

S-ar putea să vă placă și