0% found this document useful (0 votes)
40 views7 pages

Python Beginner Notes

Comments provide context, help others understand code, and allow ignoring lines of code. Print outputs messages and is used in calculations. Strings are blocks of text surrounded by either single or double quotes. Variables store data for reuse and are assigned with =. Errors include syntax errors from incorrect writing and name errors if a variable is not defined. Numbers can be integers or floats. Variables can be treated like numbers and updated with +=. Control flow directs execution based on conditions using relational operators like == and !=.

Uploaded by

Rev M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
40 views7 pages

Python Beginner Notes

Comments provide context, help others understand code, and allow ignoring lines of code. Print outputs messages and is used in calculations. Strings are blocks of text surrounded by either single or double quotes. Variables store data for reuse and are assigned with =. Errors include syntax errors from incorrect writing and name errors if a variable is not defined. Numbers can be integers or floats. Variables can be treated like numbers and updated with +=. Control flow directs execution based on conditions using relational operators like == and !=.

Uploaded by

Rev M
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 7

Comments

Text written in a program but not run by the computer is called a comment. Python
interprets anything after a # as a comment. Comments

1. Provide context for why something is written the way it is


2. Help other people reading the code understand it faster
3. Ignore a line of code and see how a program will run without it

Print
In Python, the print() function is used to tell a computer to talk. The message
to be printed should be surrounded by quotes. Print is also used in calculations.

Strings
Computer programmers refer to blocks of text as strings. In our last exercise, we
created the string “Hello world!”. In Python a string is either surrounded by double
quotes ("Hello world") or single quotes ('Hello world'). It doesn’t
matter which kind you use, just be consistent.

Variables
Programming languages offer a method of storing data for reuse. If there is a
greeting we want to present, a date we need to reuse, or a user ID we need to
remember we can create a variable which can store a value. In Python, we assign
variables by using the equals sign (=).

Variables can’t have spaces or symbols in their names other than an underscore
(_). They can’t begin with numbers but they can have numbers after the first

letter (e.g., cool_variable_5 is OK).


Errors (2 types) Python points to the error location with a ^ character. When
programs throw errors that we didn’t expect to encounter we call those errors
bugs. Programmers call the process of updating the program so that it no longer
produces unexpected errors debugging.

● SyntaxError something wrong with how your program is written —

punctuation that does not belong, a command where it is not expected, or


a missing parenthesis
● A NameError not recognized word. Code that contains something that

looks like a variable but was never defined

Numbers
An integer, or int, whole number, no decimal point, A floating-point number, or a

float, decimal number used to represent fractional quantities as well as


precise measurements, averages, etc. Numbers can be assigned to variables or
used literally in a program:

Changing Numbers
Variables that are assigned numeric values can
be treated the same as the numbers
themselves. Two variables can be added
together, divided by 2, and multiplied by a third

variable without Python distinguishing between


the variables and literals (like the number 2 in

this example). Performing arithmetic on


variables does not change the variable — you
can only update a variable using the = sign.
Exponents (**)
Since this operation is so related to multiplication, we use the notation **.

Modulo (%)
Python offers a companion to the division operator called the modulo operator.
Modulo operator is indicated by % and gives remainder of a division calculation. If

the number is divisible, then the result of the modulo operator will be 0.

Concatenation
The + operator doesn’t just add two

numbers, it can also “add” two strings!


string concatenation. If you want to

concatenate a string with a number


you will need to make the number a
string first, using str() function

If you’re trying to print() a numeric


variable you can use commas to
pass it as a different argument
rather than converting it to a string.
Plus Equals
Python offers a shorthand for
updating variables. When you
have a number saved in a
variable and want to add to the
current value of the variable, you
can use += (plus-equals)

+= (plus-equals) also
can be used for string
concatenation: We create
social media captions,
but then update caption
to include important tags

Multi-line Strings
Python strings are very flexible, but if we try to create a string that occupies
multiple lines we find ourselves face-to-face with a SyntaxError. Python

offers a solution: multi-line strings. By using three quote-marks (""" or ''')

instead of one, we tell the


program that the string doesn’t
end until the next triple-quote.
Control Flow Each action/step is a product of the conditions of the day and your

surroundings. In Python, your script will execute from the top down, until there is nothing
left to run. It is your job to include gateways, known as conditional statements, to tell
the computer when it should execute certain blocks of code. If these conditions are
met, then run this function.

Relational Operators: Equals & Not Equals


● Equals: ==
● Not Equals: !=

Boolean Variables
True False when typed in (uppercase T&F), they appear in different colors cuz they

are a special type of their own: bool. no quotation marks, must capitalize 1st letter!

True and False are the only bool types, and any variable that is assigned one of
these values is called a boolean variable.

You might also like