Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
422 views

01 Python Fundamentals CodeChum

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
422 views

01 Python Fundamentals CodeChum

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 108

• an open-source (free) programming language

• used for numerous scientific applications, including web development,


data research, and artificial intelligence
• enables the coder to concentrate on problem-solving rather than
syntax
• has a simple syntax similar to English
• allows programmers to write programs in fewer lines than other
programming languages
• designed with readability in mind
• has mathematical influences
• completes commands with new line
• whitespace indentation defines scopes like loops, functions, and
classes
• supports the Object-Oriented programming paradigm
• fantastic language for beginning programmers
Perl, C, and Java are all closely related to Python in syntax.
The Basic structure of a Python Program consists of the following:
• expressions
• statements
• comments
• function
• Blocks & indentation
• Line 8 and Line 9: Expression is any legally permitted string of symbols can
be used to express a value as an expression.
• A Python expression is a representation of something that is evaluated and
yields a value.
Example:
• Lines 8 to 10: Statement is a programming command that causes action,
meaning it causes something to happen.
Example:
• Lines 1,2, and 7: Comments provide additional readable information that
clarifies the source code.
• These are non-executable statements that start with a hash symbol (#) and
typically end with the line’s end.
Example:
• Blocks & indentation: In Python, a collection of statements that make up
another statement or a function are referred to as a block, code-block, or
suite.
• In Python, blocks are indicated using indentation.
Example:
• Lines 3 to 5: Function is a piece of code that has a name and may be utilized
(and executed again) by giving it the appropriate name in the program.
• A def statement is used to start a function.
Example:
• This is the place where the actual functionality is implemented.
• In other words, they contain a function body.
• We typically see libraries at the uppermost part of our source code along
with the #include, or import keyword.
• Categories of libraries:
• Static
• Shared / Dynamic
• They are linked with an end-user program and include object code.
• These are only used during compilation.
• They must be available in the proper place when the user wishes to create
an application.
• These are only required at run-time.
• The user can compile his/her code without using these libraries.
• They are linked against at compile time to resolve undefined references and
then its distributed to the application so that the application can load it at
run time.
• Typically, all certified implementations of a programming language contain
the most widely used libraries.
• Libraries are groups of non-volatile resources utilized by programs in
programming.
• These include configuration information, textual instructions, sample code,
subroutines, and classes.
• Reusability is the key justification for utilizing libraries.
• The majority of libraries have functions that a regular user could not
duplicate.
• Without these libraries, each user would need to be familiar with every
nuance of their own programming language.
• The huge reduction in coding time is another clear advantage.
• means how to arrange words, characters, special characters, to make
a meaningful statement, or expression, etc.
• the set of rules that must be followed while developing a program.
• To achieve the intended results, appropriate syntax must be followed
when coding.
• We need to study the syntax of a programming language so we can
properly communicate with the computer.
• With proper syntax, errors can be minimized.
• Python is case-sensitive, Name and name, for instance, have distinct
meanings.
• In programming, English names are the norm.
• Each variable must have a lowercase letter at the beginning.
Examples: var = 4
• Functions begin with lowercase.
• Classes begins with a capital letter.
Class Example:
• When programming, the programmer must adhere to specific
guidelines.
• Any rule broken will result in a “compilation error,” where the compiler
is unable to translate the code and the computer is left unsure on
what to do.
Here are a few Python fundamentals:
• A variable, function, or class’s initial letter must be one of the letters (a-
z) or (A-Z).
• Numbers or special characters such as & and % are not allowed.
• No special characters are permitted in name entries.
• There are several reserved terms that are prohibited from use in naming,
including and, if, otherwise, break, import, and others.
• Python is sensitive to indents, which in your code indicate a block
section.
• a flaw or defect in a program
• causes the program to behave abnormally
• occurs when a software does an action that it was not designed to
perform
• involves:
• generating inaccurate or unexpected results
• terminating a running program
• preventing the code from compiling
• remove all errors from the code
• mistakes in inputting the code’s syntax or has typos
• occur when a programmer disregards the rules governing the syntax of a
programming language
• also known as compilation errors
• The most commonly occurring syntax errors are:
• Missing semi-colon (;)
• Missing parenthesis ({})
• Assigning value to a variable without declaring it
Example:
NOTE: In this code, there is an indention before printing the variable
value, that is why it would result to a Syntax Error.
• errors that occur during the execution (or running) of a program
• appear after the software has been correctly compiled
• can only be identified once the program is running
• some scenarios of the common Run-Time Errors:
• number not divisible by zero
• array index out of bounds
• string index out of bounds
Run-time errors can arise for a variety of causes:
• Mistakes in the Code
• Memory Leaks
• Mathematically incorrect operations
• Undefined Variables
Example:
Run-Time error will be thrown due to the value assigned to integer variable var.
Integer variables can only hold up to 2147483647. Since the value assigned to
var is greater than the maximum value, then it will throw integer overflow error.
• occur when the code is right because it compiles without problems
• runs without errors, but the outcome is not what we intended
Example:
Intended result: addition between a and b.
Logical Error: Multiplication between a and b.
• also known as Special Characters
• characters with “special” built-in meanings in the language
• can not be used as identifiers
• a primitive data type with a distinct human-readable form
• add clarity to the source code
• crucial in large projects with hundreds or thousands of lines of source
code
• begin with a hash mark (#) and whitespace character and continue to
the end of the line.
• used in a programming language to document the program
• gives an explanation about the source code
• act as documentation in the source code
• increase the readability of the program
• a name for a memory place where data is stored

Syntax:
• in order to store multiple sorts of data in the program
• utilized in the program to accomplish any operation or activity
Python has no command for declaring a variable.
• equal sign (=)
• used to assign values to variables
• variable’s name
• left of the = operator
• value stored
• right of the = operator
Multiple variables can also be assigned to multiple values.
• First character
• begin with either an alphabet or an underscore (_) or a dollar sign ($)
• strictly cannot begin with a number
• No special characters
• does not encourage the use of special characters
• cannot use special characters like the exclamatory mark or the @
symbol
• No keywords
• using keywords as identifiers is strictly forbidden
• No white spaces
• Leaving a gap between identifiers is discouraged
• Word limit
• use of an arbitrarily long sequence of identifier names is
restrained
• name of the identifier must not exceed 31 characters
• Case sensitive
• uppercase and lowercase characters connote different
meanings
• limit the kind of data that may be stored
• used to inform variables about the sorts of data they may hold
• declarations for variables
• identify the type of the variable when it declared.
• identify the type of the return value of a function.
• identify the type of a parameter expected by a function.
• determines the values that the variable can have
• attribute associated with a piece of data that tells a computer system
how to interpret its value
• include integers, floating-point numbers, and complex numbers
• defined as int, float, and complex classes in Python
• type() function
• determine which class a variable or value belongs to
• isinstance() function
• determines whether or not an object belongs to a specific class
Example:
Example:
Integers can be any length, limited only by the amount of memory available.

A floating-point number has 15 decimal places of accuracy.


Decimal points separate integers and floating points. 1
.0 is a floating-point number, while 1 is an integer.

The formula for complex numbers is x + yj, where x is the real part and y is the
imaginary part.
Examples of Python Numbers:
• collection of items in a specific order
• one of Python’s most popular and versatile data types
• does not have to contain items of the same type

Declaring a list is relatively simple—commas separate items enclosed in


brackets [].
We can extract an item or a range of items from a list using the slicing operator
[]. In Python, the index starts at 0.
We can extract an item or a range of items from a list using the slicing operator
[]. In Python, the index starts at 0.
Lists are mutable, meaning, the value of elements of a list can be altered.
• an ordered sequence of items
• are immutable
• defined within parentheses (), where commas separate items
• use the slicing operator [] to extract items, but we cannot change their
value.
Example:
• a sequence of Unicode characters
• can be represented with either single or double-quotes
• multi-line strings can be denoted using triple quotes, ‘’’ or “”"

Example:
• a sequence of Unicode characters
• can be represented with either single or double-quotes
• multi-line strings can be denoted using triple quotes, ‘’’ or “”"

Example:
Just like a list and tuple, the slicing operator [ ] can be used with strings.
Strings, however, are immutable.
The plus + sign is the string concatenation operator and the asterisk * is the
repetition operator.
Example:
Example:
• unsorted group of unique items
• set is defined by values separated by a comma
• items are not ordered

Example:
• unsorted group of unique items
• set is defined by values separated by a comma
• items are not ordered

Example:
It is also possible to perform set operations such as union and intersection on
two sets. The values of sets are distinct. They take out duplicates.
For example:

Note that since set are unordered collection, indexing has no meaning.
Hence, the slicing operator [] does not work.
• a collection of key-value pairs that are not ordered
• used when dealing with large amounts of data
• defined inside braces, with each item being a key: value pair
Example:

Note that dictionaries have no concept of order among elements.


It is incorrect to say that the elements are “out of order”;
they are simply unordered.
• fixed values that the program may not alter once they’re set
• also called literals
• handled like ordinary variables except that their values cannot be
updated after declaration
• can be of any of the basic data types
• can be divided into:
• Integer Numerals
• Floating-Point Numerals
• Characters
• Strings
• Boolean Values
Both programmers and compilers can benefit from using constants:
• Programmers
• serve as a form of self-documenting code that facilitates
thinking about correctness
• Compilers
• enable compile-time and run-time consistency checks and the
ability to conduct or simplify some compiler optimizations.
• program can be easier to read
• saves time by helping to avoid errors that might be dangerous
• a type of variable that has a fixed value
• consider information-containing containers that are immutable in the
future
• typically declared and assigned in modules in Python
• capitalized and separated by underscores inside the module
• module
• a new file containing variables, functions, and other items
imported into the main file
 Create a constant.py:

 Create a main.py:

NOTE: In reality, constants are not used in Python. They are typically given
names in all capital letters to distinguish them from variables.
However, it does not prevent reassignment.
1. Constant and variable names should have a combination of letters in
lowercase (a to z) or uppercase (A to Z) or digits (0 to 9) or an
underscore (_)
2. Create a name that makes sense
3. Use capital letters possible to declare a constant
4. Use capital letters possible to declare a constant
5. Never use special symbols like !, @, #, $, %, etc.
6. Don’t start a variable name with a digit
• predefined, reserved words used in programming that have special
meanings to the compiler
• C is a case sensitive language
• all keywords must be written in lowercase
• used to change certain of the program’s functionalities

You might also like