0% found this document useful (0 votes)
2 views

pythoncrushcourse

The document introduces Python programming, focusing on variables and simple data types. It explains how to name variables, the concept of strings, and methods for manipulating string cases. Additionally, it covers the use of whitespace in strings and basic operations with integers.

Uploaded by

moseliem31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

pythoncrushcourse

The document introduces Python programming, focusing on variables and simple data types. It explains how to name variables, the concept of strings, and methods for manipulating string cases. Additionally, it covers the use of whitespace in strings and basic operations with integers.

Uploaded by

moseliem31
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python is a great language to learn, so let’s get started!

Chapter 1- VARIABLES AND SIMPLE DATA T YPES


Naming and Using Variables:
Variable names can contain only letters, numbers, and
underscores. They can start with a letter or an underscore, but
not with a number. For instance, you can call a variable
message_1 but not 1_message.
Spaces are not allowed in variable names, but underscores can
be used to separate words in variable names. For example,
greeting_message works but "greeting message" will cause
errors.
Avoid using Python keywords and function names as variable
names. For example, do not use the word print as a variable
name; Python has reserved it for a particular programmatic
purpose.
Variable names should be short but descriptive. For example,
name is better than n, student_name is better than s_n, and
name_length is better than length_of_persons_name.
meaningful names - a traceback- misspelled word –
complicated- Variables –
identify the variable name provided-

A name error usually means we either forgot to set a variable’s


value before using it, or we made a spelling mistake when
entering the variable’s name.
Programming languages are strict – ‫صارمة‬
Variables Are Labels
Variables are often described as boxes you can store values in.
This idea can be helpful the first few times you use a variable,
but it isn’t an accurate way to describe how variables are
represented internally in Python. It’s much better to think of
variables as labels that you can assign to values. You can also
say that a variable references a certain value.

Strings
A string is a series of characters. Anything inside quotes is
considered a string in Python, and you can use single or double
quotes around your strings like this:
"This is a string." 'This is also a string.'
Changing Case in a String with Methods
One of the simplest tasks you can do with strings is change the
case of the words in a string.

The method title() appears after the variable in the print()


call. A method is an action that Python can perform on a piece
of data. The dot (.) after name in name.title() tells Python to
make the title() method act on the variable name. Every
method is followed by a set of parentheses, because methods
often need additional information to do their work. That
information is provided inside the parentheses. The title()
function doesn’t need any additional information, so its
parentheses are empty.
name = "Ada Lovelace"
print(name.upper())
print(name.lower())
This will display the following:
ADA LOVELACE
ada lovelace
The lower() method is particularly useful for storing data. You
typically won’t want to trust the capitalization that your users
provide, so you’ll convert strings to lowercase before storing
them. Then when you want to display the information, you’ll
use the case that makes the most sense for each string.
Using Variables in Strings
In some situations, you’ll want to use a variable’s value inside a
string. For example, you might want to use two variables to
represent a first name and a last name, respectively, and then
combine those values to display someone’s full name:

These strings are called f-strings. The f is for format,


because Python formats the string by replacing the name of
any variable in braces with its value. The output of this code is
ada lovelace.
nonprinting characters –
In programming, whitespace refers to any nonprinting
characters, such as spaces, tabs, and end-of-line symbols. You
can use whitespace to organize your output so it’s easier for
users to read. \t
To add a tab to your text, use the character combination \t:
To add a newline in a string, use the character combination \n:
>>> print("Languages:\n\tPython\n\tC\n\tJavaScript")
Languages:
Python
C
JavaScript
To ensure that no whitespace exists at the right side of a string,
use the rstrip() method:

Removing Prefixes
When working with strings, another common task is to remove
a prefix. Consider a URL with the common prefix https://. We
want to remove this prefix, so we can focus on just the part of
the URL that users need to enter into an address bar. Here’s
how to do that:

Parentheses ()- Single quotes' ' - Double quotes " "- Square
brackets []- Backslash \ - Underscore _ - Forward slash / –
Dot. - Curly brackets {}
– Semi colon; - colon : - Plus sign + - Equal sign = - Division
sign / - Asterisk * - Dollar sign$ - Hash sign #
“ ” (Curly/Smart Quotes) - an apostrophe ,
Albert Einstein once said, “A person who never made a
mistake never tried anything new.”
Numbers
Numbers are used quite often in programming to keep score
in games, represent data in visualizations, store information
in web applications, and so on. Python treats numbers in
several different ways, depending on how they’re being
used. Let’s first look at how Python manages integers,
because they’re the simplest to work with.
Integers
You can add (+), subtract (-), multiply (*), and divide (/)
integers in Python.

You might also like